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