-
Notifications
You must be signed in to change notification settings - Fork 5
/
application.js.coffee
211 lines (163 loc) · 6.11 KB
/
application.js.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
#= require vendor/d3.v3
#= require vendor/dagre
#= require vendor/jquery
#= require vendor/ui.jquery
#= require vendor/sugar-1.3.8
#= require vendor/stacktrace-0.4
#= require vendor/chosen.jquery
#= require vendor/mustache
#= require vendor/hotkeys.jquery
#= require_tree ./lib
#= require_self
$ -> window.app = new Application
class Application
constructor: ->
@data = window.data
Drawer.attach('svg')
@toolbar = $('#toolbar')
@title = $('#title .name')
@transform = $('#title .transform')
@selector = $('#functions select')
@zoomButton = $('#zoom button')
@container = $('#container')
@svg = $('svg')
@timeline = $('#timeline')
@slider = $('#timeline .slider')
@sliderInput = $('#timeline input')
@sliderApply = $('#timeline button.ok')
@sliderNext = $('#timeline button.next')
@sliderPrev = $('#timeline button.prev')
@sliderFNext = $('#timeline button.fnext')
@sliderFPrev = $('#timeline button.fprev')
@transforms = $('#transforms')
@buildZoomer()
@buildSelector()
@buildSlider()
redraw = =>
@dehasherize()
@currentFunction ||= 0
@draw()
redraw(); $(window).bind 'hashchange', redraw
resize = =>
@container.find('.stretch').height($(window).height() - @toolbar.outerHeight() - 4)
@slider.height($(window).height() - @toolbar.outerHeight() - 280)
resize(); $(window).bind('resize', resize)
draw: ->
try
@timeline.show()
@title.removeClass('error')
@transform.removeClass('active').html('')
input = @data[@currentFunction]
Drawer.reset() if @input?.source.name != input.source.name
Drawer.clear()
@input = input
@input.rebuild(@currentStep)
@drawer = new Drawer(new Graph(@input))
@renewZoomer()
@renewSelector()
@renewSlider()
@renewNaming()
@title.html @input.function.title()
catch error
console.log error
@timeline.hide()
@title.addClass('error').text 'Unable to build graph: problem dumped to console'
dehasherize: ->
hash = window.location.hash.from(1)
hash = "0:#{@data[0].events.length-1}" if hash.length == 0
[@currentFunction, @currentStep] = hash.split(":").map (x) -> x.toNumber()
jumpTo: (func, step) ->
if Object.isString(func)
@selector.find('option').each (x) ->
func = $(@).attr('value').toNumber() if $(@).text() == func
@currentFunction = func; step = @data[func].events.length-1 if step == undefined
@currentStep = [step, @data[func].events.length-1].min()
@currentStep = [0, @currentStep].max()
window.location.hash = "#{@currentFunction}:#{@currentStep || 0}"
jumpToBlock: (name) ->
@drawer.zoomNode @svg.width(), @svg.height(), name
renewSlider: (max, value) ->
@slider.slider
max: @input.events.length-1
value: @input.stop
@slider.find('.label').remove()
@input.transforms.each (entry) =>
height = entry.length/(@input.events.length-1)*100
top = 100-entry.id/(@input.events.length-1)*100-height
label = "#{entry.label} (#{entry.id}+#{entry.length})"
if @currentStep >= entry.id && @currentStep < entry.id+entry.length
klass = "current"
@transform.addClass('active').html(entry.label)
else
klass = ""
@transforms.append $("<div class='label #{klass}'><span>#{label}</span></div>")
.attr("style", "top: #{top}%; height: #{height}%;")
.mousedown (e) =>
e.stopPropagation()
@jumpTo @currentFunction, entry.id
renewSelector: ->
@selector.val(@currentFunction).trigger('liszt:updated')
renewZoomer: ->
@zoomButton.removeClass 'active'
renewNaming: ->
elements = $('.naming')
drawer = @drawer
elements.filter('.link').click ->
element = $(@)
active = element.hasClass('active')
needle = element.attr("data-naming") || @innerHTML
elements.removeClass('active')
unless active
elements.filter(->
($(@).attr("data-naming") || @innerHTML) == needle
).addClass('active')
drawer.repaint()
buildZoomer: ->
@zoomButton.click =>
if @zoomButton.hasClass('active')
@zoomButton.removeClass('active')
@drawer.unfit()
else
@zoomButton.addClass('active')
@drawer.fit(@svg.width(), @svg.height())
buildSelector: ->
groups = ['Present', 'Removed'].map (x) -> $("<optgroup label='#{x}'></optgroup>")
@data.each (f, i) =>
group = if f.source.present then groups[0] else groups[1]
group.append "<option value='#{i}'>#{f.source.name}</option>"
groups.each (x) => @selector.append x
@selector.chosen(search_contains: true).change => @jumpTo @selector.val().toNumber()
@selector.val(@currentFunction).trigger('liszt:updated')
buildSlider: ->
$('button').button()
@sliderInput.focus -> $(@).blur()
@slider.slider
min: 0
orientation: 'vertical'
slide: (event, ui) => @sliderInput.val ui.value
change: (event) =>
@sliderInput.val @slider.slider('value')
# Redraw if triggered by manual slider scroll
@jumpTo @currentFunction, @sliderInput.val().toNumber() if event.originalEvent
@slider.on 'mouseenter', '.label', -> $(this).find('span').show()
@slider.on 'mouseout', '.label', -> $(this).find('span').hide()
next = => @jumpTo @currentFunction, @currentStep+1
prev = => @jumpTo @currentFunction, @currentStep-1
fnext = =>
step = @input.transforms.find (x) => x.id > @currentStep
@jumpTo @currentFunction, step.id if step
fprev = =>
step = @input.transforms.findAll((x) => x.id < @currentStep).last()
@jumpTo @currentFunction, step.id if step
@sliderPrev.click prev
@sliderNext.click next
@sliderFNext.click fnext
@sliderFPrev.click fprev
$(document).bind 'keydown.h', fprev
$(document).bind 'keydown.j', prev
$(document).bind 'keydown.k', next
$(document).bind 'keydown.l', fnext
$(document).bind 'keydown.x', => @zoomButton.click()
$(document).bind 'keydown.backspace', (e) ->
e.preventDefault()
history.back()