2017-02-07 13:34:26 +00:00
|
|
|
import {module} from '../module';
|
2017-01-31 13:13:06 +00:00
|
|
|
import splitingRegister from './splitingRegister';
|
|
|
|
|
2018-02-04 18:39:56 +00:00
|
|
|
factory.$inject = ['$http', '$window', '$ocLazyLoad', '$translatePartialLoader', '$translate'];
|
|
|
|
export function factory($http, $window, $ocLazyLoad, $translatePartialLoader, $translate) {
|
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) {
|
2018-02-04 18:39:56 +00:00
|
|
|
let loaded = this._loaded;
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2018-02-04 18:39:56 +00:00
|
|
|
if (loaded[moduleName])
|
|
|
|
return loaded[moduleName];
|
|
|
|
|
|
|
|
loaded[moduleName] = Promise.resolve(true);
|
2017-01-31 13:13:06 +00:00
|
|
|
|
|
|
|
let deps = splitingRegister.getDependencies(moduleName);
|
2018-02-04 18:39:56 +00:00
|
|
|
let depPromises = [];
|
|
|
|
|
|
|
|
if (deps)
|
|
|
|
for (let dep of deps)
|
|
|
|
depPromises.push(this.load(dep, validations));
|
|
|
|
|
|
|
|
loaded[moduleName] = new Promise((resolve, reject) => {
|
|
|
|
Promise.all(depPromises)
|
|
|
|
.then(() => {
|
|
|
|
let promises = [];
|
|
|
|
|
|
|
|
// FIXME: https://github.com/angular-translate/angular-translate/pull/1674
|
|
|
|
$translatePartialLoader.addPart(moduleName);
|
|
|
|
promises.push($translate.refresh());
|
|
|
|
|
|
|
|
if (validations)
|
|
|
|
promises.push(new Promise(resolve => {
|
|
|
|
$http.get(`/${moduleName}/validations`).then(
|
|
|
|
json => this.onValidationsReady(json, resolve),
|
|
|
|
json => resolve()
|
|
|
|
);
|
|
|
|
}));
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2017-07-03 06:38:45 +00:00
|
|
|
promises.push(new Promise(resolve => {
|
2018-02-04 18:39:56 +00:00
|
|
|
splitingRegister.modules[moduleName](resolve);
|
2017-07-03 06:38:45 +00:00
|
|
|
}));
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2018-02-04 18:39:56 +00:00
|
|
|
Promise.all(promises)
|
|
|
|
.then(() => {
|
|
|
|
this._loaded[moduleName] = true;
|
|
|
|
resolve($ocLazyLoad.load({name: moduleName}));
|
|
|
|
})
|
|
|
|
.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
|
|
|
}
|
2017-01-31 13:13:06 +00:00
|
|
|
module.factory('vnModuleLoader', factory);
|