From 340a8e4176c301a9dc352c8429b5f3a86f2040dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric?= Date: Mon, 21 Sep 2020 22:36:54 +0200 Subject: [PATCH 1/5] (test react for in my repo) --- js/loader.js | 1 + 1 file changed, 1 insertion(+) diff --git a/js/loader.js b/js/loader.js index 4ba75578..5ea155fa 100644 --- a/js/loader.js +++ b/js/loader.js @@ -98,6 +98,7 @@ var Loader = (function () { file: moduleName + ".js", position: moduleData.position, header: moduleData.header, + configDeepMerge: (typeof moduleData.configDeepMerge === "boolean") ? moduleData.configDeepMerge : false, config: moduleData.config, classes: typeof moduleData.classes !== "undefined" ? moduleData.classes + " " + module : module }); From 1f473228ce8fb7f8312f297b09464c35b617d6a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric?= Date: Mon, 21 Sep 2020 23:17:41 +0200 Subject: [PATCH 2/5] New purpose for configMerge() --- js/module.js | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/js/module.js b/js/module.js index 8bb5990e..921c41f4 100644 --- a/js/module.js +++ b/js/module.js @@ -221,13 +221,14 @@ var Module = Class.extend({ this.identifier = data.identifier; this.hidden = false; - this.setConfig(data.config); + this.setConfig(data.config,data.configDeepMerge); }, /** * Set the module config and combine it with the module defaults. * * @param {object} config The combined module config. + * @param {boolean} config Merge module config in deep. */ setConfig: function (config) { this.config = Object.assign({}, this.defaults, config); @@ -440,6 +441,48 @@ var Module = Class.extend({ } }); +/** Merging MagicMirror (or other) default/config script + * merge 2 objects or/with array + * using: + * ------- + * this.config = configMerge({}, this.defaults, this.config) + * ------- + * arg1: initial objet + * arg2: config model + * arg3: config to merge + * ------- + * why using it ? + * Object.assign() function don't to all job + * it don't merge all thing in deep + * -> object in object and array is not merging + * ------- + * @bugsounet + * @Todo: idea of Mich determinate what do you want to merge or not + */ + +function configMerge(result) { + var stack = Array.prototype.slice.call(arguments, 1); + var item; + var key; + while (stack.length) { + item = stack.shift(); + for (key in item) { + if (item.hasOwnProperty(key)) { + if (typeof result[key] === "object" && result[key] && Object.prototype.toString.call(result[key]) !== "[object Array]") { + if (typeof item[key] === "object" && item[key] !== null) { + result[key] = configMerge({}, result[key], item[key]); + } else { + result[key] = item[key]; + } + } else { + result[key] = item[key]; + } + } + } + } + return result; +}; + Module.definitions = {}; Module.create = function (name) { From 97c2bab58a7d5f49bc4eda1595bbd4b525b01771 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric?= Date: Mon, 21 Sep 2020 23:21:43 +0200 Subject: [PATCH 3/5] apply ConfigMerge() if configDeepMerge: true --- js/module.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/js/module.js b/js/module.js index 921c41f4..81a83343 100644 --- a/js/module.js +++ b/js/module.js @@ -230,8 +230,8 @@ var Module = Class.extend({ * @param {object} config The combined module config. * @param {boolean} config Merge module config in deep. */ - setConfig: function (config) { - this.config = Object.assign({}, this.defaults, config); + setConfig: function (config, deep) { + this.config= deep ? configMerge({}, this.defaults, config) : Object.assign({}, this.defaults, config); }, /** From 19148cfc84bfbbe6c04256eb33b674a38f52a3bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric?= Date: Tue, 22 Sep 2020 00:04:05 +0200 Subject: [PATCH 4/5] Update loader.js --- js/loader.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/loader.js b/js/loader.js index 5ea155fa..91a17329 100644 --- a/js/loader.js +++ b/js/loader.js @@ -98,7 +98,7 @@ var Loader = (function () { file: moduleName + ".js", position: moduleData.position, header: moduleData.header, - configDeepMerge: (typeof moduleData.configDeepMerge === "boolean") ? moduleData.configDeepMerge : false, + configDeepMerge: typeof moduleData.configDeepMerge === "boolean" ? moduleData.configDeepMerge : false, config: moduleData.config, classes: typeof moduleData.classes !== "undefined" ? moduleData.classes + " " + module : module }); From 102ff15a99ee0fbe18552e2a73903be374e88351 Mon Sep 17 00:00:00 2001 From: bugsounet Date: Tue, 22 Sep 2020 00:26:24 +0200 Subject: [PATCH 5/5] Checking formatting... All matched files use Prettier code style! --- js/module.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/js/module.js b/js/module.js index 81a83343..40f9b311 100644 --- a/js/module.js +++ b/js/module.js @@ -221,7 +221,7 @@ var Module = Class.extend({ this.identifier = data.identifier; this.hidden = false; - this.setConfig(data.config,data.configDeepMerge); + this.setConfig(data.config, data.configDeepMerge); }, /** @@ -231,7 +231,7 @@ var Module = Class.extend({ * @param {boolean} config Merge module config in deep. */ setConfig: function (config, deep) { - this.config= deep ? configMerge({}, this.defaults, config) : Object.assign({}, this.defaults, config); + this.config = deep ? configMerge({}, this.defaults, config) : Object.assign({}, this.defaults, config); }, /** @@ -481,7 +481,7 @@ function configMerge(result) { } } return result; -}; +} Module.definitions = {};