-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws.coffee
289 lines (228 loc) · 7.53 KB
/
ws.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
$(document).ready ->
abs_width = 490
abs_height = 180
debugging = false
max = (x, y) -> if x > y then x else y
min = (x, y) -> if x < y then x else y
confine = (infimum, n, supremum) ->
n = max infimum, n
n = min n, supremum
n
debug_count = 0
if debugging
$('body').append '<div id="debug"><h1>Debug</h1></div>'
debug = (str) ->
if debugging
debug_count = debug_count + 1
str = JSON.stringify str unless typeof str == 'string'
$('#debug').append "<p>#{debug_count}: #{str}</p>"
colors = d3.scale.category20()
duration = 1000
standard_delay = duration / 5
update_data = (parcel) ->
# Should modify selection (svg) width and height here
w = 40
font_factor = 2 / 3
# Add new objects (array elements) to be referenced elsewhere
catalogue = []
for pkg in parcel
catalogue = catalogue.concat pkg['obj']
elems = d3.select('svg#assets defs')
.selectAll('.elem')
# Need to ensure the following line isn't
# making tons of duplicate entries
.data(catalogue, (d) -> d)
.enter()
.append('svg')
.attr('id', (d) -> "elem_#{d}")
.classed('elem', true)
.attr('x', 0)
.attr('y', 0)
.attr('width', w)
.attr('height', w)
elems.append('rect')
.attr('x', 0)
.attr('y', 0)
.attr('width', '100%')
.attr('height', '100%')
.attr('fill', (d) -> colors d)
elems.append('text')
.attr('x', '50%')
.attr('y', '75%')
.attr('text-anchor', 'middle')
.attr('fill', 'white')
.attr('font-size', w * font_factor)
.attr('font-family', 'sans-serif')
.attr('font-style', 'bold')
.text((d) -> d)
# Update actual charts
d3.select('#charts')
.classed('one-child', -> parcel.length == 1)
.classed('not-one-child', -> parcel.length != 1)
.selectAll('.chart')
.data(parcel, (d) -> d['name'])
.call(charts)
.select('.methods')
.selectAll('.method')
.data((d) -> d['methods'])
.call(methods)
charts = (selection) ->
enteror = selection
.enter()
.append('div')
.classed('chart', true)
enteror.append('h1').append('span').append('div').text((d) -> d['name'])
selection.exit().remove()
enteror
.append('div')
.classed('demo_wrapper', true)
.append('svg')
.classed('demo_object', true)
enteror
.append('section')
.classed('methods', true)
selection
.select('.demo_object')
.each demo_object
demo_object = (datar) ->
selection = d3.select @
datar = datar['obj']
# Should modify selection (svg) width and height here
# w = abs_width / datar.length
# w = abs_width / 15
w = 40
font_factor = 2 / 3
main_transition = selection
.transition()
.duration(duration / 2)
.delay(standard_delay)
.attr('height', w)
x = d3.scale
.ordinal()
.domain(datar)
.rangeBands([0, min abs_width, w * datar.length])
joiner = selection
.selectAll('svg.mover')
.data(datar, (d) -> d)
joiner
.enter()
.append('svg')
.classed('mover', true)
.each((d) ->
d3.select(@).classed("elem_#{d}", true))
.attr('x', 0)
.attr('y', 0)
.attr('width', w)
.attr('height', w)
.append('use')
.attr('xlink:href', (d) -> "#elem_#{d}")
joiner.order()
group_transition = main_transition
.selectAll('svg.mover')
.transition()
.attr('x', (d) -> x d)
.attr('y', 0)
joiner.exit()
.transition()
.duration(duration / 5)
.delay(standard_delay)
.ease('quad-in')
.attr('y', 750)
.remove()
methods = (selection) ->
# Suuuuper messy right now!
# Ho no!
# So bad, sad, et al.
yield_width = '40px'
selection.exit()
.transition()
.duration(duration / 10)
.remove()
props = ['name', 'args', 'block', 'yield', 'result']
meths = selection.enter()
.append('div')
.classed('method', true)
meths
.append('span')
.classed('name', true)
.text((d) -> ":#{d['name']}")
meths
.append('span')
.classed('args', true)
.text((d) -> d['args'])
meths
.append('span')
.classed('block', true)
.text((d) -> d['block'])
meths
.append('svg')
.attr('height', yield_width)
.attr('width', yield_width)
.classed('yield', true)
meths
.append('span')
.classed('result', true)
.text((d) -> d['result'])
selection
.select("span.name")
.text((d) -> "#{d['name']}")
selection
.select("span.args")
.text((d) -> "#{d['args']}")
selection
.select("span.block")
.text((d) -> "#{d['block']}")
selection
.select("span.result")
.text((d) -> "#{d['result']}")
yielder = selection.select('svg.yield')
.selectAll('use')
.data(((d) -> [d['yield']]), (d) -> d)
yielder.exit().remove()
yielder.enter()
.append('use')
.attr('xlink:href', (d) -> "#elem_#{d}")
.attr('x', 0)
.attr('y', 0)
.each (d) ->
$(@)
.parents('.chart')
.find(".elem_#{d}")
# Is there a more elegant 'flash-
# this-element' method?
.toggle('visible')
.toggle('visible')
queue = []
reset_queue = -> queue = [ queue[0] ] if queue.length
update_queue = (parcel) ->
queue.unshift parcel unless queue[0] == parcel
setInterval((->
update_data queue.pop() if queue.length
), duration * 2)
# TODO: possibly use a d3 selection instead of
# jquery
slide = (num) ->
num_slides = $('#slides .slide').size()
num = confine 1, num, num_slides
d3.select('#slides')
.selectAll('.slide')
.attr('hidden', (d, i) ->
if i == num - 1 then null else true)
routes =
slide: slide
update: update_queue
msg: debug
reset: reset_queue
debug: debug
route = ({label, parcel}) -> routes[label] parcel
# IMPORTANT: Do not change this function name (or the next
# several lines) without adusting rake opts_update!
phone_home = (count = 0) ->
debug "Connection attempt #{count}"
ws = new WebSocket 'ws://localhost:8080'
ws.onmessage = (evt) -> route JSON.parse evt.data
ws.onclose = ->
debug 'socket closed'
setTimeout((-> phone_home(count + 1)), 1000)
ws.onopen = -> debug 'connected...'
phone_home()