2 # Copyright Simon Lydell 2015.
4 # This file is part of VimFx.
6 # VimFx is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # VimFx is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
20 defaults = require('./defaults')
21 translate = require('./l10n')
22 prefs = require('./prefs')
23 utils = require('./utils')
31 observe = (options) ->
32 observer = new Observer(options)
33 utils.observe('addon-options-displayed', observer)
34 utils.observe('addon-options-hidden', observer)
39 # Generalized observer.
41 constructor: (@options) ->
48 listen: (element, event, action) ->
49 element.addEventListener(event, action, @useCapture)
50 @listeners.push([element, event, action, @useCapture])
53 for [element, event, action, useCapture] in @listeners
54 element.removeEventListener(event, action, useCapture)
57 type: (value) -> TYPE_MAP[typeof value]
61 appendSetting: (attributes) ->
62 setting = @document.createElement('setting')
63 utils.setAttributes(setting, attributes)
64 @container.appendChild(setting)
67 observe: (@document, topic, addonId) ->
68 return unless addonId == @options.id
70 when 'addon-options-displayed'
72 when 'addon-options-hidden'
76 @container = @document.getElementById('detail-rows')
82 # VimFx specific observer.
83 class Observer extends BaseObserver
84 constructor: (@vimfx) ->
85 super({id: @vimfx.id})
94 injectInstructions: ->
95 setting = @appendSetting({
97 title: translate('prefs.instructions.title')
98 desc: translate('prefs.instructions.desc',
99 @vimfx.options['options.key.quote'],
100 @vimfx.options['options.key.insert_default'],
101 @vimfx.options['options.key.reset_default'],
105 href = "#{ @vimfx.info.homepageURL }/tree/master/documentation"
106 docsLink = @document.createElement('label')
107 utils.setAttributes(docsLink, {
108 value: translate('prefs.documentation')
113 setting.appendChild(docsLink)
116 for key, value of defaults.options
117 setting = @appendSetting({
118 pref: "#{ defaults.BRANCH }#{ key }"
120 title: translate("pref.#{ key }.title")
121 desc: translate("pref.#{ key }.desc")
126 for mode in @vimfx.getGroupedCommands()
133 for category in mode.categories
141 for { command } in category.commands
145 title: command.description()
146 desc: @generateErrorMessage(command.pref)
152 generateErrorMessage: (pref) ->
153 commandErrors = @vimfx.errors[pref] ? []
154 return commandErrors.map(({ id, context, subject }) ->
155 return translate("error.#{ id }", context ? subject, subject)
160 @listen(@container, 'keydown', (event) =>
161 setting = event.target
162 isString = (setting.type == 'string')
164 { input, pref } = setting
165 keyString = @vimfx.stringifyKeyEvent(event)
167 # Some shortcuts only make sense for string settings. We still match
168 # those shortcuts and suppress the default behavior for _all_ types of
169 # settings for consistency. For example, pressing <c-d> in a number input
170 # (which looks like a text input) would otherwise bookmark the page, and
171 # <c-q> would close the window!
176 break unless isString
177 utils.insertText(input, keyString)
179 when keyString == @vimfx.options['options.key.quote']
180 break unless isString
182 # Override `<force>` commands (such as `<escape>` and `<tab>`).
183 vim = @vimfx.currentVim
184 @vimfx.modes.normal.commands.quote.run({vim, count: 1})
185 when keyString == @vimfx.options['options.key.insert_default']
186 break unless isString
187 utils.insertText(input, prefs.root.default.get(pref))
188 when keyString == @vimfx.options['options.key.reset_default']
189 prefs.root.set(pref, null)
193 event.preventDefault()
194 setting.valueToPreference()
195 @refreshShortcutErrors()
197 @listen(@container, 'blur', -> quote = false)
200 @listen(@container, 'input', (event) =>
201 setting = event.target
202 # Disable default behavior of updating the pref of the setting on each
203 # input. Do it on the 'change' event instead (see below), because all
204 # settings are validated and auto-adjusted as soon as the pref changes.
205 event.stopPropagation()
206 if setting.classList.contains('is-shortcut')
207 # However, for the shortcuts we _do_ want live validation, because they
208 # cannot be auto-adjusted. Instead an error message is shown.
209 setting.valueToPreference()
210 @refreshShortcutErrors()
212 @listen(@container, 'change', (event) ->
213 setting = event.target
214 unless setting.classList.contains('is-shortcut')
215 setting.valueToPreference()
219 refreshShortcutErrors: ->
220 for setting in @container.getElementsByClassName('is-shortcut')
221 setting.setAttribute('desc', @generateErrorMessage(setting.pref))