diff -r e2c1a2386124 -r 593250f3a286 web/sweet-tweet/script.js
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/web/sweet-tweet/script.js Thu Dec 15 18:24:26 2011 +0100
@@ -0,0 +1,259 @@
+var AVATARWIDTH = 35,
+ COLUMNWIDTH = 2,
+ COLUMNHEIGHT = 420,
+ THRESHOLD = 24,
+ DROPCOUNT = 12;
+
+var swTw = {
+ "keyword" : "#enmi",
+ "columns_words" : [
+ "confiance",
+ "croyance",
+ "crédit",
+ "trace",
+ "foi",
+ "risque",
+ "assurance",
+ "démocratie",
+ "expertise",
+ "catastrophe",
+ "transparence",
+ "politique"
+ ],
+ "tweets" : [],
+ "tweetsIndex" : [],
+ "firstDisplayedTweet" : 0,
+ "cursor" : -1,
+/* "annotations" : {
+ "positive" : {
+ "keyword" : "+",
+ "colors" : {
+ "tweet" : "#c5e7cd",
+ "timeline" : "#00ff00"
+ }
+ },
+ "negative" : {
+ "keyword" : "-",
+ "colors" : {
+ "tweet" : "#f6ced0",
+ "timeline" : "#ff0000"
+ }
+ },
+ "reference" : {
+ "keyword" : "#",
+ "colors" : {
+ "tweet" : "#efefa1",
+ "timeline" : "#ffff00"
+ }
+ },
+ "question" : {
+ "keyword" : "?",
+ "colors" : {
+ "tweet" : "#bfdbec",
+ "timeline" : "#0000ff"
+ }
+ }
+} */
+}
+
+function highlightKeyword(stra, strb) {
+ var rgxp = RegExp( '(' + strb.replace(/(\W)/gm, '\\$1') + ')', "gim");
+ return stra.replace(rgxp, '$1');
+}
+
+function highlightText(txt) {
+ var res = highlightKeyword(txt, swTw.keyword);
+ res = swTw.columns_words.reduce(function(a, b) {
+ return highlightKeyword(a,b);
+ }, res);
+/* res = _(swTw.annotations).reduce(function(a, b) {
+ return (b.keyword ? highlightKeyword(a,b.keyword) : a);
+ }, res); */
+ return res;
+}
+
+function nextTweet() {
+ if (!swTw.tweets.length) {
+ return;
+ }
+ if (swTw.cursor < swTw.tweets.length - 1) {
+ swTw.cursor = Math.max(swTw.cursor + 1, swTw.tweets.length - 120);
+ var nTweet = swTw.cursor;
+ } else {
+ var nTweet = swTw.tweets.length - 1 - ~~( Math.random() * Math.min(swTw.tweets.length,50) );
+ }
+ var tweet = swTw.tweets[nTweet];
+ $("#tweetcont").html('
@' + + tweet.from_user + + ' (' + + tweet.from_user_name + + ')' + + '
' + + highlightText(tweet.text) + + '
'); +/* var bgcolor = ''; + for (var i in swTw.annotations) { + if (swTw.annotations[i].keyword) { + if (tweet.text.indexOf(swTw.annotations[i].keyword) != -1) { + bgcolor = swTw.annotations[i].colors.tweet; + break; + } + } + } */ + $("#tweetcont").css("background",bgcolor); +} + +function dropOldTweets() { + var _newPos = swTw.firstDisplayedTweet + DROPCOUNT; + _(swTw.tweets.slice(swTw.firstDisplayedTweet,_newPos)).each(function(tweet) { + swTw.twInCol = _(swTw.twInCol).map(function(col) { + return _(col).without(tweet.id_str); + }); + _(tweet.elements).each(function(elid) { + $("#" + elid).fadeOut(2000, function() { + $(this).detach(); + }); + }); + }); + + _(swTw.tweets.slice(_newPos)).each(function(tweet) { + _(tweet.elements).each(function(elid) { + var iword = parseInt(elid.split('_')[2]), + iel = swTw.twInCol[iword].indexOf(tweet.id_str), + posx = COLUMNHEIGHT - AVATARWIDTH * (1 + ~~( iel / COLUMNWIDTH)), + posy = AVATARWIDTH * (iel % COLUMNWIDTH); + $("#" + elid).delay(500).animate({ + "top" : posy + "px", + "left" : posx + "px" + }, + 500); + }); + }); + console.log("fin"); + swTw.firstDisplayedTweet = _newPos; +} + +function callbackTweets(tweets) { + _(tweets).each(function(tweet) { + var tl = tweet.text.toLowerCase(); + tweet.columns = swTw.columns_words.filter(function(word) { + return tl.search(word) != -1 + }); + tweet.elements = []; + _(tweet.columns).each(function(word) { + var iword = swTw.columns_words.indexOf(word), + tcl = swTw.twInCol[iword], + tclen = tcl.length; + var posx = COLUMNHEIGHT - AVATARWIDTH * (1 + ~~( tclen / COLUMNWIDTH)), + posy = AVATARWIDTH * (tclen % COLUMNWIDTH), + elid = 'avatar_' + tweet.id_str + '_' + iword, + bgcolor = '#999999'; +/* for (var i in swTw.annotations) { + if (swTw.annotations[i].keyword) { + if (tweet.text.indexOf(swTw.annotations[i].keyword) != -1) { + bgcolor = swTw.annotations[i].colors.timeline; + break; + } + } + } */ + $('#column_' + iword).append( + '