-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
furls.coffee
375 lines (351 loc) · 12.9 KB
/
furls.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
class Furls
constructor: ->
@inputs = []
#@inputsByName = {} # unclear this data structure is worthwhile
@listeners =
loadURL: []
inputChange: []
stateChange: []
## Coalesce multiple inputChange events into single stateChange event
@inputsChanged = {}
@on 'inputChange', (input) =>
## Radio buttons might trigger multiple changes, but we only want to
## store the one that is now checked, to avoid overwriting that one
## with the same name.
return unless input.value?
@inputsChanged[input.name] = input
unless @microtask
@microtask = true
@queueMicrotask =>
@microtask = false
return if (key for key of @inputsChanged).length == 0
@trigger 'stateChange', @inputsChanged
@inputsChanged = {}
queueMicrotask: (task) ->
if window?.queueMicrotask?
window.queueMicrotask task
else
setTimeout task, 0
on: (event, listener) ->
@listeners[event].push listener
@ # for chaining
off: (event, listener) ->
if 0 <= i = @listeners[event].indexOf listener
@listeners[event].splice i, 1
@ # for chaining
trigger: (event, ...args) ->
for listener in @listeners[event]
listener.call @, ...args
@ # for chaining
findInput: (input, noCrash) ->
## Support various ways to specify input: internal input object,
## string ID of <input> element, or DOM object of <input> element.
if input.dom? ## Internal interface
input
else
if typeof input == 'string' ## String ID
input = document.getElementById input
## DOM object
for inputObj in @inputs
if inputObj.dom == input
return inputObj
return if noCrash
throw new Error "Could not find input given #{input}"
configInput: (input, options) ->
input = @findInput input
input[key] = value for key, value of options if options?
@ # for chaining
## Use .checked for checkboxes and radio buttons, .value for other inputs.
## Radio buttons use `undefined` to denote "not checked", to avoid overwriting
## the correct value from the checked button.
## Automatically parse value for type=number.
## <select> uses an array of values if `multiple` is set, value otherwise.
getInputValue: (input) ->
switch input.type
when 'radio'
if input.dom.checked
input.dom.value
when 'checkbox'
input.dom.checked
when 'number', 'range'
parseFloat input.dom.value
when 'select'
if input.dom.multiple
for option in input.dom.selectedOptions
option.value
else
input.dom.value
else
input.dom.value
getInputDefaultValue: (input) ->
switch input.type
when 'radio'
if input.dom.defaultChecked
# dom.value only works when dom.checked is true
input.dom.getAttribute 'value'
when 'checkbox'
input.dom.defaultChecked
when 'number', 'range'
parseFloat input.dom.defaultValue
when 'select'
multiple = input.dom.multiple
for option in input.dom.options
continue unless option.defaultSelected
if multiple
option.value
else
return option.value
else
input.dom.defaultValue
setInputValue: (dom, value) ->
switch dom.type
when 'radio'
dom.checked = (value == dom.getAttribute 'value')
when 'checkbox'
## Convert to Boolean checked for checkboxes
switch value
when '1', 'true', true
value = true
when '0', 'false', false
value = false
else
value = !!value
dom.checked = value
when 'select'
if input.dom.multiple
value = [value] unless Array.isArray value
for option in input.dom.options
option.selected = (option.value in value)
else
for option in input.dom.options
if option.value == value
option.selected = true
break
else
dom.value = value
maybeChange: (input, recurse = true, trigger = true) ->
input = @findInput input
if input.value != (value = @getInputValue input)
input.oldValue = input.value
input.value = value
@trigger 'inputChange', input if trigger
## Auto-trigger change of all inputs with same name: radio buttons get
## events on the clicked button, but not on all the unset buttons.
if recurse
#for input2 in @inputsByName[input.name] when input != input2
for input2 in @inputs
if input2.name == input.name and input != input2
input2.lastEvent = input.lastEvent
@maybeChange input2, false
input.committed = input.value if @isCommitChange input
@ # for chaining
get: (input) ->
@findInput(input).value
set: (input, value) ->
input = @findInput input
input.lastEvent = 'set'
@setInputValue input.dom, value
@maybeChange input
## <input>s and <textarea>s should trigger 'input' events during every change,
## (according to HTML5), and 'change' events when the change is "committed"
## (for text fields, when losing focus). In some browsers, checkboxes and
## radio buttons don't trigger 'input', but they immediately trigger 'change'.
## So check for both, and just ignore the event if nothing changed.
getInputEvents: (input) ->
['input', 'change']
## Detect whether the last change to an input is a "commit":
## a 'change' event instead of an 'input' event, on inputs supporting both.
## (True for inputs not supporting both, and string events like "set".)
isCommitChange: (input) ->
input.type in ['select', 'radio', 'checkbox', 'button', 'submit', 'reset'] or
typeof input.lastEvent == 'string' or
input.lastEvent?.type == 'change'
addInput: (input, options) ->
if @findInput input, true
return @configInput input, options
if typeof input == 'string'
input = id: input
else if input instanceof HTMLElement
input = dom: input
unless input.dom?
input.dom = document.getElementById input.id
unless input.id?
input.id = input.dom.getAttribute 'id'
unless input.type?
input.type =
switch input.dom.tagName.toLowerCase()
when 'textarea' then 'textarea'
when 'select' then 'select'
when 'input' then input.dom.getAttribute('type').toLowerCase()
unless input.name?
input.name = input.dom.getAttribute('name') ? input.id
unless input.defaultValue?
input.defaultValue = @getInputDefaultValue input
input.value = input.committed = @getInputValue input
@configInput input, options
@inputs.push input
#(@inputsByName[input.name] ?= []).push input
input.listeners =
for event in @getInputEvents input
input.dom.addEventListener event, listener = (e) =>
input.lastEvent = e
@maybeChange input
listener
@ # for chaining
addInputs: (selector = 'input, select, textarea') ->
if typeof selector == 'string'
selector = document.querySelectorAll selector
for input in selector
@addInput input
@ # for chaining
removeInput: (input) ->
input = @findInput input
for event, i in @getInputEvents input
input.dom.removeEventListener event, input.listeners[i]
if @_syncClass? and target.value?
for target in @_syncClass.selector
target.classList.remove "#{@_syncClass.prefix}#{input.name}-#{input.value}"
@ # for chaining
removeInputs: (selector) ->
if typeof selector == 'string'
selector = document.querySelectorAll selector
else if not Array.isArray selector
selector = [selector]
@inputs =
for input in @inputs
if input in selector or input.dom in selector
@removeInput input
continue
else
input
@ # for chaining
clearInputs: ->
@removeInputs @inputs
getState: ->
state = {}
for input in @inputs
value = @getInputValue input
## Avoid overwriting the correct value for radio buttons sharing a name
if value?
state[input.name] = value
state
getSearch: ->
search = (
for input in @inputs
value = @getInputValue input
## Don't store default values
continue if value == input.defaultValue
## Don't store off radio buttons; just need the "on" one
continue unless value?
## Custom encoding
value = input.encode value if input.encode?
## Stringify booleans for checkboxes
switch value
when true
value = '1'
when false
value = '0'
"#{input.name}=#{encodeURIComponent(value).replace /%20/g, '+'}"
).join '&'
search = "?#{search}" if search
search
getRelativeURL: ->
"#{window.location.pathname}#{@getSearch()}"
## Based on jolly.exe's code from http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
getParameterByName: (name, search = window.location.search) ->
## https://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex
name = name.replace /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"
regex = new RegExp "[\\?&]#{name}=([^&#]*)"
results = regex.exec search
return null unless results?
decodeURIComponent results[1].replace /\+/g, " "
loadURL: (url = window.location, trigger = true) ->
@loading = true
if url.search?
search = url.search
else if url[0] == '?'
search = url
else
search = /\?.*$/.exec(url)?[0] ? ''
## Reset all inputs to defaults before loading new values, because we
## only put deviation from defaults in the URL. This needs to be in a
## separate stage because of checkboxes.
for input in @inputs
@setInputValue input.dom, input.defaultValue
input.lastEvent = 'load'
## Do custom decoding in a separate phase after inputs without
## custom decoding, so that custom decoding can depend on those inputs.
for customDecode in [false, true]
for input in @inputs when input.decode? == customDecode
value = @getParameterByName input.name, search
if value?
value = input.decode value if input.decode?
else
value = input.defaultValue
@setInputValue input.dom, value
## Update value and oldValue, and optionally trigger inputChange event
## which eventually triggers a stateChange event.
## Don't recurse on identically named inputs, as we process all inputs.
@maybeChange input, false, trigger
for input in @inputs
## Don't recurse on identically named inputs, as we process all inputs.
@maybeChange input, false, trigger
@trigger 'loadURL', search
## Schedule after possibly triggered stateChange event.
@queueMicrotask => @loading = false
@ # for chaining
setURL: (history = 'push', force) ->
search = @getSearch()
if force or search != window.location.search
window.history[history+'State'] null, 'furls', "#{window.location.pathname}#{search}"
@ # for chaining
replaceState: (force) -> @setURL 'replace', force
pushState: (force) -> @setURL 'push', force
syncState: (history = 'auto', loadNow = true) ->
@on 'stateChange', (changed) =>
return if @loading
## Check whether all changes are minor, and force 'replace' in that case.
minor = true
for name, input of changed
unless input.minor or (
history == 'auto' and
input.committed not in [input.oldValue, input.value]
)
minor = false
break
if minor
@setURL 'replace'
else if history == 'auto'
@setURL 'push'
else
@setURL history
window.addEventListener 'popstate', => @loadURL()
## On initial load, treat as transition from undefined values to defaults.
if loadNow
input.value = undefined for input in @inputs
@loadURL()
@ # for chaining
## Which types have discrete values like `true` and `false`, and thus are
## appropriate for classes.
discreteValue: (input) ->
input.type in ['checkbox', 'radio']
syncClass: (selector = [document.documentElement], prefix = '',
updateNow = true) ->
if typeof selector == 'string'
selector = document.querySelectorAll selector
else if not Array.isArray selector
selector = [selector]
@on 'inputChange', (input) =>
if @discreteValue input
for target in selector
target.classList.remove "#{prefix}#{input.name}-#{input.oldValue}" if input.oldValue?
target.classList.add "#{prefix}#{input.name}-#{input.value}" if input.value?
if updateNow
for input in @inputs when input.value?
if @discreteValue input
for target in selector
target.classList.add "#{prefix}#{input.name}-#{input.value}"
@_syncClass = {selector, prefix}
@ # for chaining
module?.exports = Furls
window?.Furls = Furls