import ngModule from '../module'; import EventEmitter from './event-emitter'; import {kebabToCamel} from './string'; /** * Base class for component controllers. */ export default class Component extends EventEmitter { /** * Contructor. * * @param {HTMLElement} $element The main component element * @param {$rootScope.Scope} $scope The element scope */ constructor($element, $scope) { super(); if (!$element) return; this.element = $element[0]; this.element.$ctrl = this; this.$element = $element; this.$ = $scope; } $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); } } /** * The component owner window. */ get window() { return this.document.defaultView; } /** * The component owner document. */ get document() { return this.element.ownerDocument; } /** * 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); } createBoundTranscludeFn(linkFn) { let scope = this.$; let previousBoundTranscludeFn = this.$transclude.$$boundTransclude; function vnBoundTranscludeFn(transcludedScope, cloneFn, controllers, futureParentElement, containingScope) { if (!transcludedScope) { transcludedScope = scope.$new(false, containingScope); transcludedScope.$$transcluded = true; } return linkFn(transcludedScope, cloneFn, { parentBoundTranscludeFn: previousBoundTranscludeFn, transcludeControllers: controllers, futureParentElement: futureParentElement }); } vnBoundTranscludeFn.$$slots = previousBoundTranscludeFn.$$slots; return vnBoundTranscludeFn; } fillDefaultSlot(template) { let linkFn = this.$compile(template); this.$transclude.$$boundTransclude = this.createBoundTranscludeFn(linkFn); } fillSlot(slot, template) { let slots = this.$transclude.$$boundTransclude.$$slots; let linkFn = this.$compile(template); slots[slot] = this.createBoundTranscludeFn(linkFn); } copySlot(slot, $transclude) { this.$transclude.$$boundTransclude.$$slots[slot] = $transclude.$$boundTransclude.$$slots[slot]; } } Component.$inject = ['$element', '$scope']; function runFn( $translate, $q, $http, $state, $stateParams, $timeout, $transitions, $compile, $filter, $interpolate, vnApp) { Object.assign(Component.prototype, { $translate, $q, $http, $state, $params: $stateParams, $timeout, $transitions, $compile, $filter, $interpolate, vnApp }); } runFn.$inject = [ '$translate', '$q', '$http', '$state', '$stateParams', '$timeout', '$transitions', '$compile', '$filter', '$interpolate', 'vnApp' ]; ngModule.run(runFn);