]> git.gir.st - VimFx.git/blob - extension/lib/options.coffee
Merge branch 'prefs' into develop
[VimFx.git] / extension / lib / options.coffee
1 ###
2 # Copyright Simon Lydell 2013, 2014.
3 # Copyright Wang Zhuochun 2013.
4 #
5 # This file is part of VimFx.
6 #
7 # VimFx is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # VimFx is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
19 ###
20
21 utils = require('./utils')
22 defaults = require('./defaults')
23 help = require('./help')
24 _ = require('./l10n')
25
26 observe = ->
27 observer = new Observer(defaults, validators)
28 observer.hooks.injectSettings = setupCustomizeButton
29
30 Services.obs.addObserver(observer, 'addon-options-displayed', false)
31 Services.obs.addObserver(observer, 'addon-options-hidden', false)
32
33 module.onShutdown(->
34 Services.obs.removeObserver(observer, 'addon-options-displayed')
35 Services.obs.removeObserver(observer, 'addon-options-hidden')
36 )
37
38 class Observer
39 constructor: (@defaults, @validators = {}) ->
40 @document = null
41 @container = null
42 @listeners = []
43 @hooks = {}
44
45 useCapture: false
46
47 listen: (element, event, action) ->
48 element.addEventListener(event, action, @useCapture)
49 @listeners.push([element, event, action, @useCapture])
50
51 unlisten: ->
52 for [element, event, action, useCapture] in @listeners
53 element.removeEventListener(event, action, useCapture)
54 @listeners.length = 0
55
56 typeMap:
57 string: 'string'
58 number: 'integer'
59 boolean: 'bool'
60
61 injectSettings: ->
62 @container = @document.getElementById('detail-rows')
63
64 for key, value of @defaults.options
65 desc = _("pref_#{ key }_desc")
66 if typeof value == 'string' and value != ''
67 desc += "\n#{ _('prefs_default', value) }"
68 setting = utils.createElement(@document, 'setting', {
69 pref: "#{ @defaults.BRANCH }#{ key }"
70 type: @typeMap[typeof value]
71 title: _("pref_#{ key }_title")
72 desc: desc.trim()
73 })
74 @listen(setting, 'change', @validators[key]) if key of @validators
75 @container.appendChild(setting)
76
77 @hooks.injectSettings?.call(this)
78
79 @container.querySelector('setting').setAttribute('first-row', 'true')
80
81 observe: (@document, topic) ->
82 switch topic
83 when 'addon-options-displayed'
84 @injectSettings()
85 when 'addon-options-hidden'
86 @unlisten()
87
88 setupCustomizeButton = ->
89 shortcuts = utils.createElement(@document, 'setting', {
90 type: 'control',
91 title: _('prefs_customize_shortcuts_title')
92 })
93 button = utils.createElement(@document, 'button', {
94 label: _('prefs_customize_shortcuts_label')
95 })
96 shortcuts.appendChild(button)
97 @listen(button, 'command',
98 help.injectHelp.bind(undefined, @document, require('./modes')))
99 @container.appendChild(shortcuts)
100
101 filterChars = (event) ->
102 input = event.target
103 input.value = utils.removeDuplicateCharacters(input.value).replace(/\s/g, '')
104 input.valueToPreference()
105
106 validatePatterns = (event) ->
107 input = event.target
108 input.value =
109 utils.removeDuplicates(utils.splitListString(input.value))
110 .filter((pattern) -> pattern != '')
111 .join(',')
112 input.valueToPreference()
113
114 validators =
115 'hint_chars': filterChars
116 'black_list': utils.updateBlacklist
117 'prev_patterns': validatePatterns
118 'next_patterns': validatePatterns
119
120 exports.observe = observe
Imprint / Impressum