aboutsummaryrefslogtreecommitdiff
path: root/src/shared
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2017-11-25 22:14:28 +0900
committerShin'ya Ueoka <ueokande@i-beam.org>2017-11-26 11:45:05 +0900
commitd15de42a75ee0f722e8779af1ecff8b51e59c56d (patch)
treee5df392c8a1f1bf0007ab2095e8b8c2ca779f097 /src/shared
parentf27d21908a2b402e3274288d4411a48160f38e4c (diff)
settings helpers
Diffstat (limited to 'src/shared')
-rw-r--r--src/shared/settings/values.js95
1 files changed, 80 insertions, 15 deletions
diff --git a/src/shared/settings/values.js b/src/shared/settings/values.js
index d86cfdc..4482fbb 100644
--- a/src/shared/settings/values.js
+++ b/src/shared/settings/values.js
@@ -1,4 +1,6 @@
-const operationFromName = (name) => {
+import DefaultSettings from './default';
+
+const operationFromFormName = (name) => {
let [type, argStr] = name.split('?');
let args = {};
if (argStr) {
@@ -7,29 +9,92 @@ const operationFromName = (name) => {
return Object.assign({ type }, args);
};
-const fromJson = (json) => {
+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 fromForm = (form) => {
- let keymaps = {};
- for (let name of Object.keys(form.keymaps)) {
- let keys = form.keymaps[name];
- keymaps[keys] = operationFromName(name);
+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 engines = {};
- for (let { name, url } of form.search.engines) {
- engines[name] = url;
+ 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;
+ }
+ }
}
- let search = {
- default: form.search.default,
- engines,
- };
let blacklist = form.blacklist;
return { keymaps, search, blacklist };
};
-export { fromJson, fromForm };
+const jsonFromValue = (value) => {
+ return JSON.stringify(value, undefined, 2);
+};
+
+const formFromValue = (value) => {
+
+ let keymaps = undefined;
+ if (value.keymaps) {
+ let allowedOps = new Set(Object.keys(DefaultSettings.form.keymaps));
+
+ keymaps = {};
+ for (let keys of Object.keys(value.keymaps)) {
+ let op = operationToFormName(value.keymaps[keys]);
+ if (allowedOps.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 blacklist = value.blacklist;
+
+ return { keymaps, search, blacklist };
+};
+
+const jsonFromForm = (form) => {
+ return jsonFromValue(valueFromForm(form));
+};
+
+const formFromJson = (json) => {
+ let value = valueFromJson(json);
+ return formFromValue(value);
+};
+
+export {
+ valueFromJson, valueFromForm, jsonFromValue, formFromValue,
+ jsonFromForm, formFromJson
+};