define(["require", "exports", "@syncfusion/ej2-base"], function (require, exports, ej2_base_1) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var MODULE_SUFFIX = 'Module';
function getValue(nameSpace, obj) {
var value = obj;
var splits = nameSpace.replace(/\[/g, '.').replace(/\]/g, '').split('.');
for (var j = 0; j < splits.length && !ej2_base_1.isUndefined(value); j++) {
value = value[splits[j]];
}
return value;
}
exports.getValue = getValue;
function setValue(nameSpace, value, obj) {
var keyValues = nameSpace.replace(/\[/g, '.').replace(/\]/g, '').split('.');
var start = obj || {};
var fromObj = start;
var j;
var length = keyValues.length;
var key;
for (j = 0; j < length; j++) {
key = keyValues[j];
Eif (j + 1 === length) {
fromObj["" + key] = value === undefined ? {} : value;
}
else if (ej2_base_1.isNullOrUndefined(fromObj["" + key])) {
fromObj["" + key] = {};
}
fromObj = fromObj["" + key];
}
return start;
}
exports.setValue = setValue;
var ModuleLoader = (function () {
function ModuleLoader(parent) {
this.loadedModules = [];
this.parent = parent;
}
ModuleLoader.prototype.inject = function (requiredModules, moduleList) {
var reqLengthVal = requiredModules.length;
Iif (reqLengthVal === 0) {
this.clean();
return;
}
Iif (this.loadedModules.length) {
this.clearUnusedModule(requiredModules);
}
for (var i = 0; i < reqLengthVal; i++) {
var modl = requiredModules[i];
for (var _i = 0, moduleList_1 = moduleList; _i < moduleList_1.length; _i++) {
var module = moduleList_1[_i];
var modName = modl.member;
Eif (module.prototype.getModuleName() === modl.member && !this.isModuleLoaded(modName)) {
var moduleObject = this.createInstance(module, modl.args);
var memberName = this.getMemberName(modName);
Iif (modl.isProperty) {
setValue(memberName, module, this.parent);
}
else {
setValue(memberName, moduleObject, this.parent);
}
var loadedModule = modl;
loadedModule.member = memberName;
this.loadedModules.push(loadedModule);
}
}
}
};
ModuleLoader.prototype.createInstance = function (classFunction, params) {
var arrayParam = params;
arrayParam.unshift(undefined);
return new (Function.prototype.bind.apply(classFunction, arrayParam));
};
ModuleLoader.prototype.clean = function () {
for (var _i = 0, _a = this.loadedModules; _i < _a.length; _i++) {
var modules = _a[_i];
if (!modules.isProperty) {
getValue(modules.member, this.parent).destroy();
}
}
this.loadedModules = [];
};
ModuleLoader.prototype.clearUnusedModule = function (moduleListName) {
var _this = this;
var usedModule = moduleListName.map(function (arg) { return _this.getMemberName(arg.member); });
var removeModule = this.loadedModules.filter(function (module) {
return usedModule.indexOf(module.member) === -1;
});
for (var _i = 0, removeModule_1 = removeModule; _i < removeModule_1.length; _i++) {
var moduleName = removeModule_1[_i];
if (!moduleName.isProperty) {
getValue(moduleName.member, this.parent).destroy();
}
this.loadedModules.splice(this.loadedModules.indexOf(moduleName), 1);
this.deleteObject(this.parent, moduleName.member);
}
};
ModuleLoader.prototype.getMemberName = function (name) {
return name[0].toLowerCase() + name.substring(1) + MODULE_SUFFIX;
};
ModuleLoader.prototype.deleteObject = function (obj, key) {
delete obj["" + key];
};
ModuleLoader.prototype.isModuleLoaded = function (modName) {
for (var _i = 0, _a = this.loadedModules; _i < _a.length; _i++) {
var mod = _a[_i];
if (mod.member === this.getMemberName(modName)) {
return true;
}
}
return false;
};
return ModuleLoader;
}());
exports.ModuleLoader = ModuleLoader;
});
|