]> git.gir.st - VimFx.git/blob - extension/lib/options.coffee
Enable the `no_implicit_braces` CoffeeLint rule
[VimFx.git] / extension / lib / options.coffee
1 ###
2 # Copyright Simon Lydell 2015, 2016.
3 #
4 # This file is part of VimFx.
5 #
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.
10 #
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.
15 #
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/>.
18 ###
19
20 # This file constructs VimFx’s options UI in the Add-ons Manager.
21
22 defaults = require('./defaults')
23 translate = require('./l10n')
24 prefs = require('./prefs')
25 utils = require('./utils')
26
27 TYPE_MAP = {
28 string: 'string'
29 number: 'integer'
30 boolean: 'bool'
31 }
32
33 observe = (options) ->
34 observer = new Observer(options)
35 utils.observe('addon-options-displayed', observer)
36 utils.observe('addon-options-hidden', observer)
37 module.onShutdown(->
38 observer.destroy()
39 )
40
41 # Generalized observer.
42 class BaseObserver
43 constructor: (@options) ->
44 @document = null
45 @container = null
46 @listeners = []
47
48 useCapture: true
49
50 listen: (element, event, action) ->
51 element.addEventListener(event, action, @useCapture)
52 @listeners.push([element, event, action, @useCapture])
53
54 unlisten: ->
55 for [element, event, action, useCapture] in @listeners
56 element.removeEventListener(event, action, useCapture)
57 @listeners.length = 0
58
59 type: (value) -> TYPE_MAP[typeof value]
60
61 injectSettings: ->
62
63 appendSetting: (attributes) ->
64 setting = @document.createElement('setting')
65 utils.setAttributes(setting, attributes)
66 @container.appendChild(setting)
67 return setting
68
69 observe: (@document, topic, addonId) ->
70 return unless addonId == @options.id
71 switch topic
72 when 'addon-options-displayed'
73 @init()
74 when 'addon-options-hidden'
75 @destroy()
76
77 init: ->
78 @container = @document.getElementById('detail-rows')
79 @injectSettings()
80
81 destroy: ->
82 @unlisten()
83
84 # VimFx specific observer.
85 class Observer extends BaseObserver
86 constructor: (@vimfx) ->
87 super({id: @vimfx.id})
88
89 injectSettings: ->
90 @injectInstructions()
91 @injectOptions()
92 @injectShortcuts()
93 @setupKeybindings()
94 @setupValidation()
95
96 if @vimfx.goToCommand
97 utils.nextTick(@document.ownerGlobal, =>
98 {pref} = @vimfx.goToCommand
99 setting = @container.querySelector("setting[pref='#{pref}']")
100 setting.scrollIntoView()
101 setting.input.select()
102 @vimfx.goToCommand = null
103 )
104
105 injectInstructions: ->
106 setting = @appendSetting({
107 type: 'control'
108 title: translate('prefs.instructions.title')
109 desc: translate('prefs.instructions.desc',
110 @vimfx.options['options.key.quote'],
111 @vimfx.options['options.key.insert_default'],
112 @vimfx.options['options.key.reset_default'],
113 '<c-z>')
114 'first-row': 'true'
115 })
116 href = "#{@vimfx.info.homepageURL}/tree/master/documentation"
117 docsLink = @document.createElement('label')
118 utils.setAttributes(docsLink, {
119 value: translate('prefs.documentation')
120 href
121 crop: 'end'
122 class: 'text-link'
123 })
124 setting.appendChild(docsLink)
125
126 injectOptions: ->
127 for key, value of defaults.options
128 setting = @appendSetting({
129 pref: "#{defaults.BRANCH}#{key}"
130 type: @type(value)
131 title: translate("pref.#{key}.title")
132 desc: translate("pref.#{key}.desc")
133 })
134 return
135
136 injectShortcuts: ->
137 for mode in @vimfx.getGroupedCommands()
138 @appendSetting({
139 type: 'control'
140 title: mode.name
141 'first-row': 'true'
142 })
143
144 for category in mode.categories
145 if category.name
146 @appendSetting({
147 type: 'control'
148 title: category.name
149 'first-row': 'true'
150 })
151
152 for {command} in category.commands
153 @appendSetting({
154 pref: command.pref
155 type: 'string'
156 title: command.description
157 desc: @generateErrorMessage(command.pref)
158 class: 'is-shortcut'
159 })
160
161 return
162
163 generateErrorMessage: (pref) ->
164 commandErrors = @vimfx.errors[pref] ? []
165 return commandErrors.map(({id, context, subject}) ->
166 return translate("error.#{id}", context ? subject, subject)
167 ).join('\n')
168
169 setupKeybindings: ->
170 # Note that `setting = event.originalTarget` does _not_ return the correct
171 # element in these listeners!
172 quote = false
173 @listen(@container, 'keydown', (event) =>
174 setting = event.target
175 isString = (setting.type == 'string')
176
177 {input, pref} = setting
178 keyString = @vimfx.stringifyKeyEvent(event)
179
180 # Some shortcuts only make sense for string settings. We still match
181 # those shortcuts and suppress the default behavior for _all_ types of
182 # settings for consistency. For example, pressing <c-d> in a number input
183 # (which looks like a text input) would otherwise bookmark the page, and
184 # <c-q> would close the window!
185 switch
186 when not keyString
187 return
188 when quote
189 break unless isString
190 utils.insertText(input, keyString)
191 quote = false
192 when keyString == @vimfx.options['options.key.quote']
193 break unless isString
194 quote = true
195 # Override `<force>` commands (such as `<escape>` and `<tab>`).
196 return unless vim = @vimfx.getCurrentVim(utils.getCurrentWindow())
197 @vimfx.modes.normal.commands.quote.run({vim, count: 1})
198 when keyString == @vimfx.options['options.key.insert_default']
199 break unless isString
200 utils.insertText(input, prefs.root.default.get(pref))
201 when keyString == @vimfx.options['options.key.reset_default']
202 prefs.root.set(pref, null)
203 else
204 return
205
206 event.preventDefault()
207 setting.valueToPreference()
208 @refreshShortcutErrors()
209 )
210 @listen(@container, 'blur', -> quote = false)
211
212 setupValidation: ->
213 @listen(@container, 'input', (event) =>
214 setting = event.target
215 # Disable default behavior of updating the pref of the setting on each
216 # input. Do it on the 'change' event instead (see below), because all
217 # settings are validated and auto-adjusted as soon as the pref changes.
218 event.stopPropagation()
219 if setting.classList.contains('is-shortcut')
220 # However, for the shortcuts we _do_ want live validation, because they
221 # cannot be auto-adjusted. Instead an error message is shown.
222 setting.valueToPreference()
223 @refreshShortcutErrors()
224 )
225
226 @listen(@container, 'change', (event) ->
227 setting = event.target
228 unless setting.classList.contains('is-shortcut')
229 setting.valueToPreference()
230 )
231
232 refreshShortcutErrors: ->
233 for setting in @container.getElementsByClassName('is-shortcut')
234 setting.setAttribute('desc', @generateErrorMessage(setting.pref))
235 return
236
237 module.exports = {
238 observe
239 }
Imprint / Impressum