aboutsummaryrefslogtreecommitdiff
path: root/src/shared/settings/values.js
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2018-05-01 13:51:07 +0900
committerShin'ya Ueoka <ueokande@i-beam.org>2018-05-01 13:51:07 +0900
commit4d7c24f38a6861e1d498b6e7dd5c7be2d1a0ad15 (patch)
tree0948f730a25eeda127c56a0f01009f14d066e4dc /src/shared/settings/values.js
parent177940981ed9c4f096ad7db20f0b7ee044fd7b17 (diff)
parentfb8a0f36aa4d070df936cc7598ef8dd988ee1b15 (diff)
Merge remote-tracking branch 'origin/master' into background-adjacent-tabs
Diffstat (limited to 'src/shared/settings/values.js')
-rw-r--r--src/shared/settings/values.js108
1 files changed, 108 insertions, 0 deletions
diff --git a/src/shared/settings/values.js b/src/shared/settings/values.js
new file mode 100644
index 0000000..bd03be2
--- /dev/null
+++ b/src/shared/settings/values.js
@@ -0,0 +1,108 @@
+import * as properties from './properties';
+
+const operationFromFormName = (name) => {
+ let [type, argStr] = name.split('?');
+ let args = {};
+ if (argStr) {
+ args = JSON.parse(argStr);
+ }
+ return Object.assign({ type }, args);
+};
+
+const operationToFormName = (op) => {
+ let type = op.type;
+ let args = Object.assign({}, op);
+ delete args.type;
+
+ if (Object.keys(args).length === 0) {
+ return type;
+ }
+ return op.type + '?' + JSON.stringify(args);
+};
+
+const valueFromJson = (json) => {
+ return JSON.parse(json);
+};
+
+const valueFromForm = (form) => {
+ let keymaps = undefined;
+ if (form.keymaps) {
+ keymaps = {};
+ for (let name of Object.keys(form.keymaps)) {
+ let keys = form.keymaps[name];
+ keymaps[keys] = operationFromFormName(name);
+ }
+ }
+
+ let search = undefined;
+ if (form.search) {
+ search = { default: form.search.default };
+
+ if (form.search.engines) {
+ search.engines = {};
+ for (let [name, url] of form.search.engines) {
+ search.engines[name] = url;
+ }
+ }
+ }
+
+ return {
+ keymaps,
+ search,
+ blacklist: form.blacklist,
+ properties: form.properties
+ };
+};
+
+const jsonFromValue = (value) => {
+ return JSON.stringify(value, undefined, 2);
+};
+
+const formFromValue = (value, allowedOps) => {
+ let keymaps = undefined;
+
+ if (value.keymaps) {
+ let allowedSet = new Set(allowedOps);
+
+ keymaps = {};
+ for (let keys of Object.keys(value.keymaps)) {
+ let op = operationToFormName(value.keymaps[keys]);
+ if (allowedSet.has(op)) {
+ keymaps[op] = keys;
+ }
+ }
+ }
+
+ let search = undefined;
+ if (value.search) {
+ search = { default: value.search.default };
+ if (value.search.engines) {
+ search.engines = Object.keys(value.search.engines).map((name) => {
+ return [name, value.search.engines[name]];
+ });
+ }
+ }
+
+ let formProperties = Object.assign({}, properties.defaults, value.properties);
+
+ return {
+ keymaps,
+ search,
+ blacklist: value.blacklist,
+ properties: formProperties,
+ };
+};
+
+const jsonFromForm = (form) => {
+ return jsonFromValue(valueFromForm(form));
+};
+
+const formFromJson = (json, allowedOps) => {
+ let value = valueFromJson(json);
+ return formFromValue(value, allowedOps);
+};
+
+export {
+ valueFromJson, valueFromForm, jsonFromValue, formFromValue,
+ jsonFromForm, formFromJson
+};