| author | veltr |
| Fri, 21 Dec 2012 16:22:08 +0100 | |
| changeset 44 | 869410bab434 |
| parent 42 | 48d825187d67 |
| child 56 | a9b9e6c7be63 |
| permissions | -rw-r--r-- |
| 34 | 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 |
} |
|
| 21 | 12 |
|
| 34 | 13 |
Rkns.Twitter.Search.prototype.getSearchTitle = function() { |
|
36
d249d36ecc37
Add Edge button, French translation and various bugfixes
veltr
parents:
34
diff
changeset
|
14 |
return this.renkan.l10n.search_on_twitter; |
| 34 | 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( |
|
| 21 | 28 |
'<li class="Rk-Twitter-Tweet Rk-Bin-Item" data-uri="http://twitter.com/<%=tweet.from_user%>/status/<%=tweet.id_str%>" ' |
| 37 | 29 |
+ 'data-title="Tweet by @<%=tweet.from_user%>" data-description="<%-tweet.text%>" data-image="<%=tweet.profile_image_url%>">' |
| 21 | 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 |
||
| 34 | 40 |
Rkns.Twitter.Bin.prototype._init = function(_renkan, _opts) { |
| 21 | 41 |
this.search = _opts.search; |
| 34 | 42 |
this.title_icon_$.addClass('Rk-Twitter-Title-Icon'); |
43 |
this.title_$.html(this.search).addClass("Rk-Twitter-Title"); |
|
| 42 | 44 |
this.refresh(); |
45 |
} |
|
46 |
||
| 44 | 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 |
||
| 42 | 139 |
Rkns.Twitter.Bin.prototype.refresh = function() { |
| 21 | 140 |
var _this = this; |
| 42 | 141 |
Rkns.$.ajax({ |
| 44 | 142 |
url: "http://search.twitter.com/search.json", |
| 42 | 143 |
dataType: "jsonp", |
| 44 | 144 |
data: { |
145 |
q: this.search, |
|
146 |
rpp: 100 |
|
147 |
}, |
|
| 42 | 148 |
success: function(_data) { |
| 44 | 149 |
_this.data = _data; |
150 |
_this.render(); |
|
| 21 | 151 |
} |
| 42 | 152 |
}); |
| 21 | 153 |
} |