-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalexandria.js
255 lines (214 loc) · 7.95 KB
/
alexandria.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
jQuery.extend(Date.prototype, {
toLocaleFormat: function(str) {
var daysOfTheWeek = 'Sunday Monday Tuesday Wednesday Thursday Friday Saturday'.split(' ');
var monthsOfTheYear = 'January February March April May June July August September October November December'.split(' ');
str = str.replace(/%c/g, '%D %T');
str = str.replace(/%D/g, '%m/%d/%y');
str = str.replace(/%r/g, '%I:%M:%S %p');
str = str.replace(/%R/g, '%H:%M');
str = str.replace(/%T/g, '%H:%M:%S');
str = str.replace(/%x/g, '%A, %B %d');
str = str.replace(/%X/g, '%H:%M%p');
str = str.replace(/%a/g, daysOfTheWeek[this.getDay()].substr(0, 3));
str = str.replace(/%A/g, daysOfTheWeek[this.getDay()])
str = str.replace(/%b/g, monthsOfTheYear[this.getMonth()].substr(0, 3));
str = str.replace(/%B/g, monthsOfTheYear[this.getMonth()]);
// %c is handled above
str = str.replace(/%C/g, this.getFullYear() / 100);
str = str.replace(/%d/g, this.getDate());
// $D is handled above
str = str.replace(/%h/g, monthsOfTheYear[this.getMonth()].substr(0, 3));
str = str.replace(/%H/g, this.getHours());
str = str.replace(/%I/g, (this.getHours() % 12) + 1);
str = str.replace(/%p/g, this.getHours() < 12 ? "am" : "pm");
str = str.replace(/%S/g, this.getSeconds());
str = str.replace(/%u/g, this.getDay() + 1);
str = str.replace(/%w/g, this.getDay());
str = str.replace(/%m/g, this.getMonth() + 1);
str = str.replace(/%M/g, this.getMinutes());
str = str.replace(/%y/g, this.getFullYear().toString().substr(2));
str = str.replace(/%Y/g, this.getFullYear());
return str
}
});
(function($){
function userUrl(screenName) {
if (screenName.substr(0, 1) == '@') screenName = screenName.substr(1);
return "http://twitter.com/" + screenName;
}
function tweetUrl(screenName, idStr) {
return userUrl(screenName) + "/status/" + idStr;
}
function tagUrl(tag) {
return "http://search.twitter.com/?q=#{tag}"
}
function addEntity(text, entity, wrapper) {
var range = entity.indices;
var start = range[0];
var end = range[1];
var before = text.substr(0, start);
var during = text.substr(start, end - start);
var after = text.substr(end);
return before + wrapper(during, entity) + after;
}
var methods = {
init: function(opts) {
var $this = $(this);
var settings = $.extend({
tweets: []
}, opts || {});
$this.data('tweetlib', settings);
$("<div class='controls'>").appendTo($this).append(
$('<input>').keyup(function() {
$this.tweetlib('filter', function(tweet) {
return tweet.text && tweet.text.match($(this).val());
})
})
);
$.each(settings.tweets, function(ix, tweet) {
var tweetDiv = $this.tweetlib('formatTweetDiv', tweet);
var dayDiv = $this.tweetlib('getDayDiv', tweet.created_at);
dayDiv.append(tweetDiv);
});
$this.find('.tweets-in-month').each(function(ix, monthDiv) {
monthDiv = $(monthDiv);
var count = monthDiv.find('.tweet').length;
var str = count + " tweet" + (count == 1 ? '' : "s");
monthDiv.children('.meta').text(str);
});
$this.find('.tweets-on-day').each(function(ix, dayDiv) {
dayDiv = $(dayDiv);
var count = dayDiv.find('.tweet').length;
var str = count + " tweet" + (count == 1 ? '' : "s");
dayDiv.find('h4 .meta').text(str);
});
return $this;
},
formatTweetUrl: function(user_screen_name, id_str) {
return tweetUrl(user_screen_name, id_str);
},
formatTweetDiv: function(tweet) {
// {"truncated":false,"created_at":"Fri Nov 26 05:04:17 +0000 2010","geo":null,
// "favorited":false,"source":"<a href=\"http://twitterrific.com\" rel=\"nofollow\">Twitterrific</a>",
// "in_reply_to_status_id_str":"8020197273239552","id_str":"8023212889739265","contributors":null,
// "coordinates":null,"in_reply_to_screen_name":"JssSandals","in_reply_to_user_id_str":"15693316",
// "place":null,"user":{"id_str":"10588782"}, "retweet_count":null,"retweeted":false,
// "text":"@JssSandals but tomorrow's family feast exists in a time warp where it is still Thanksgiving, so no Christmas music there."}
var $this = this;
var tweetDiv = $("<div class='tweet'/>").attr('id', tweet.id_str);
var contentDiv = $("<div class='content'>");
var metaDiv = $("<div class='meta'>");
tweetDiv.data('tweet', tweet);
tweet.div = tweetDiv;
tweet.created_at_str = tweet.created_at;
tweet.created_at = new Date(tweet.created_at_str);
contentDiv.append(tweet.text);
if (tweet.truncated) tweetDiv.append('…');
if (tweet.favorited) tweetDiv.addClass('favorite');
if (tweet.retweeted) tweetDiv.addClass('retweeted');
var permalink = $("<a>");
var user = $this.data('tweetlib').users[tweet.user.id_str] || {};
permalink.attr('href', tweetUrl(user.screen_name, tweet.id_str));
permalink.append(tweet.created_at.toString());
metaDiv.append(permalink);
if (!('autolinked' in tweet)) tweetDiv.addClass('from-archive');
//TODO: add geo
//TODO: add coordinates
//TODO: add contributors
//TODO: add places
metaDiv.append($("<div class='source'>").append('Via ').append(tweet.source));
if (tweet.in_reply_to_screen_name) {
tweetDiv.addClass('reply');
var replyDiv = $("<div class='reply-info'>");
replyDiv.append('In reply to ');
var replyLink = $('<a>').append(tweet.in_reply_to_screen_name);
var replyToUrl = (tweet.in_reply_to_status_id_str)
? tweetUrl(tweet.in_reply_to_screen_name, tweet.in_reply_to_status_id_str)
: userUrl(tweet.in_reply_to_screen_name);
replyLink.attr('href', replyToUrl);
replyDiv.append(replyLink)
metaDiv.append(replyDiv);
}
if (tweet.retweet_count) {
var retweetDiv = $("<div class='retweet-info'>");
retweetDiv.append("Retweeted " + tweet.retweet_count + " times.");
metaDiv.append(retweetDiv);
}
tweetDiv.append(contentDiv)
tweetDiv.append(metaDiv)
return tweetDiv;
},
getDayDiv: function(date) {
//TODO scope this inside $this
date = new Date(date);
var id = date.toLocaleFormat("tweets-on-%Y-%m-%d");
var div = $('#' + id);
if (div.length) return div;
return $("<div class='tweets-on-day'>").
attr('id', id).
appendTo($(this).tweetlib('getMonthDiv', date)).
append($('<h4>').
append(date.toLocaleFormat("%a, %b %d '%y")).
append($("<div class='meta'>").append(' ')));
},
getMonthDiv: function(date) {
//TODO scope this inside $this
date = new Date(date);
var id = date.toLocaleFormat("tweets-in-%Y-%m");
var div = $('#' + id);
if (div.length) return div;
return $("<div class='tweets-in-month'>").
attr('id', id).
appendTo($(this)).
append($('<h4>').append(date.toLocaleFormat("%b '%y"))).
append($("<div class='meta'>").append(' '));
},
getTweets: function() {
return $(this).data('tweetlib').tweets;
},
filter: function(predicate) {
if (typeof(predicate) == "string") {
var searchTerm = predicate;
predicate = function(t) {
return t.text && t.text.toLowerCase().match(searchTerm.toLowerCase());
}
}
var $this = $(this);
$.each($this.tweetlib('getTweets'), function(ix, tweet) {
if (predicate(tweet)) {
tweet.div.show();
} else tweet.div.hide();
});
return $this;
}
};
$.fn.tweetlib = function(method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || ! method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.tweetlib');
}
};
$.extend($.fn.tweetlib, methods);
})(jQuery);
$(document).ready(function() {
if (document.location.href.match(/subset/)) {
tweets = [
tweets[0],
tweets[100],
tweets[101],
tweets[200],
tweets[201],
tweets[1000],
tweets[tweets.length - 1]
];
}
tweets = tweets.sort(function(a, b) {
return a.created_at > b.created_at
? 1
: -1;
});
$(document.body).tweetlib({tweets: tweets, users: users});
})