src/js/widgets/tweetsWidget.js
branchtweet-widget
changeset 275 a4d2dd99187b
parent 274 fe02d003c6c4
child 276 8a5a34ff1202
equal deleted inserted replaced
274:fe02d003c6c4 275:a4d2dd99187b
     1 /* a widget that displays tweet - used in conjunction with the polemicWidget */
     1 /* a widget that displays tweet - used in conjunction with the polemicWidget */
     2 
     2 
     3 IriSP.TweetsWidget = function(Popcorn, config, Serializer) {
     3 IriSP.TweetsWidget = function(Popcorn, config, Serializer) {
     4   IriSP.Widget.call(this, Popcorn, config, Serializer);
     4   IriSP.Widget.call(this, Popcorn, config, Serializer);
     5   
     5   
       
     6   /* this is a bit complex : we use a list to queue the tweets to display, for
       
     7      two reasons :
       
     8      - first, We want the pane to retract when there's no tweets to see - it's doable
       
     9        but it would lead for race conditions - imagine that the user clicks on a tweet
       
    10        and wants it displayed, it would display in an empty pane
       
    11      - second case, the pane displays a tweet and the user clicks before that the tweet
       
    12        has finished displaying - he wants his tweet to be displayed now, not after the
       
    13        previous tweet has finished.
       
    14        
       
    15      So, we queue every tweet in a message queue, which gets consumed at every call of
       
    16      displayTweet.
       
    17   */
       
    18   this._messageQueue = [];
       
    19   
       
    20   /* a variable for an edge case : when the user has clicked on a tweet and clicks on another
       
    21      one before that the first has been cleared. In this case, the second one will be displayed
       
    22      but cleared before its time.
       
    23   */
       
    24   this._tweetClearedBeforeEnd = false;
     6 };
    25 };
     7 
    26 
     8 
    27 
     9 IriSP.TweetsWidget.prototype = new IriSP.Widget();
    28 IriSP.TweetsWidget.prototype = new IriSP.Widget();
    10 
    29 
    11 IriSP.TweetsWidget.prototype.displayTweet = function(annotation) {    
    30 
       
    31 IriSP.TweetsWidget.prototype.displayTweet = function(annotation) {
       
    32     
    12     var title = annotation.content.title;
    33     var title = annotation.content.title;
    13     var img = annotation.content.img.src;
    34     var img = annotation.content.img.src;
    14     if (typeof(img) === "undefined" || img === "" || img === "None") {
    35     if (typeof(img) === "undefined" || img === "" || img === "None") {
    15       img = IriSP.widgetsDefaults.TweetsWidget.default_profile_picture;
    36       img = IriSP.widgetsDefaults.TweetsWidget.default_profile_picture;
    16     }
    37     }
    19                                        {src : img});
    40                                        {src : img});
    20 
    41 
    21     this.selector.find(".Ldt-tweetContents").text(title);
    42     this.selector.find(".Ldt-tweetContents").text(title);
    22     this.selector.find(".Ldt-tweetAvatar").html(imageMarkup);
    43     this.selector.find(".Ldt-tweetAvatar").html(imageMarkup);
    23     this.selector.show(50);
    44     this.selector.show(50);
    24     window.setTimeout(IriSP.wrap(this, function() { this.selector.hide(50) }), 10000);
    45 };
       
    46 
       
    47 IriSP.TweetsWidget.prototype.pushQueue = function(annotation) {
       
    48   this._messageQueue.push(annotation);
       
    49   
       
    50   // we're pushing in the queue before another tweet has been
       
    51   // displayed
       
    52   if (this._messageQueue.length != 0)
       
    53     this._tweetClearedBeforeEnd = true;
       
    54     
       
    55   var annotation = this._messageQueue[0];
       
    56   
       
    57   this.displayTweet(annotation);
       
    58 
       
    59   var time = this._Popcorn.currentTime();
       
    60   this._Popcorn = this._Popcorn.code({ start : time + 0.1, end: time + 10, 
       
    61                                        onEnd: IriSP.wrap(this, this.clearQueue)});
       
    62 
       
    63 };
       
    64 
       
    65 /* this does essentially the same job than handleQueue, except that it's only
       
    66    called by handleQueue to cleanup after a tweet. */
       
    67 IriSP.TweetsWidget.prototype.clearQueue = function() {  
       
    68   var annotation = this._messageQueue.shift();
       
    69   
       
    70   if( this._tweetClearedBeforeEnd === true) {  
       
    71     this._tweetClearedBeforeEnd;
       
    72     return;
       
    73   }
       
    74   
       
    75   if (this._messageQueue === []) {
       
    76     this.closePanel();
       
    77   }
       
    78 };
       
    79 
       
    80 IriSP.TweetsWidget.prototype.closePanel = function() {
       
    81   if (this._displayingTweet)
       
    82     return;
       
    83   else {
       
    84     this.selector.hide(50);
       
    85   }
    25 };
    86 };
    26 
    87 
    27 IriSP.TweetsWidget.prototype.draw = function() {
    88 IriSP.TweetsWidget.prototype.draw = function() {
    28   var _this = this;
    89   var _this = this;
    29   
    90   
    45     
   106     
    46   if (annotation.id !== tweet_id)
   107   if (annotation.id !== tweet_id)
    47       /* we haven't found it */
   108       /* we haven't found it */
    48       return;
   109       return;
    49   
   110   
    50 
   111   this.pushQueue(annotation);
    51   this.displayTweet(annotation);
   112     
    52   
       
    53   var time = this._Popcorn.currentTime();
       
    54   this._Popcorn = this._Popcorn.code({ start : time + 0.1, end: time + 10, 
       
    55                                        onEnd: IriSP.wrap(this, this.clearTweet)});
       
    56   
       
    57   return;
   113   return;
    58 };
   114 };