Merge pull request #2134 from bugsounet/patch-1

New ConfigMerge code purpose
This commit is contained in:
Michael Teeuw 2020-09-22 10:54:35 +02:00 committed by GitHub
commit 57b6ea1297
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 3 deletions

View File

@ -98,6 +98,7 @@ var Loader = (function () {
file: moduleName + ".js", file: moduleName + ".js",
position: moduleData.position, position: moduleData.position,
header: moduleData.header, header: moduleData.header,
configDeepMerge: typeof moduleData.configDeepMerge === "boolean" ? moduleData.configDeepMerge : false,
config: moduleData.config, config: moduleData.config,
classes: typeof moduleData.classes !== "undefined" ? moduleData.classes + " " + module : module classes: typeof moduleData.classes !== "undefined" ? moduleData.classes + " " + module : module
}); });

View File

@ -221,16 +221,17 @@ var Module = Class.extend({
this.identifier = data.identifier; this.identifier = data.identifier;
this.hidden = false; this.hidden = false;
this.setConfig(data.config); this.setConfig(data.config, data.configDeepMerge);
}, },
/** /**
* Set the module config and combine it with the module defaults. * Set the module config and combine it with the module defaults.
* *
* @param {object} config The combined module config. * @param {object} config The combined module config.
* @param {boolean} config Merge module config in deep.
*/ */
setConfig: function (config) { setConfig: function (config, deep) {
this.config = Object.assign({}, this.defaults, config); 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.definitions = {};
Module.create = function (name) { Module.create = function (name) {