1 # This file constructs VimFx’s options UI in the Add-ons Manager.
3 defaults = require('./defaults')
4 prefs = require('./prefs')
5 prefsBulk = require('./prefs-bulk')
6 translate = require('./translate')
7 utils = require('./utils')
15 observe = (options) ->
16 observer = new Observer(options)
17 utils.observe('addon-options-displayed', observer)
18 utils.observe('addon-options-hidden', observer)
23 # Generalized observer.
25 constructor: (@options) ->
32 listen: (element, event, action) ->
33 element.addEventListener(event, action, @useCapture)
34 @listeners.push([element, event, action, @useCapture])
37 for [element, event, action, useCapture] in @listeners
38 element.removeEventListener(event, action, useCapture)
41 type: (value) -> TYPE_MAP[typeof value]
45 appendSetting: (attributes) ->
46 setting = @document.createElement('setting')
47 utils.setAttributes(setting, attributes)
48 @container.appendChild(setting)
51 observe: (@document, topic, addonId) ->
52 return unless addonId == @options.id
54 when 'addon-options-displayed'
56 when 'addon-options-hidden'
60 @container = @document.getElementById('detail-rows')
66 # VimFx specific observer.
67 class Observer extends BaseObserver
68 constructor: (@vimfx) ->
69 super({id: @vimfx.id})
79 utils.nextTick(@document.ownerGlobal, =>
80 {pref} = @vimfx.goToCommand
81 setting = @container.querySelector("setting[pref='#{pref}']")
82 setting.scrollIntoView()
83 setting.input.select()
84 @vimfx.goToCommand = null
88 setting = @appendSetting({
90 title: translate('prefs.instructions.title')
92 'prefs.instructions.desc',
93 @vimfx.options['options.key.quote'],
94 @vimfx.options['options.key.insert_default'],
95 @vimfx.options['options.key.reset_default'],
101 href = "#{@vimfx.info.homepageURL}/tree/master/documentation#contents"
102 docsLink = @document.createElement('label')
103 utils.setAttributes(docsLink, {
104 value: translate('prefs.documentation')
109 docsLink.style.MozBoxFlex = '1'
110 setting.appendChild(docsLink)
112 for key, fn of BUTTONS
113 button = @document.createElement('button')
114 button.setAttribute('label', translate("prefs.#{key}.label"))
115 button.onclick = runWithVim.bind(null, @vimfx, fn)
116 setting.appendChild(button)
121 for key, value of defaults.options
122 setting = @appendSetting({
123 pref: "#{defaults.BRANCH}#{key}"
125 title: translate("pref.#{key}.title")
126 desc: translate("pref.#{key}.desc")
131 for mode in @vimfx.getGroupedCommands()
138 for category in mode.categories
146 for {command} in category.commands
150 title: command.description
151 desc: @generateErrorMessage(command.pref)
157 generateErrorMessage: (pref) ->
158 commandErrors = @vimfx.errors[pref] ? []
159 return commandErrors.map(({id, context, subject}) ->
160 return translate("error.#{id}", context ? subject, subject)
164 # Note that `setting = event.originalTarget` does _not_ return the correct
165 # element in these listeners!
167 @listen(@container, 'keydown', (event) =>
168 setting = event.target
169 isString = (setting.type == 'string')
171 {input, pref} = setting
172 keyString = @vimfx.stringifyKeyEvent(event)
174 # Some shortcuts only make sense for string settings. We still match
175 # those shortcuts and suppress the default behavior for _all_ types of
176 # settings for consistency. For example, pressing <c-d> in a number input
177 # (which looks like a text input) would otherwise bookmark the page, and
178 # <c-q> would close the window!
183 break unless isString
184 utils.insertText(input, keyString)
186 when keyString == @vimfx.options['options.key.quote']
187 break unless isString
189 # Override `<force>` commands (such as `<escape>` and `<tab>`).
190 return unless vim = @vimfx.getCurrentVim(utils.getCurrentWindow())
191 @vimfx.modes.normal.commands.quote.run({vim, count: 1})
192 when keyString == @vimfx.options['options.key.insert_default']
193 break unless isString
194 utils.insertText(input, prefs.root.default.get(pref))
195 when keyString == @vimfx.options['options.key.reset_default']
196 prefs.root.set(pref, null)
200 event.preventDefault()
201 setting.valueToPreference()
202 @refreshShortcutErrors()
204 @listen(@container, 'blur', -> quote = false)
207 @listen(@container, 'input', (event) =>
208 setting = event.target
209 # Disable default behavior of updating the pref of the setting on each
210 # input. Do it on the 'change' event instead (see below), because all
211 # settings are validated and auto-adjusted as soon as the pref changes.
212 event.stopPropagation()
213 if setting.classList.contains('is-shortcut')
214 # However, for the shortcuts we _do_ want live validation, because they
215 # cannot be auto-adjusted. Instead an error message is shown.
216 setting.valueToPreference()
217 @refreshShortcutErrors()
220 @listen(@container, 'change', (event) ->
221 setting = event.target
222 unless setting.classList.contains('is-shortcut')
223 setting.valueToPreference()
226 refreshShortcutErrors: ->
227 for setting in @container.getElementsByClassName('is-shortcut')
228 setting.setAttribute('desc', @generateErrorMessage(setting.pref))
231 resetAllPrefs = (vim) ->
232 vim._modal('confirm', [translate('prefs.reset.enter')], (ok) ->
235 vim.notify(translate('prefs.reset.success'))
238 exportAllPrefs = (vim) ->
239 exported = prefsBulk.exportAll()
240 if Object.keys(exported).length == 0
241 vim.notify(translate('prefs.export.none'))
243 utils.writeToClipboard(JSON.stringify(exported, null, 2))
244 vim.notify(translate('prefs.export.success'))
246 importExportedPrefs = (vim) ->
247 vim._modal('prompt', [translate('prefs.import.enter')], (input) ->
248 return if input == null or input.trim() == ''
249 result = prefsBulk.importExported(input.trim())
250 if result.errors.length == 0
251 vim.notify(translate('prefs.import.success'))
253 vim._modal('alert', [prefsBulk.createImportErrorReport(result)])
256 runWithVim = (vimfx, fn) ->
257 return unless vim = vimfx.getCurrentVim(utils.getCurrentWindow())
261 export: exportAllPrefs
262 import: importExportedPrefs