salix/front/core/module.js

101 lines
3.4 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';
2020-04-25 09:50:04 +00:00
/**
* Extended component options.
*
* @property {Boolean} installClassses Whether to install CSS classes equivalent to the component's and parents name
* @property {String} slotTemplate HTML template used to fill component transclude slots
*/
const defaultOptions = {
installClasses: true
};
/**
* 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
*/
2019-11-10 10:08:44 +00:00
function vnComponent(name, options) {
let controller = options.controller;
let parent = Object.getPrototypeOf(controller);
2020-04-25 09:50:04 +00:00
let parentOptions = parent.$options || defaultOptions;
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;
2020-04-25 09:50:04 +00:00
let $options = Object.assign({},
parentOptions,
options,
{
2020-04-25 09:50:04 +00:00
name,
2019-10-26 23:30:01 +00:00
transclude,
bindings: Object.assign({},
parentOptions.bindings,
options.bindings
),
require: Object.assign({},
parentOptions.require,
options.require
)
}
);
2020-04-25 09:50:04 +00:00
let parentSlotTemplates = parent.slotTemplates || [];
if (options.slotTemplate)
controller.slotTemplates = parentSlotTemplates.concat([options.slotTemplate]);
2019-10-26 23:30:01 +00:00
let classNames = [camelToKebab(name)];
if (parent.$classNames) classNames = classNames.concat(parent.$classNames);
controller.$classNames = classNames;
2020-04-25 09:50:04 +00:00
controller.$options = $options;
return this.component(name, $options);
2019-11-10 10:08:44 +00:00
}
const ngModuleFn = ng.module;
ng.module = function(...args) {
let ngModule = ngModuleFn.apply(this, args);
ngModule.vnComponent = vnComponent;
return ngModule;
};
2019-11-10 10:08:44 +00:00
const ngModule = ng.module('vnCore', ngDeps);
export default ngModule;
config.$inject = ['$translateProvider', '$translatePartialLoaderProvider', '$animateProvider'];
export function config($translateProvider, $translatePartialLoaderProvider, $animateProvider) {
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
$translateProvider
.useSanitizeValueStrategy('escape')
2020-04-25 09:50:04 +00:00
.useLoader('$translatePartialLoader', {
urlTemplate: '/locale/{part}/{lang}.json'
2018-02-10 15:18:01 +00:00
});
2020-04-25 09:50:04 +00:00
$translatePartialLoaderProvider.addPart('core');
2019-11-10 10:08:44 +00:00
$animateProvider.customFilter(
node => node.tagName == 'UI-VIEW');
2018-02-10 15:18:01 +00:00
}
ngModule.config(config);