-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.coffee
56 lines (46 loc) · 1.66 KB
/
options.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
root = exports ? this
# Saves options to localStorage.
wpm = null
beep_freq = null
enable_popup = null
save_options = ->
wpm.save()
beep_freq.save()
enable_popup.save()
status = document.getElementById("status")
status.innerHTML = "Options Saved."
setTimeout(->
status.innerHTML = ""
, 750)
# Restores select box state to saved value from localStorage.
restore_options = ->
wpm = new StoredOption("wpm", "wpm", value_updater("wpm_value"), 20)
beep_freq = new StoredOption("freq", "beep_freq", value_updater("beep_value"), 600)
enable_popup = new StoredBoolean("popup", "popup")
wpm.load()
beep_freq.load()
enable_popup.load()
value_updater = (id) ->
div = document.getElementById(id)
(value) -> div.innerHTML = value
class StoredOption
constructor: (id, @storage_key, @databinder, @default_value) ->
@element = document.getElementById(id)
@element.onchange = =>
@databinder(@value())
save: -> localStorage[@storage_key] = @value()
load: ->
@element.value = localStorage[@storage_key] || @default_value
@databinder(@value()) # initialize
value: -> @element.value
class StoredBoolean extends StoredOption
constructor: (id, storage_key) ->
super(id, storage_key, (value) -> )
value: -> @element.checked
load: -> @element.checked = localStorage[@storage_key] != "false"
sample = -> encode("SOS", wpm.value(), beep_freq.value(), enable_popup.value())
document.addEventListener("DOMContentLoaded", () ->
restore_options()
document.getElementById("sample").onclick = sample
document.getElementById("save").onclick = save_options
)