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) {