salix/client/core/src/lib/moduleLoader.js

73 lines
2.5 KiB
JavaScript
Raw Normal View History

2017-02-07 13:34:26 +00:00
import {module} from '../module';
import splitingRegister from './splitingRegister';
factory.$inject = ['$translatePartialLoader', '$http', '$window', '$ocLazyLoad', '$q', '$translate'];
export function factory($translatePartialLoader, $http, $window, $ocLazyLoad, $q, $translate) {
class ModuleLoader {
constructor() {
this._loadedModules = {};
}
load(moduleName) {
if(this._loadedModules[moduleName])
return;
this._loadedModules[moduleName] = true;
let deps = splitingRegister.getDependencies(moduleName);
let modules = splitingRegister.modules;
let promises = [];
for(let dep of deps) {
this._loadedModules[dep] = true;
promises.push(modules[dep]());
promises.push(new Promise(resolve => {
2017-02-01 17:55:02 +00:00
$http.get(`/${dep}/validations`).then(
json => this.onValidationsReady(json, resolve)
);
}));
$translatePartialLoader.addPart(dep);
// FIXME: https://github.com/angular-translate/angular-translate/pull/1674
//promises.push($translate.refresh());
setTimeout (() => $translate.refresh(), 500);
}
let ocDeps = deps.map(item => { return { name: item } });
return new Promise (resolve => {
Promise.all(promises).then(
() => resolve($ocLazyLoad.load(ocDeps))
)
});
}
2017-02-01 17:55:02 +00:00
parseValidation(val) {
switch(val.validation) {
case 'custom':
// TODO: Reemplazar eval
val.customValidator = eval(`(${val.customValidator})`);
break;
case 'format':
val.with = new RegExp(val.with);
break;
}
}
onValidationsReady(json, resolve) {
let entities = json.data;
for(let entity in entities) {
let fields = entities[entity].validations;
for(let field in fields) {
let validations = fields[field];
for(let validation of validations)
this.parseValidation(validation);
}
}
Object.assign($window.validations, json.data);
resolve();
}
}
return new ModuleLoader();
};
module.factory('vnModuleLoader', factory);