salix/front/core/module.js

107 lines
3.5 KiB
JavaScript

import {ng, ngDeps} from './vendor';
import {camelToKebab} from './lib/string';
/**
* Acts like native Module.component() function but merging component options
* with parent component options. This method establishes the $options property
* to the component controller class with the merged component options. To
* retrieve parent options, it reads the same property of the parent class, so
* for the parent options to be copied, it must have been declared using this
* same function. If any of the options (template, transclude, bindings...) is
* redeclared in the child component, the last has preference.
*
* @param {String} name Coponent name in camelCase
* @param {Object} options The component options
* @return {angularModule} The same angular module
*/
function vnComponent(name, options) {
let controller = options.controller;
let parent = Object.getPrototypeOf(controller);
let parentOptions = parent.$options || {};
let parentTransclude = parentOptions.transclude;
let transclude = parentTransclude instanceof Object
? Object.assign({}, parentTransclude)
: parentTransclude;
if (options.transclude instanceof Object) {
if (transclude instanceof Object)
Object.assign(transclude, options.transclude);
else
transclude = options.transclude;
} else if (options.transclude !== undefined)
transclude = options.transclude;
let mergedOptions = Object.assign({},
parentOptions,
options,
{
transclude,
bindings: Object.assign({},
parentOptions.bindings,
options.bindings
),
require: Object.assign({},
parentOptions.require,
options.require
)
}
);
controller.$options = mergedOptions;
let classNames = [camelToKebab(name)];
if (parent.$classNames) classNames = classNames.concat(parent.$classNames);
controller.$classNames = classNames;
return this.component(name, mergedOptions);
}
const ngModuleFn = ng.module;
ng.module = function(...args) {
let ngModule = ngModuleFn.apply(this, args);
ngModule.vnComponent = vnComponent;
return ngModule;
};
const ngModule = ng.module('vnCore', ngDeps);
export default ngModule;
config.$inject = ['$translateProvider', '$translatePartialLoaderProvider', '$animateProvider'];
export function config($translateProvider, $translatePartialLoaderProvider, $animateProvider) {
// For CSS browser targeting
document.documentElement.setAttribute('data-browser', navigator.userAgent);
$translatePartialLoaderProvider.addPart('core');
let conf = {urlTemplate: '/locale/{part}/{lang}.json'};
let fallbackLang = 'es';
let langs = ['en', 'es'];
let langAliases = {
en_US: 'en',
en_GB: 'en',
es_ES: 'es',
es_AR: 'es'
};
$translateProvider
.useSanitizeValueStrategy('escape')
.useLoader('$translatePartialLoader', conf)
.registerAvailableLanguageKeys(langs, langAliases)
// FIXME: Circular dependency due to vnInterceptor
// .fallbackLanguage(fallbackLang)
.determinePreferredLanguage(() => {
let locale = $translateProvider.resolveClientLocale();
if (langs.indexOf(locale) !== -1)
return locale;
if (langAliases[locale])
return langAliases[locale];
return fallbackLang;
});
$animateProvider.customFilter(
node => node.tagName == 'UI-VIEW');
}
ngModule.config(config);