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