| author | Raphael Velt <raph.velt@gmail.com> |
| Wed, 24 Apr 2013 10:56:14 +0200 | |
| changeset 872 | 6b7c534792b7 |
| parent 534 | 0a2505c3b547 |
| child 1558 | 761ba7426984 |
| permissions | -rw-r--r-- |
| 433 | 1 |
var THRESHOLD = 10, |
|
415
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
2 |
DROPCOUNT = 12; |
|
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
3 |
|
| 414 | 4 |
var swTw = { |
5 |
"tweets" : [], |
|
6 |
"tweetsIndex" : [], |
|
| 420 | 7 |
"twInCol" : [], |
| 414 | 8 |
"firstDisplayedTweet" : 0, |
|
415
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
9 |
"cursor" : -1, |
| 420 | 10 |
"annotations" : { |
|
415
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
11 |
"positive" : { |
| 422 | 12 |
"keyword" : "++", |
|
415
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
13 |
"colors" : { |
|
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
14 |
"tweet" : "#c5e7cd", |
| 420 | 15 |
"timeline" : "#1D973D" |
|
415
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
16 |
} |
|
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
17 |
}, |
|
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
18 |
"negative" : { |
| 422 | 19 |
"keyword" : "--", |
|
415
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
20 |
"colors" : { |
|
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
21 |
"tweet" : "#f6ced0", |
| 420 | 22 |
"timeline" : "#CE0A15" |
|
415
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
23 |
} |
|
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
24 |
}, |
|
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
25 |
"reference" : { |
| 422 | 26 |
"keyword" : "==", |
|
415
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
27 |
"colors" : { |
|
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
28 |
"tweet" : "#efefa1", |
| 420 | 29 |
"timeline" : "#C5A62D" |
|
415
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
30 |
} |
|
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
31 |
}, |
|
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
32 |
"question" : { |
| 422 | 33 |
"keyword" : "??", |
|
415
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
34 |
"colors" : { |
|
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
35 |
"tweet" : "#bfdbec", |
| 420 | 36 |
"timeline" : "#036AAE" |
|
415
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
37 |
} |
|
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
38 |
} |
| 420 | 39 |
} |
|
415
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
40 |
} |
|
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
41 |
|
| 420 | 42 |
function highlightKeyword(stra, strb, color) { |
|
415
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
43 |
var rgxp = RegExp( '(' + strb.replace(/(\W)/gm, '\\$1') + ')', "gim"); |
| 420 | 44 |
return stra.replace(rgxp, '<span class="highlight" style="background:' + color + ';">$1</span>'); |
|
415
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
45 |
} |
|
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
46 |
|
|
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
47 |
function highlightText(txt) { |
|
469
99d105834315
Semantic board est maintenant dans les répertoires projets
Raphael Velt <raph.velt@gmail.com>
parents:
433
diff
changeset
|
48 |
res = suggested_keywords.reduce(function(a, b) { |
| 420 | 49 |
return highlightKeyword(a,b, '#333333'); |
50 |
}, txt); |
|
51 |
res = _(swTw.annotations).reduce(function(a, b) { |
|
52 |
return (b.keyword ? highlightKeyword(a,b.keyword,b.colors.timeline) : a); |
|
|
415
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
53 |
}, res); |
|
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
54 |
return res; |
| 414 | 55 |
} |
56 |
||
57 |
function nextTweet() { |
|
58 |
if (!swTw.tweets.length) { |
|
59 |
return; |
|
60 |
} |
|
61 |
if (swTw.cursor < swTw.tweets.length - 1) { |
|
|
415
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
62 |
swTw.cursor = Math.max(swTw.cursor + 1, swTw.tweets.length - 120); |
| 420 | 63 |
var tweet = swTw.tweets[swTw.cursor], |
64 |
html = '<img src="' |
|
65 |
+ tweet.profile_image_url |
|
66 |
+ '" /><p>@' |
|
67 |
+ tweet.from_user |
|
68 |
+ ' (' |
|
69 |
+ tweet.from_user_name |
|
70 |
+ ')' |
|
71 |
+ '</p><p class="tweet_text">' |
|
72 |
+ highlightText(tweet.text) |
|
73 |
+ '</p>'; |
|
74 |
$("#tweetcont").html(html) |
|
| 414 | 75 |
} else { |
| 420 | 76 |
$("#tweetcont").html("") |
| 414 | 77 |
} |
78 |
} |
|
79 |
||
80 |
function dropOldTweets() { |
|
|
415
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
81 |
var _newPos = swTw.firstDisplayedTweet + DROPCOUNT; |
| 414 | 82 |
_(swTw.tweets.slice(swTw.firstDisplayedTweet,_newPos)).each(function(tweet) { |
83 |
swTw.twInCol = _(swTw.twInCol).map(function(col) { |
|
84 |
return _(col).without(tweet.id_str); |
|
85 |
}); |
|
86 |
_(tweet.elements).each(function(elid) { |
|
| 420 | 87 |
$("#" + elid).animate({ |
88 |
"width": "0px" |
|
89 |
}, |
|
90 |
2000, |
|
91 |
function() { |
|
| 414 | 92 |
$(this).detach(); |
| 420 | 93 |
}) |
| 414 | 94 |
}); |
95 |
}); |
|
96 |
swTw.firstDisplayedTweet = _newPos; |
|
97 |
} |
|
98 |
||
99 |
function callbackTweets(tweets) { |
|
100 |
_(tweets).each(function(tweet) { |
|
101 |
var tl = tweet.text.toLowerCase(); |
|
|
469
99d105834315
Semantic board est maintenant dans les répertoires projets
Raphael Velt <raph.velt@gmail.com>
parents:
433
diff
changeset
|
102 |
tweet.columns = suggested_keywords.filter(function(word) { |
| 414 | 103 |
return tl.search(word) != -1 |
104 |
}); |
|
105 |
tweet.elements = []; |
|
106 |
_(tweet.columns).each(function(word) { |
|
|
469
99d105834315
Semantic board est maintenant dans les répertoires projets
Raphael Velt <raph.velt@gmail.com>
parents:
433
diff
changeset
|
107 |
var iword = suggested_keywords.indexOf(word), |
| 420 | 108 |
icol = ( swTw.twInCol[iword*2+1].length < swTw.twInCol[iword*2].length ? iword*2+1 : iword*2 ), |
|
415
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
109 |
elid = 'avatar_' + tweet.id_str + '_' + iword, |
| 420 | 110 |
colA = swTw.colAnnot[iword], |
111 |
ttl = 0; |
|
112 |
_(swTw.annotations).each(function(v,k) { |
|
113 |
if (tweet.text.indexOf(v.keyword) != -1) { |
|
114 |
colA[k]++; |
|
115 |
ttl++; |
|
116 |
} |
|
117 |
}); |
|
118 |
colA.total += Math.max(1,ttl); |
|
119 |
$('#tube_' + icol).append( |
|
|
415
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
120 |
'<div class="avatar" id="' |
|
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
121 |
+ elid |
| 420 | 122 |
+ '" style="margin-right: 500px;"><img src="' |
|
415
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
123 |
+ tweet.profile_image_url |
|
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
124 |
+ '" /></div>' |
|
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
125 |
); |
| 414 | 126 |
tweet.elements.push(elid); |
| 420 | 127 |
swTw.twInCol[icol].push(tweet.id_str); |
| 414 | 128 |
$("#" + elid).animate({ |
| 420 | 129 |
"margin-right" : "0px" |
130 |
}, |
|
131 |
2000); |
|
| 414 | 132 |
}) |
133 |
swTw.tweets.push(tweet); |
|
134 |
swTw.tweetsIndex.push(tweet.id_str); |
|
135 |
}); |
|
136 |
while (_(swTw.twInCol).any(function(col) { |
|
|
415
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
137 |
return col.length > THRESHOLD |
| 414 | 138 |
})) { |
139 |
dropOldTweets(); |
|
140 |
} |
|
| 420 | 141 |
_(swTw.colAnnot).each(function(colA, i) { |
142 |
_(swTw.annotations).each(function(v, k) { |
|
143 |
$("#polemic_" + i + "_" + k).css("width", ~~(100 * colA[k] / colA.total) + "%"); |
|
144 |
}); |
|
145 |
}) |
|
| 414 | 146 |
} |
147 |
||
148 |
function retrieveTweets() { |
|
149 |
var options = { |
|
|
421
c8db83b70def
Added "sweet-tweet" & Knowtex iframes in client
Raphael Velt <raph.velt@gmail.com>
parents:
420
diff
changeset
|
150 |
// "keyword" : swTw.columns_words.join(" OR "), |
|
c8db83b70def
Added "sweet-tweet" & Knowtex iframes in client
Raphael Velt <raph.velt@gmail.com>
parents:
420
diff
changeset
|
151 |
// "lang" : "fr", |
|
469
99d105834315
Semantic board est maintenant dans les répertoires projets
Raphael Velt <raph.velt@gmail.com>
parents:
433
diff
changeset
|
152 |
"keyword" : tracking_keywords.join(" OR "), |
| 414 | 153 |
"pages" : 1, |
| 420 | 154 |
"rpp" : 100, |
| 414 | 155 |
"cbEnd" : function() { |
156 |
callbackTweets(this.tweets); |
|
157 |
} |
|
158 |
} |
|
159 |
if (swTw.tweets.length) { |
|
160 |
options.since_id = swTw.tweets[swTw.tweets.length - 1].id_str; |
|
161 |
} |
|
162 |
getTweets(options); |
|
163 |
} |
|
164 |
||
165 |
function getTweets(options) { |
|
166 |
function getTweetUrl(url) { |
|
167 |
$.getJSON(url, function(data) { |
|
168 |
options.tweets = options.tweets.concat(data.results); |
|
169 |
options.currentPage = data.page; |
|
170 |
if (options.cbData) { |
|
171 |
options.cbData(); |
|
172 |
} |
|
173 |
if (data.next_page && data.page < options.pages) { |
|
174 |
getTweetUrl(baseurl + data.next_page + suffix); |
|
175 |
} else { |
|
176 |
options.tweets.sort(function(a,b) { |
|
177 |
return a.id - b.id; |
|
178 |
}); |
|
179 |
if (options.cbEnd) { |
|
180 |
options.cbEnd(); |
|
181 |
} |
|
182 |
} |
|
183 |
}); |
|
184 |
} |
|
185 |
|
|
186 |
options.tweets = []; |
|
187 |
options.pages || (options.pages = 1); |
|
188 |
options.rpp || (options.rpp = 100); |
|
189 |
options.currentPage = 0; |
|
190 |
|
|
191 |
var baseurl = "http://search.twitter.com/search.json", |
|
192 |
suffix = (options.since_id ? "&since_id=" + options.since_id : '' ) + "&callback=?", |
|
193 |
jsonurl = baseurl + "?q=" + encodeURIComponent(options.keyword)+ "&rpp=" + options.rpp |
|
194 |
+ (options.lang ? "&lang=" + options.lang : '' ) + suffix; |
|
195 |
getTweetUrl(jsonurl); |
|
196 |
} |
|
197 |
||
198 |
$(document).ready(function() { |
|
|
469
99d105834315
Semantic board est maintenant dans les répertoires projets
Raphael Velt <raph.velt@gmail.com>
parents:
433
diff
changeset
|
199 |
$("#columncont").html( suggested_keywords.map( |
|
415
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
200 |
function(mot, i) { |
|
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
201 |
return '<div class="column" id="column_' |
|
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
202 |
+ i |
| 420 | 203 |
+ '"><div class="column-tube"><div class="tube" id="tube_' |
204 |
+ (2*i) |
|
205 |
+ '"></div><div class="tube" id="tube_' |
|
206 |
+ (2*i+1) |
|
207 |
+ '"></div></div><div class="column-title">' |
|
208 |
+ _(swTw.annotations).map(function(v,k) { |
|
209 |
return '<div class="polemicvol" id="polemic_' |
|
210 |
+ i |
|
211 |
+ '_' |
|
212 |
+ k |
|
213 |
+ '" style="background:' |
|
214 |
+ v.colors.timeline |
|
215 |
+ '"></div>' |
|
216 |
}).join('') |
|
217 |
+'<h3>' |
|
|
415
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
218 |
+ mot |
|
593250f3a286
Sweet Tweet modifications
Raphael Velt <raph.velt@gmail.com>
parents:
414
diff
changeset
|
219 |
+ '</h3></div></div>' |
| 414 | 220 |
} |
221 |
).join("") ); |
|
| 420 | 222 |
|
|
469
99d105834315
Semantic board est maintenant dans les répertoires projets
Raphael Velt <raph.velt@gmail.com>
parents:
433
diff
changeset
|
223 |
for (var i = 0; i < suggested_keywords.length * 2; i++) { |
| 420 | 224 |
swTw.twInCol.push([]); |
225 |
} |
|
226 |
|
|
|
469
99d105834315
Semantic board est maintenant dans les répertoires projets
Raphael Velt <raph.velt@gmail.com>
parents:
433
diff
changeset
|
227 |
swTw.colAnnot = suggested_keywords.map(function() { |
| 420 | 228 |
var res = { "total" : 0 } |
229 |
_(swTw.annotations).map(function(v,k) { |
|
230 |
res[k] = 0; |
|
231 |
}); |
|
232 |
return res; |
|
| 414 | 233 |
}); |
234 |
|
|
235 |
retrieveTweets(); |
|
236 |
|
|
| 420 | 237 |
setInterval(retrieveTweets, 5000); |
| 414 | 238 |
|
| 420 | 239 |
setInterval(nextTweet, 6000); |
| 414 | 240 |
|
241 |
}); |