diff options
author | siikamiika <siikamiika@users.noreply.github.com> | 2019-10-27 20:11:23 +0200 |
---|---|---|
committer | siikamiika <siikamiika@users.noreply.github.com> | 2019-10-27 20:11:23 +0200 |
commit | 70418202cf2739258a4e95fdc7f0fbe4c7c46821 (patch) | |
tree | 0e40711ded4536e5756f1bc8a85fdaa3af616d57 /ext/bg/js/api.js | |
parent | 48776145d6bdb8aff82e82546583c790353e75b6 (diff) |
make search page checkbox options persist
Diffstat (limited to 'ext/bg/js/api.js')
-rw-r--r-- | ext/bg/js/api.js | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 88eef431..6c109614 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -21,6 +21,55 @@ function apiOptionsGet(optionsContext) { return utilBackend().getOptions(optionsContext); } +async function apiOptionsSet(changedOptions, optionsContext, source) { + const backend = utilBackend(); + const {depth} = optionsContext; + let options = await apiOptionsGetFull(); + + function getValuePaths(obj) { + let valuePaths = []; + let nodes = [{ + obj: changedOptions, + path: [] + }]; + while (nodes.length > 0) { + let node = nodes.pop(); + Object.keys(node.obj).forEach((key) => { + let path = node.path.concat(key); + let value = node.obj[key]; + if (typeof value === 'object') { + nodes.unshift({ + obj: value, + path: path + }); + } else { + valuePaths.push([value, path]); + } + }); + } + return valuePaths; + } + + function modifyOption(path, value, options) { + let pivot = options; + for (let pathKey of path.slice(0, -1)) { + if (!(pathKey in pivot)) { + return false; + } + pivot = pivot[pathKey]; + } + pivot[path[path.length - 1]] = value; + return true; + } + + for (let [value, path] of getValuePaths(changedOptions)) { + modifyOption(path, value, options.profiles[depth].options); + } + + await optionsSave(options); + backend.onOptionsUpdated(source); +} + function apiOptionsGetFull() { return utilBackend().getFullOptions(); } |