src/js/widgets/tweetsWidget.js
branchtweet-widget
changeset 276 8a5a34ff1202
parent 275 a4d2dd99187b
child 277 ff416475397a
equal deleted inserted replaced
275:a4d2dd99187b 276:8a5a34ff1202
     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
     6   this._isDisplayingTweet = false;
     7      two reasons :
     7   this._ignoreClear = false;
     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;
       
    25 };
     8 };
    26 
     9 
    27 
    10 
    28 IriSP.TweetsWidget.prototype = new IriSP.Widget();
    11 IriSP.TweetsWidget.prototype = new IriSP.Widget();
    29 
    12 
    30 
    13 
    31 IriSP.TweetsWidget.prototype.displayTweet = function(annotation) {
    14 IriSP.TweetsWidget.prototype.drawTweet = function(annotation) {
    32     
    15     
    33     var title = annotation.content.title;
    16     var title = annotation.content.title;
    34     var img = annotation.content.img.src;
    17     var img = annotation.content.img.src;
    35     if (typeof(img) === "undefined" || img === "" || img === "None") {
    18     if (typeof(img) === "undefined" || img === "" || img === "None") {
    36       img = IriSP.widgetsDefaults.TweetsWidget.default_profile_picture;
    19       img = IriSP.widgetsDefaults.TweetsWidget.default_profile_picture;
    42     this.selector.find(".Ldt-tweetContents").text(title);
    25     this.selector.find(".Ldt-tweetContents").text(title);
    43     this.selector.find(".Ldt-tweetAvatar").html(imageMarkup);
    26     this.selector.find(".Ldt-tweetAvatar").html(imageMarkup);
    44     this.selector.show(50);
    27     this.selector.show(50);
    45 };
    28 };
    46 
    29 
    47 IriSP.TweetsWidget.prototype.pushQueue = function(annotation) {
    30 IriSP.TweetsWidget.prototype.displayTweet = function(annotation) {
    48   this._messageQueue.push(annotation);
    31   if (this._isDisplayingTweet === false) {
       
    32     this._isDisplayingTweet = true;
       
    33   } else { /* we're already displaying a tweet */
       
    34     this._ignoreClear = true;
       
    35   }
    49   
    36   
    50   // we're pushing in the queue before another tweet has been
    37   this.drawTweet(annotation);
    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 
    38 
    59   var time = this._Popcorn.currentTime();
    39   var time = this._Popcorn.currentTime();  
    60   this._Popcorn = this._Popcorn.code({ start : time + 0.1, end: time + 10, 
    40   // this._Popcorn.exec(time + 10, IriSP.wrap(this, this.clearPanel)); 
    61                                        onEnd: IriSP.wrap(this, this.clearQueue)});
    41   window.setTimeout(IriSP.wrap(this, this.clearPanel), 10000);
    62 
       
    63 };
    42 };
    64 
    43 
    65 /* this does essentially the same job than handleQueue, except that it's only
    44 
    66    called by handleQueue to cleanup after a tweet. */
    45 IriSP.TweetsWidget.prototype.clearPanel = function() {
    67 IriSP.TweetsWidget.prototype.clearQueue = function() {  
    46   debugger;
    68   var annotation = this._messageQueue.shift();
    47   if (this._ignoreClear === true) {
    69   
    48     this._ignoreClear = false;
    70   if( this._tweetClearedBeforeEnd === true) {  
       
    71     this._tweetClearedBeforeEnd;
       
    72     return;
    49     return;
    73   }
    50   } else {
    74   
    51     /* clear the display */
    75   if (this._messageQueue === []) {
       
    76     this.closePanel();
    52     this.closePanel();
       
    53     this._isDisplayingTweet = false;    
       
    54     this._ignoreClear = false;    
    77   }
    55   }
    78 };
    56 };
    79 
    57 
    80 IriSP.TweetsWidget.prototype.closePanel = function() {
    58 IriSP.TweetsWidget.prototype.closePanel = function() {
    81   if (this._displayingTweet)
    59   if (this._displayingTweet)
   106     
    84     
   107   if (annotation.id !== tweet_id)
    85   if (annotation.id !== tweet_id)
   108       /* we haven't found it */
    86       /* we haven't found it */
   109       return;
    87       return;
   110   
    88   
   111   this.pushQueue(annotation);
    89   this.displayTweet(annotation);
   112     
       
   113   return;
    90   return;
   114 };
    91 };