tweetcast/nodejs/client/js/script.js
changeset 325 7d9c576bfaac
parent 314 0f1e6ce19b6d
child 326 c28048fb63b4
--- a/tweetcast/nodejs/client/js/script.js	Fri Oct 14 17:36:34 2011 +0200
+++ b/tweetcast/nodejs/client/js/script.js	Mon Oct 17 17:40:58 2011 +0200
@@ -1,11 +1,35 @@
 var socket,
-    tweets = [],
-    waitoldtweets = true,
-    tl,
-    tc;
+    tweetData = {
+        "tweetcount" : 0,
+        "position" : -1,
+        "zoomLevel" : 3,
+        "timeLevel" : 2
+    }
+    zoomLevels = [
+        {
+            "description" : "160 tweets per page",
+            "className" : "icons",
+            "displayCount" : 160
+        },
+        {
+            "description" : "24 tweets per page",
+            "className" : "quarter",
+            "displayCount" : 24
+        },
+        {
+            "description" : "12 tweets per page",
+            "className" : "half",
+            "displayCount" : 12
+        },
+        {
+            "description" : "6 tweets per page",
+            "className" : "full",
+            "displayCount" : 6
+        }
+    ];
 
 function tweetToHtml(tweet) {
-	html = '<li class="tweet';
+	html = '<li class="tweet ' + zoomLevels[tweetData.zoomLevel].className;
 	for (var i in tweet.annotations) {
 		html += ' a_' + tweet.annotations[i]
 	}
@@ -14,7 +38,7 @@
 	if (tweet.user.profile_image_url) {
 		html += a_user + '<img class="tweet_profile_image" src="' + tweet.user.profile_image_url + '" /></a>';
 	}
-	html += '<h4>' + a_user + '@' + tweet.user.screen_name + '</a></h4><p class="tweet_created_at">' + tweet.created_at + '</p><p>';
+	html += '<h4>' + a_user + '@' + tweet.user.screen_name + '</a></h4><p class="tweet_created_at">' + new Date(tweet.created_at).toLocaleTimeString() + '</p><p>';
 	lastend = 0;
 	txt = '';
 	entities = [];
@@ -49,105 +73,33 @@
 	return html;
 }
 
-function discardTweets() {
-    if (tweets.length) {
-        while (tl.height() - (tc.scrollTop() + tc.height()) > 1000) {
- 	        tl.children().last().detach();
-            tweets.pop();
-        }
-    }
+function getTweets() {
+    var to = tweetData.position + 1,
+        from = Math.max(0, to - zoomLevels[tweetData.zoomLevel].displayCount);
+    socket.emit('getTweets',{ "from": from, "to": to });
+}
+
+function setZoom(level) {
+    tweetData.zoomLevel = Math.max(0, Math.min( zoomLevels.length - 1 , level ) );
+    getTweets();
 }
 
-// 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() {
-    tl = $("#tweetlist");
-    tc = $("#tweetcontainer");
     socket = io.connect('http://' + S_IO_HOST + ':' + S_IO_PORT );
+    socket.on('tweetSummary', function (data) {
+        if (tweetData.position == tweetData.tweetcount - 1) {
+            tweetData.position = data.tweetcount - 1;
+            getTweets();
+        }
+        tweetData.tweetcount = data.tweetcount;
+    });
     socket.on('tweets', function (data) {
-        tweets = data;
+        tweetData.tweetsOnDisplay = data;
         data.reverse();
         html = '';
         for (var i in data) {
             html += tweetToHtml(data[i]);
         }
         $("#tweetlist").html(html);
-        discardTweets();
-    });
-    socket.on('oldtweets', function (data) {
-        tweets = tweets.concat(data);
-        html = '';
-        for (var i = data.length - 1; i >= 0; i--) {
-            html += tweetToHtml(data[i]);
-        }
-        $("#tweetlist").append(html);
-        discardTweets();
-        waitoldtweets = true;
-    });
-    socket.on('newtweet', function (data) {
-        tweets.splice(0,0,data);
-        html = tweetToHtml(data);
-        var scrollpos = tc.scrollTop(),
-            fixScroll = (scrollpos > 80);
-        scrollpos -= tl.height();
-        $("#tweetlist").prepend(html);
-        if (fixScroll) {
-    		tc.scrollTop(tl.height() + scrollpos);
-		}
-        discardTweets();
-    });
-    $("#tweetcontainer").scroll(function() {
-        if ( waitoldtweets && tl.height() - (tc.scrollTop() + tc.height()) < 120 ) {
-            socket.emit('tweetsbefore', tweets[tweets.length-1].id );
-            waitoldtweets = false;
-        }
     });
 });
\ No newline at end of file