src/js/widgets/tweetsWidget.js
branchpopcorn-port
changeset 286 6252f951d11c
parent 285 b7aa28af2c10
child 293 314e6a13f841
equal deleted inserted replaced
279:2c65775623cb 286:6252f951d11c
       
     1 /* a widget that displays tweet - used in conjunction with the polemicWidget */
       
     2 
       
     3 IriSP.TweetsWidget = function(Popcorn, config, Serializer) {
       
     4   IriSP.Widget.call(this, Popcorn, config, Serializer);
       
     5 
       
     6   this._displayingTweet = false;
       
     7   this._timeoutId = undefined;  
       
     8 };
       
     9 
       
    10 
       
    11 IriSP.TweetsWidget.prototype = new IriSP.Widget();
       
    12 
       
    13 
       
    14 IriSP.TweetsWidget.prototype.drawTweet = function(annotation) {
       
    15     
       
    16     var title = IriSP.formatTweet(annotation.content.title);
       
    17     var img = annotation.content.img.src;
       
    18     if (typeof(img) === "undefined" || img === "" || img === "None") {
       
    19       img = IriSP.widgetsDefaults.TweetsWidget.default_profile_picture;
       
    20     }
       
    21     
       
    22     var imageMarkup = Mustache.to_html("<img src='{{src}}' alt='avatar'></img>", 
       
    23                                        {src : img});
       
    24 
       
    25     this.selector.find(".Ldt-tweetContents").html(title);
       
    26     this.selector.find(".Ldt-tweetAvatar").html(imageMarkup);
       
    27     this.selector.show("blind", 250); 
       
    28 };
       
    29 
       
    30 IriSP.TweetsWidget.prototype.displayTweet = function(annotation) {
       
    31   if (this._displayingTweet === false) {
       
    32     this._displayingTweet = true;
       
    33   } else {
       
    34     window.clearTimeout(this._timeoutId);
       
    35   }
       
    36 
       
    37   this.drawTweet(annotation);
       
    38 
       
    39   var time = this._Popcorn.currentTime();  
       
    40   this._timeoutId = window.setTimeout(IriSP.wrap(this, this.clearPanel), IriSP.widgetsDefaults.TweetsWidget.tweet_display_period);
       
    41 };
       
    42 
       
    43 
       
    44 IriSP.TweetsWidget.prototype.clearPanel = function() {  
       
    45     this._displayingTweet = false;
       
    46     this._timeoutId = undefined;
       
    47     this.closePanel();
       
    48     
       
    49 };
       
    50 
       
    51 IriSP.TweetsWidget.prototype.closePanel = function() {
       
    52     if (this._timeoutId != undefined) {
       
    53       /* we're called from the "close window" link */
       
    54       /* cancel the timeout */
       
    55       window.clearTimeout(this._timeoutId);
       
    56     }
       
    57     
       
    58     this.selector.hide("blind", 400);
       
    59     
       
    60 };
       
    61 
       
    62 IriSP.TweetsWidget.prototype.draw = function() {
       
    63   var _this = this;
       
    64   
       
    65   var tweetMarkup = Mustache.to_html(IriSP.tweetWidget_template, {"share_template" : IriSP.share_template});
       
    66   this.selector.append(tweetMarkup);
       
    67   this.selector.hide();
       
    68   this.selector.find(".Ldt-tweetWidgetMinimize").click(IriSP.wrap(this, this.closePanel));
       
    69   
       
    70   this._Popcorn.listen("IriSP.PolemicTweet.click", IriSP.wrap(this, this.PolemicTweetClickHandler));
       
    71 };
       
    72 
       
    73 IriSP.TweetsWidget.prototype.PolemicTweetClickHandler = function(tweet_id) {  
       
    74   var index, annotation;
       
    75   for (index in this._serializer._data.annotations) {
       
    76     annotation = this._serializer._data.annotations[index];
       
    77     
       
    78     if (annotation.id === tweet_id)
       
    79       break;
       
    80   }
       
    81     
       
    82   if (annotation.id !== tweet_id)
       
    83       /* we haven't found it */
       
    84       return;
       
    85   
       
    86   this.displayTweet(annotation);
       
    87   return;
       
    88 };