client/js/twitter-bin.js
changeset 170 603ffa4c6fa5
parent 160 408da84d4dc0
equal deleted inserted replaced
169:a60a450b8f3b 170:603ffa4c6fa5
     1 Rkns.Twitter = {
     1 Rkns.Twitter = {
     2 }
     2 };
     3 
     3 
     4 Rkns.Twitter.Search = function(_renkan, _opts) {
     4 Rkns.Twitter.Search = function(_renkan, _opts) {
     5     this.renkan = _renkan;
     5     this.renkan = _renkan;
     6     this.opts = _opts;
     6     this.opts = _opts;
     7 }
     7 };
     8 
     8 
     9 Rkns.Twitter.Search.prototype.getBgClass = function() {
     9 Rkns.Twitter.Search.prototype.getBgClass = function() {
    10     return "Rk-Twitter-Icon";
    10     return "Rk-Twitter-Icon";
    11 }
    11 };
    12 
    12 
    13 Rkns.Twitter.Search.prototype.getSearchTitle = function() {
    13 Rkns.Twitter.Search.prototype.getSearchTitle = function() {
    14     return this.renkan.translate("Twitter");
    14     return this.renkan.translate("Twitter");
    15 }
    15 };
    16 
    16 
    17 Rkns.Twitter.Search.prototype.search = function(_q) {
    17 Rkns.Twitter.Search.prototype.search = function(_q) {
    18     this.renkan.tabs.push(
    18     this.renkan.tabs.push(
    19         new Rkns.Twitter.Bin(this.renkan, {
    19         new Rkns.Twitter.Bin(this.renkan, {
    20             search: _q
    20             search: _q
    21         })
    21         })
    22     );
    22     );
    23 }
    23 };
    24 
    24 
    25 Rkns.Twitter.Bin = Rkns.Utils.inherit(Rkns._BaseBin);
    25 Rkns.Twitter.Bin = Rkns.Utils.inherit(Rkns._BaseBin);
    26 
    26 
    27 Rkns.Twitter.Bin.prototype.tweetTemplate = Rkns._.template(
    27 Rkns.Twitter.Bin.prototype.tweetTemplate = Rkns._.template(
    28     '<li class="Rk-Twitter-Tweet Rk-Bin-Item" draggable="true" data-uri="http://twitter.com/<%=tweet.from_user%>/status/<%=tweet.id_str%>" '
    28     '<li class="Rk-Twitter-Tweet Rk-Bin-Item" draggable="true" data-uri="http://twitter.com/<%=tweet.from_user%>/status/<%=tweet.id_str%>" '
    41     this.renkan = _renkan;
    41     this.renkan = _renkan;
    42     this.search = _opts.search;
    42     this.search = _opts.search;
    43     this.title_icon_$.addClass('Rk-Twitter-Title-Icon');
    43     this.title_icon_$.addClass('Rk-Twitter-Title-Icon');
    44     this.title_$.html(this.search).addClass("Rk-Twitter-Title");
    44     this.title_$.html(this.search).addClass("Rk-Twitter-Title");
    45     this.refresh();
    45     this.refresh();
    46 }
    46 };
    47 
    47 
    48 Rkns.Twitter.Bin.prototype.render = function(searchstr) {
    48 Rkns.Twitter.Bin.prototype.render = function(searchstr) {
    49     var _rgxp = new RegExp('('+(searchstr || this.search).replace(/(\W)/g,'\\$1')+')','gi');
    49     var _rgxp = new RegExp('('+(searchstr || this.search).replace(/(\W)/g,'\\$1')+')','gi');
    50     if (searchstr) {
    50     if (searchstr) {
    51         var rxtest = new RegExp(searchstr.replace(/(\W)/g,'\\$1'),'i');
    51         var rxtest = new RegExp(searchstr.replace(/(\W)/g,'\\$1'),'i');
    52     }
    52     }
    53     function highlight(_text) {
    53     function highlight(_text) {
    54         return _text.replace(_rgxp, "<span class='searchmatch'>$1</span>");
    54         return _text.replace(_rgxp, "<span class='searchmatch'>$1</span>");
    55     }
    55     }
    56     var _html = ""
    56     var _html = "",
    57         _this = this,
    57         _this = this,
    58         count = 0;
    58         count = 0;
    59     Rkns._(this.data.results).each(function(_result) {
    59     Rkns._(this.data.results).each(function(_result) {
    60         if (searchstr && !rxtest.test(_result.text)) {
    60         if (searchstr && !rxtest.test(_result.text)) {
    61             return;
    61             return;
    92                     "link" :'<a href="http://twitter.com/search?q=' + encodeURIComponent(_h) + '" onclick="filtrerTexte(\'' + _.escape(_h) + '\'); return false;" target="_blank">',
    92                     "link" :'<a href="http://twitter.com/search?q=' + encodeURIComponent(_h) + '" onclick="filtrerTexte(\'' + _.escape(_h) + '\'); return false;" target="_blank">',
    93                 });
    93                 });
    94             }
    94             }
    95         }
    95         }
    96         
    96         
    97         var _urls = _result.text.match(/(www\.|https?:\/\/)[\w./_\-]+/gim),
    97         var _urls = _result.text.match(/(www\.|https?:\/\/)[\w.\/_\-]+/gim),
    98             _lastpos = 0;
    98             _lastpos = 0;
    99         if (_urls) {
    99         if (_urls) {
   100             for (var _i = 0; _i < _urls.length; _i++) {
   100             for (var _i = 0; _i < _urls.length; _i++) {
   101                 var _m = _urls[_i],
   101                 var _m = _urls[_i],
   102                     _start = _lastpos + _result.text.substr(_lastpos).search(_m),
   102                     _start = _lastpos + _result.text.substr(_lastpos).search(_m),
   107                     "end" : _lastpos,
   107                     "end" : _lastpos,
   108                     "link" :'<a href="' + _m + '" target="_blank">',
   108                     "link" :'<a href="' + _m + '" target="_blank">',
   109                 });
   109                 });
   110             }
   110             }
   111         }
   111         }
   112         _entities = Rkns._(_entities).sortBy(function(a) { return a.start });
   112         _entities = Rkns._(_entities).sortBy(function(a) { return a.start; });
   113         var _lastend = 0,
   113         var _lastend = 0,
   114             _text = Rkns._(_entities).map(function(_e) {
   114             _text = Rkns._(_entities).map(function(_e) {
   115                 var _txt = highlight(_result.text.substring(_lastend, _e.start)) + _e.link + highlight(_e.text) + '</a>';
   115                 var _txt = highlight(_result.text.substring(_lastend, _e.start)) + _e.link + highlight(_e.text) + '</a>';
   116                 _lastend = _e.end;
   116                 _lastend = _e.end;
   117                 return _txt;
   117                 return _txt;
   133         this.$.hide();
   133         this.$.hide();
   134     } else {
   134     } else {
   135         this.$.show();
   135         this.$.show();
   136     }
   136     }
   137     this.renkan.resizeBins();
   137     this.renkan.resizeBins();
   138 }
   138 };
   139 
   139 
   140 Rkns.Twitter.Bin.prototype.refresh = function() {
   140 Rkns.Twitter.Bin.prototype.refresh = function() {
   141     var _this = this;
   141     var _this = this;
   142     Rkns.$.ajax({
   142     Rkns.$.ajax({
   143         url: "http://search.twitter.com/search.json",
   143         url: "http://search.twitter.com/search.json",
   149         success: function(_data) {
   149         success: function(_data) {
   150             _this.data = _data;
   150             _this.data = _data;
   151             _this.render();
   151             _this.render();
   152         }
   152         }
   153     });
   153     });
   154 }
   154 };