all files / calculate/common/ module-loader.js

63.53% Statements 54/85
34.62% Branches 9/26
57.14% Functions 8/14
64.29% Lines 54/84
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117                 1275× 1275× 1275× 1275× 1275× 1275× 1275× 1275× 1275× 1275×         1275×   1275×   1275× 1275×   1275× 1275×       1275×     1275× 1275× 1275× 1275× 1275× 1275× 1275× 1275× 1275×       1275×   1275× 1275× 1275×         1275× 1275× 1275×                                               1275×       1275×           1275×        
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;
});