This repository has been archived by the owner on Mar 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathtweetable.jquery.js
executable file
·237 lines (201 loc) · 6.57 KB
/
tweetable.jquery.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* tweetable 2.1.1 - jQuery twitter feed plugin
*
* Copyright (c) 2009 Philip Beel (http://www.theodin.co.uk/)
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
*
* With modifications from Philipp Robbel (http://www.robbel.com/) & Patrick DW (stackoverflow)
*
* Revision: $Id: jquery.tweetable.js 2013-12-10 $
*
*/
(function($) {
jQuery.fn.tweetable = function (opts) {
opts = $.extend({}, $.fn.tweetable.options, opts);
return this.each(function ()
{
var act = jQuery(this);
var tweetList = jQuery('<ul class="tweetList">')[opts.position.toLowerCase() + 'To'](act);
var shortMonths = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
// var api = "http://api.getmytweets.co.uk/?screenname=";
var api = "http://plugins.theodin.co.uk/tweetable/statuses/?screen_name=";
var limitcount = "&limit=";
var callback = "&callback=?";
var tweetMonth;
var tweetMonthInt;
var element;
var latestTweet;
// Return a boolean with the value of the tweets
function hasValidCachedTweets ()
{
if(window.localStorage)
{
var response = JSON.parse(localStorage.getItem("tweetable"));
if(!response)
{
return false;
}
var timestampWhenStored = response && response.timestamp || 0;
return tweetHasExpired(timestampWhenStored, opts.cacheInMilliseconds);
}
};
function getCachedTweets (cacheTimeInMilliseconds)
{
if(window.localStorage)
{
var response = JSON.parse(localStorage.getItem("tweetable"));
return response.value;
}
};
function tweetHasExpired (storedTimeInMilliseconds, expiryTimeInMilliseconds)
{
var currentTime = new Date().getTime();
return ((currentTime - expiryTimeInMilliseconds) <= storedTimeInMilliseconds);
};
function destroyTweetInLocalStorage (name)
{
localStorage.removeItem(name);
};
function storeTweetsInLocalCache (response)
{
if(window.localStorage)
{
var value = {
value:response,
timestamp: new Date().getTime()
};
localStorage.setItem("tweetable", JSON.stringify(value));
}
};
function hideLoadingMessage ()
{
jQuery("#tweet_loader").remove();
};
function showLoadingMessage ()
{
tweetList.append('<p id="tweet_loader">'+ opts.loading +'</p>');
};
function displayErrorMessage ()
{
tweetList.append('<li class="tweet_content item"><p class="tweet_link">'+ opts.failed +'</p></li>');
};
function appendTweetToList (i, tweet)
{
tweetList.append('<li class="tweet_content_' + i + '"><p class="tweet_link_' + i + '">' + tweet.response.replace(/#(.*?)(\s|$)/g, '<span class="hash">#$1 </span>').replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, '<a href="$&">$&</a> ').replace(/@(.*?)(\s|\(|\)|$)/g, '<a href="http://twitter.com/$1">@$1 </a>$2').replace(/:">/, ' ">').replace(/: <\/a>/, '</a>:') + '</p></li>');
};
function encounteredError (data)
{
return data && data.error || null;
}
function displayTimeOfTweet (i, tweet)
{
var iso8601;
var n;
for(n=0; n<=12; n++) {
if(shortMonths[n] === tweet.tweet_date.substr(4, 3)) {
tweetMonthInt = n++;
tweetMonth = (tweetMonthInt <= 9) ? '0' + tweetMonthInt : tweetMonthInt ;
}
}
// Create ISO 8601 formatted date
iso8601 = tweet.tweet_date.substr(26,4) + '-' + tweetMonth + '-' + tweet.tweet_date.substr(8, 2) + 'T' + tweet.tweet_date.substr(11,8) + 'Z';
jQuery('.tweet_link_' + i).append('<p class="timestamp"><'
+ ((opts.html5) ? 'time datetime="' + iso8601 + '"' : 'small')
+ '> ' + tweet.tweet_date.substr(8, 2) + '/' + tweetMonth + '/' + tweet.tweet_date.substr(26,4) + ', ' + tweet.tweet_date.substr(11,5) + '</'
+ ((opts.html5) ? 'time' : 'small') +
'></p>');
};
function displayTweetsOnRotation ()
{
var listItem = tweetList.find('li')
var listLength = listItem.length || null;
var current = 0;
var timeout = opts.speed;
if(!listLength)
{
return;
}
function rotateTweets()
{
listItem.eq(current++).fadeOut(400, function ()
{
current = (current === listLength) ? 0 : current;
listItem.eq(current).fadeIn(400);
});
}
//Hide all but the first tweet
listItem.slice(1).hide();
//Rotate tweets at specified interval
setInterval(rotateTweets, timeout);
};
function displayTweets(data)
{
if(encounteredError(data))
{
displayErrorMessage();
return;
}
else
{
storeTweetsInLocalCache(data);
}
// Loop through twitter API response
jQuery.each(data, function (i, tweet)
{
if(i >= opts.limit)
{
return;
}
appendTweetToList(i, tweet);
if (opts.time === true)
{
displayTimeOfTweet(i, tweet);
}
});
if ( opts.rotate === true )
{
displayTweetsOnRotation();
}
opts.onComplete(tweetList);
};
if(hasValidCachedTweets())
{
var data = getCachedTweets();
displayTweets(data);
}
else
{
showLoadingMessage();
jQuery.getJSON(api + opts.username + limitcount + opts.limit + callback)
.done(function (data)
{
hideLoadingMessage();
displayTweets(data.tweets);
}).fail(function ( jqxhr, textStatus, error )
{
hideLoadingMessage();
displayErrorMessage();
return;
});
}
});
};
// Define plugin defaults
$.fn.tweetable.options = {
limit: 5, // Number of tweets to show
username: 'philipbeel', // @username tweets to display
time: false, // Display date
rotate: false, // Rotate tweets
speed: 5000, // Speed of rotation
replies: false, // Filter out @replys
position: 'append', // Append position
failed: "No tweets available", // Twitter stream unavailable text
loading: "Loading tweets...", // Tweets loading message
html5: false, // HTML5 Support
retweets: false, // Show retweets
cacheInMilliseconds: 3600000, // The time to cache a tweet in milliseconds
onComplete: function($ul) {} // On complete callback
};
})(jQuery);
jQuery.support.cors = true;