diff --git a/js/loader.js b/js/loader.js index 4ba75578..91a17329 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 }); diff --git a/js/module.js b/js/module.js index 8bb5990e..40f9b311 100644 --- a/js/module.js +++ b/js/module.js @@ -221,16 +221,17 @@ 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); + setConfig: function (config, deep) { + this.config = deep ? configMerge({}, this.defaults, 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) {