2019-10-24 10:44:36 +00:00
|
|
|
import ngModule from '../module';
|
2018-10-18 07:24:20 +00:00
|
|
|
import EventEmitter from './event-emitter';
|
2018-10-22 15:54:04 +00:00
|
|
|
import {kebabToCamel} from './string';
|
2018-10-18 07:24:20 +00:00
|
|
|
|
2017-02-06 17:01:04 +00:00
|
|
|
/**
|
|
|
|
* Base class for component controllers.
|
|
|
|
*/
|
2018-10-18 07:24:20 +00:00
|
|
|
export default class Component extends EventEmitter {
|
2017-02-06 17:01:04 +00:00
|
|
|
/**
|
|
|
|
* Contructor.
|
2017-04-28 13:04:29 +00:00
|
|
|
*
|
2017-02-06 17:01:04 +00:00
|
|
|
* @param {HTMLElement} $element The main component element
|
2018-02-12 12:16:49 +00:00
|
|
|
* @param {$rootScope.Scope} $scope The element scope
|
2017-02-06 17:01:04 +00:00
|
|
|
*/
|
2018-02-12 12:16:49 +00:00
|
|
|
constructor($element, $scope) {
|
2018-10-22 15:54:04 +00:00
|
|
|
super();
|
|
|
|
if (!$element) return;
|
2017-02-06 17:01:04 +00:00
|
|
|
this.element = $element[0];
|
2018-02-20 09:00:19 +00:00
|
|
|
this.element.$ctrl = this;
|
|
|
|
this.$element = $element;
|
|
|
|
this.$ = $scope;
|
2017-02-06 17:01:04 +00:00
|
|
|
}
|
2018-10-22 15:54:04 +00:00
|
|
|
|
|
|
|
$postLink() {
|
|
|
|
if (!this.$element) return;
|
|
|
|
let attrs = this.$element[0].attributes;
|
|
|
|
let $scope = this.$;
|
|
|
|
for (let attr of attrs) {
|
|
|
|
if (attr.name.substr(0, 2) !== 'on') continue;
|
|
|
|
let eventName = kebabToCamel(attr.name.substr(3));
|
|
|
|
let callback = locals => $scope.$parent.$eval(attr.nodeValue, locals);
|
|
|
|
this.on(eventName, callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-18 07:24:20 +00:00
|
|
|
/**
|
|
|
|
* The component owner window.
|
|
|
|
*/
|
|
|
|
get window() {
|
|
|
|
return this.document.defaultView;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* The component owner document.
|
|
|
|
*/
|
|
|
|
get document() {
|
|
|
|
return this.element.ownerDocument;
|
|
|
|
}
|
2019-10-24 10:44:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Translates an string.
|
|
|
|
*
|
|
|
|
* @param {String} string String to translate
|
|
|
|
* @param {Array} params Translate parameters
|
|
|
|
* @return {String} The translated string
|
|
|
|
*/
|
|
|
|
$t(string, params) {
|
|
|
|
return this.$translate.instant(string, params);
|
|
|
|
}
|
2017-02-06 17:01:04 +00:00
|
|
|
}
|
2018-02-12 12:16:49 +00:00
|
|
|
Component.$inject = ['$element', '$scope'];
|
2019-10-24 10:44:36 +00:00
|
|
|
|
2019-10-26 10:04:48 +00:00
|
|
|
function runFn($translate, $q, $http, $state, $stateParams, $timeout, $transitions, $compile, vnApp) {
|
2019-10-24 10:44:36 +00:00
|
|
|
Object.assign(Component.prototype, {
|
|
|
|
$translate,
|
|
|
|
$q,
|
|
|
|
$http,
|
|
|
|
$state,
|
2019-10-26 10:04:48 +00:00
|
|
|
$params: $stateParams,
|
|
|
|
$timeout,
|
|
|
|
$transitions,
|
|
|
|
$compile,
|
|
|
|
vnApp
|
2019-10-24 10:44:36 +00:00
|
|
|
});
|
|
|
|
}
|
2019-10-26 10:04:48 +00:00
|
|
|
runFn.$inject = [
|
|
|
|
'$translate',
|
|
|
|
'$q',
|
|
|
|
'$http',
|
|
|
|
'$state',
|
|
|
|
'$stateParams',
|
|
|
|
'$timeout',
|
|
|
|
'$transitions',
|
|
|
|
'$compile',
|
|
|
|
'vnApp'
|
|
|
|
];
|
2019-10-24 10:44:36 +00:00
|
|
|
|
|
|
|
ngModule.run(runFn);
|