]> git.gir.st - VimFx.git/blob - extension/lib/prefs-bulk.coffee
Fix z-index for hint markers
[VimFx.git] / extension / lib / prefs-bulk.coffee
1 ###
2 # Copyright Simon Lydell 2016.
3 #
4 # This file is part of VimFx.
5 #
6 # VimFx is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # VimFx is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
18 ###
19
20 # This files deals with handling all, or many, of VimFx’s prefs in bulk.
21
22 defaults = require('./defaults')
23 prefs = require('./prefs')
24
25 resetAll = ->
26 prefs.set(pref, null) for pref of defaults.all_prefs
27 return
28
29 exportAll = ->
30 regular = (pref for pref of defaults.all_prefs)
31 custom = Services.prefs.getBranch(defaults.BRANCH).getChildList('custom.')
32 exported = {}
33 for pref in regular.concat(custom) when prefs.has(pref)
34 exported[pref] = prefs.get(pref)
35 return exported
36
37 importExported = (exportedString) ->
38 exported = null
39 try
40 exported = JSON.parse(exportedString)
41 catch error
42 return {
43 numSuccesses: -1
44 errors: [error.message]
45 }
46
47 unless Object::toString.call(exported) == '[object Object]' and
48 Object.keys(exported).length > 0
49 return {
50 numSuccesses: -1
51 errors: ["The input must be a non-empty object. Got: #{exportedString}"]
52 }
53
54 numSuccesses = 0
55 errors = []
56
57 for pref, value of exported
58 unless Object::hasOwnProperty.call(defaults.all_prefs, pref) or
59 pref.startsWith('custom.')
60 errors.push("#{pref}: Unknown pref.")
61 continue
62
63 try
64 # `prefs.set` handles validation of `value`.
65 prefs.set(pref, value)
66 catch error
67 errors.push("#{pref}: #{error.message.replace(/^VimFx: /, '')}")
68 continue
69
70 numSuccesses += 1
71
72 return {numSuccesses, errors}
73
74 createImportErrorReport = ({numSuccesses, errors}) ->
75 header =
76 if numSuccesses <= 0
77 'The stuff you entered is invalid:'
78 else
79 s1 = if numSuccesses == 1 then '' else 's'
80 s2 = if errors.length == 1 then '' else 's'
81 """
82 #{numSuccesses} option#{s1} imported successfully.
83
84 #{errors.length} error#{s2} also occurred:
85 """
86
87 return """
88 #{header}
89
90 #{errors.join('\n\n')}
91 """
92
93 module.exports = {
94 resetAll
95 exportAll
96 importExported
97 createImportErrorReport
98 }
Imprint / Impressum