src/js/widgets/tweetsWidget.js
branchnew-model
changeset 881 f11b234497f7
parent 880 4c7b33bf2795
child 882 61c384dda19e
equal deleted inserted replaced
880:4c7b33bf2795 881:f11b234497f7
     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   this._hidden = false; /* hidden means that the createAnnotationWidget is shown */
       
     9 };
       
    10 
       
    11 
       
    12 IriSP.TweetsWidget.prototype = new IriSP.Widget();
       
    13 
       
    14 
       
    15 IriSP.TweetsWidget.prototype.drawTweet = function(annotation) {
       
    16     if (this._hidden)
       
    17       return;
       
    18     
       
    19     var title = IriSP.formatTweet(annotation.content.title);
       
    20     var img = annotation.content.img.src;
       
    21     if (typeof(img) === "undefined" || img === "" || img === "None") {
       
    22       img = this.default_profile_picture;
       
    23     }
       
    24 
       
    25     var imageMarkup = IriSP.templToHTML("<img src='{{src}}' alt='user image'></img>", 
       
    26                                        {src : img});
       
    27     
       
    28     if (typeof(IriSP.get_aliased(annotation.meta, ["dc:source", "source"]).content) !== "undefined") {
       
    29       var tweetContents = JSON.parse(IriSP.get_aliased(annotation.meta, ["dc:source", "source"]).content);
       
    30       var creator = tweetContents.user.screen_name;
       
    31       var real_name = tweetContents.user.name;
       
    32 
       
    33       imageMarkup = IriSP.templToHTML("<a href='http://twitter.com/{{creator}}'><img src='{{src}}' alt='user image'></img></a>", 
       
    34                                        {src : img, creator: creator});
       
    35             
       
    36       var formatted_date = new Date(tweetContents.created_at).toLocaleDateString();
       
    37       title = IriSP.templToHTML("<a class='Ldt-tweet_userHandle' href='http://twitter.com/{{creator}}'>@{{creator}}</a> - " + 
       
    38                                 "<div class='Ldt-tweet_realName'>{{real_name}}</div>" +
       
    39                                 "<div class='Ldt-tweet_tweetContents'>{{{ contents }}}</div>" +
       
    40                                 "<div class='Ldt-tweet_date'>{{ date }}</div>", 
       
    41                                 {creator: creator, real_name: real_name, contents : title, date : formatted_date});
       
    42 
       
    43       this.selector.find(".Ldt-TweetReply").attr("href", "http://twitter.com/home?status=@" + creator + ":%20");
       
    44 
       
    45 
       
    46       var rtText = Mustache.to_html("http://twitter.com/home?status=RT @{{creator}}: {{text}}",
       
    47                                     {creator: creator, text: IriSP.encodeURI(annotation.content.title)});
       
    48       this.selector.find(".Ldt-Retweet").attr("href", rtText);
       
    49     }
       
    50 
       
    51     this.selector.find(".Ldt-tweetContents").html(title);
       
    52     this.selector.find(".Ldt-tweetAvatar").html(imageMarkup);
       
    53     this.selector.show("blind", 250); 
       
    54 };
       
    55 
       
    56 IriSP.TweetsWidget.prototype.displayTweet = function(annotation) {
       
    57   if (this._displayingTweet === false) {
       
    58     this._displayingTweet = true;
       
    59   } else {
       
    60     window.clearTimeout(this._timeoutId);
       
    61   }
       
    62 
       
    63   this.drawTweet(annotation);
       
    64 
       
    65   var time = this._Popcorn.currentTime();  
       
    66   this._timeoutId = window.setTimeout(IriSP.wrap(this, this.clearPanel), this.tweet_display_period);
       
    67 };
       
    68 
       
    69 
       
    70 IriSP.TweetsWidget.prototype.clearPanel = function() {  
       
    71     this._displayingTweet = false;
       
    72     this._timeoutId = undefined;
       
    73     this.closePanel();
       
    74     
       
    75 };
       
    76 
       
    77 IriSP.TweetsWidget.prototype.closePanel = function() {
       
    78     if (this._timeoutId != undefined) {
       
    79       /* we're called from the "close window" link */
       
    80       /* cancel the timeout */
       
    81       window.clearTimeout(this._timeoutId);
       
    82       this._timeoutId = null;
       
    83     }
       
    84     
       
    85     this.selector.hide("blind", 400);
       
    86     
       
    87 };
       
    88 
       
    89 /* cancel the timeout if the user clicks on the keep panel open button */
       
    90 IriSP.TweetsWidget.prototype.keepPanel = function() {
       
    91     if (this._timeoutId != undefined) {
       
    92       /* we're called from the "close window" link */
       
    93       /* cancel the timeout */
       
    94       window.clearTimeout(this._timeoutId);
       
    95       this._timeoutId = null;
       
    96     }
       
    97 };
       
    98 
       
    99 IriSP.TweetsWidget.prototype.draw = function() {
       
   100   var _this = this;
       
   101   
       
   102   var tweetMarkup = IriSP.templToHTML(IriSP.tweetWidget_template, {"share_template" : IriSP.share_template});
       
   103   this.selector.append(tweetMarkup);
       
   104   this.selector.hide();
       
   105   this.selector.find(".Ldt-tweetWidgetMinimize").click(IriSP.wrap(this, this.closePanel));
       
   106   this.selector.find(".Ldt-tweetWidgetKeepOpen").click(IriSP.wrap(this, this.keepPanel));
       
   107   
       
   108   this._Popcorn.listen("IriSP.PolemicTweet.click", IriSP.wrap(this, this.PolemicTweetClickHandler));
       
   109   this._Popcorn.listen("IriSP.PlayerWidget.AnnotateButton.clicked", 
       
   110                         IriSP.wrap(this, this.handleAnnotateSignal));  
       
   111 };
       
   112 
       
   113 IriSP.TweetsWidget.prototype.PolemicTweetClickHandler = function(tweet_id) {  
       
   114   var index, annotation;
       
   115   for (index in this._serializer._data.annotations) {
       
   116     annotation = this._serializer._data.annotations[index];
       
   117     
       
   118     if (annotation.id === tweet_id)
       
   119       break;
       
   120   }
       
   121     
       
   122   if (annotation.id !== tweet_id)
       
   123       /* we haven't found it */
       
   124       return;
       
   125   
       
   126   this.displayTweet(annotation);
       
   127   return;
       
   128 };
       
   129 
       
   130 /** handle clicks on the annotate button by hiding/showing itself */
       
   131 IriSP.TweetsWidget.prototype.handleAnnotateSignal = function() {
       
   132   if (this._hidden == false) {
       
   133     this.selector.hide();
       
   134     this._hidden = true;
       
   135   } else {
       
   136     if (this._displayingTweet !== false)
       
   137       this.selector.show();
       
   138       
       
   139     
       
   140     this._hidden = false;
       
   141   }
       
   142 };