web/sweet-tweet/script.js
changeset 416 49ff61b8baf6
parent 415 593250f3a286
child 420 eb7c2cff1816
equal deleted inserted replaced
413:972af2c4af80 416:49ff61b8baf6
       
     1 var AVATARWIDTH = 35,
       
     2     COLUMNWIDTH = 2,
       
     3     COLUMNHEIGHT = 420,
       
     4     THRESHOLD = 24,
       
     5     DROPCOUNT = 12;
       
     6 
       
     7 var swTw = {
       
     8     "keyword" : "#enmi",
       
     9     "columns_words" : [
       
    10         "confiance",
       
    11         "croyance",
       
    12         "crédit",
       
    13         "trace",
       
    14         "foi",
       
    15         "risque", 
       
    16         "assurance", 
       
    17         "démocratie", 
       
    18         "expertise",
       
    19         "catastrophe",
       
    20         "transparence", 
       
    21         "politique"
       
    22     ],
       
    23     "tweets" : [],
       
    24     "tweetsIndex" : [],
       
    25     "firstDisplayedTweet" : 0,
       
    26     "cursor" : -1,
       
    27 /*    "annotations" : {
       
    28         "positive" : {
       
    29             "keyword" : "+",
       
    30             "colors" : {
       
    31                 "tweet" : "#c5e7cd",
       
    32                 "timeline" : "#00ff00"
       
    33             }
       
    34         },
       
    35         "negative" : {
       
    36             "keyword" : "-",
       
    37             "colors" : {
       
    38                 "tweet" : "#f6ced0",
       
    39                 "timeline" : "#ff0000"
       
    40             }
       
    41         },
       
    42         "reference" : {
       
    43             "keyword" : "#",
       
    44             "colors" : {
       
    45                 "tweet" : "#efefa1",
       
    46                 "timeline" : "#ffff00"
       
    47             }
       
    48         },
       
    49         "question" : {
       
    50             "keyword" : "?",
       
    51             "colors" : {
       
    52                 "tweet" : "#bfdbec",
       
    53                 "timeline" : "#0000ff"
       
    54             }
       
    55         }
       
    56 } */
       
    57 }
       
    58 
       
    59 function highlightKeyword(stra, strb) {
       
    60     var rgxp = RegExp( '(' + strb.replace(/(\W)/gm, '\\$1') + ')', "gim");
       
    61     return stra.replace(rgxp, '<span class="highlight">$1</span>');
       
    62 }
       
    63 
       
    64 function highlightText(txt) {
       
    65     var res = highlightKeyword(txt, swTw.keyword);
       
    66     res = swTw.columns_words.reduce(function(a, b) {
       
    67         return highlightKeyword(a,b);
       
    68     }, res);
       
    69 /*    res = _(swTw.annotations).reduce(function(a, b) {
       
    70         return (b.keyword ? highlightKeyword(a,b.keyword) : a);
       
    71     }, res); */
       
    72     return res;
       
    73 }
       
    74 
       
    75 function nextTweet() {
       
    76     if (!swTw.tweets.length) {
       
    77         return;
       
    78     }
       
    79     if (swTw.cursor < swTw.tweets.length - 1) {
       
    80         swTw.cursor = Math.max(swTw.cursor + 1, swTw.tweets.length - 120);
       
    81         var nTweet = swTw.cursor;
       
    82     } else {
       
    83         var nTweet = swTw.tweets.length - 1 - ~~( Math.random() * Math.min(swTw.tweets.length,50) );
       
    84     }
       
    85     var tweet = swTw.tweets[nTweet];
       
    86     $("#tweetcont").html('<img src="'
       
    87         + tweet.profile_image_url
       
    88         + '" /><p>@'
       
    89         + tweet.from_user
       
    90         + ' ('
       
    91         + tweet.from_user_name
       
    92         + ')'
       
    93         + '</p><p class="tweet_text">'
       
    94         + highlightText(tweet.text)
       
    95         + '</p>');
       
    96 /*    var bgcolor = '';
       
    97     for (var i in swTw.annotations) {
       
    98         if (swTw.annotations[i].keyword) {
       
    99             if (tweet.text.indexOf(swTw.annotations[i].keyword) != -1) {
       
   100                 bgcolor = swTw.annotations[i].colors.tweet;
       
   101                 break;
       
   102             }
       
   103         }
       
   104     } */
       
   105     $("#tweetcont").css("background",bgcolor);
       
   106 }
       
   107 
       
   108 function dropOldTweets() {
       
   109     var _newPos = swTw.firstDisplayedTweet + DROPCOUNT;
       
   110     _(swTw.tweets.slice(swTw.firstDisplayedTweet,_newPos)).each(function(tweet) {
       
   111         swTw.twInCol = _(swTw.twInCol).map(function(col) {
       
   112            return _(col).without(tweet.id_str);
       
   113         });
       
   114         _(tweet.elements).each(function(elid) {
       
   115             $("#" + elid).fadeOut(2000, function() {
       
   116                 $(this).detach();
       
   117             });
       
   118         });
       
   119     });
       
   120     
       
   121     _(swTw.tweets.slice(_newPos)).each(function(tweet) {
       
   122         _(tweet.elements).each(function(elid) {
       
   123             var iword = parseInt(elid.split('_')[2]),
       
   124                 iel = swTw.twInCol[iword].indexOf(tweet.id_str),
       
   125                 posx = COLUMNHEIGHT - AVATARWIDTH * (1 + ~~( iel / COLUMNWIDTH)),
       
   126                 posy = AVATARWIDTH * (iel % COLUMNWIDTH);
       
   127             $("#" + elid).delay(500).animate({
       
   128                 "top" : posy + "px",
       
   129                 "left" : posx + "px"
       
   130             },
       
   131             500);
       
   132         });
       
   133     });
       
   134     console.log("fin");
       
   135     swTw.firstDisplayedTweet = _newPos;
       
   136 }
       
   137 
       
   138 function callbackTweets(tweets) {
       
   139     _(tweets).each(function(tweet) {
       
   140         var tl = tweet.text.toLowerCase();
       
   141         tweet.columns = swTw.columns_words.filter(function(word) {
       
   142             return tl.search(word) != -1
       
   143         });
       
   144         tweet.elements = [];
       
   145         _(tweet.columns).each(function(word) {
       
   146             var iword = swTw.columns_words.indexOf(word),
       
   147                 tcl = swTw.twInCol[iword],
       
   148                 tclen = tcl.length;
       
   149             var posx = COLUMNHEIGHT - AVATARWIDTH * (1 + ~~( tclen / COLUMNWIDTH)),
       
   150                 posy = AVATARWIDTH * (tclen % COLUMNWIDTH),
       
   151                 elid = 'avatar_' + tweet.id_str + '_' + iword,
       
   152                 bgcolor = '#999999';
       
   153 /*            for (var i in swTw.annotations) {
       
   154                 if (swTw.annotations[i].keyword) {
       
   155                     if (tweet.text.indexOf(swTw.annotations[i].keyword) != -1) {
       
   156                         bgcolor = swTw.annotations[i].colors.timeline;
       
   157                         break;
       
   158                     }
       
   159                 }
       
   160         } */
       
   161             $('#column_' + iword).append(
       
   162                 '<div class="avatar" id="'
       
   163                 + elid
       
   164                 + '" style="left: -200px; top: '
       
   165                 + posy
       
   166                 + 'px; background: '
       
   167                 + bgcolor
       
   168                 + '"><img src="'
       
   169                 + tweet.profile_image_url
       
   170                 + '" /></div>'
       
   171             );
       
   172             tweet.elements.push(elid);
       
   173             tcl.push(tweet.id_str);
       
   174             $("#" + elid).animate({
       
   175                 "left" : posx + "px"
       
   176             }, 2000);
       
   177         })
       
   178         swTw.tweets.push(tweet);
       
   179         swTw.tweetsIndex.push(tweet.id_str);
       
   180     });
       
   181     while (_(swTw.twInCol).any(function(col) {
       
   182         return col.length > THRESHOLD
       
   183     })) {
       
   184         dropOldTweets();
       
   185     }
       
   186 }
       
   187 
       
   188 function retrieveTweets() {
       
   189     var options =  {
       
   190         "keyword" : swTw.columns_words.join(" OR "),
       
   191         "lang" : "fr",
       
   192 //        "keyword" : "#enmi",
       
   193         "pages" : 1,
       
   194         "rpp" : 50,
       
   195         "cbEnd" : function() {
       
   196             callbackTweets(this.tweets);
       
   197             }
       
   198         }
       
   199     if (swTw.tweets.length) {
       
   200         options.since_id = swTw.tweets[swTw.tweets.length - 1].id_str;
       
   201     }
       
   202     getTweets(options);
       
   203 }
       
   204 
       
   205 function getTweets(options) {
       
   206     function getTweetUrl(url) {
       
   207         $.getJSON(url, function(data) {
       
   208             options.tweets = options.tweets.concat(data.results);
       
   209             options.currentPage = data.page;
       
   210             if (options.cbData) {
       
   211                 options.cbData();
       
   212             }
       
   213             if (data.next_page && data.page < options.pages) {
       
   214                 getTweetUrl(baseurl + data.next_page + suffix);
       
   215             } else {
       
   216                 options.tweets.sort(function(a,b) {
       
   217                    return a.id - b.id; 
       
   218                 });
       
   219                 if (options.cbEnd) {
       
   220                     options.cbEnd();
       
   221                 }
       
   222             }
       
   223         });
       
   224     }
       
   225     
       
   226     options.tweets = [];
       
   227     options.pages || (options.pages = 1);
       
   228     options.rpp || (options.rpp = 100);
       
   229     options.currentPage = 0;
       
   230     
       
   231     var baseurl = "http://search.twitter.com/search.json",
       
   232         suffix = (options.since_id ? "&since_id=" + options.since_id : '' ) + "&callback=?",
       
   233         jsonurl = baseurl + "?q=" + encodeURIComponent(options.keyword)+ "&rpp=" + options.rpp
       
   234             + (options.lang ? "&lang=" + options.lang : '' ) + suffix;
       
   235     getTweetUrl(jsonurl);
       
   236 }
       
   237 
       
   238 $(document).ready(function() {
       
   239     $("#columncont").html( swTw.columns_words.map(
       
   240         function(mot, i) {
       
   241             return '<div class="column" id="column_'
       
   242             + i
       
   243             + '"><div class="column-tube"></div><div class="column-title"><h3>'
       
   244             + mot
       
   245             + '</h3></div></div>'
       
   246         }
       
   247     ).join("") );
       
   248     
       
   249    swTw.twInCol = swTw.columns_words.map(function() {
       
   250        return [];
       
   251    });
       
   252    
       
   253    retrieveTweets();
       
   254    
       
   255    setInterval(retrieveTweets,5000);
       
   256    
       
   257    setInterval(nextTweet, 3000);
       
   258     
       
   259 });