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 { getPref } = require('./prefs')
23 help = require('./help')
26 Services.obs.addObserver(observer, 'addon-options-displayed', false)
27 Services.obs.addObserver(observer, 'addon-options-hidden', false)
30 observe: (document, topic, addon) ->
32 'setting[pref="extensions.VimFx.hint_chars"]':
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
41 command: help.injectHelp.bind(undefined, document, require('./modes'))
44 when 'addon-options-displayed'
45 applySpec(document, spec, true)
46 when 'addon-options-hidden'
47 applySpec(document, spec, false)
49 filterChars = (event) ->
51 input.value = utils.removeDuplicateCharacters(input.value).replace(/\s/g, '')
52 input.valueToPreference()
54 validatePatterns = (event) ->
57 utils.removeDuplicates(utils.splitListString(input.value))
58 .filter((pattern) -> pattern != '')
60 input.valueToPreference()
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)
70 Services.obs.removeObserver(observer, 'addon-options-displayed')
71 Services.obs.removeObserver(observer, 'addon-options-hidden')
74 exports.observe = observe