2 # Copyright Simon Lydell 2013, 2014.
3 # Copyright Wang Zhuochun 2013.
5 # This file is part of VimFx.
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.
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.
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/>.
21 utils = require('./utils')
22 defaults = require('./defaults')
23 help = require('./help')
27 observer = new Observer(defaults, validators)
28 observer.hooks.injectSettings = setupCustomizeButton
30 Services.obs.addObserver(observer, 'addon-options-displayed', false)
31 Services.obs.addObserver(observer, 'addon-options-hidden', false)
34 Services.obs.removeObserver(observer, 'addon-options-displayed')
35 Services.obs.removeObserver(observer, 'addon-options-hidden')
39 constructor: (@defaults, @validators = {}) ->
47 listen: (element, event, action) ->
48 element.addEventListener(event, action, @useCapture)
49 @listeners.push([element, event, action, @useCapture])
52 for [element, event, action, useCapture] in @listeners
53 element.removeEventListener(event, action, useCapture)
62 @container = @document.getElementById('detail-rows')
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")
74 @listen(setting, 'change', @validators[key]) if key of @validators
75 @container.appendChild(setting)
77 @hooks.injectSettings?.call(this)
79 @container.querySelector('setting').setAttribute('first-row', 'true')
81 observe: (@document, topic) ->
83 when 'addon-options-displayed'
85 when 'addon-options-hidden'
88 setupCustomizeButton = ->
89 shortcuts = utils.createElement(@document, 'setting', {
91 title: _('prefs_customize_shortcuts_title')
93 button = utils.createElement(@document, 'button', {
94 label: _('prefs_customize_shortcuts_label')
96 shortcuts.appendChild(button)
97 @listen(button, 'command',
98 help.injectHelp.bind(undefined, @document, require('./modes')))
99 @container.appendChild(shortcuts)
101 filterChars = (event) ->
103 input.value = utils.removeDuplicateCharacters(input.value).replace(/\s/g, '')
104 input.valueToPreference()
106 validatePatterns = (event) ->
109 utils.removeDuplicates(utils.splitListString(input.value))
110 .filter((pattern) -> pattern != '')
112 input.valueToPreference()
115 'hint_chars': filterChars
116 'black_list': utils.updateBlacklist
117 'prev_patterns': validatePatterns
118 'next_patterns': validatePatterns
120 exports.observe = observe