]> git.gir.st - VimFx.git/blob - extension/lib/options.coffee
Merge branch 'master' into develop
[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 # Note that `setting = event.originalTarget` does _not_ return the correct
161 # element in these listeners!
162 quote = false
163 @listen(@container, 'keydown', (event) =>
164 setting = event.target
165 isString = (setting.type == 'string')
166
167 {input, pref} = setting
168 keyString = @vimfx.stringifyKeyEvent(event)
169
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!
175 switch
176 when not keyString
177 return
178 when quote
179 break unless isString
180 utils.insertText(input, keyString)
181 quote = false
182 when keyString == @vimfx.options['options.key.quote']
183 break unless isString
184 quote = true
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)
193 else
194 return
195
196 event.preventDefault()
197 setting.valueToPreference()
198 @refreshShortcutErrors()
199 )
200 @listen(@container, 'blur', -> quote = false)
201
202 setupValidation: ->
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()
214 )
215
216 @listen(@container, 'change', (event) ->
217 setting = event.target
218 unless setting.classList.contains('is-shortcut')
219 setting.valueToPreference()
220 )
221
222 refreshShortcutErrors: ->
223 for setting in @container.getElementsByClassName('is-shortcut')
224 setting.setAttribute('desc', @generateErrorMessage(setting.pref))
225 return
226
227 module.exports = {
228 observe
229 }
Imprint / Impressum