new functions getSetting and setSetting
authorLaurent Mazet <mazet@softndesign.org>
Wed, 9 Sep 2020 05:32:48 +0000 (07:32 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Wed, 9 Sep 2020 05:32:48 +0000 (07:32 +0200)
public/trivabble.js

index 24bf94761cf895f0de7e421825dce7cad3c326b7..f471e06904501fa43a9cb569d2ff169a89c61ad1 100644 (file)
     setConf("FLASH_LIGHT_DURATIONS", [800, 1600, 3200]);
     setConf("FLASH_LIGHT_COLOR", "#E3E");
 
+    function getSetting(key) {
+        let type = undefined;
+        let value;
+
+        /* get default value from configuration */
+        if (Object.prototype.hasOwnProperty.call(Conf, key)) {
+            value = Conf[key];
+            type = typeof value;
+        }
+
+        /* try to retrieve value from localstorage */
+        if (Object.prototype.hasOwnProperty.call(localStorage, key)) {
+            value = localStorage.getItem(key);
+
+            /* get type from localStore if no default is set */
+            if (type === "undefined") {
+                type = localStorage.getItem(key + "_type");
+            }
+
+            /* cast from string to type */
+            if (type === "bigint") {
+                value = BigInt(value);
+            } else if (type === "boolean") {
+                value = (value) ? true : false;
+            } else if (type === "number") {
+                value = Number(value);
+            } else if (type === "object") {
+                value = JSON.parse(value);
+            } else if (type === "string") {
+                value = String(value);
+            } else {
+                console.error("Unsupported type");
+            }
+        }
+
+        return value;
+    }
+
+    function setSetting(key, value) {
+        let type = undefined;
+
+        /* try to retrieve type from configuration */
+        if (Object.prototype.hasOwnProperty.call(Conf, key)) {
+            type = typeof Conf[key];
+        }
+
+        /* try to retrieve type from localstorage */
+        if (type === undefined) {
+            if (Object.prototype.hasOwnProperty.call(localStorage, key)) {
+                type = localStorage.getItem(key + "_type");
+            }
+        }
+
+        /* if not set type is defined from value */
+        if (type === undefined) {
+            typeof value;
+        }
+
+        /* storage value in localstorage */
+        if (type !== typeof value) {
+            console.error("incoherent type");
+        } else {
+            if (type === "object") {
+                value = JSON.stringify(value);
+            }
+            if ((type === "bigint") ||
+                (type === "boolean") ||
+                (type === "number") ||
+                (type === "object") ||
+                (type === "string")) {
+                localStore.setItem(key, value);
+
+                /* store type into localstorage if no default value in configuration */
+                if (Object.prototype.hasOwnProperty.call(Conf, key)) {
+                    localStore.setItem(key + "_type", type);
+                }
+            } else {
+                console.error("Unsupported type");
+            }
+        }
+    }
+
     const _ = (window.libD && libD.l10n) ? libD.l10n() : function (s) {
         return s;
     };