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')
24 { commands } = require('./commands')
27 Services.obs.addObserver(observer, 'addon-options-displayed', false)
28 Services.obs.addObserver(observer, 'addon-options-hidden', false)
31 observe: (document, topic, addon) ->
33 'setting[pref="extensions.VimFx.hint_chars"]':
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
42 command: help.injectHelp.bind(undefined, document, commands)
45 when 'addon-options-displayed'
46 applySpec(document, spec, true)
47 when 'addon-options-hidden'
48 applySpec(document, spec, false)
50 filterChars = (event) ->
52 input.value = utils.removeDuplicateCharacters(input.value).replace(/\s/g, '')
53 input.valueToPreference()
55 validatePatterns = (event) ->
58 utils.removeDuplicates(utils.splitListString(input.value))
59 .filter((pattern) -> pattern != '')
61 input.valueToPreference()
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)
71 Services.obs.removeObserver(observer, 'addon-options-displayed')
72 Services.obs.removeObserver(observer, 'addon-options-hidden')
75 exports.observe = observe