UNPKG

ng2-bootstrap

Version:
163 lines 6.63 kB
import { TemplateRef, EventEmitter, ReflectiveInjector } from '@angular/core'; import { ContentRef } from './content-ref.class'; import { listenToTriggers } from '../utils/triggers'; export var ComponentLoader = (function () { /** * Do not use this directly, it should be instanced via * `ComponentLoadFactory.attach` * @internal * @param _viewContainerRef * @param _elementRef * @param _injector * @param _renderer * @param _componentFactoryResolver * @param _ngZone * @param _posService */ // tslint:disable-next-line function ComponentLoader(_viewContainerRef, _renderer, _elementRef, _injector, _componentFactoryResolver, _ngZone, _posService) { this.onBeforeShow = new EventEmitter(); this.onShown = new EventEmitter(); this.onBeforeHide = new EventEmitter(); this.onHidden = new EventEmitter(); this._providers = []; this._ngZone = _ngZone; this._injector = _injector; this._renderer = _renderer; this._elementRef = _elementRef; this._posService = _posService; this._viewContainerRef = _viewContainerRef; this._componentFactoryResolver = _componentFactoryResolver; } Object.defineProperty(ComponentLoader.prototype, "isShown", { get: function () { return !!this._componentRef; }, enumerable: true, configurable: true }); ; ComponentLoader.prototype.attach = function (compType) { this._componentFactory = this._componentFactoryResolver .resolveComponentFactory(compType); return this; }; // todo: add behaviour: to target element, `body`, custom element ComponentLoader.prototype.to = function (container) { this.container = container || this.container; return this; }; ComponentLoader.prototype.position = function (opts) { this.attachment = opts.attachment || this.attachment; this._elementRef = opts.target || this._elementRef; return this; }; ComponentLoader.prototype.provide = function (provider) { this._providers.push(provider); return this; }; ComponentLoader.prototype.show = function (opts) { if (opts === void 0) { opts = {}; } this._subscribePositioning(); if (!this._componentRef) { this.onBeforeShow.emit(); this._contentRef = this._getContentRef(opts.content); var injector = ReflectiveInjector.resolveAndCreate(this._providers, this._injector); this._componentRef = this._viewContainerRef .createComponent(this._componentFactory, 0, injector, this._contentRef.nodes); this.instance = this._componentRef.instance; Object.assign(this._componentRef.instance, opts); if (this.container === 'body' && typeof document !== 'undefined') { document.querySelector(this.container) .appendChild(this._componentRef.location.nativeElement); } // we need to manually invoke change detection since events registered // via // Renderer::listen() are not picked up by change detection with the // OnPush strategy this._componentRef.changeDetectorRef.markForCheck(); this.onShown.emit(this._componentRef.instance); } return this._componentRef; }; ComponentLoader.prototype.hide = function () { if (this._componentRef) { this.onBeforeHide.emit(this._componentRef.instance); this._viewContainerRef.remove(this._viewContainerRef.indexOf(this._componentRef.hostView)); this._componentRef = null; if (this._contentRef.viewRef) { this._viewContainerRef.remove(this._viewContainerRef.indexOf(this._contentRef.viewRef)); this._contentRef = null; } this._componentRef = null; this.onHidden.emit(); } return this; }; ComponentLoader.prototype.toggle = function () { if (this.isShown) { this.hide(); return; } this.show(); }; ComponentLoader.prototype.dispose = function () { if (this.isShown) { this.hide(); } this._unsubscribePositioning(); if (this._unregisterListenersFn) { this._unregisterListenersFn(); } }; ComponentLoader.prototype.listen = function (listenOpts) { var _this = this; this.triggers = listenOpts.triggers || this.triggers; listenOpts.target = listenOpts.target || this._elementRef; listenOpts.show = listenOpts.show || (function () { return _this.show(); }); listenOpts.hide = listenOpts.hide || (function () { return _this.hide(); }); listenOpts.toggle = listenOpts.toggle || (function () { return _this.isShown ? listenOpts.hide() : listenOpts.show(); }); this._unregisterListenersFn = listenToTriggers(this._renderer, listenOpts.target.nativeElement, this.triggers, listenOpts.show, listenOpts.hide, listenOpts.toggle); return this; }; ComponentLoader.prototype._subscribePositioning = function () { var _this = this; if (this._zoneSubscription || !this.attachment) { return; } this._zoneSubscription = this._ngZone .onStable.subscribe(function () { if (!_this._componentRef) { return; } _this._posService.position({ element: _this._componentRef.location, target: _this._elementRef, attachment: _this.attachment, appendToBody: _this.container === 'body' }); }); }; ComponentLoader.prototype._unsubscribePositioning = function () { if (!this._zoneSubscription) { return; } this._zoneSubscription.unsubscribe(); this._zoneSubscription = null; }; ComponentLoader.prototype._getContentRef = function (content) { if (!content) { return new ContentRef([]); } if (content instanceof TemplateRef) { var viewRef = this._viewContainerRef .createEmbeddedView(content); return new ContentRef([viewRef.rootNodes], viewRef); } return new ContentRef([[this._renderer.createText(null, "" + content)]]); }; return ComponentLoader; }()); //# sourceMappingURL=component-loader.class.js.map