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