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

74 lines
2.6 KiB
JavaScript

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 => {
$http.get(`/${dep}/validations`).then(
json => this.onValidationsReady(json, resolve),
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))
)
});
}
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);