1 gtto = null; |
|
2 tweets = []; |
|
3 |
|
4 function tweetToHtml(tweet) { |
|
5 html = '<li class="tweet'; |
|
6 for (var i in tweet.annotations) { |
|
7 html += ' a_' + tweet.annotations[i].name |
|
8 } |
|
9 html += '" id="tweet_' + tweet.id + '">'; |
|
10 a_user = '<a href="http://twitter.com/"' + tweet.user.screen_name + ' target="_blank">'; |
|
11 if (tweet.user.profile_image_url) { |
|
12 html += a_user + '<img class="tweet_profile_image" src="' + tweet.user.profile_image_url + '" /></a>'; |
|
13 } |
|
14 html += '<h4>' + a_user + '@' + tweet.user.screen_name + '</a></h4><p class="tweet_created_at">' + tweet.created_at + '</p><p>'; |
|
15 lastend = 0; |
|
16 txt = ''; |
|
17 tweet.entities.sort(function(a, b) { return a.indice_start - b.indice_start }); |
|
18 for (var i in tweet.entities) { |
|
19 txt += tweet.text.substring(lastend, tweet.entities[i].indice_start); |
|
20 lastend = tweet.entities[i].indice_end; |
|
21 switch(tweet.entities[i].type) { |
|
22 case "entity_hashtag": |
|
23 txt += '<a href="http://twitter.com/search?q=%23' + tweet.entities[i].entity.text + '" target="_blank">#' + tweet.entities[i].entity.text + '</a>'; |
|
24 break; |
|
25 case "entity_user": |
|
26 txt += '<a href="http://twitter.com/' + tweet.entities[i].entity.screen_name + '" target="_blank">@' + tweet.entities[i].entity.screen_name + '</a>'; |
|
27 break; |
|
28 case "entity_url": |
|
29 case "entity_media": |
|
30 txt += '<a href="' + tweet.entities[i].entity.expanded_url + '" target="_blank">' + tweet.entities[i].entity.expanded_url + '</a>'; |
|
31 break; |
|
32 } |
|
33 } |
|
34 txt += tweet.text.substring(lastend); |
|
35 html += txt + '</p></li>'; |
|
36 return html; |
|
37 } |
|
38 |
|
39 function scheduleTweets() { |
|
40 var tl = $("#tweetlist"), tc = $("#tweetcontainer"); |
|
41 if (tweets.length != tl.children().length) { |
|
42 console.log("Tweet count error"); |
|
43 } |
|
44 if (tweets.length) { |
|
45 while (tl.height() - (tc.scrollTop() + tc.height()) > 1000) { |
|
46 tl.children().last().detach(); |
|
47 tweets.pop(); |
|
48 } |
|
49 if (tl.height() - (tc.scrollTop() + tc.height()) < 120) { |
|
50 getTweets({ |
|
51 before_id : tweets[tweets.length - 1].id, |
|
52 limit : 5 |
|
53 }); |
|
54 } |
|
55 getTweets({ |
|
56 after_id : tweets[0].id |
|
57 }); |
|
58 } else { |
|
59 getTweets({ |
|
60 limit: 15 |
|
61 }); |
|
62 } |
|
63 } |
|
64 |
|
65 function getTweets(params) { |
|
66 $.getJSON("http://" + document.location.hostname + ":8888/?callback=?", params, function(data, a, b) { |
|
67 if (data.tweets && data.tweets.length) { |
|
68 var tl = $("#tweetlist"), tc = $("#tweetcontainer"); |
|
69 html = ''; |
|
70 for (var i in data.tweets) { |
|
71 html += tweetToHtml(data.tweets[i]); |
|
72 } |
|
73 if (params.before_id) { |
|
74 tl.append(html); |
|
75 tweets = tweets.concat(data.tweets); |
|
76 } else { |
|
77 var pos = tc.scrollTop(), fixScroll = (pos > 80); |
|
78 pos -= tl.height(); |
|
79 tl.prepend(html); |
|
80 tweets = data.tweets.concat(tweets); |
|
81 if (fixScroll) { |
|
82 tc.scrollTop(tl.height() + pos); |
|
83 } |
|
84 } |
|
85 } |
|
86 }); |
|
87 } |
|
88 |
|
89 $(document).ready(function() { |
|
90 gtto = setInterval(scheduleTweets, 1500); |
|
91 }); |
|