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) {
|
2019-11-01 12:02:47 +00:00
|
|
|
/**
|
|
|
|
* Used to load application modules lazily.
|
|
|
|
*/
|
2017-01-31 13:13:06 +00:00
|
|
|
class ModuleLoader {
|
|
|
|
constructor() {
|
2019-11-01 12:02:47 +00:00
|
|
|
this.loaded = {};
|
|
|
|
this.imports = {};
|
|
|
|
this.moduleImport = moduleImport;
|
|
|
|
this.modelInfo = $http.get(`modelInfo`)
|
|
|
|
.then(json => {
|
|
|
|
this.onModelInfoReady(json);
|
|
|
|
this.modelInfo = true;
|
|
|
|
});
|
2017-01-31 13:13:06 +00:00
|
|
|
}
|
2019-11-01 12:02:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the passed module and it's dependencies. Loading a module
|
|
|
|
* implies load the webpack chunk, translations, recursively load
|
|
|
|
* module dependencies and finally register all of them into Angular.
|
|
|
|
*
|
|
|
|
* @param {String} mod The module name to load
|
|
|
|
* @return {Promise} Will be resolved when loaded, when module is
|
|
|
|
* already loaded it returns a resolved promise
|
|
|
|
*/
|
|
|
|
load(mod) {
|
|
|
|
let mods = [];
|
|
|
|
return this.loadRec(mod, mods);
|
|
|
|
}
|
|
|
|
|
|
|
|
loadRec(mod, mods) {
|
|
|
|
let loaded = this.loaded[mod];
|
|
|
|
|
|
|
|
if (loaded === true || mods.indexOf(mod) != -1)
|
|
|
|
return $q.resolve(true);
|
|
|
|
if (loaded instanceof $q)
|
|
|
|
return loaded;
|
|
|
|
|
|
|
|
let moduleConf = $window.routes.find(i => i && i.module == mod);
|
2019-01-26 01:30:46 +00:00
|
|
|
if (!moduleConf)
|
2019-11-01 12:02:47 +00:00
|
|
|
return $q.reject(new Error(`Module not found: ${mod}`));
|
2019-01-26 01:30:46 +00:00
|
|
|
|
2019-11-01 12:02:47 +00:00
|
|
|
let promises = [];
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2019-11-01 12:02:47 +00:00
|
|
|
if (this.modelInfo instanceof $q)
|
|
|
|
promises.push(this.modelInfo);
|
2018-02-04 18:39:56 +00:00
|
|
|
|
2019-11-01 12:02:47 +00:00
|
|
|
$translatePartialLoader.addPart(mod);
|
|
|
|
promises.push($translate.refresh());
|
|
|
|
|
|
|
|
let modImport = this.imports[mod];
|
|
|
|
|
|
|
|
if (!modImport) {
|
|
|
|
modImport = this.imports[mod] = this.moduleImport(mod)
|
|
|
|
.then(() => this.imports[mod] = true);
|
|
|
|
}
|
|
|
|
if (modImport && modImport.then)
|
|
|
|
promises.push(modImport);
|
2017-01-31 13:13:06 +00:00
|
|
|
|
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) {
|
2019-11-01 12:02:47 +00:00
|
|
|
mods.push(mod);
|
2018-02-04 18:39:56 +00:00
|
|
|
for (let dep of deps)
|
2019-11-01 12:02:47 +00:00
|
|
|
promises.push(this.loadRec(dep, mods));
|
|
|
|
mods.pop();
|
2019-01-23 12:11:44 +00:00
|
|
|
}
|
2018-02-04 18:39:56 +00:00
|
|
|
|
2019-11-01 12:02:47 +00:00
|
|
|
this.loaded[mod] = $q.all(promises)
|
|
|
|
.then(() => $ocLazyLoad.load({name: mod}))
|
|
|
|
.then(() => this.loaded[mod] = true);
|
|
|
|
return this.loaded[mod];
|
2017-02-01 17:55:02 +00:00
|
|
|
}
|
2019-11-01 12:02:47 +00:00
|
|
|
|
|
|
|
onModelInfoReady(json) {
|
2017-02-01 17:55:02 +00:00
|
|
|
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);
|
|
|
|
}
|
2019-11-01 12:02:47 +00:00
|
|
|
|
2018-02-04 18:39:56 +00:00
|
|
|
parseValidation(val) {
|
|
|
|
switch (val.validation) {
|
|
|
|
case 'custom':
|
2019-11-01 12:02:47 +00:00
|
|
|
// TODO: Don't use eval() because it's "evil".
|
|
|
|
// How to do the same without eval?
|
2018-02-04 18:39:56 +00:00
|
|
|
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);
|