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 # This file constructs VimFx’s options UI in the Add-ons Manager.
22 defaults = require('./defaults')
23 translate = require('./l10n')
24 prefs = require('./prefs')
25 utils = require('./utils')
32 observe = (options) ->
33 observer = new Observer(options)
34 utils.observe('addon-options-displayed', observer)
35 utils.observe('addon-options-hidden', observer)
40 # Generalized observer.
42 constructor: (@options) ->
49 listen: (element, event, action) ->
50 element.addEventListener(event, action, @useCapture)
51 @listeners.push([element, event, action, @useCapture])
54 for [element, event, action, useCapture] in @listeners
55 element.removeEventListener(event, action, useCapture)
58 type: (value) -> TYPE_MAP[typeof value]
62 appendSetting: (attributes) ->
63 setting = @document.createElement('setting')
64 utils.setAttributes(setting, attributes)
65 @container.appendChild(setting)
68 observe: (@document, topic, addonId) ->
69 return unless addonId == @options.id
71 when 'addon-options-displayed'
73 when 'addon-options-hidden'
77 @container = @document.getElementById('detail-rows')
83 # VimFx specific observer.
84 class Observer extends BaseObserver
85 constructor: (@vimfx) ->
86 super({id: @vimfx.id})
95 injectInstructions: ->
96 setting = @appendSetting({
98 title: translate('prefs.instructions.title')
99 desc: translate('prefs.instructions.desc',
100 @vimfx.options['options.key.quote'],
101 @vimfx.options['options.key.insert_default'],
102 @vimfx.options['options.key.reset_default'],
106 href = "#{ @vimfx.info.homepageURL }/tree/master/documentation"
107 docsLink = @document.createElement('label')
108 utils.setAttributes(docsLink, {
109 value: translate('prefs.documentation')
114 setting.appendChild(docsLink)
117 for key, value of defaults.options
118 setting = @appendSetting({
119 pref: "#{ defaults.BRANCH }#{ key }"
121 title: translate("pref.#{ key }.title")
122 desc: translate("pref.#{ key }.desc")
127 for mode in @vimfx.getGroupedCommands()
134 for category in mode.categories
142 for { command } in category.commands
146 title: command.description()
147 desc: @generateErrorMessage(command.pref)
153 generateErrorMessage: (pref) ->
154 commandErrors = @vimfx.errors[pref] ? []
155 return commandErrors.map(({ id, context, subject }) ->
156 return translate("error.#{ id }", context ? subject, subject)
160 # Note that `setting = event.originalTarget` does _not_ return the correct
161 # element in these listeners!
163 @listen(@container, 'keydown', (event) =>
164 setting = event.target
165 isString = (setting.type == 'string')
167 { input, pref } = setting
168 keyString = @vimfx.stringifyKeyEvent(event)
170 # Some shortcuts only make sense for string settings. We still match
171 # those shortcuts and suppress the default behavior for _all_ types of
172 # settings for consistency. For example, pressing <c-d> in a number input
173 # (which looks like a text input) would otherwise bookmark the page, and
174 # <c-q> would close the window!
179 break unless isString
180 utils.insertText(input, keyString)
182 when keyString == @vimfx.options['options.key.quote']
183 break unless isString
185 # Override `<force>` commands (such as `<escape>` and `<tab>`).
186 return unless vim = @vimfx.getCurrentVim(utils.getCurrentWindow())
187 @vimfx.modes.normal.commands.quote.run({vim, count: 1})
188 when keyString == @vimfx.options['options.key.insert_default']
189 break unless isString
190 utils.insertText(input, prefs.root.default.get(pref))
191 when keyString == @vimfx.options['options.key.reset_default']
192 prefs.root.set(pref, null)
196 event.preventDefault()
197 setting.valueToPreference()
198 @refreshShortcutErrors()
200 @listen(@container, 'blur', -> quote = false)
203 @listen(@container, 'input', (event) =>
204 setting = event.target
205 # Disable default behavior of updating the pref of the setting on each
206 # input. Do it on the 'change' event instead (see below), because all
207 # settings are validated and auto-adjusted as soon as the pref changes.
208 event.stopPropagation()
209 if setting.classList.contains('is-shortcut')
210 # However, for the shortcuts we _do_ want live validation, because they
211 # cannot be auto-adjusted. Instead an error message is shown.
212 setting.valueToPreference()
213 @refreshShortcutErrors()
216 @listen(@container, 'change', (event) ->
217 setting = event.target
218 unless setting.classList.contains('is-shortcut')
219 setting.valueToPreference()
222 refreshShortcutErrors: ->
223 for setting in @container.getElementsByClassName('is-shortcut')
224 setting.setAttribute('desc', @generateErrorMessage(setting.pref))