src/js/widgets/tweetsWidget.js
author hamidouk
Thu, 24 Nov 2011 12:40:54 +0100
branchpopcorn-port
changeset 321 21d840371c6b
parent 298 eccdc619ede3
child 350 c637688bd680
permissions -rw-r--r--
Arrow Positioning that Just Works (tm) cleaned up the code, removed useless signal IriSP.SegmentClick. The arrowWidget now watches time changes and updates itself accordingly.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
277
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
     1
/* a widget that displays tweet - used in conjunction with the polemicWidget */
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
     2
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
     3
IriSP.TweetsWidget = function(Popcorn, config, Serializer) {
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
     4
  IriSP.Widget.call(this, Popcorn, config, Serializer);
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
     5
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
     6
  this._displayingTweet = false;
278
ff20c80c6845 added a minimize functionality to the tweetwidget.
hamidouk
parents: 277
diff changeset
     7
  this._timeoutId = undefined;  
277
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
     8
};
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
     9
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    10
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    11
IriSP.TweetsWidget.prototype = new IriSP.Widget();
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    12
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    13
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    14
IriSP.TweetsWidget.prototype.drawTweet = function(annotation) {
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    15
    
285
b7aa28af2c10 added a function to format a tweet.
hamidouk
parents: 282
diff changeset
    16
    var title = IriSP.formatTweet(annotation.content.title);
277
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    17
    var img = annotation.content.img.src;
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    18
    if (typeof(img) === "undefined" || img === "" || img === "None") {
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    19
      img = IriSP.widgetsDefaults.TweetsWidget.default_profile_picture;
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    20
    }
293
314e6a13f841 tweetWidget : display better information, if available
hamidouk
parents: 285
diff changeset
    21
314e6a13f841 tweetWidget : display better information, if available
hamidouk
parents: 285
diff changeset
    22
    var imageMarkup = Mustache.to_html("<img src='{{src}}' alt='user image'></img>", 
314e6a13f841 tweetWidget : display better information, if available
hamidouk
parents: 285
diff changeset
    23
                                       {src : img});
277
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    24
    
293
314e6a13f841 tweetWidget : display better information, if available
hamidouk
parents: 285
diff changeset
    25
    if (typeof(annotation.meta["dc:source"].content) !== "undefined") {
314e6a13f841 tweetWidget : display better information, if available
hamidouk
parents: 285
diff changeset
    26
      var tweetContents = JSON.parse(annotation.meta["dc:source"].content);
314e6a13f841 tweetWidget : display better information, if available
hamidouk
parents: 285
diff changeset
    27
      var creator = tweetContents.user.screen_name;
314e6a13f841 tweetWidget : display better information, if available
hamidouk
parents: 285
diff changeset
    28
      
314e6a13f841 tweetWidget : display better information, if available
hamidouk
parents: 285
diff changeset
    29
      imageMarkup = Mustache.to_html("<a href='http://twitter.com/{{creator}}'><img src='{{src}}' alt='user image'></img></a>", 
314e6a13f841 tweetWidget : display better information, if available
hamidouk
parents: 285
diff changeset
    30
                                       {src : img, creator: creator});
314e6a13f841 tweetWidget : display better information, if available
hamidouk
parents: 285
diff changeset
    31
            
314e6a13f841 tweetWidget : display better information, if available
hamidouk
parents: 285
diff changeset
    32
      title = Mustache.to_html(IriSP.rich_tweet_template, {contents : title, date : tweetContents.created_at});
314e6a13f841 tweetWidget : display better information, if available
hamidouk
parents: 285
diff changeset
    33
    }
277
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    34
285
b7aa28af2c10 added a function to format a tweet.
hamidouk
parents: 282
diff changeset
    35
    this.selector.find(".Ldt-tweetContents").html(title);
277
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    36
    this.selector.find(".Ldt-tweetAvatar").html(imageMarkup);
278
ff20c80c6845 added a minimize functionality to the tweetwidget.
hamidouk
parents: 277
diff changeset
    37
    this.selector.show("blind", 250); 
277
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    38
};
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    39
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    40
IriSP.TweetsWidget.prototype.displayTweet = function(annotation) {
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    41
  if (this._displayingTweet === false) {
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    42
    this._displayingTweet = true;
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    43
  } else {
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    44
    window.clearTimeout(this._timeoutId);
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    45
  }
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    46
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    47
  this.drawTweet(annotation);
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    48
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    49
  var time = this._Popcorn.currentTime();  
282
1001234e5e6e added a var to configure the num of seconds to display the tweet.
hamidouk
parents: 278
diff changeset
    50
  this._timeoutId = window.setTimeout(IriSP.wrap(this, this.clearPanel), IriSP.widgetsDefaults.TweetsWidget.tweet_display_period);
277
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    51
};
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    52
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    53
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    54
IriSP.TweetsWidget.prototype.clearPanel = function() {  
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    55
    this._displayingTweet = false;
278
ff20c80c6845 added a minimize functionality to the tweetwidget.
hamidouk
parents: 277
diff changeset
    56
    this._timeoutId = undefined;
277
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    57
    this.closePanel();
278
ff20c80c6845 added a minimize functionality to the tweetwidget.
hamidouk
parents: 277
diff changeset
    58
    
277
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    59
};
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    60
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    61
IriSP.TweetsWidget.prototype.closePanel = function() {
278
ff20c80c6845 added a minimize functionality to the tweetwidget.
hamidouk
parents: 277
diff changeset
    62
    if (this._timeoutId != undefined) {
ff20c80c6845 added a minimize functionality to the tweetwidget.
hamidouk
parents: 277
diff changeset
    63
      /* we're called from the "close window" link */
ff20c80c6845 added a minimize functionality to the tweetwidget.
hamidouk
parents: 277
diff changeset
    64
      /* cancel the timeout */
ff20c80c6845 added a minimize functionality to the tweetwidget.
hamidouk
parents: 277
diff changeset
    65
      window.clearTimeout(this._timeoutId);
ff20c80c6845 added a minimize functionality to the tweetwidget.
hamidouk
parents: 277
diff changeset
    66
    }
ff20c80c6845 added a minimize functionality to the tweetwidget.
hamidouk
parents: 277
diff changeset
    67
    
ff20c80c6845 added a minimize functionality to the tweetwidget.
hamidouk
parents: 277
diff changeset
    68
    this.selector.hide("blind", 400);
ff20c80c6845 added a minimize functionality to the tweetwidget.
hamidouk
parents: 277
diff changeset
    69
    
277
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    70
};
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    71
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    72
IriSP.TweetsWidget.prototype.draw = function() {
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    73
  var _this = this;
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    74
  
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    75
  var tweetMarkup = Mustache.to_html(IriSP.tweetWidget_template, {"share_template" : IriSP.share_template});
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    76
  this.selector.append(tweetMarkup);
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    77
  this.selector.hide();
278
ff20c80c6845 added a minimize functionality to the tweetwidget.
hamidouk
parents: 277
diff changeset
    78
  this.selector.find(".Ldt-tweetWidgetMinimize").click(IriSP.wrap(this, this.closePanel));
277
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    79
  
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    80
  this._Popcorn.listen("IriSP.PolemicTweet.click", IriSP.wrap(this, this.PolemicTweetClickHandler));
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    81
};
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    82
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    83
IriSP.TweetsWidget.prototype.PolemicTweetClickHandler = function(tweet_id) {  
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    84
  var index, annotation;
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    85
  for (index in this._serializer._data.annotations) {
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    86
    annotation = this._serializer._data.annotations[index];
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    87
    
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    88
    if (annotation.id === tweet_id)
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    89
      break;
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    90
  }
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    91
    
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    92
  if (annotation.id !== tweet_id)
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    93
      /* we haven't found it */
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    94
      return;
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    95
  
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    96
  this.displayTweet(annotation);
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    97
  return;
267
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
    98
};