|
1 var socket, |
|
2 tweets = [], |
|
3 waitoldtweets = true, |
|
4 tl, |
|
5 tc; |
|
6 |
|
7 function tweetToHtml(tweet) { |
|
8 html = '<li class="tweet'; |
|
9 for (var i in tweet.annotations) { |
|
10 html += ' a_' + tweet.annotations[i] |
|
11 } |
|
12 html += '" id="tweet_' + tweet.id + '">'; |
|
13 a_user = '<a href="http://twitter.com/' + tweet.user.screen_name + '" target="_blank" title="' + tweet.user.name + '">'; |
|
14 if (tweet.user.profile_image_url) { |
|
15 html += a_user + '<img class="tweet_profile_image" src="' + tweet.user.profile_image_url + '" /></a>'; |
|
16 } |
|
17 html += '<h4>' + a_user + '@' + tweet.user.screen_name + '</a></h4><p class="tweet_created_at">' + tweet.created_at + '</p><p>'; |
|
18 lastend = 0; |
|
19 txt = ''; |
|
20 entities = []; |
|
21 for (var i in tweet.entities.hashtag) { |
|
22 entities.push({ |
|
23 "start" : tweet.entities.hashtag[i].indices[0], |
|
24 "end" : tweet.entities.hashtag[i].indices[1], |
|
25 "html" : '<a href="http://twitter.com/search?q=%23' + tweet.entities.hashtag[i].text + '" target="_blank">#' + tweet.entities.hashtag[i].text + '</a>' |
|
26 }); |
|
27 } |
|
28 for (var i in tweet.entities.urls) { |
|
29 entities.push({ |
|
30 "start" : tweet.entities.urls[i].indices[0], |
|
31 "end" : tweet.entities.urls[i].indices[1], |
|
32 "html" : '<a href="' + tweet.entities.urls[i].expanded_url + '" target="_blank">' + tweet.entities.urls[i].display_url + '</a>' |
|
33 }); |
|
34 } |
|
35 for (var i in tweet.entities.user_mentions) { |
|
36 entities.push({ |
|
37 "start" : tweet.entities.user_mentions[i].indices[0], |
|
38 "end" : tweet.entities.user_mentions[i].indices[1], |
|
39 "html" : '<a href="http://twitter.com/' + tweet.entities.user_mentions[i].screen_name + '" target="_blank" title="' + tweet.entities.user_mentions[i].name + '">@' + tweet.entities.user_mentions[i].screen_name + '</a>' |
|
40 }); |
|
41 } |
|
42 entities.sort(function(a, b) { return a.start - b.start }); |
|
43 for (var i in entities) { |
|
44 txt += tweet.text.substring(lastend, entities[i].start) + entities[i].html; |
|
45 lastend = entities[i].end; |
|
46 } |
|
47 txt += tweet.text.substring(lastend); |
|
48 html += txt + '</p></li>'; |
|
49 return html; |
|
50 } |
|
51 |
|
52 function discardTweets() { |
|
53 if (tweets.length) { |
|
54 while (tl.height() - (tc.scrollTop() + tc.height()) > 1000) { |
|
55 tl.children().last().detach(); |
|
56 tweets.pop(); |
|
57 } |
|
58 } |
|
59 } |
|
60 |
|
61 // function scheduleTweets() { |
|
62 // var tl = $("#tweetlist"), tc = $("#tweetcontainer"); |
|
63 // if (tweets.length != tl.children().length) { |
|
64 // console.log("Tweet count error"); |
|
65 // } |
|
66 // if (tweets.length) { |
|
67 // while (tl.height() - (tc.scrollTop() + tc.height()) > 1000) { |
|
68 // tl.children().last().detach(); |
|
69 // tweets.pop(); |
|
70 // } |
|
71 // if (tl.height() - (tc.scrollTop() + tc.height()) < 120) { |
|
72 // getTweets({ |
|
73 // before_id : tweets[tweets.length - 1].id, |
|
74 // limit : 5 |
|
75 // }); |
|
76 // } |
|
77 // getTweets({ |
|
78 // after_id : tweets[0].id |
|
79 // }); |
|
80 // } else { |
|
81 // getTweets({ |
|
82 // limit: 15 |
|
83 // }); |
|
84 // } |
|
85 // } |
|
86 // |
|
87 // function getTweets(params) { |
|
88 // $.getJSON("http://" + document.location.hostname + ":8888/?callback=?", params, function(data, a, b) { |
|
89 // if (data.tweets && data.tweets.length) { |
|
90 // var tl = $("#tweetlist"), tc = $("#tweetcontainer"); |
|
91 // html = ''; |
|
92 // for (var i in data.tweets) { |
|
93 // html += tweetToHtml(data.tweets[i]); |
|
94 // } |
|
95 // if (params.before_id) { |
|
96 // tl.append(html); |
|
97 // tweets = tweets.concat(data.tweets); |
|
98 // } else { |
|
99 // var pos = tc.scrollTop(), fixScroll = (pos > 80); |
|
100 // pos -= tl.height(); |
|
101 // tl.prepend(html); |
|
102 // tweets = data.tweets.concat(tweets); |
|
103 // if (fixScroll) { |
|
104 // tc.scrollTop(tl.height() + pos); |
|
105 // } |
|
106 // } |
|
107 // } |
|
108 // }); |
|
109 // } |
|
110 |
|
111 $(document).ready(function() { |
|
112 tl = $("#tweetlist"); |
|
113 tc = $("#tweetcontainer"); |
|
114 socket = io.connect('http://' + S_IO_HOST + ':' + S_IO_PORT ); |
|
115 socket.on('tweets', function (data) { |
|
116 tweets = data; |
|
117 data.reverse(); |
|
118 html = ''; |
|
119 for (var i in data) { |
|
120 html += tweetToHtml(data[i]); |
|
121 } |
|
122 $("#tweetlist").html(html); |
|
123 discardTweets(); |
|
124 }); |
|
125 socket.on('oldtweets', function (data) { |
|
126 tweets = tweets.concat(data); |
|
127 html = ''; |
|
128 for (var i = data.length - 1; i >= 0; i--) { |
|
129 html += tweetToHtml(data[i]); |
|
130 } |
|
131 $("#tweetlist").append(html); |
|
132 discardTweets(); |
|
133 waitoldtweets = true; |
|
134 }); |
|
135 socket.on('newtweet', function (data) { |
|
136 tweets.splice(0,0,data); |
|
137 html = tweetToHtml(data); |
|
138 var scrollpos = tc.scrollTop(), |
|
139 fixScroll = (scrollpos > 80); |
|
140 scrollpos -= tl.height(); |
|
141 $("#tweetlist").prepend(html); |
|
142 if (fixScroll) { |
|
143 tc.scrollTop(tl.height() + scrollpos); |
|
144 } |
|
145 discardTweets(); |
|
146 }); |
|
147 $("#tweetcontainer").scroll(function() { |
|
148 if ( waitoldtweets && tl.height() - (tc.scrollTop() + tc.height()) < 120 ) { |
|
149 socket.emit('tweetsbefore', tweets[tweets.length-1].id ); |
|
150 waitoldtweets = false; |
|
151 } |
|
152 }); |
|
153 }); |