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