1 var socket, |
1 var socket, |
2 tweets = [], |
2 tweetData = { |
3 waitoldtweets = true, |
3 "tweetcount" : 0, |
4 tl, |
4 "position" : -1, |
5 tc; |
5 "zoomLevel" : 3, |
|
6 "timeLevel" : 2 |
|
7 } |
|
8 zoomLevels = [ |
|
9 { |
|
10 "description" : "160 tweets per page", |
|
11 "className" : "icons", |
|
12 "displayCount" : 160 |
|
13 }, |
|
14 { |
|
15 "description" : "24 tweets per page", |
|
16 "className" : "quarter", |
|
17 "displayCount" : 24 |
|
18 }, |
|
19 { |
|
20 "description" : "12 tweets per page", |
|
21 "className" : "half", |
|
22 "displayCount" : 12 |
|
23 }, |
|
24 { |
|
25 "description" : "6 tweets per page", |
|
26 "className" : "full", |
|
27 "displayCount" : 6 |
|
28 } |
|
29 ]; |
6 |
30 |
7 function tweetToHtml(tweet) { |
31 function tweetToHtml(tweet) { |
8 html = '<li class="tweet'; |
32 html = '<li class="tweet ' + zoomLevels[tweetData.zoomLevel].className; |
9 for (var i in tweet.annotations) { |
33 for (var i in tweet.annotations) { |
10 html += ' a_' + tweet.annotations[i] |
34 html += ' a_' + tweet.annotations[i] |
11 } |
35 } |
12 html += '" id="tweet_' + tweet.id + '">'; |
36 html += '" id="tweet_' + tweet.id + '">'; |
13 a_user = '<a href="http://twitter.com/' + tweet.user.screen_name + '" target="_blank" title="' + tweet.user.name + '">'; |
37 a_user = '<a href="http://twitter.com/' + tweet.user.screen_name + '" target="_blank" title="' + tweet.user.name + '">'; |
14 if (tweet.user.profile_image_url) { |
38 if (tweet.user.profile_image_url) { |
15 html += a_user + '<img class="tweet_profile_image" src="' + tweet.user.profile_image_url + '" /></a>'; |
39 html += a_user + '<img class="tweet_profile_image" src="' + tweet.user.profile_image_url + '" /></a>'; |
16 } |
40 } |
17 html += '<h4>' + a_user + '@' + tweet.user.screen_name + '</a></h4><p class="tweet_created_at">' + tweet.created_at + '</p><p>'; |
41 html += '<h4>' + a_user + '@' + tweet.user.screen_name + '</a></h4><p class="tweet_created_at">' + new Date(tweet.created_at).toLocaleTimeString() + '</p><p>'; |
18 lastend = 0; |
42 lastend = 0; |
19 txt = ''; |
43 txt = ''; |
20 entities = []; |
44 entities = []; |
21 for (var i in tweet.entities.hashtag) { |
45 for (var i in tweet.entities.hashtag) { |
22 entities.push({ |
46 entities.push({ |
47 txt += tweet.text.substring(lastend); |
71 txt += tweet.text.substring(lastend); |
48 html += txt + '</p></li>'; |
72 html += txt + '</p></li>'; |
49 return html; |
73 return html; |
50 } |
74 } |
51 |
75 |
52 function discardTweets() { |
76 function getTweets() { |
53 if (tweets.length) { |
77 var to = tweetData.position + 1, |
54 while (tl.height() - (tc.scrollTop() + tc.height()) > 1000) { |
78 from = Math.max(0, to - zoomLevels[tweetData.zoomLevel].displayCount); |
55 tl.children().last().detach(); |
79 socket.emit('getTweets',{ "from": from, "to": to }); |
56 tweets.pop(); |
|
57 } |
|
58 } |
|
59 } |
80 } |
60 |
81 |
61 // function scheduleTweets() { |
82 function setZoom(level) { |
62 // var tl = $("#tweetlist"), tc = $("#tweetcontainer"); |
83 tweetData.zoomLevel = Math.max(0, Math.min( zoomLevels.length - 1 , level ) ); |
63 // if (tweets.length != tl.children().length) { |
84 getTweets(); |
64 // console.log("Tweet count error"); |
85 } |
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 |
86 |
111 $(document).ready(function() { |
87 $(document).ready(function() { |
112 tl = $("#tweetlist"); |
|
113 tc = $("#tweetcontainer"); |
|
114 socket = io.connect('http://' + S_IO_HOST + ':' + S_IO_PORT ); |
88 socket = io.connect('http://' + S_IO_HOST + ':' + S_IO_PORT ); |
|
89 socket.on('tweetSummary', function (data) { |
|
90 if (tweetData.position == tweetData.tweetcount - 1) { |
|
91 tweetData.position = data.tweetcount - 1; |
|
92 getTweets(); |
|
93 } |
|
94 tweetData.tweetcount = data.tweetcount; |
|
95 }); |
115 socket.on('tweets', function (data) { |
96 socket.on('tweets', function (data) { |
116 tweets = data; |
97 tweetData.tweetsOnDisplay = data; |
117 data.reverse(); |
98 data.reverse(); |
118 html = ''; |
99 html = ''; |
119 for (var i in data) { |
100 for (var i in data) { |
120 html += tweetToHtml(data[i]); |
101 html += tweetToHtml(data[i]); |
121 } |
102 } |
122 $("#tweetlist").html(html); |
103 $("#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 }); |
104 }); |
153 }); |
105 }); |