1 | (function($) {
|
---|
2 |
|
---|
3 | $.fn.tweet = function(o){
|
---|
4 | var s = $.extend({
|
---|
5 | username: ["ansimuz"], // [string] required, unless you want to display our tweets. :) it can be an array, just do ["username1","username2","etc"]
|
---|
6 | list: null, // [string] optional name of list belonging to username
|
---|
7 | favorites: false, // [boolean] display the user's favorites instead of his tweets
|
---|
8 | avatar_size: null, // [integer] height and width of avatar if displayed (48px max)
|
---|
9 | count: 3, // [integer] how many tweets to display?
|
---|
10 | fetch: null, // [integer] how many tweets to fetch via the API (set this higher than 'count' if using the 'filter' option)
|
---|
11 | intro_text: null, // [string] do you want text BEFORE your your tweets?
|
---|
12 | outro_text: null, // [string] do you want text AFTER your tweets?
|
---|
13 | join_text: null, // [string] optional text in between date and tweet, try setting to "auto"
|
---|
14 | auto_join_text_default: "i said,", // [string] auto text for non verb: "i said" bullocks
|
---|
15 | auto_join_text_ed: "i", // [string] auto text for past tense: "i" surfed
|
---|
16 | auto_join_text_ing: "i am", // [string] auto tense for present tense: "i was" surfing
|
---|
17 | auto_join_text_reply: "i replied to", // [string] auto tense for replies: "i replied to" @someone "with"
|
---|
18 | auto_join_text_url: "i was looking at", // [string] auto tense for urls: "i was looking at" http:...
|
---|
19 | loading_text: null, // [string] optional loading text, displayed while tweets load
|
---|
20 | query: null, // [string] optional search query
|
---|
21 | refresh_interval: null , // [integer] optional number of seconds after which to reload tweets
|
---|
22 | twitter_url: "twitter.com", // [string] custom twitter url, if any (apigee, etc.)
|
---|
23 | twitter_api_url: "api.twitter.com", // [string] custom twitter api url, if any (apigee, etc.)
|
---|
24 | twitter_search_url: "search.twitter.com", // [string] custom twitter search url, if any (apigee, etc.)
|
---|
25 | template: function(info) { // [function] template used to construct each tweet <li>
|
---|
26 | return info["avatar"] + info["time"] + info["join"] + info["text"];
|
---|
27 | },
|
---|
28 | comparator: function(tweet1, tweet2) { // [function] comparator used to sort tweets (see Array.sort)
|
---|
29 | return tweet2["tweet_time"] - tweet1["tweet_time"];
|
---|
30 | },
|
---|
31 | filter: function(tweet) { // [function] whether or not to include a particular tweet (be sure to also set 'fetch')
|
---|
32 | return true;
|
---|
33 | }
|
---|
34 | }, o);
|
---|
35 |
|
---|
36 | $.fn.extend({
|
---|
37 | linkUrl: function() {
|
---|
38 | var returning = [];
|
---|
39 | // See http://daringfireball.net/2010/07/improved_regex_for_matching_urls
|
---|
40 | var regexp = /\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»ââââ]))/gi;
|
---|
41 | this.each(function() {
|
---|
42 | returning.push(this.replace(regexp,
|
---|
43 | function(match) {
|
---|
44 | var url = (/^[a-z]+:/i).test(match) ? match : "http://"+match;
|
---|
45 | return "<a href=\""+url+"\">"+match+"</a>";
|
---|
46 | }));
|
---|
47 | });
|
---|
48 | return $(returning);
|
---|
49 | },
|
---|
50 | linkUser: function() {
|
---|
51 | var returning = [];
|
---|
52 | var regexp = /[\@]+([A-Za-z0-9-_]+)/gi;
|
---|
53 | this.each(function() {
|
---|
54 | returning.push(this.replace(regexp,"<a href=\"http://"+s.twitter_url+"/$1\">@$1</a>")); });
|
---|
55 | return $(returning);
|
---|
56 | },
|
---|
57 | linkHash: function() {
|
---|
58 | var returning = [];
|
---|
59 | var regexp = /(?:^| )[\#]+([A-Za-z0-9-_]+)/gi;
|
---|
60 | this.each(function() {
|
---|
61 | returning.push(this.replace(regexp, ' <a href="http://'+s.twitter_search_url+'/search?q=&tag=$1&lang=all&from='+s.username.join("%2BOR%2B")+'">#$1</a>'));
|
---|
62 | });
|
---|
63 | return $(returning);
|
---|
64 | },
|
---|
65 | capAwesome: function() {
|
---|
66 | var returning = [];
|
---|
67 | this.each(function() {
|
---|
68 | returning.push(this.replace(/\b(awesome)\b/gi, '<span class="awesome">$1</span>'));
|
---|
69 | });
|
---|
70 | return $(returning);
|
---|
71 | },
|
---|
72 | capEpic: function() {
|
---|
73 | var returning = [];
|
---|
74 | this.each(function() {
|
---|
75 | returning.push(this.replace(/\b(epic)\b/gi, '<span class="epic">$1</span>'));
|
---|
76 | });
|
---|
77 | return $(returning);
|
---|
78 | },
|
---|
79 | makeHeart: function() {
|
---|
80 | var returning = [];
|
---|
81 | this.each(function() {
|
---|
82 | returning.push(this.replace(/(<)+[3]/gi, "<tt class='heart'>♥</tt>"));
|
---|
83 | });
|
---|
84 | return $(returning);
|
---|
85 | }
|
---|
86 | });
|
---|
87 |
|
---|
88 | function parse_date(date_str) {
|
---|
89 | // The non-search twitter APIs return inconsistently-formatted dates, which Date.parse
|
---|
90 | // cannot handle in IE. We therefore perform the following transformation:
|
---|
91 | // "Wed Apr 29 08:53:31 +0000 2009" => "Wed, Apr 29 2009 08:53:31 +0000"
|
---|
92 | return Date.parse(date_str.replace(/^([a-z]{3})( [a-z]{3} \d\d?)(.*)( \d{4})$/i, '$1,$2$4$3'));
|
---|
93 | }
|
---|
94 |
|
---|
95 | function relative_time(date) {
|
---|
96 | var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
|
---|
97 | var delta = parseInt((relative_to.getTime() - date) / 1000, 10);
|
---|
98 | var r = '';
|
---|
99 | if (delta < 60) {
|
---|
100 | r = delta + ' seconds ago';
|
---|
101 | } else if(delta < 120) {
|
---|
102 | r = 'a minute ago';
|
---|
103 | } else if(delta < (45*60)) {
|
---|
104 | r = (parseInt(delta / 60, 10)).toString() + ' minutes ago';
|
---|
105 | } else if(delta < (2*60*60)) {
|
---|
106 | r = 'an hour ago';
|
---|
107 | } else if(delta < (24*60*60)) {
|
---|
108 | r = '' + (parseInt(delta / 3600, 10)).toString() + ' hours ago';
|
---|
109 | } else if(delta < (48*60*60)) {
|
---|
110 | r = 'a day ago';
|
---|
111 | } else {
|
---|
112 | r = (parseInt(delta / 86400, 10)).toString() + ' days ago';
|
---|
113 | }
|
---|
114 | return 'about ' + r;
|
---|
115 | }
|
---|
116 |
|
---|
117 | function build_url() {
|
---|
118 | var proto = ('https:' == document.location.protocol ? 'https:' : 'http:');
|
---|
119 | var count = (s.fetch === null) ? s.count : s.fetch;
|
---|
120 | if (s.list) {
|
---|
121 | return proto+"//"+s.twitter_api_url+"/1/"+s.username[0]+"/lists/"+s.list+"/statuses.json?per_page="+count+"&callback=?";
|
---|
122 | } else if (s.favorites) {
|
---|
123 | return proto+"//"+s.twitter_api_url+"/favorites/"+s.username[0]+".json?count="+s.count+"&callback=?";
|
---|
124 | } else if (s.query === null && s.username.length == 1) {
|
---|
125 | return proto+'//'+s.twitter_api_url+'/1/statuses/user_timeline.json?screen_name='+s.username[0]+'&count='+count+'&include_rts=1&callback=?';
|
---|
126 | } else {
|
---|
127 | var query = (s.query || 'from:'+s.username.join(' OR from:'));
|
---|
128 | return proto+'//'+s.twitter_search_url+'/search.json?&q='+encodeURIComponent(query)+'&rpp='+count+'&callback=?';
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | return this.each(function(i, widget){
|
---|
133 | var list = $('<ul class="tweet_list">').appendTo(widget);
|
---|
134 | var intro = '<p class="tweet_intro">'+s.intro_text+'</p>';
|
---|
135 | var outro = '<p class="tweet_outro">'+s.outro_text+'</p>';
|
---|
136 | var loading = $('<p class="loading">'+s.loading_text+'</p>');
|
---|
137 |
|
---|
138 | if(typeof(s.username) == "string"){
|
---|
139 | s.username = [s.username];
|
---|
140 | }
|
---|
141 |
|
---|
142 | if (s.loading_text) $(widget).append(loading);
|
---|
143 | $(widget).bind("load", function(){
|
---|
144 | $.getJSON(build_url(), function(data){
|
---|
145 | if (s.loading_text) loading.remove();
|
---|
146 | if (s.intro_text) list.before(intro);
|
---|
147 | list.empty();
|
---|
148 |
|
---|
149 | var tweets = $.map(data.results || data, function(item){
|
---|
150 | var join_text = s.join_text;
|
---|
151 |
|
---|
152 | // auto join text based on verb tense and content
|
---|
153 | if (s.join_text == "auto") {
|
---|
154 | if (item.text.match(/^(@([A-Za-z0-9-_]+)) .*/i)) {
|
---|
155 | join_text = s.auto_join_text_reply;
|
---|
156 | } else if (item.text.match(/(^\w+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+) .*/i)) {
|
---|
157 | join_text = s.auto_join_text_url;
|
---|
158 | } else if (item.text.match(/^((\w+ed)|just) .*/im)) {
|
---|
159 | join_text = s.auto_join_text_ed;
|
---|
160 | } else if (item.text.match(/^(\w*ing) .*/i)) {
|
---|
161 | join_text = s.auto_join_text_ing;
|
---|
162 | } else {
|
---|
163 | join_text = s.auto_join_text_default;
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | // Basic building blocks for constructing tweet <li> using a template
|
---|
168 | var screen_name = item.from_user || item.user.screen_name;
|
---|
169 | var source = item.source;
|
---|
170 | var user_url = "http://"+s.twitter_url+"/"+screen_name;
|
---|
171 | var avatar_size = s.avatar_size;
|
---|
172 | var avatar_url = item.profile_image_url || item.user.profile_image_url;
|
---|
173 | var tweet_url = "http://"+s.twitter_url+"/"+screen_name+"/statuses/"+item.id_str;
|
---|
174 | var tweet_time = parse_date(item.created_at);
|
---|
175 | var tweet_relative_time = relative_time(tweet_time);
|
---|
176 | var tweet_raw_text = item.text;
|
---|
177 | var tweet_text = $([tweet_raw_text]).linkUrl().linkUser().linkHash()[0];
|
---|
178 |
|
---|
179 | // Default spans, and pre-formatted blocks for common layouts
|
---|
180 | var user = '<a class="tweet_user" href="'+user_url+'">'+screen_name+'</a>';
|
---|
181 | var join = ((s.join_text) ? ('<span class="tweet_join"> '+join_text+' </span>') : ' ');
|
---|
182 | var avatar = (avatar_size ?
|
---|
183 | ('<a class="tweet_avatar" href="'+user_url+'"><img src="'+avatar_url+
|
---|
184 | '" height="'+avatar_size+'" width="'+avatar_size+
|
---|
185 | '" alt="'+screen_name+'\'s avatar" title="'+screen_name+'\'s avatar" border="0"/></a>') : '');
|
---|
186 | var time = '<span class="tweet_time"><a href="'+tweet_url+'" title="view tweet on twitter">'+tweet_relative_time+'</a></span>';
|
---|
187 | var text = '<span class="tweet_text">'+$([tweet_text]).makeHeart().capAwesome().capEpic()[0]+ '</span>';
|
---|
188 |
|
---|
189 | return { item: item, // For advanced users who want to dig out other info
|
---|
190 | screen_name: screen_name,
|
---|
191 | user_url: user_url,
|
---|
192 | avatar_size: avatar_size,
|
---|
193 | avatar_url: avatar_url,
|
---|
194 | source: source,
|
---|
195 | tweet_url: tweet_url,
|
---|
196 | tweet_time: tweet_time,
|
---|
197 | tweet_relative_time: tweet_relative_time,
|
---|
198 | tweet_raw_text: tweet_raw_text,
|
---|
199 | tweet_text: tweet_text,
|
---|
200 | user: user,
|
---|
201 | join: join,
|
---|
202 | avatar: avatar,
|
---|
203 | time: time,
|
---|
204 | text: text
|
---|
205 | };
|
---|
206 | });
|
---|
207 |
|
---|
208 | tweets = $.grep(tweets, s.filter).slice(0, s.count);
|
---|
209 | list.append($.map(tweets.sort(s.comparator),
|
---|
210 | function(t) { return "<li>" + s.template(t) + "</li>"; }).join('')).
|
---|
211 | children('li:first').addClass('tweet_first').end().
|
---|
212 | children('li:odd').addClass('tweet_even').end().
|
---|
213 | children('li:even').addClass('tweet_odd');
|
---|
214 |
|
---|
215 | if (s.outro_text) list.after(outro);
|
---|
216 | $(widget).trigger("loaded").trigger((tweets.length === 0 ? "empty" : "full"));
|
---|
217 | if (s.refresh_interval) {
|
---|
218 | window.setTimeout(function() { $(widget).trigger("load"); }, 1000 * s.refresh_interval);
|
---|
219 | }
|
---|
220 | });
|
---|
221 | }).trigger("load");
|
---|
222 | });
|
---|
223 | };
|
---|
224 | })(jQuery);
|
---|