-
Notifications
You must be signed in to change notification settings - Fork 20
/
scrubby.coffee
208 lines (188 loc) · 5.48 KB
/
scrubby.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
toRun = []
sources = {}
$values = {}
merge = (a,b) ->
for k,v of b
a[k] = v
prepare = (js, name) ->
xfmd = xform js, name+' '
merge $values, xfmd.values
sources[name] = { orig: js, xfmd }
toRun.push escodegen.generate xfmd.ast
hasRun = false
run = ->
throw "can't dynamically load scrubbys yet" if hasRun
hasRun = true
window.eval escodegen.generate
type: 'Program'
body: [
type: 'VariableDeclaration'
kind: 'var'
declarations: [
type: 'VariableDeclarator'
id: { type: 'Identifier', name: '$values' }
init:
type: 'ObjectExpression',
properties: (
{
type: 'Property'
key:
type: 'Literal'
value: k
value:
type: 'Literal'
value: v.value
kind: 'init'
} for k, v of $values
)
]
]
for s in toRun
window.eval s
scrubby.emit 'loaded'
load = (url, cb) ->
xhr = if window.ActiveXObject
new window.ActiveXObject('Microsoft.XMLHTTP')
else
new XMLHttpRequest()
xhr.open 'GET', url, true
xhr.overrideMimeType 'text/plain' if 'overrideMimeType' of xhr
handled = false
xhr.onreadystatechange = ->
return if handled
if xhr.readyState is 4
handled = true
if xhr.status in [0, 200]
prepare xhr.responseText, url
else
throw new Error "Could not load #{url}"
cb() if cb
xhr.send null
deltaForNumber = (n) ->
# big ol' hax to get an approximately okay order-of-magnitude delta for
# dragging a number around.
# right now this has a tendency to make your number more specific all the
# time, which might be problematic.
return 1 if n is 0
return 0.1 if n is 1
lastDigit = (n) ->
Math.round((n/10-Math.floor(n/10))*10)
firstSig = (n) ->
n = Math.abs(n)
i = 0
while lastDigit(n) is 0
i++
n /= 10
i
specificity = (n) ->
s = 0
loop
abs = Math.abs(n)
fraction = abs - Math.floor(abs)
if fraction < 0.000001
return s
s++
n = n * 10
s = specificity n
if s > 0
Math.pow(10, -s)
else
n = Math.abs n
Math.pow 10, Math.max 0, firstSig(n)-1
attachScrubber = (w, s) ->
s.addEventListener 'mousedown', (e) ->
e.preventDefault()
mx = e.pageX; my = e.pageY
originalValue = Number(s.textContent)
delta = deltaForNumber originalValue
w.document.documentElement.classList.add('dragging')
moved = (e) ->
e.preventDefault()
d = Number((Math.round((e.pageX - mx)/2)*delta + originalValue).toFixed(5))
s.textContent = d
window.$values[s.value_id] = d
window.scrubby.emit 'scrubbed'
w.addEventListener('mousemove', moved)
up = (e) ->
w.removeEventListener('mousemove', moved)
w.removeEventListener('mouseup', up)
w.document.documentElement.classList.remove('dragging')
w.addEventListener('mouseup', up)
makeScrubbingContext = (w, name) ->
w.document.head.appendChild(document.createElement('style')).textContent = '''
.scrub {
cursor: ew-resize;
border-bottom: 1px dashed blue;
}
html.dragging {
cursor: ew-resize;
}
'''
code_text = sources[name].orig
curpos = 0
newCode = w.document.createElement('pre')
for i,val of sources[name].xfmd.values
newCode.appendChild(document.createTextNode(code_text.substring(curpos, val.range[0])))
scrubber = newCode.appendChild(document.createElement('span'))
scrubber.textContent = window.$values[i]
scrubber.className = 'scrub'
scrubber.value_id = i
attachScrubber w, scrubber
curpos = val.range[1]
newCode.appendChild(document.createTextNode(code_text.substring(curpos)))
return newCode
makeScrubbyButton = (name) ->
b = document.createElement('button')
b.textContent = name.replace(location.origin+'/', '')
b.onclick = ->
bounds = 'left='+screenX+',top='+screenY+',width=600,height=500'
w = window.open('','_blank','menubar=no,location=no,resizable=yes,scrollbars=yes,status=no,'+bounds)
newCode = makeScrubbingContext w, name
w.document.body.appendChild newCode
b
runScripts = ->
scripts = document.getElementsByTagName 'script'
scrubbys = (s for s in scripts when s.type is 'text/scrubby')
return unless scrubbys.length
document.head.appendChild(document.createElement('style')).textContent = '''
.scrubby-files {
position: fixed;
top: 4px;
right: 4px;
padding: 4px;
border-radius: 2px;
background: rgba(0,0,255,0.4);
}
'''
scrubbyFiles = document.body.appendChild(document.createElement('div'))
scrubbyFiles.className = 'scrubby-files'
id = 1
do execute = ->
if scrubbys.length is 0
run()
script = scrubbys.shift()
if script?.type is 'text/scrubby'
if script.src
scrubbyFiles.appendChild(makeScrubbyButton(script.src))
load script.src, execute
else
name = 'inline#'+id++
scrubbyFiles.appendChild(makeScrubbyButton(name))
prepare script.innerHTML, name
execute()
null
# Listen for window load, both in browsers and in IE.
if window.addEventListener
addEventListener 'DOMContentLoaded', runScripts, no
else
attachEvent 'onload', runScripts
window.scrubby =
on: (e, f) ->
@_listeners ?= {}
(@_listeners[e] ?= []).push f
emit: (e, args...) ->
return unless @_listeners[e]?
l.call(undefined, args...) for l in @_listeners[e]
null
makeScrubbingFrame: (name) ->
makeScrubbingContext window, name