src/js/widgets/tweetsWidget.js
author hamidouk
Fri, 18 Nov 2011 12:12:28 +0100
branchtweet-widget
changeset 275 a4d2dd99187b
parent 274 fe02d003c6c4
child 276 8a5a34ff1202
permissions -rw-r--r--
WIP - do not use in production ! buggy implementations of queues to handle concurrency. only saved as a snapshot.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
267
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
     1
/* a widget that displays tweet - used in conjunction with the polemicWidget */
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
     2
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
     3
IriSP.TweetsWidget = function(Popcorn, config, Serializer) {
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
     4
  IriSP.Widget.call(this, Popcorn, config, Serializer);
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
     5
  
275
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
     6
  /* this is a bit complex : we use a list to queue the tweets to display, for
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
     7
     two reasons :
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
     8
     - first, We want the pane to retract when there's no tweets to see - it's doable
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
     9
       but it would lead for race conditions - imagine that the user clicks on a tweet
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    10
       and wants it displayed, it would display in an empty pane
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    11
     - second case, the pane displays a tweet and the user clicks before that the tweet
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    12
       has finished displaying - he wants his tweet to be displayed now, not after the
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    13
       previous tweet has finished.
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    14
       
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    15
     So, we queue every tweet in a message queue, which gets consumed at every call of
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    16
     displayTweet.
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    17
  */
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    18
  this._messageQueue = [];
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    19
  
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    20
  /* a variable for an edge case : when the user has clicked on a tweet and clicks on another
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    21
     one before that the first has been cleared. In this case, the second one will be displayed
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    22
     but cleared before its time.
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    23
  */
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    24
  this._tweetClearedBeforeEnd = false;
267
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
    25
};
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
    26
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
    27
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
    28
IriSP.TweetsWidget.prototype = new IriSP.Widget();
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
    29
275
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    30
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    31
IriSP.TweetsWidget.prototype.displayTweet = function(annotation) {
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    32
    
267
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
    33
    var title = annotation.content.title;
274
fe02d003c6c4 added support for a default profile picture in case it's not defined.
hamidouk
parents: 271
diff changeset
    34
    var img = annotation.content.img.src;
fe02d003c6c4 added support for a default profile picture in case it's not defined.
hamidouk
parents: 271
diff changeset
    35
    if (typeof(img) === "undefined" || img === "" || img === "None") {
fe02d003c6c4 added support for a default profile picture in case it's not defined.
hamidouk
parents: 271
diff changeset
    36
      img = IriSP.widgetsDefaults.TweetsWidget.default_profile_picture;
fe02d003c6c4 added support for a default profile picture in case it's not defined.
hamidouk
parents: 271
diff changeset
    37
    }
fe02d003c6c4 added support for a default profile picture in case it's not defined.
hamidouk
parents: 271
diff changeset
    38
    
268
576cc32f0688 added display over the tweeter's avatar.
hamidouk
parents: 267
diff changeset
    39
    var imageMarkup = Mustache.to_html("<img src='{{src}}' alt='avatar'></img>", 
274
fe02d003c6c4 added support for a default profile picture in case it's not defined.
hamidouk
parents: 271
diff changeset
    40
                                       {src : img});
fe02d003c6c4 added support for a default profile picture in case it's not defined.
hamidouk
parents: 271
diff changeset
    41
268
576cc32f0688 added display over the tweeter's avatar.
hamidouk
parents: 267
diff changeset
    42
    this.selector.find(".Ldt-tweetContents").text(title);
576cc32f0688 added display over the tweeter's avatar.
hamidouk
parents: 267
diff changeset
    43
    this.selector.find(".Ldt-tweetAvatar").html(imageMarkup);
271
41258988e132 made the tweetwidget appear and disappear.
hamidouk
parents: 268
diff changeset
    44
    this.selector.show(50);
275
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    45
};
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    46
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    47
IriSP.TweetsWidget.prototype.pushQueue = function(annotation) {
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    48
  this._messageQueue.push(annotation);
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    49
  
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    50
  // we're pushing in the queue before another tweet has been
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    51
  // displayed
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    52
  if (this._messageQueue.length != 0)
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    53
    this._tweetClearedBeforeEnd = true;
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    54
    
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    55
  var annotation = this._messageQueue[0];
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    56
  
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    57
  this.displayTweet(annotation);
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    58
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    59
  var time = this._Popcorn.currentTime();
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    60
  this._Popcorn = this._Popcorn.code({ start : time + 0.1, end: time + 10, 
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    61
                                       onEnd: IriSP.wrap(this, this.clearQueue)});
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    62
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    63
};
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    64
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    65
/* this does essentially the same job than handleQueue, except that it's only
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    66
   called by handleQueue to cleanup after a tweet. */
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    67
IriSP.TweetsWidget.prototype.clearQueue = function() {  
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    68
  var annotation = this._messageQueue.shift();
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    69
  
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    70
  if( this._tweetClearedBeforeEnd === true) {  
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    71
    this._tweetClearedBeforeEnd;
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    72
    return;
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    73
  }
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    74
  
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    75
  if (this._messageQueue === []) {
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    76
    this.closePanel();
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    77
  }
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    78
};
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    79
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    80
IriSP.TweetsWidget.prototype.closePanel = function() {
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    81
  if (this._displayingTweet)
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    82
    return;
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    83
  else {
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    84
    this.selector.hide(50);
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
    85
  }
267
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
    86
};
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
    87
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
    88
IriSP.TweetsWidget.prototype.draw = function() {
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
    89
  var _this = this;
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
    90
  
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
    91
  var tweetMarkup = Mustache.to_html(IriSP.tweetWidget_template, {"share_template" : IriSP.share_template});
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
    92
	this.selector.append(tweetMarkup);
271
41258988e132 made the tweetwidget appear and disappear.
hamidouk
parents: 268
diff changeset
    93
  this.selector.hide();
41258988e132 made the tweetwidget appear and disappear.
hamidouk
parents: 268
diff changeset
    94
  
267
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
    95
  this._Popcorn.listen("IriSP.PolemicTweet.click", IriSP.wrap(this, this.PolemicTweetClickHandler));
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
    96
};
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
    97
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
    98
IriSP.TweetsWidget.prototype.PolemicTweetClickHandler = function(tweet_id) {  
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
    99
  var index, annotation;
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
   100
  for (index in this._serializer._data.annotations) {
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
   101
    annotation = this._serializer._data.annotations[index];
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
   102
    
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
   103
    if (annotation.id === tweet_id)
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
   104
      break;
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
   105
  }
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
   106
    
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
   107
  if (annotation.id !== tweet_id)
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
   108
      /* we haven't found it */
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
   109
      return;
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
   110
  
275
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
   111
  this.pushQueue(annotation);
a4d2dd99187b WIP - do not use in production !
hamidouk
parents: 274
diff changeset
   112
    
267
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
   113
  return;
f84013fb19dc added a new widget, to display the contents of tweets in a separate pane.
hamidouk
parents:
diff changeset
   114
};