]> git.gir.st - VimFx.git/blob - extension/packages/prefs.coffee
closes #41. All help commands can be disabled now.
[VimFx.git] / extension / packages / prefs.coffee
1 { classes: Cc, interfaces: Ci } = Components
2
3 PREF_BRANCH = "extensions.VimFx.";
4
5 # Default values for the preference
6 # All used preferences should be mentioned here becuase
7 # preference type is derived from here
8 DEFAULT_PREF_VALUES =
9 addon_id: 'VimFx@akhodakivskiy.github.com'
10 hint_chars: 'asdfgercvhjkl;uinm'
11 disabled: false
12 scroll_step: 60
13 scroll_time: 100
14 black_list: '*mail.google.com*'
15 blur_on_esc: true
16
17 getPref = do ->
18 branch = Services.prefs.getBranch PREF_BRANCH
19
20 return (key, defaultValue=undefined) ->
21 type = branch.getPrefType(key)
22
23 switch type
24 when branch.PREF_BOOL
25 return branch.getBoolPref key
26 when branch.PREF_INT
27 return branch.getIntPref key
28 when branch.PREF_STRING
29 return branch.getCharPref key
30 else
31 if defaultValue != undefined
32 return defaultValue
33 else
34 return DEFAULT_PREF_VALUES[key];
35
36 # Assign and save Firefox preference value
37 setPref = do ->
38 branch = Services.prefs.getBranch PREF_BRANCH
39
40 return (key, value) ->
41 switch typeof value
42 when 'boolean'
43 branch.setBoolPref(key, value)
44 when 'number'
45 branch.setIntPref(key, value)
46 when 'string'
47 branch.setCharPref(key, value);
48 else
49 branch.clearUserPref(key);
50
51
52 DISABLED_COMMANDS = do ->
53 str = getPref 'disabled_commands'
54 try
55 return JSON.parse str
56 catch err
57 dc = []
58 for key in str.split('||')
59 for c in key.split('|')
60 dc.push c if c
61
62 return dc
63
64 # Enables command
65 enableCommand = (key) ->
66 for c in key.split('|')
67 while (idx = DISABLED_COMMANDS.indexOf(c)) > -1
68 DISABLED_COMMANDS.splice(idx, 1)
69
70 setPref 'disabled_commands', JSON.stringify DISABLED_COMMANDS
71
72 # Adds command to the disabled list
73 disableCommand = (key) ->
74 for c in key.split('|')
75 if DISABLED_COMMANDS.indexOf(c) == -1
76 DISABLED_COMMANDS.push c
77
78 setPref 'disabled_commands', JSON.stringify DISABLED_COMMANDS
79
80 # Checks if given command is disabled in the preferences
81 isCommandDisabled = (key) ->
82 for c in key.split('|')
83 if DISABLED_COMMANDS.indexOf(c) > -1
84 return true
85
86 return false
87
88 exports.getPref = getPref
89 exports.setPref = setPref
90 exports.isCommandDisabled = isCommandDisabled
91 exports.disableCommand = disableCommand
92 exports.enableCommand = enableCommand
Imprint / Impressum