-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
application.coffee
314 lines (271 loc) · 10.4 KB
/
application.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
###
config
###
# animate ALL the things!
@css_animation = true
# cache selectors for great justice
@use_cached_selectors = true
# only one of the below css animation techniques may be set to true
@replace_technique = false
@reflow_technique = false
@timeout_technique = true
# load css sheets of images instead of individual files
@use_css_sheets = true
# use the 60 events per second capped rollup stream instead of raw?
@use_capped_stream = true
# send cleanup events when closing event streams for SUPER LAME servers like heroku :(
# heroku labs:enable websockets may now resolve this!
@force_stream_close = false
# some urls
emojistatic_img_path = 'http://emojistatic.github.io/images/32/'
emojistatic_css_uri = 'http://emojistatic.github.io/css-sheets/emoji-32px.min.css'
###
inits
###
@score_cache = {}
@selector_cache = {}
@iOS = false
p = navigator.platform
@iOS = true if( p == 'iPad' || p == 'iPhone' || p == 'iPod' || p == 'iPhone Simulator' || p == 'iPad Simulator' )
###
methods related to the polling UI
###
# grab the initial data and scores and pass along to draw the grid
@refreshUIFromServer = (callback) ->
$.get('/data', (response) ->
drawEmojiStats(response, callback)
, "json")
###
methods related to the streaming UI
###
@startScoreStreaming = ->
if use_capped_stream then startCappedScoreStreaming() else startRawScoreStreaming()
@startRawScoreStreaming = ->
console.log "Subscribing to score stream (raw)"
@source = new EventSource('/subscribe/raw')
@source.onmessage = (event) -> incrementScore(event.data)
@startCappedScoreStreaming = ->
console.log "Subscribing to score stream (60eps rollup)"
@source = new EventSource('/subscribe/eps')
@source.onmessage = (event) -> incrementMultipleScores(event.data)
@stopScoreStreaming = (async=true) ->
console.log "Unsubscribing to score stream"
@source.close()
forceCloseScoreStream(async) if @force_stream_close
@startDetailStreaming = (id) ->
console.log "Subscribing to detail stream for #{id}"
@detail_id = id
@detail_source = new EventSource("/subscribe/details/#{id}")
@detail_source.addEventListener("stream.tweet_updates.#{id}", processDetailTweetUpdate, false)
@stopDetailStreaming = (async=true) ->
console.log "Unsubscribing to detail stream #{@detail_id}"
@detail_source.close()
forceCloseDetailStream(@detail_id, async) if @force_stream_close
@forceCloseDetailStream = (id, async=true) ->
console.log "Forcing disconnect cleanup for #{id}..."
$.ajax({
type: 'POST'
url: "/subscribe/cleanup/details/#{id}"
success: (data) ->
console.log(" ...Received #{JSON.stringify data} from server.")
async: async
})
true
@forceCloseScoreStream = (async=true) ->
console.log "Forcing disconnect cleanup for score stream..."
$.ajax({
type: 'POST'
url: "/subscribe/cleanup/scores"
success: (data) ->
console.log(" ...Received #{JSON.stringify data} from server.")
async: async
})
true
processDetailTweetUpdate = (event) ->
appendTweetList $.parseJSON(event.data), true
###
index page UI helpers
###
# redraw the entire emoji grid and scores based on data
drawEmojiStats = (stats, callback) ->
selector = $("#data")
selector.empty()
for emoji_char in stats
do (emoji_char) ->
@score_cache[emoji_char.id] = emoji_char.score
selector.append "
<a href='/details/#{emoji_char.id}' title='#{emoji_char.name}' data-id='#{emoji_char.id}'>
<li class='emoji_char' id='#{emoji_char.id}' data-title='#{emoji_char.name}'>
<span class='char emojifont'>#{emoji.replace_unified(emoji_char.char)}</span>
<span class='score' id='score-#{emoji_char.id}'>#{emoji_char.score}</span>
</li>
</a>"
callback() if (callback)
# getter for cached score_selector elements
get_cached_selectors = (id) ->
if @selector_cache[id] != undefined
return [@selector_cache[id][0], @selector_cache[id][1]]
else
score_selector = document.getElementById("score-#{id}")
container_selector = document.getElementById(id)
@selector_cache[id] = [score_selector, container_selector]
return [score_selector, container_selector]
# increment multiple scores from a JSON hash
incrementMultipleScores = (data) ->
scores = $.parseJSON(data)
incrementScore(key,value) for key,value of scores
# increment the score of a single emoji char
incrementScore = (id, incrby=1) ->
if @use_cached_selectors
[score_selector, container_selector] = get_cached_selectors(id)
else
score_selector = document.getElementById("score-#{id}")
container_selector = document.getElementById(id)
score_selector.innerHTML = (@score_cache[id] += incrby);
if css_animation
# various ways to do this....
# some discussion at http://stackoverflow.com/questions/12814612/css3-transition-to-highlight-new-elements-created-in-jquery
if replace_technique
new_container = container_selector.cloneNode(true)
new_container.classList.add('highlight_score_update_anim')
container_selector.parentNode.replaceChild(new_container, container_selector)
selector_cache[id] = [new_container.childNodes[3], new_container] if use_cached_selectors
else if reflow_technique
container_selector.classList.remove('highlight_score_update_anim')
container_selector.focus()
container_selector.classList.add('highlight_score_update_anim')
# this has WAY worse performance it seems like on low power devices
else if timeout_technique
container_selector.classList.add('highlight_score_update_trans')
setTimeout -> container_selector.classList.remove('highlight_score_update_trans')
###
detail page/view UI helpers
###
@emptyTweetList = ->
tweet_list = $('#tweet_list')
tweet_list.empty()
@appendTweetList = (tweet, new_marker = false) ->
tweet_list = $('#tweet_list')
tweet_list_elements = $("#tweet_list li")
tweet_list_elements.last().remove() if tweet_list_elements.size() >= 20
new_entry = $(formattedTweet(tweet, new_marker))
tweet_list.prepend( new_entry )
if css_animation
new_entry.focus()
# new_entry.removeClass('new') # no longer needed with animation style
###
general purpose UI helpers
###
String.prototype.linkifyHashtags = () ->
this.replace /#(\w+)/g, "<a href='https://twitter.com/search?q=%23$1&src=hash' target='_blank'>#$1</a>"
String.prototype.linkifyUsernames = () ->
this.replace /@(\w+)/g, "<a href='https://twitter.com/$1' target='_blank'>@$1</a>"
String.prototype.linkifyUrls = () ->
# this.replace /(https?:\/\/[^\s]+)/g, "<a href='$1' target='_blank'>$1</a>"
this.replace /(https?:\/\/t.co\/\w+)/g, "<a href='$1' target='_blank'>$1</a>"
String.prototype.linkify = () ->
this.linkifyUrls().linkifyUsernames().linkifyHashtags()
formattedTweet = (tweet, new_marker = false) ->
tweet_url = "https://twitter.com/#{tweet.screen_name}/status/#{tweet.id}"
#mini_profile_url = tweet.avatar.replace('_normal','_mini')
prepared_tweet = tweet.text.linkify()
class_to_be = "styled_tweet"
class_to_be += " new" if new_marker && css_animation
"<li class='#{class_to_be}'>
<i class='icon-li icon-angle-right'></i>
<blockquote class='twitter-tweet'>
<p class='emojifont-restricted'>
#{emoji.replace_unified prepared_tweet}
</p>
—
<a href='https://twitter.com/#{tweet.screen_name}' target='_blank'>
<strong class='emojifont-restricted'>#{emoji.replace_unified tweet.name}</strong>
</a>
<span class='screen_name'>@#{tweet.screen_name}</span>
<span class='intents'>
<a class='icon' href='https://twitter.com/intent/tweet?in_reply_to=#{tweet.id}'><i class='icon-reply'></i></a>
<a class='icon' href='https://twitter.com/intent/retweet?tweet_id=#{tweet.id}'><i class='icon-retweet'></i></a>
<a class='icon' href='https://twitter.com/intent/favorite?tweet_id=#{tweet.id}'><i class='icon-star'></i></a>
<a class='icon' href='#{tweet_url}'><i class='icon-external-link'></i></a>
</span>
</blockquote>
</li>"
###
Polling
###
@startRefreshTimer = ->
@refreshTimer = setInterval refreshUIFromServer, 3000
@stopRefreshTimer = ->
clearInterval(@refreshTimer)
###
Shit to dynamically load css-sheets only on browsers that don't properly support emoji fun
###
@loadEmojiSheet = (css_url) ->
cssId = 'emoji-css-sheet' # you could encode the css path itself to generate id..
if (!document.getElementById(cssId))
head = document.getElementsByTagName('head')[0]
link = document.createElement('link')
link.id = cssId
link.rel = 'stylesheet'
link.type = 'text/css'
link.href = css_url
link.media = 'all'
head.appendChild(link)
###
Secret disco mode (easter egg)
###
@enableDiscoMode = () ->
@disco_time = true
console.log "woo disco time!!!!"
$('body').append("<div id='discoball'></div>")
$('#discoball').focus()
start_music = ->
@audio = new Audio();
canPlayOgg = !!audio.canPlayType && audio.canPlayType('audio/ogg; codecs="vorbis"') != ""
canPlayMP3 = !!audio.canPlayType && audio.canPlayType('audio/mpeg; codecs="mp3"') != ""
if canPlayMP3
console.log "can haz play mp3"
@audio.setAttribute("src","/disco/getlucky-64.mp3")
else if canPlayOgg
console.log "can haz play ogg"
@audio.setAttribute("src","/disco/getlucky-64.ogg")
@audio.load()
@audio.play()
setTimeout start_music, 2000
$('body').addClass('disco')
$('.emoji_char').addClass('disco')
$('.navbar').addClass('navbar-inverse')
$('#discoball').addClass('in-position')
@disableDiscoMode = () ->
@disco_time = false
$('#discoball').removeClass('in-position')
$('.disco').removeClass('disco')
$('.navbar').removeClass('navbar-inverse')
kill_music = -> @audio.pause()
setTimeout kill_music, 2000
initDiscoMode = () ->
@disco_time = false
disco_keys = [68,73,83,67,79]
disco_index = 0
$(document).keydown (e) ->
if e.keyCode is disco_keys[disco_index++]
if disco_index is disco_keys.length
enableDiscoMode()
else
disco_index = 0
$(document).keyup (e) ->
if e.keyCode is 27
if disco_time is true
disableDiscoMode()
###
Configuration vars we need to set globally
###
$ ->
emoji.img_path = emojistatic_img_path
emoji.init_env()
console.log "INFO: js-emoji replace mode is #{emoji.replace_mode}"
if emoji.replace_mode == 'css' && use_css_sheets
console.log "In a browser that supports CSS fanciness but not emoji characters, dynamically injecting css-sheet!"
emoji.use_css_imgs = true
loadEmojiSheet(emojistatic_css_uri)
initDiscoMode()