diff -r 526d3e411736 -r 13702105c5ee tweetcast/gevent/client/js/script.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tweetcast/gevent/client/js/script.js Wed Oct 12 18:11:19 2011 +0200 @@ -0,0 +1,91 @@ +gtto = null; +tweets = []; + +function tweetToHtml(tweet) { + html = '
  • '; + a_user = ''; + if (tweet.user.profile_image_url) { + html += a_user + ''; + } + html += '

    ' + a_user + '@' + tweet.user.screen_name + '

    ' + tweet.created_at + '

    '; + lastend = 0; + txt = ''; + tweet.entities.sort(function(a, b) { return a.indice_start - b.indice_start }); + for (var i in tweet.entities) { + txt += tweet.text.substring(lastend, tweet.entities[i].indice_start); + lastend = tweet.entities[i].indice_end; + switch(tweet.entities[i].type) { + case "entity_hashtag": + txt += '#' + tweet.entities[i].entity.text + ''; + break; + case "entity_user": + txt += '@' + tweet.entities[i].entity.screen_name + ''; + break; + case "entity_url": + case "entity_media": + txt += '' + tweet.entities[i].entity.expanded_url + ''; + break; + } + } + txt += tweet.text.substring(lastend); + html += txt + '

  • '; + return html; +} + +function scheduleTweets() { + var tl = $("#tweetlist"), tc = $("#tweetcontainer"); + if (tweets.length != tl.children().length) { + console.log("Tweet count error"); + } + if (tweets.length) { + while (tl.height() - (tc.scrollTop() + tc.height()) > 1000) { + tl.children().last().detach(); + tweets.pop(); + } + if (tl.height() - (tc.scrollTop() + tc.height()) < 120) { + getTweets({ + before_id : tweets[tweets.length - 1].id, + limit : 5 + }); + } + getTweets({ + after_id : tweets[0].id + }); + } else { + getTweets({ + limit: 15 + }); + } +} + +function getTweets(params) { + $.getJSON("http://" + document.location.hostname + ":8888/?callback=?", params, function(data, a, b) { + if (data.tweets && data.tweets.length) { + var tl = $("#tweetlist"), tc = $("#tweetcontainer"); + html = ''; + for (var i in data.tweets) { + html += tweetToHtml(data.tweets[i]); + } + if (params.before_id) { + tl.append(html); + tweets = tweets.concat(data.tweets); + } else { + var pos = tc.scrollTop(), fixScroll = (pos > 80); + pos -= tl.height(); + tl.prepend(html); + tweets = data.tweets.concat(tweets); + if (fixScroll) { + tc.scrollTop(tl.height() + pos); + } + } + } + }); +} + +$(document).ready(function() { + gtto = setInterval(scheduleTweets, 1500); +}); \ No newline at end of file