1 Rkns.Twitter = { |
|
2 }; |
|
3 |
|
4 Rkns.Twitter.Search = function(_renkan, _opts) { |
|
5 this.renkan = _renkan; |
|
6 this.opts = _opts; |
|
7 }; |
|
8 |
|
9 Rkns.Twitter.Search.prototype.getBgClass = function() { |
|
10 return "Rk-Twitter-Icon"; |
|
11 }; |
|
12 |
|
13 Rkns.Twitter.Search.prototype.getSearchTitle = function() { |
|
14 return this.renkan.translate("Twitter"); |
|
15 }; |
|
16 |
|
17 Rkns.Twitter.Search.prototype.search = function(_q) { |
|
18 this.renkan.tabs.push( |
|
19 new Rkns.Twitter.Bin(this.renkan, { |
|
20 search: _q |
|
21 }) |
|
22 ); |
|
23 }; |
|
24 |
|
25 Rkns.Twitter.Bin = Rkns.Utils.inherit(Rkns._BaseBin); |
|
26 |
|
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%>" ' |
|
29 + 'data-title="Tweet by @<%- tweet.from_user %>" data-description="<%-tweet.text%>" data-image="<%- tweet.profile_image_url %>">' |
|
30 + '<img class="Rk-Twitter-TwImage" src="<%=tweet.profile_image_url%>" />' |
|
31 + '<h4 class="Rk-Twitter-TwTitle"><a href="http://twitter.com/<%=tweet.from_user%>" target="_blank">@<%=tweet.from_user%></a> (<%=tweet.from_user_name%>)</h4>' |
|
32 + '<p class="Rk-Twitter-TwDate"><%=date%></p>' |
|
33 + '<p class="Rk-Twitter-TwText"><%=text%></p>' |
|
34 + '<p class="Rk-Twitter-TwActions"><a class="Rk-Twitter-TwAction" href="http://twitter.com/<%=tweet.from_user%>/status/<%=tweet.id_str%>" target="_blank">show original</a> · ' |
|
35 + '<a href="http://twitter.com/intent/tweet?in_reply_to=<%=tweet.id_str%>" target="_blank">reply</a> · ' |
|
36 + '<a href="http://twitter.com/intent/retweet?tweet_id=<%=tweet.id_str%>" target="_blank">retweet</a> · ' |
|
37 + '<a href="http://twitter.com/intent/favorite?tweet_id=<%=tweet.id_str%>" target="_blank">favorite</a></p></li>' |
|
38 ); |
|
39 |
|
40 Rkns.Twitter.Bin.prototype._init = function(_renkan, _opts) { |
|
41 this.renkan = _renkan; |
|
42 this.search = _opts.search; |
|
43 this.title_icon_$.addClass('Rk-Twitter-Title-Icon'); |
|
44 this.title_$.html(this.search).addClass("Rk-Twitter-Title"); |
|
45 this.refresh(); |
|
46 }; |
|
47 |
|
48 Rkns.Twitter.Bin.prototype.render = function(searchstr) { |
|
49 var _rgxp = new RegExp('('+(searchstr || this.search).replace(/(\W)/g,'\\$1')+')','gi'); |
|
50 if (searchstr) { |
|
51 var rxtest = new RegExp(searchstr.replace(/(\W)/g,'\\$1'),'i'); |
|
52 } |
|
53 function highlight(_text) { |
|
54 return _text.replace(_rgxp, "<span class='searchmatch'>$1</span>"); |
|
55 } |
|
56 var _html = "", |
|
57 _this = this, |
|
58 count = 0; |
|
59 Rkns._(this.data.results).each(function(_result) { |
|
60 if (searchstr && !rxtest.test(_result.text)) { |
|
61 return; |
|
62 } |
|
63 count++; |
|
64 var _entities = [], |
|
65 _users = _result.text.match(/@[\w_]+/gm), |
|
66 _lastpos = 0; |
|
67 if (_users) { |
|
68 for (var _i = 0; _i < _users.length; _i++) { |
|
69 var _m = _users[_i], |
|
70 _start = _lastpos + _result.text.substr(_lastpos).search(_m), |
|
71 _lastpos = _start + _m.length; |
|
72 _entities.push({ |
|
73 "text" : _m, |
|
74 "start" : _start, |
|
75 "end" : _lastpos, |
|
76 "link" :'<a href="http://twitter.com/' + _m.substr(1) + '" onclick="filtrerTexte(\'' + _m + '\'); return false;" target="_blank">', |
|
77 }); |
|
78 } |
|
79 } |
|
80 var _hashes = _result.text.match(/([^&]|^)#[^\s,.!?=#@&;()]+/gm), |
|
81 _lastpos = 0; |
|
82 if (_hashes) { |
|
83 for (var _i = 0; _i < _hashes.length; _i++) { |
|
84 var _m = _hashes[_i], |
|
85 _h = ( _m[0] == '#' ? _m : _m.substr(1) ), |
|
86 _start = _lastpos + _result.text.substr(_lastpos).search(_h), |
|
87 _lastpos = _start + _h.length; |
|
88 _entities.push({ |
|
89 "text" : _h, |
|
90 "start" : _start, |
|
91 "end" : _lastpos, |
|
92 "link" :'<a href="http://twitter.com/search?q=' + encodeURIComponent(_h) + '" onclick="filtrerTexte(\'' + _.escape(_h) + '\'); return false;" target="_blank">', |
|
93 }); |
|
94 } |
|
95 } |
|
96 |
|
97 var _urls = _result.text.match(/(www\.|https?:\/\/)[\w.\/_\-]+/gim), |
|
98 _lastpos = 0; |
|
99 if (_urls) { |
|
100 for (var _i = 0; _i < _urls.length; _i++) { |
|
101 var _m = _urls[_i], |
|
102 _start = _lastpos + _result.text.substr(_lastpos).search(_m), |
|
103 _lastpos = _start + _m.length; |
|
104 _entities.push({ |
|
105 "text" : _m, |
|
106 "start" : _start, |
|
107 "end" : _lastpos, |
|
108 "link" :'<a href="' + _m + '" target="_blank">', |
|
109 }); |
|
110 } |
|
111 } |
|
112 _entities = Rkns._(_entities).sortBy(function(a) { return a.start; }); |
|
113 var _lastend = 0, |
|
114 _text = Rkns._(_entities).map(function(_e) { |
|
115 var _txt = highlight(_result.text.substring(_lastend, _e.start)) + _e.link + highlight(_e.text) + '</a>'; |
|
116 _lastend = _e.end; |
|
117 return _txt; |
|
118 }).join("") + highlight(_result.text.substring(_lastend)); |
|
119 |
|
120 _html += _this.tweetTemplate({ |
|
121 tweet: _result, |
|
122 date: new Date(_result.created_at.replace(/(\+|-)/,'UTC$1')).toLocaleString(), |
|
123 text: _text |
|
124 }); |
|
125 }); |
|
126 this.main_$.html(_html); |
|
127 if (searchstr && count) { |
|
128 this.count_$.text(count).show(); |
|
129 } else { |
|
130 this.count_$.hide(); |
|
131 } |
|
132 if (searchstr && !count) { |
|
133 this.$.hide(); |
|
134 } else { |
|
135 this.$.show(); |
|
136 } |
|
137 this.renkan.resizeBins(); |
|
138 }; |
|
139 |
|
140 Rkns.Twitter.Bin.prototype.refresh = function() { |
|
141 var _this = this; |
|
142 Rkns.$.ajax({ |
|
143 url: "http://search.twitter.com/search.json", |
|
144 dataType: "jsonp", |
|
145 data: { |
|
146 q: this.search, |
|
147 rpp: 100 |
|
148 }, |
|
149 success: function(_data) { |
|
150 _this.data = _data; |
|
151 _this.render(); |
|
152 } |
|
153 }); |
|
154 }; |
|