2018-02-10 15:18:01 +00:00
|
|
|
import ngModule from '../module';
|
2019-01-26 01:30:46 +00:00
|
|
|
import moduleImport from 'module-import';
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2018-08-30 09:02:50 +00:00
|
|
|
factory.$inject = ['$http', '$window', '$ocLazyLoad', '$translatePartialLoader', '$translate', '$q'];
|
|
|
|
export function factory($http, $window, $ocLazyLoad, $translatePartialLoader, $translate, $q) {
|
2017-01-31 13:13:06 +00:00
|
|
|
class ModuleLoader {
|
|
|
|
constructor() {
|
2018-02-04 18:39:56 +00:00
|
|
|
this._loaded = {};
|
2017-01-31 13:13:06 +00:00
|
|
|
}
|
2017-07-03 06:38:45 +00:00
|
|
|
load(moduleName, validations) {
|
2019-01-26 01:30:46 +00:00
|
|
|
let moduleConf = $window.routes.find(i => i && i.module == moduleName);
|
|
|
|
if (!moduleConf)
|
|
|
|
return $q.reject(new Error(`Module not found: ${moduleName}`));
|
|
|
|
|
2018-02-04 18:39:56 +00:00
|
|
|
let loaded = this._loaded;
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2018-02-10 10:41:29 +00:00
|
|
|
if (loaded[moduleName] === true)
|
|
|
|
return Promise.resolve(true);
|
|
|
|
if (loaded[moduleName] instanceof Promise)
|
2018-02-04 18:39:56 +00:00
|
|
|
return loaded[moduleName];
|
2018-02-10 10:41:29 +00:00
|
|
|
if (loaded[moduleName] === false)
|
2019-02-12 08:03:56 +00:00
|
|
|
return Promise.resolve(true);
|
2018-02-04 18:39:56 +00:00
|
|
|
|
2018-02-10 10:41:29 +00:00
|
|
|
loaded[moduleName] = false;
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2018-02-04 18:39:56 +00:00
|
|
|
let depPromises = [];
|
2019-01-26 01:30:46 +00:00
|
|
|
let deps = moduleConf.dependencies;
|
2018-02-04 18:39:56 +00:00
|
|
|
|
2019-01-23 12:11:44 +00:00
|
|
|
if (deps) {
|
2018-02-04 18:39:56 +00:00
|
|
|
for (let dep of deps)
|
|
|
|
depPromises.push(this.load(dep, validations));
|
2019-01-23 12:11:44 +00:00
|
|
|
}
|
2018-02-04 18:39:56 +00:00
|
|
|
|
|
|
|
loaded[moduleName] = new Promise((resolve, reject) => {
|
2019-01-23 12:11:44 +00:00
|
|
|
Promise.all(depPromises).then(() => {
|
2018-02-04 18:39:56 +00:00
|
|
|
let promises = [];
|
|
|
|
|
|
|
|
$translatePartialLoader.addPart(moduleName);
|
2018-02-10 10:41:29 +00:00
|
|
|
promises.push(new Promise(resolve => {
|
2019-02-12 08:03:56 +00:00
|
|
|
$translate.refresh().then(resolve, resolve);
|
2018-02-10 10:41:29 +00:00
|
|
|
}));
|
2018-02-04 18:39:56 +00:00
|
|
|
|
2019-01-23 12:11:44 +00:00
|
|
|
if (validations) {
|
2018-02-04 18:39:56 +00:00
|
|
|
promises.push(new Promise(resolve => {
|
2019-01-25 16:05:53 +00:00
|
|
|
$http.get(`/${moduleName}/api/modelInfo`).then(
|
2018-02-04 18:39:56 +00:00
|
|
|
json => this.onValidationsReady(json, resolve),
|
2018-02-10 10:41:29 +00:00
|
|
|
() => resolve()
|
2018-02-04 18:39:56 +00:00
|
|
|
);
|
|
|
|
}));
|
2019-01-23 12:11:44 +00:00
|
|
|
}
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2019-01-26 01:30:46 +00:00
|
|
|
promises.push(moduleImport(moduleName));
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2019-01-23 12:11:44 +00:00
|
|
|
Promise.all(promises).then(() => {
|
2018-02-10 10:41:29 +00:00
|
|
|
loaded[moduleName] = true;
|
2018-02-04 18:39:56 +00:00
|
|
|
resolve($ocLazyLoad.load({name: moduleName}));
|
2019-01-23 12:11:44 +00:00
|
|
|
}).catch(reject);
|
|
|
|
}).catch(reject);
|
2017-09-21 06:48:25 +00:00
|
|
|
});
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2018-02-04 18:39:56 +00:00
|
|
|
return loaded[moduleName];
|
2017-02-01 17:55:02 +00:00
|
|
|
}
|
|
|
|
onValidationsReady(json, resolve) {
|
|
|
|
let entities = json.data;
|
2017-06-06 11:06:47 +00:00
|
|
|
for (let entity in entities) {
|
2017-02-01 17:55:02 +00:00
|
|
|
let fields = entities[entity].validations;
|
2017-06-06 11:06:47 +00:00
|
|
|
for (let field in fields) {
|
2017-02-01 17:55:02 +00:00
|
|
|
let validations = fields[field];
|
2017-06-06 11:06:47 +00:00
|
|
|
for (let validation of validations)
|
2017-02-01 17:55:02 +00:00
|
|
|
this.parseValidation(validation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.assign($window.validations, json.data);
|
|
|
|
resolve();
|
|
|
|
}
|
2018-02-04 18:39:56 +00:00
|
|
|
parseValidation(val) {
|
|
|
|
switch (val.validation) {
|
|
|
|
case 'custom':
|
|
|
|
// TODO: Replace eval
|
|
|
|
val.bindedFunction = eval(`(${val.bindedFunction})`);
|
|
|
|
break;
|
|
|
|
case 'format':
|
|
|
|
val.with = new RegExp(val.with);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-01-31 13:13:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return new ModuleLoader();
|
2017-09-21 06:48:25 +00:00
|
|
|
}
|
2018-02-10 15:18:01 +00:00
|
|
|
ngModule.factory('vnModuleLoader', factory);
|