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 { unloader } = require 'unloader'
23 { getPref } = require 'prefs'
25 { commands } = require 'commands'
28 Services.obs.addObserver(observer, 'addon-options-displayed', false)
29 Services.obs.addObserver(observer, 'addon-options-hidden', false)
32 observe: (document, topic, addon) ->
34 'setting[pref="extensions.VimFx.hint_chars"]':
36 'setting[pref="extensions.VimFx.black_list"]':
37 change: utils.updateBlacklist
38 'setting[pref="extensions.VimFx.prev_patterns"]':
39 change: validatePatterns
40 'setting[pref="extensions.VimFx.next_patterns"]':
41 change: validatePatterns
43 command: help.injectHelp.bind(undefined, document, commands)
46 when 'addon-options-displayed'
47 applySpec(document, spec, true)
48 when 'addon-options-hidden'
49 applySpec(document, spec, false)
51 filterChars = (event) ->
53 input.value = utils.removeDuplicateCharacters(input.value).replace(/\s/g, '')
54 input.valueToPreference()
56 validatePatterns = (event) ->
59 utils.removeDuplicates(utils.splitListString(input.value))
60 .filter((pattern) -> pattern != '')
62 input.valueToPreference()
64 applySpec = (document, spec, enable) ->
65 for selector, events of spec
66 element = document.querySelector(selector)
67 method = if enable then 'addEventListener' else 'removeEventListener'
68 for event, action of events
69 element[method](event, action, false)
72 Services.obs.removeObserver(observer, 'addon-options-displayed')
73 Services.obs.removeObserver(observer, 'addon-options-hidden')
76 exports.observe = observe