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