salix/front/core/module.js

96 lines
3.3 KiB
JavaScript
Raw Normal View History

2018-12-27 11:54:16 +00:00
import {ng, ngDeps} from './vendor';
2019-10-26 23:30:01 +00:00
import {camelToKebab} from './lib/string';
2018-12-27 11:54:16 +00:00
const ngModule = ng.module('vnCore', ngDeps);
2018-02-10 15:18:01 +00:00
export default ngModule;
/**
* 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
*/
ngModule.vnComponent = function(name, options) {
let controller = options.controller;
let parent = Object.getPrototypeOf(controller);
let parentOptions = parent.$options || {};
2019-10-26 23:30:01 +00:00
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,
{
2019-10-26 23:30:01 +00:00
transclude,
bindings: Object.assign({},
parentOptions.bindings,
options.bindings
),
require: Object.assign({},
parentOptions.require,
options.require
)
}
);
controller.$options = mergedOptions;
2019-10-26 23:30:01 +00:00
let classNames = [camelToKebab(name)];
if (parent.$classNames) classNames = classNames.concat(parent.$classNames);
controller.$classNames = classNames;
return this.component(name, mergedOptions);
};
2018-02-10 15:18:01 +00:00
config.$inject = ['$translateProvider', '$translatePartialLoaderProvider'];
export function config($translateProvider, $translatePartialLoaderProvider) {
2019-10-18 19:36:30 +00:00
// For CSS browser targeting
document.documentElement.setAttribute('data-browser', navigator.userAgent);
2018-02-10 15:18:01 +00:00
$translatePartialLoaderProvider.addPart('core');
2019-01-25 22:02:29 +00:00
let conf = {urlTemplate: '/locale/{part}/{lang}.json'};
2018-02-10 15:18:01 +00:00
let fallbackLang = 'es';
let langs = ['en', 'es'];
let langAliases = {
en_US: 'en',
en_UK: '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;
});
}
ngModule.config(config);