src/widgets/Tweet.js
changeset 944 8a6c9e3d0158
parent 918 5cd368dba1b9
child 957 4da0a5740b6c
child 1019 3ab36f402b0c
equal deleted inserted replaced
907:27b248a13355 944:8a6c9e3d0158
       
     1 IriSP.Widgets.Tweet = function(player, config) {
       
     2     IriSP.Widgets.Widget.call(this, player, config);
       
     3     this.lastAnnotation = false;
       
     4 };
       
     5 
       
     6 IriSP.Widgets.Tweet.prototype = new IriSP.Widgets.Widget();
       
     7 
       
     8 IriSP.Widgets.Tweet.prototype.defaults = {
       
     9     hide_timeout: 5000,
       
    10     polemics : [
       
    11         {
       
    12             "keywords" : [ "++" ],
       
    13             "color" : "#30d765"
       
    14         },
       
    15         {
       
    16             "keywords" : [ "--" ],
       
    17             "color" : "#f51123"
       
    18         },
       
    19         {
       
    20             "keywords" : [ "==" ],
       
    21             "color" : "#f1e24a"  
       
    22         },
       
    23         {
       
    24             "keywords" : [ "??" ],
       
    25             "color" : "#05aae6"
       
    26         }
       
    27     ],
       
    28     pin_at_start: false
       
    29 }
       
    30 
       
    31 IriSP.Widgets.Tweet.prototype.messages = {
       
    32     "fr": {
       
    33         retweet: "Retweeter",
       
    34         reply: "Répondre",
       
    35         keep_visible: "Garder visible",
       
    36         dont_keep_visible: "Permettre la fermeture automatique",
       
    37         close_widget: "Fermer l'affichage du tweet",
       
    38         original_time: "Heure d'envoi : ",
       
    39         video_time: "Temps de la vidéo : ",
       
    40         show_original: "Voir l'original"
       
    41     },
       
    42     "en": {
       
    43         retweet: "Retweet",
       
    44         reply: "Reply",
       
    45         keep_visible: "Keep visible",
       
    46         dont_keep_visible: "Don't keep visible",
       
    47         close_widget: "Close tweet display",
       
    48         original_time: "Tweet sent at: ",
       
    49         video_time: "Video time: ",
       
    50         show_original: "Show original"
       
    51     }
       
    52 }
       
    53 
       
    54 IriSP.Widgets.Tweet.prototype.template =
       
    55     '<div class="Ldt-Tweet-Widget"><div class="Ldt-Tweet-Inner"><div class="Ldt-Tweet-PinClose-Buttons">'
       
    56     + '<a href="#" class="Ldt-Tweet-Pin Ldt-TraceMe{{#pin_at_start}} active" title="{{l10n.dont_keep_visible}}{{/pin_at_start}}{{^pin_at_start}}" title="{{l10n.keep_visible}}{{/pin_at_start}}"></a>'
       
    57     + '<a href="#" class="Ldt-Tweet-Close Ldt-TraceMe" title="{{l10n.close_widget}}"></a>'
       
    58     + '</div><div class="Ldt-Tweet-AvatarContainer"><a href="#" class="Ldt-Tweet-ProfileLink" target="_blank">'
       
    59     + '<img src="" class="Ldt-Tweet-Avatar"/></a></div><h3><a href="#" class="Ldt-Tweet-ProfileLink Ldt-Tweet-ScreenName" target="_blank">'
       
    60     + '</a> (<span class="Ldt-Tweet-FullName"></span>)</h3><p class="Ldt-Tweet-Contents"></p><div class="Ldt-Tweet-Bottom">'
       
    61     + '<span class="Ldt-Tweet-Time"></span>'
       
    62     + '<a class="Ldt-Tweet-Original" href="" target="_blank">{{l10n.show_original}}</a>'
       
    63     + '<a href="" target="_blank" class="Ldt-Tweet-Retweet"><div class="Ldt-Tweet-Icon"></div>{{l10n.retweet}}</a>'
       
    64     + '<a href="" target="_blank" class="Ldt-Tweet-Reply"><div class="Ldt-Tweet-Icon"></div>{{l10n.reply}}</a></div></div></div>';
       
    65     
       
    66 
       
    67 IriSP.Widgets.Tweet.prototype.draw = function() {
       
    68     this.renderTemplate();
       
    69     this.bindPopcorn("IriSP.Tweet.show","show");
       
    70     this.pinned = this.pin_at_start;
       
    71     var _this = this;
       
    72     this.$.find(".Ldt-Tweet-Pin").click(function() {
       
    73         _this.pinned = !_this.pinned;
       
    74         var _el = IriSP.jQuery(this)
       
    75         if (_this.pinned) {
       
    76             _el.addClass("active").attr("title",_this.l10n.dont_keep_visible);
       
    77             _this.cancelTimeout();
       
    78         } else {
       
    79             _el.removeClass("active").attr("title",_this.l10n.keep_visible);
       
    80             _this.hideTimeout();
       
    81         }
       
    82     });
       
    83     this.$.find(".Ldt-Tweet-Close").click(function() {
       
    84         _this.hide();
       
    85     });
       
    86     this.$.hide();
       
    87 }
       
    88 
       
    89 IriSP.Widgets.Tweet.prototype.show = function(_id) {
       
    90     var _tweet = this.source.getElement(_id);
       
    91     if (typeof _tweet !== "undefined" && typeof _tweet.source !== "undefined") {
       
    92         var _entities = [];
       
    93         for (var _i = 0; _i < _tweet.source.entities.hashtags.length; _i++) {
       
    94             var _hash = _tweet.source.entities.hashtags[_i];
       
    95             _entities.push({
       
    96                 is_link: true,
       
    97                 text: '#' + _hash.text,
       
    98                 url: 'http://twitter.com/search?q=%23' + encodeURIComponent(_hash.text),
       
    99                 indices: _hash.indices
       
   100             });
       
   101         }
       
   102         for (var _i = 0; _i < _tweet.source.entities.urls.length; _i++) {
       
   103             var _url = _tweet.source.entities.urls[_i],
       
   104                 _displayurl = (typeof _url.display_url !== "undefined" && _url.display_url !== null ? _url.display_url : _url.url),
       
   105                 _linkurl = (typeof _url.expanded_url !== "undefined" && _url.expanded_url !== null ? _url.expanded_url : _url.url);
       
   106             _displayurl = _displayurl.replace(/^\w+:\/\//,'');
       
   107             if (!/^\w+:\/\//.test(_linkurl)) {
       
   108                 _linkurl = 'http://' + _linkurl;
       
   109             }
       
   110             _entities.push({
       
   111                 is_link: true,
       
   112                 text: _displayurl,
       
   113                 url: _linkurl,
       
   114                 indices: _url.indices
       
   115             });
       
   116         }
       
   117         for (var _i = 0; _i < _tweet.source.entities.user_mentions.length; _i++) {
       
   118             var _user = _tweet.source.entities.user_mentions[_i];
       
   119             _entities.push({
       
   120                 is_link: true,
       
   121                 text: '@' + _user.screen_name,
       
   122                 url: 'http://twitter.com/' + encodeURIComponent(_user.screen_name),
       
   123                 indices: _user.indices
       
   124             });
       
   125         }
       
   126         for (var _i = 0; _i < this.polemics.length; _i++) {
       
   127             for (var _j = 0; _j < this.polemics[_i].keywords.length; _j++) {
       
   128                 var _p = _tweet.source.text.indexOf(this.polemics[_i].keywords[_j]);
       
   129                 while (_p !== -1) {
       
   130                     var _end = (_p + this.polemics[_i].keywords[_j].length);
       
   131                     _entities.push({
       
   132                         is_link: false,
       
   133                         text: this.polemics[_i].keywords[_j],
       
   134                         color: this.polemics[_i].color,
       
   135                         indices: [_p, _end]
       
   136                     });
       
   137                     _p = _tweet.source.text.indexOf(this.polemics[_i].keywords[_j], _end);
       
   138                 }
       
   139             }
       
   140         }
       
   141         _entities = IriSP._(_entities).sortBy(function(_entity) {
       
   142             return _entity.indices[0];
       
   143         });
       
   144         var _currentPos = 0,
       
   145             _txt = '';
       
   146         for (var _i = 0; _i < _entities.length; _i++) {
       
   147             if (_entities[_i].indices[0] >= _currentPos) {
       
   148                 _txt += _tweet.source.text.substring(_currentPos, _entities[_i].indices[0]);
       
   149                 _currentPos = _entities[_i].indices[1];
       
   150                 if (_entities[_i].is_link) {
       
   151                     _txt += '<a href="' + _entities[_i].url + '" target="_blank">';
       
   152                 } else {
       
   153                     _txt += '<span style="background:' + _entities[_i].color + '">';
       
   154                 }
       
   155                 _txt += _entities[_i].text;
       
   156                 if (_entities[_i].is_link) {
       
   157                     _txt += '</a>';
       
   158                 } else {
       
   159                     _txt += '</span>';
       
   160                 }
       
   161             }
       
   162         }
       
   163         _txt += _tweet.source.text.substring(_currentPos);
       
   164         this.$.find(".Ldt-Tweet-Avatar").attr("src",_tweet.source.user.profile_image_url);
       
   165         this.$.find(".Ldt-Tweet-ScreenName").html('@'+_tweet.source.user.screen_name);
       
   166         this.$.find(".Ldt-Tweet-ProfileLink").attr("href", "https://twitter.com/" + _tweet.source.user.screen_name);
       
   167         this.$.find(".Ldt-Tweet-FullName").html(_tweet.source.user.name);
       
   168         this.$.find(".Ldt-Tweet-Contents").html(_txt);
       
   169         this.$.find(".Ldt-Tweet-Time").html(this.l10n.original_time + new Date(_tweet.source.created_at).toLocaleTimeString() + " / " + this.l10n.video_time + _tweet.begin.toString());
       
   170         this.$.find(".Ldt-Tweet-Retweet").attr("href", "https://twitter.com/intent/retweet?tweet_id=" + _tweet.source.id_str);
       
   171         this.$.find(".Ldt-Tweet-Reply").attr("href", "https://twitter.com/intent/tweet?in_reply_to=" + _tweet.source.id_str);
       
   172         this.$.find(".Ldt-Tweet-Original").attr("href", "https://twitter.com/" + _tweet.source.user.screen_name + "/status/" + _tweet.source.id_str);
       
   173         this.player.popcorn.trigger("IriSP.Annotation.minimize");
       
   174         this.$.slideDown();
       
   175         this.cancelTimeout();
       
   176         if (!this.pinned) {
       
   177             this.hideTimeout();
       
   178         }
       
   179     } else {
       
   180         this.hide();
       
   181     }
       
   182 }
       
   183 
       
   184 IriSP.Widgets.Tweet.prototype.hide = function() {
       
   185     this.player.popcorn.trigger("IriSP.Annotation.maximize");
       
   186     this.$.slideUp();
       
   187     this.cancelTimeout();
       
   188 }
       
   189 
       
   190 IriSP.Widgets.Tweet.prototype.cancelTimeout = function() {
       
   191     if (typeof this.hide_timer !== "undefined") {
       
   192         window.clearTimeout(this.hide_timer);
       
   193         this.hide_timer = undefined;
       
   194     }  
       
   195 }
       
   196 
       
   197 IriSP.Widgets.Tweet.prototype.hideTimeout = function() {
       
   198     this.cancelTimeout();
       
   199     this.hide_timer = window.setTimeout(this.functionWrapper("hide"), this.hide_timeout);
       
   200 }