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