src/js/widgets/tweetsWidget.js
author hamidouk
Fri, 27 Jan 2012 11:29:03 +0100
branchpopcorn-port
changeset 729 7ba63d0315ad
parent 554 de53cbe1b052
child 820 7968346b9689
permissions -rw-r--r--
load the widget when the video has finished loading.
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;
541
f7667a1dde8e react to said signal.
hamidouk
parents: 378
diff changeset
     7
  this._timeoutId = undefined; 
554
de53cbe1b052 fixed bug in the way the tweetWidget restored itself after that the annotation
hamidouk
parents: 541
diff changeset
     8
  this._hidden = false; /* hidden means that the createAnnotationWidget is shown */
277
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
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    12
IriSP.TweetsWidget.prototype = new IriSP.Widget();
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
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    15
IriSP.TweetsWidget.prototype.drawTweet = function(annotation) {
541
f7667a1dde8e react to said signal.
hamidouk
parents: 378
diff changeset
    16
    if (this._hidden)
f7667a1dde8e react to said signal.
hamidouk
parents: 378
diff changeset
    17
      return;
277
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    18
    
285
b7aa28af2c10 added a function to format a tweet.
hamidouk
parents: 282
diff changeset
    19
    var title = IriSP.formatTweet(annotation.content.title);
277
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    20
    var img = annotation.content.img.src;
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    21
    if (typeof(img) === "undefined" || img === "" || img === "None") {
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    22
      img = IriSP.widgetsDefaults.TweetsWidget.default_profile_picture;
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    23
    }
293
314e6a13f841 tweetWidget : display better information, if available
hamidouk
parents: 285
diff changeset
    24
363
8d4b329836b6 converted the tweet widget to use the new template engine. removed a template of
hamidouk
parents: 350
diff changeset
    25
    var imageMarkup = IriSP.templToHTML("<img src='{{src}}' alt='user image'></img>", 
293
314e6a13f841 tweetWidget : display better information, if available
hamidouk
parents: 285
diff changeset
    26
                                       {src : img});
277
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    27
    
293
314e6a13f841 tweetWidget : display better information, if available
hamidouk
parents: 285
diff changeset
    28
    if (typeof(annotation.meta["dc:source"].content) !== "undefined") {
314e6a13f841 tweetWidget : display better information, if available
hamidouk
parents: 285
diff changeset
    29
      var tweetContents = JSON.parse(annotation.meta["dc:source"].content);
314e6a13f841 tweetWidget : display better information, if available
hamidouk
parents: 285
diff changeset
    30
      var creator = tweetContents.user.screen_name;
367
277a9f5f342c more graphical integration for the tweetsWidget.
hamidouk
parents: 363
diff changeset
    31
      var real_name = tweetContents.user.name;
277a9f5f342c more graphical integration for the tweetsWidget.
hamidouk
parents: 363
diff changeset
    32
363
8d4b329836b6 converted the tweet widget to use the new template engine. removed a template of
hamidouk
parents: 350
diff changeset
    33
      imageMarkup = IriSP.templToHTML("<a href='http://twitter.com/{{creator}}'><img src='{{src}}' alt='user image'></img></a>", 
293
314e6a13f841 tweetWidget : display better information, if available
hamidouk
parents: 285
diff changeset
    34
                                       {src : img, creator: creator});
314e6a13f841 tweetWidget : display better information, if available
hamidouk
parents: 285
diff changeset
    35
            
350
c637688bd680 tweetWidget : format the tweet date according to the locale.
hamidouk
parents: 298
diff changeset
    36
      var formatted_date = new Date(tweetContents.created_at).toLocaleDateString();
367
277a9f5f342c more graphical integration for the tweetsWidget.
hamidouk
parents: 363
diff changeset
    37
      title = IriSP.templToHTML("<a class='Ldt-tweet_userHandle' href='http://twitter.com/{{creator}}'>@{{creator}}</a> - " + 
277a9f5f342c more graphical integration for the tweetsWidget.
hamidouk
parents: 363
diff changeset
    38
                                "<div class='Ldt-tweet_realName'>{{real_name}}</div>" +
277a9f5f342c more graphical integration for the tweetsWidget.
hamidouk
parents: 363
diff changeset
    39
                                "<div class='Ldt-tweet_tweetContents'>{{{ contents }}}</div>" +
277a9f5f342c more graphical integration for the tweetsWidget.
hamidouk
parents: 363
diff changeset
    40
                                "<div class='Ldt-tweet_date'>{{ date }}</div>", 
277a9f5f342c more graphical integration for the tweetsWidget.
hamidouk
parents: 363
diff changeset
    41
                                {creator: creator, real_name: real_name, contents : title, date : formatted_date});
370
2ee4da8122f4 reply and retweet links now work.
hamidouk
parents: 367
diff changeset
    42
2ee4da8122f4 reply and retweet links now work.
hamidouk
parents: 367
diff changeset
    43
      this.selector.find(".Ldt-TweetReply").attr("href", "http://twitter.com/home?status=@" + creator + ":%20");
2ee4da8122f4 reply and retweet links now work.
hamidouk
parents: 367
diff changeset
    44
2ee4da8122f4 reply and retweet links now work.
hamidouk
parents: 367
diff changeset
    45
2ee4da8122f4 reply and retweet links now work.
hamidouk
parents: 367
diff changeset
    46
      var rtText = Mustache.to_html("http://twitter.com/home?status=RT @{{creator}}: {{text}}",
2ee4da8122f4 reply and retweet links now work.
hamidouk
parents: 367
diff changeset
    47
                                    {creator: creator, text: IriSP.encodeURI(annotation.content.title)});
2ee4da8122f4 reply and retweet links now work.
hamidouk
parents: 367
diff changeset
    48
      this.selector.find(".Ldt-Retweet").attr("href", rtText);
293
314e6a13f841 tweetWidget : display better information, if available
hamidouk
parents: 285
diff changeset
    49
    }
277
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    50
285
b7aa28af2c10 added a function to format a tweet.
hamidouk
parents: 282
diff changeset
    51
    this.selector.find(".Ldt-tweetContents").html(title);
277
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    52
    this.selector.find(".Ldt-tweetAvatar").html(imageMarkup);
278
ff20c80c6845 added a minimize functionality to the tweetwidget.
hamidouk
parents: 277
diff changeset
    53
    this.selector.show("blind", 250); 
277
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    54
};
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    55
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    56
IriSP.TweetsWidget.prototype.displayTweet = function(annotation) {
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    57
  if (this._displayingTweet === false) {
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    58
    this._displayingTweet = true;
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    59
  } else {
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    60
    window.clearTimeout(this._timeoutId);
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    61
  }
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    62
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    63
  this.drawTweet(annotation);
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    64
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    65
  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
    66
  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
    67
};
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    68
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    69
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    70
IriSP.TweetsWidget.prototype.clearPanel = function() {  
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    71
    this._displayingTweet = false;
278
ff20c80c6845 added a minimize functionality to the tweetwidget.
hamidouk
parents: 277
diff changeset
    72
    this._timeoutId = undefined;
277
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    73
    this.closePanel();
278
ff20c80c6845 added a minimize functionality to the tweetwidget.
hamidouk
parents: 277
diff changeset
    74
    
277
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    75
};
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    76
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    77
IriSP.TweetsWidget.prototype.closePanel = function() {
278
ff20c80c6845 added a minimize functionality to the tweetwidget.
hamidouk
parents: 277
diff changeset
    78
    if (this._timeoutId != undefined) {
ff20c80c6845 added a minimize functionality to the tweetwidget.
hamidouk
parents: 277
diff changeset
    79
      /* we're called from the "close window" link */
ff20c80c6845 added a minimize functionality to the tweetwidget.
hamidouk
parents: 277
diff changeset
    80
      /* cancel the timeout */
ff20c80c6845 added a minimize functionality to the tweetwidget.
hamidouk
parents: 277
diff changeset
    81
      window.clearTimeout(this._timeoutId);
378
110311d59b2f implemented a second button to keep the tweetWidget open.
hamidouk
parents: 370
diff changeset
    82
      this._timeoutId = null;
278
ff20c80c6845 added a minimize functionality to the tweetwidget.
hamidouk
parents: 277
diff changeset
    83
    }
ff20c80c6845 added a minimize functionality to the tweetwidget.
hamidouk
parents: 277
diff changeset
    84
    
ff20c80c6845 added a minimize functionality to the tweetwidget.
hamidouk
parents: 277
diff changeset
    85
    this.selector.hide("blind", 400);
ff20c80c6845 added a minimize functionality to the tweetwidget.
hamidouk
parents: 277
diff changeset
    86
    
277
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
378
110311d59b2f implemented a second button to keep the tweetWidget open.
hamidouk
parents: 370
diff changeset
    89
/* cancel the timeout if the user clicks on the keep panel open button */
110311d59b2f implemented a second button to keep the tweetWidget open.
hamidouk
parents: 370
diff changeset
    90
IriSP.TweetsWidget.prototype.keepPanel = function() {
110311d59b2f implemented a second button to keep the tweetWidget open.
hamidouk
parents: 370
diff changeset
    91
    if (this._timeoutId != undefined) {
110311d59b2f implemented a second button to keep the tweetWidget open.
hamidouk
parents: 370
diff changeset
    92
      /* we're called from the "close window" link */
110311d59b2f implemented a second button to keep the tweetWidget open.
hamidouk
parents: 370
diff changeset
    93
      /* cancel the timeout */
110311d59b2f implemented a second button to keep the tweetWidget open.
hamidouk
parents: 370
diff changeset
    94
      window.clearTimeout(this._timeoutId);
110311d59b2f implemented a second button to keep the tweetWidget open.
hamidouk
parents: 370
diff changeset
    95
      this._timeoutId = null;
110311d59b2f implemented a second button to keep the tweetWidget open.
hamidouk
parents: 370
diff changeset
    96
    }
110311d59b2f implemented a second button to keep the tweetWidget open.
hamidouk
parents: 370
diff changeset
    97
};
110311d59b2f implemented a second button to keep the tweetWidget open.
hamidouk
parents: 370
diff changeset
    98
277
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
    99
IriSP.TweetsWidget.prototype.draw = function() {
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
   100
  var _this = this;
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
   101
  
363
8d4b329836b6 converted the tweet widget to use the new template engine. removed a template of
hamidouk
parents: 350
diff changeset
   102
  var tweetMarkup = IriSP.templToHTML(IriSP.tweetWidget_template, {"share_template" : IriSP.share_template});
277
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
   103
  this.selector.append(tweetMarkup);
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
   104
  this.selector.hide();
278
ff20c80c6845 added a minimize functionality to the tweetwidget.
hamidouk
parents: 277
diff changeset
   105
  this.selector.find(".Ldt-tweetWidgetMinimize").click(IriSP.wrap(this, this.closePanel));
378
110311d59b2f implemented a second button to keep the tweetWidget open.
hamidouk
parents: 370
diff changeset
   106
  this.selector.find(".Ldt-tweetWidgetKeepOpen").click(IriSP.wrap(this, this.keepPanel));
277
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
   107
  
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
   108
  this._Popcorn.listen("IriSP.PolemicTweet.click", IriSP.wrap(this, this.PolemicTweetClickHandler));
541
f7667a1dde8e react to said signal.
hamidouk
parents: 378
diff changeset
   109
  this._Popcorn.listen("IriSP.PlayerWidget.AnnotateButton.clicked", 
f7667a1dde8e react to said signal.
hamidouk
parents: 378
diff changeset
   110
                        IriSP.wrap(this, this.handleAnnotateSignal));  
277
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
   111
};
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
   112
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
   113
IriSP.TweetsWidget.prototype.PolemicTweetClickHandler = function(tweet_id) {  
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
   114
  var index, annotation;
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
   115
  for (index in this._serializer._data.annotations) {
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
   116
    annotation = this._serializer._data.annotations[index];
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
   117
    
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
   118
    if (annotation.id === tweet_id)
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
   119
      break;
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
   120
  }
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
   121
    
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
   122
  if (annotation.id !== tweet_id)
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
   123
      /* we haven't found it */
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
   124
      return;
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
   125
  
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
   126
  this.displayTweet(annotation);
ff416475397a fixed the bug with a clearTimeout.
hamidouk
parents: 276
diff changeset
   127
  return;
350
c637688bd680 tweetWidget : format the tweet date according to the locale.
hamidouk
parents: 298
diff changeset
   128
};
541
f7667a1dde8e react to said signal.
hamidouk
parents: 378
diff changeset
   129
f7667a1dde8e react to said signal.
hamidouk
parents: 378
diff changeset
   130
/** handle clicks on the annotate button by hiding/showing itself */
f7667a1dde8e react to said signal.
hamidouk
parents: 378
diff changeset
   131
IriSP.TweetsWidget.prototype.handleAnnotateSignal = function() {
f7667a1dde8e react to said signal.
hamidouk
parents: 378
diff changeset
   132
  if (this._hidden == false) {
f7667a1dde8e react to said signal.
hamidouk
parents: 378
diff changeset
   133
    this.selector.hide();
f7667a1dde8e react to said signal.
hamidouk
parents: 378
diff changeset
   134
    this._hidden = true;
f7667a1dde8e react to said signal.
hamidouk
parents: 378
diff changeset
   135
  } else {
554
de53cbe1b052 fixed bug in the way the tweetWidget restored itself after that the annotation
hamidouk
parents: 541
diff changeset
   136
    if (this._displayingTweet !== false)
de53cbe1b052 fixed bug in the way the tweetWidget restored itself after that the annotation
hamidouk
parents: 541
diff changeset
   137
      this.selector.show();
de53cbe1b052 fixed bug in the way the tweetWidget restored itself after that the annotation
hamidouk
parents: 541
diff changeset
   138
      
de53cbe1b052 fixed bug in the way the tweetWidget restored itself after that the annotation
hamidouk
parents: 541
diff changeset
   139
    
541
f7667a1dde8e react to said signal.
hamidouk
parents: 378
diff changeset
   140
    this._hidden = false;
f7667a1dde8e react to said signal.
hamidouk
parents: 378
diff changeset
   141
  }
f7667a1dde8e react to said signal.
hamidouk
parents: 378
diff changeset
   142
};