aboutsummaryrefslogtreecommitdiffstats
path: root/pages/options.coffee
blob: 776432d2fc740b1de2f206d21463b858cb75fb46 (plain)
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
$ = (id) -> document.getElementById id
bgSettings = chrome.extension.getBackgroundPage().Settings
Exclusions = chrome.extension.getBackgroundPage().Exclusions

#
# Class hierarchy for various types of option.
class Option
  # Base class for all option classes.
  # Abstract. Option does not define @populateElement or @readValueFromElement.

  # Static. Array of all options.
  @all = []

  constructor: (field,enableSaveButton) ->
    @field = field
    @onUpdated = enableSaveButton
    @element = $(@field)
    @element.addEventListener "change", enableSaveButton
    @fetch()
    Option.all.push @

  # Fetch a setting from localStorage, remember the @previous value and populate the DOM element.
  # Return the fetched value.
  fetch: ->
    @populateElement @previous = bgSettings.get @field
    @previous

  # Write this option's new value back to localStorage, if necessary.
  save: ->
    value = @readValueFromElement()
    if not @areEqual value, @previous
      bgSettings.set @field, @previous = value
      bgSettings.performPostUpdateHook @field, value

  # Compare values; this is overridden by sub-classes.
  areEqual: (a,b) -> a == b

  restoreToDefault: ->
    bgSettings.clear @field
    @fetch()

  # Static method.
  @saveOptions: ->
    Option.all.map (option) -> option.save()
    # These are only relevant on the options page; catch the exception on the popup page.
    try
      $("saveOptions").disabled = true
      $("saveOptions").innerHTML = "No Changes"

  # Abstract method; only implemented in sub-classes.
  # Populate the option's DOM element (@element) with the setting's current value.
  # populateElement: (value) -> DO_SOMETHING

  # Abstract method; only implemented in sub-classes.
  # Extract the setting's new value from the option's DOM element (@element).
  # readValueFromElement: -> RETURN_SOMETHING

class NumberOption extends Option
  populateElement: (value) -> @element.value = value
  readValueFromElement: -> parseFloat @element.value

class TextOption extends Option
  constructor: (field,enableSaveButton) ->
    super(field,enableSaveButton)
    @element.addEventListener "input", enableSaveButton
  populateElement: (value) -> @element.value = value
  readValueFromElement: -> @element.value.trim()

class NonEmptyTextOption extends Option
  constructor: (field,enableSaveButton) ->
    super(field,enableSaveButton)
    @element.addEventListener "input", enableSaveButton

  populateElement: (value) -> @element.value = value
  # If the new value is not empty, then return it. Otherwise, restore the default value.
  readValueFromElement: -> if value = @element.value.trim() then value else @restoreToDefault()

class CheckBoxOption extends Option
  populateElement: (value) -> @element.checked = value
  readValueFromElement: -> @element.checked

class ExclusionRulesOption extends Option
  constructor: (field, onUpdated, @url=null) ->
    super(field, onUpdated)
    $("exclusionAddButton").addEventListener "click", (event) =>
      @appendRule { pattern: "", passKeys: "" }
      # Focus the pattern element in the new rule.
      @element.children[@element.children.length-1].children[0].children[0].focus()
      # Scroll the new rule into view.
      exclusionScrollBox = $("exclusionScrollBox")
      exclusionScrollBox.scrollTop = exclusionScrollBox.scrollHeight

  populateElement: (rules) ->
    for rule in rules
      @appendRule rule

    # If this is the popup page (@url is defined), then hide rules which do not match @url.  If no rules
    # match, then add a default rule.
    if @url
      haveMatch = false
      for element in @element.getElementsByClassName "exclusionRuleTemplateInstance"
        pattern = element.children[0].firstChild.value.trim()
        unless @url.match Exclusions.RegexpCache.get pattern
          element.style.display = 'none'
          haveMatch = true

  # Append a row for a new rule.
  appendRule: (rule) ->
    content = document.querySelector('#exclusionRuleTemplate').content
    row = document.importNode content, true

    for field in ["pattern", "passKeys"]
      element = row.querySelector ".#{field}"
      element.value = rule[field]
      for event in [ "input", "change" ]
        element.addEventListener event, @onUpdated

    remove = row.querySelector ".exclusionRemoveButton"
    remove.addEventListener "click", (event) =>
      row = event.target.parentNode.parentNode
      row.parentNode.removeChild row
      @onUpdated()

    @element.appendChild row

  readValueFromElement: ->
    rules =
      for element in @element.getElementsByClassName "exclusionRuleTemplateInstance"
        pattern = element.children[0].firstChild.value.trim()
        passKeys = element.children[1].firstChild.value.trim()
        { pattern: pattern, passKeys: passKeys }
    rules.filter (rule) -> rule.pattern

  areEqual: (a,b) ->
    # Flatten each list of rules to a newline-separated string representation, and then use string equality.
    # This is correct because patterns and passKeys cannot themselves contain newlines.
    flatten = (rule) -> if rule and rule.pattern then rule.pattern + "\n" + rule.passKeys else ""
    a.map(flatten).join("\n") == b.map(flatten).join("\n")

#
# Operations for page elements.
enableSaveButton = ->
  $("saveOptions").removeAttribute "disabled"
  $("saveOptions").innerHTML = "Save Changes"

# Display either "linkHintNumbers" or "linkHintCharacters", depending upon "filterLinkHints".
maintainLinkHintsView = ->
  hide = (el) -> el.parentNode.parentNode.style.display = "none"
  show = (el) -> el.parentNode.parentNode.style.display = "table-row"
  if $("filterLinkHints").checked
    hide $("linkHintCharacters")
    show $("linkHintNumbers")
  else
    show $("linkHintCharacters")
    hide $("linkHintNumbers")

toggleAdvancedOptions =
  do (advancedMode=false) ->
    (event) ->
      if advancedMode
        $("advancedOptions").style.display = "none"
        $("advancedOptionsLink").innerHTML = "Show advanced options…"
      else
        $("advancedOptions").style.display = "table-row-group"
        $("advancedOptionsLink").innerHTML = "Hide advanced options"
      advancedMode = !advancedMode
      event.preventDefault()
      # Prevent the "advanced options" link from retaining the focus.
      document.activeElement.blur()

activateHelpDialog = ->
  showHelpDialog chrome.extension.getBackgroundPage().helpDialogHtml(true, true, "Command Listing"), frameId
  # Prevent the "show help" link from retaining the focus.
  document.activeElement.blur()

initOptions = ->
  options =
    exclusionRules: ExclusionRulesOption
    filterLinkHints: CheckBoxOption
    hideHud: CheckBoxOption
    keyMappings: TextOption
    linkHintCharacters: NonEmptyTextOption
    linkHintNumbers: NonEmptyTextOption
    newTabUrl: NonEmptyTextOption
    nextPatterns: NonEmptyTextOption
    previousPatterns: NonEmptyTextOption
    regexFindMode: CheckBoxOption
    scrollStepSize: NumberOption
    smoothScroll: CheckBoxOption
    searchEngines: TextOption
    searchUrl: NonEmptyTextOption
    userDefinedLinkHintCss: TextOption

  # Populate options.  The constructor adds each new object to "Option.all".
  for name, type of options
    new type(name,enableSaveButton)

  $("saveOptions").addEventListener "click", Option.saveOptions
  $("advancedOptionsLink").addEventListener "click", toggleAdvancedOptions
  $("showCommands").addEventListener "click", activateHelpDialog
  $("filterLinkHints").addEventListener "click", maintainLinkHintsView

  for element in document.getElementsByClassName "nonEmptyTextOption"
    element.className = element.className + " example info"
    element.innerHTML = "Leave empty to reset this option."

  maintainLinkHintsView()
  window.onbeforeunload = -> "You have unsaved changes to options." unless $("saveOptions").disabled

  document.addEventListener "keyup", (event) ->
    if event.ctrlKey and event.keyCode == 13
      document.activeElement.blur() if document?.activeElement?.blur
      Option.saveOptions()

initPopup = ->
  chrome.tabs.getSelected null, (tab) ->
    document.getElementById("optionsLink").setAttribute "href", chrome.runtime.getURL("pages/options.html")
    updated = false

    onUpdated = ->
      $("helpText").innerHTML = "Type <strong>Ctrl-Enter</strong> to save and close; <strong>Esc</strong> to cancel."
      updated = true

    document.addEventListener "keyup", (event) ->
      if event.ctrlKey and event.keyCode == 13
        Option.saveOptions()
        window.close()

    new ExclusionRulesOption("exclusionRules", onUpdated, tab.url)

#
# Initialization.
document.addEventListener "DOMContentLoaded", ->
  switch location.pathname
    when "/pages/options.html" then initOptions()
    when "/pages/popup.html" then initPopup()