]> git.gir.st - VimFx.git/blob - extension/lib/options.coffee
Merge branch 'blacklist-keys' of https://github.com/harsh1618/VimFx into harsh1618...
[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 { getPref } = require('./prefs')
23 help = require('./help')
24 { commands } = require('./commands')
25
26 observe = ->
27 Services.obs.addObserver(observer, 'addon-options-displayed', false)
28 Services.obs.addObserver(observer, 'addon-options-hidden', false)
29
30 observer =
31 observe: (document, topic, addon) ->
32 spec =
33 'setting[pref="extensions.VimFx.hint_chars"]':
34 change: filterChars
35 'setting[pref="extensions.VimFx.black_list"]':
36 change: utils.updateBlacklist
37 'setting[pref="extensions.VimFx.prev_patterns"]':
38 change: validatePatterns
39 'setting[pref="extensions.VimFx.next_patterns"]':
40 change: validatePatterns
41 '#customizeButton':
42 command: help.injectHelp.bind(undefined, document, commands)
43
44 switch topic
45 when 'addon-options-displayed'
46 applySpec(document, spec, true)
47 when 'addon-options-hidden'
48 applySpec(document, spec, false)
49
50 filterChars = (event) ->
51 input = event.target
52 input.value = utils.removeDuplicateCharacters(input.value).replace(/\s/g, '')
53 input.valueToPreference()
54
55 validatePatterns = (event) ->
56 input = event.target
57 input.value =
58 utils.removeDuplicates(utils.splitListString(input.value))
59 .filter((pattern) -> pattern != '')
60 .join(',')
61 input.valueToPreference()
62
63 applySpec = (document, spec, enable) ->
64 for selector, events of spec
65 element = document.querySelector(selector)
66 method = if enable then 'addEventListener' else 'removeEventListener'
67 for event, action of events
68 element[method](event, action, false)
69
70 module.onShutdown(->
71 Services.obs.removeObserver(observer, 'addon-options-displayed')
72 Services.obs.removeObserver(observer, 'addon-options-hidden')
73 )
74
75 exports.observe = observe
Imprint / Impressum