client/js/twitter-bin.js
changeset 49 d0da34fda4b4
parent 44 869410bab434
child 56 a9b9e6c7be63
equal deleted inserted replaced
48:01fb9167ad75 49:d0da34fda4b4
    42     this.title_icon_$.addClass('Rk-Twitter-Title-Icon');
    42     this.title_icon_$.addClass('Rk-Twitter-Title-Icon');
    43     this.title_$.html(this.search).addClass("Rk-Twitter-Title");
    43     this.title_$.html(this.search).addClass("Rk-Twitter-Title");
    44     this.refresh();
    44     this.refresh();
    45 }
    45 }
    46 
    46 
       
    47 Rkns.Twitter.Bin.prototype.render = function(searchstr) {
       
    48     var _rgxp = new RegExp('('+(searchstr || this.search).replace(/(\W)/g,'\\$1')+')','gi');
       
    49     if (searchstr) {
       
    50         var rxtest = new RegExp(searchstr.replace(/(\W)/g,'\\$1'),'i');
       
    51     }
       
    52     function highlight(_text) {
       
    53         return _text.replace(_rgxp, "<span class='searchmatch'>$1</span>");
       
    54     }
       
    55     var _html = ""
       
    56         _this = this,
       
    57         count = 0;
       
    58     Rkns._(this.data.results).each(function(_result) {
       
    59         if (searchstr && !rxtest.test(_result.text)) {
       
    60             return;
       
    61         }
       
    62         count++;
       
    63         var _entities = [],
       
    64             _users = _result.text.match(/@[\w_]+/gm),
       
    65             _lastpos = 0;
       
    66         if (_users) {
       
    67             for (var _i = 0; _i < _users.length; _i++) {
       
    68                 var _m = _users[_i],
       
    69                     _start = _lastpos + _result.text.substr(_lastpos).search(_m),
       
    70                     _lastpos = _start + _m.length;
       
    71                 _entities.push({
       
    72                     "text" : _m,
       
    73                     "start" : _start,
       
    74                     "end" : _lastpos,
       
    75                     "link" :'<a href="http://twitter.com/' + _m.substr(1) + '" onclick="filtrerTexte(\'' + _m + '\'); return false;" target="_blank">',
       
    76                 });
       
    77             }
       
    78         }
       
    79         var _hashes = _result.text.match(/([^&]|^)#[^\s,.!?=#@&;()]+/gm),
       
    80             _lastpos = 0;
       
    81         if (_hashes) {
       
    82             for (var _i = 0; _i < _hashes.length; _i++) {
       
    83                 var _m = _hashes[_i],
       
    84                     _h = ( _m[0] == '#' ? _m : _m.substr(1) ),
       
    85                     _start = _lastpos + _result.text.substr(_lastpos).search(_h),
       
    86                     _lastpos = _start + _h.length;
       
    87                 _entities.push({
       
    88                     "text" : _h,
       
    89                     "start" : _start,
       
    90                     "end" : _lastpos,
       
    91                     "link" :'<a href="http://twitter.com/search?q=' + encodeURIComponent(_h) + '" onclick="filtrerTexte(\'' + _.escape(_h) + '\'); return false;" target="_blank">',
       
    92                 });
       
    93             }
       
    94         }
       
    95         
       
    96         var _urls = _result.text.match(/(www\.|https?:\/\/)[\w./_\-]+/gim),
       
    97             _lastpos = 0;
       
    98         if (_urls) {
       
    99             for (var _i = 0; _i < _urls.length; _i++) {
       
   100                 var _m = _urls[_i],
       
   101                     _start = _lastpos + _result.text.substr(_lastpos).search(_m),
       
   102                     _lastpos = _start + _m.length;
       
   103                 _entities.push({
       
   104                     "text" : _m,
       
   105                     "start" : _start,
       
   106                     "end" : _lastpos,
       
   107                     "link" :'<a href="' + _m + '" target="_blank">',
       
   108                 });
       
   109             }
       
   110         }
       
   111         _entities = Rkns._(_entities).sortBy(function(a) { return a.start });
       
   112         var _lastend = 0,
       
   113             _text = Rkns._(_entities).map(function(_e) {
       
   114                 var _txt = highlight(_result.text.substring(_lastend, _e.start)) + _e.link + highlight(_e.text) + '</a>';
       
   115                 _lastend = _e.end;
       
   116                 return _txt;
       
   117             }).join("") + highlight(_result.text.substring(_lastend));
       
   118         
       
   119         _html += _this.tweetTemplate({
       
   120             tweet: _result,
       
   121             date: new Date(_result.created_at.replace(/(\+|-)/,'UTC$1')).toLocaleString(),
       
   122             text: _text
       
   123         });
       
   124     });
       
   125     this.main_$.html(_html);
       
   126     if (searchstr && count) {
       
   127         this.count_$.text(count).show();
       
   128     } else {
       
   129         this.count_$.hide();
       
   130     }
       
   131     if (searchstr && !count) {
       
   132         this.$.hide();
       
   133     } else {
       
   134         this.$.show();
       
   135     }
       
   136     _renkan.resizeBins();
       
   137 }
       
   138 
    47 Rkns.Twitter.Bin.prototype.refresh = function() {
   139 Rkns.Twitter.Bin.prototype.refresh = function() {
    48     var _this = this;
   140     var _this = this;
    49     Rkns.$.ajax({
   141     Rkns.$.ajax({
    50         url: "http://search.twitter.com/search.json?q=" + encodeURIComponent(this.search),
   142         url: "http://search.twitter.com/search.json",
    51         dataType: "jsonp",
   143         dataType: "jsonp",
       
   144         data: {
       
   145             q: this.search,
       
   146             rpp: 100
       
   147         },
    52         success: function(_data) {
   148         success: function(_data) {
    53             var _rgxp = new RegExp('('+_this.search.replace(/(\W)/g,'\\$1')+')','gi');
   149             _this.data = _data;
    54             function highlight(_text) {
   150             _this.render();
    55                 return _text.replace(_rgxp, "<span class='searchmatch'>$1</span>");
       
    56             }
       
    57             var _html = Rkns._(_data.results).map(function(_result) {
       
    58                 var _entities = [],
       
    59                     _users = _result.text.match(/@[\w_]+/gm),
       
    60                     _lastpos = 0;
       
    61                 if (_users) {
       
    62                     for (var _i = 0; _i < _users.length; _i++) {
       
    63                         var _m = _users[_i],
       
    64                             _start = _lastpos + _result.text.substr(_lastpos).search(_m),
       
    65                             _lastpos = _start + _m.length;
       
    66                         _entities.push({
       
    67                             "text" : _m,
       
    68                             "start" : _start,
       
    69                             "end" : _lastpos,
       
    70                             "link" :'<a href="http://twitter.com/' + _m.substr(1) + '" onclick="filtrerTexte(\'' + _m + '\'); return false;" target="_blank">',
       
    71                         });
       
    72                     }
       
    73                 }
       
    74                 var _hashes = _result.text.match(/([^&]|^)#[^\s,.!?=#@&;()]+/gm),
       
    75                     _lastpos = 0;
       
    76                 if (_hashes) {
       
    77                     for (var _i = 0; _i < _hashes.length; _i++) {
       
    78                         var _m = _hashes[_i],
       
    79                             _h = ( _m[0] == '#' ? _m : _m.substr(1) ),
       
    80                             _start = _lastpos + _result.text.substr(_lastpos).search(_h),
       
    81                             _lastpos = _start + _h.length;
       
    82                         _entities.push({
       
    83                             "text" : _h,
       
    84                             "start" : _start,
       
    85                             "end" : _lastpos,
       
    86                             "link" :'<a href="http://twitter.com/search?q=' + encodeURIComponent(_h) + '" onclick="filtrerTexte(\'' + _.escape(_h) + '\'); return false;" target="_blank">',
       
    87                         });
       
    88                     }
       
    89                 }
       
    90                 
       
    91                 var _urls = _result.text.match(/(www\.|https?:\/\/)[\w./_\-]+/gim),
       
    92                     _lastpos = 0;
       
    93                 if (_urls) {
       
    94                     for (var _i = 0; _i < _urls.length; _i++) {
       
    95                         var _m = _urls[_i],
       
    96                             _start = _lastpos + _result.text.substr(_lastpos).search(_m),
       
    97                             _lastpos = _start + _m.length;
       
    98                         _entities.push({
       
    99                             "text" : _m,
       
   100                             "start" : _start,
       
   101                             "end" : _lastpos,
       
   102                             "link" :'<a href="' + _m + '" target="_blank">',
       
   103                         });
       
   104                     }
       
   105                 }
       
   106                 _entities = Rkns._(_entities).sortBy(function(a) { return a.start });
       
   107                 var _lastend = 0,
       
   108                     _text = Rkns._(_entities).map(function(_e) {
       
   109                         var _txt = highlight(_result.text.substring(_lastend, _e.start)) + _e.link + highlight(_e.text) + '</a>';
       
   110                         _lastend = _e.end;
       
   111                         return _txt;
       
   112                     }).join("") + highlight(_result.text.substring(_lastend));
       
   113                 
       
   114                 return _this.tweetTemplate({
       
   115                     tweet: _result,
       
   116                     date: new Date(_result.created_at.replace(/(\+|-)/,'UTC$1')).toLocaleString(),
       
   117                     text: _text
       
   118                 });
       
   119             }).join("");
       
   120             _this.main_$.html(_html);
       
   121             _renkan.resizeBins();
       
   122         }
   151         }
   123     });
   152     });
   124 }
   153 }