| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469 |
- import { EventEmitter, Injector, ElementRef, TemplateRef, Injectable, ComponentFactoryResolver, NgZone, ApplicationRef } from '@angular/core';
- import { listenToTriggersV2, registerOutsideClick, registerEscClick } from 'ngx-bootstrap/utils';
- import { PositioningService } from 'ngx-bootstrap/positioning';
- /**
- * @fileoverview added by tsickle
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
- */
- /**
- * @template T
- */
- class BsComponentRef {
- }
- /**
- * @fileoverview added by tsickle
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
- */
- /**
- * @copyright Valor Software
- * @copyright Angular ng-bootstrap team
- */
- class ContentRef {
- /**
- * @param {?} nodes
- * @param {?=} viewRef
- * @param {?=} componentRef
- */
- constructor(
- /* tslint:disable-next-line: no-any */
- nodes, viewRef,
- /* tslint:disable-next-line: no-any */
- componentRef) {
- this.nodes = nodes;
- this.viewRef = viewRef;
- this.componentRef = componentRef;
- }
- }
- /**
- * @fileoverview added by tsickle
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
- */
- /**
- * @template T
- */
- class ComponentLoader {
- /**
- * Do not use this directly, it should be instanced via
- * `ComponentLoadFactory.attach`
- * \@internal
- * @param {?} _viewContainerRef
- * @param {?} _renderer
- * @param {?} _elementRef
- * @param {?} _injector
- * @param {?} _componentFactoryResolver
- * @param {?} _ngZone
- * @param {?} _applicationRef
- * @param {?} _posService
- */
- // tslint:disable-next-line
- constructor(_viewContainerRef, _renderer, _elementRef, _injector, _componentFactoryResolver, _ngZone, _applicationRef, _posService) {
- this._viewContainerRef = _viewContainerRef;
- this._renderer = _renderer;
- this._elementRef = _elementRef;
- this._injector = _injector;
- this._componentFactoryResolver = _componentFactoryResolver;
- this._ngZone = _ngZone;
- this._applicationRef = _applicationRef;
- this._posService = _posService;
- this.onBeforeShow = new EventEmitter();
- /* tslint:disable-next-line: no-any*/
- this.onShown = new EventEmitter();
- /* tslint:disable-next-line: no-any*/
- this.onBeforeHide = new EventEmitter();
- this.onHidden = new EventEmitter();
- this._providers = [];
- this._isHiding = false;
- /**
- * A selector used if container element was not found
- */
- this.containerDefaultSelector = 'body';
- this._listenOpts = {};
- this._globalListener = Function.prototype;
- }
- /**
- * @return {?}
- */
- get isShown() {
- if (this._isHiding) {
- return false;
- }
- return !!this._componentRef;
- }
- /**
- * @param {?} compType
- * @return {?}
- */
- attach(compType) {
- this._componentFactory = this._componentFactoryResolver
- .resolveComponentFactory(compType);
- return this;
- }
- // todo: add behaviour: to target element, `body`, custom element
- /**
- * @param {?=} container
- * @return {?}
- */
- to(container) {
- this.container = container || this.container;
- return this;
- }
- /**
- * @param {?=} opts
- * @return {?}
- */
- position(opts) {
- this.attachment = opts.attachment || this.attachment;
- /* tslint:disable-next-line: no-unnecessary-type-assertion */
- this._elementRef = ((/** @type {?} */ (opts.target))) || this._elementRef;
- return this;
- }
- /**
- * @param {?} provider
- * @return {?}
- */
- provide(provider) {
- this._providers.push(provider);
- return this;
- }
- // todo: appendChild to element or document.querySelector(this.container)
- /**
- * @param {?=} opts
- * @return {?}
- */
- show(opts = {}) {
- this._subscribePositioning();
- this._innerComponent = null;
- if (!this._componentRef) {
- this.onBeforeShow.emit();
- this._contentRef = this._getContentRef(opts.content, opts.context, opts.initialState);
- /** @type {?} */
- const injector = Injector.create({
- providers: this._providers,
- parent: this._injector
- });
- this._componentRef = this._componentFactory.create(injector, this._contentRef.nodes);
- this._applicationRef.attachView(this._componentRef.hostView);
- // 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 instanceof ElementRef) {
- this.container.nativeElement.appendChild(this._componentRef.location.nativeElement);
- }
- if (typeof this.container === 'string' && typeof document !== 'undefined') {
- /** @type {?} */
- const selectedElement = document.querySelector(this.container) ||
- document.querySelector(this.containerDefaultSelector);
- selectedElement.appendChild(this._componentRef.location.nativeElement);
- }
- if (!this.container &&
- this._elementRef &&
- this._elementRef.nativeElement.parentElement) {
- this._elementRef.nativeElement.parentElement.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
- if (this._contentRef.componentRef) {
- this._innerComponent = this._contentRef.componentRef.instance;
- this._contentRef.componentRef.changeDetectorRef.markForCheck();
- this._contentRef.componentRef.changeDetectorRef.detectChanges();
- }
- this._componentRef.changeDetectorRef.markForCheck();
- this._componentRef.changeDetectorRef.detectChanges();
- this.onShown.emit(this._componentRef.instance);
- }
- this._registerOutsideClick();
- return this._componentRef;
- }
- /**
- * @return {?}
- */
- hide() {
- if (!this._componentRef) {
- return this;
- }
- this._posService.deletePositionElement(this._componentRef.location);
- this.onBeforeHide.emit(this._componentRef.instance);
- /** @type {?} */
- const componentEl = this._componentRef.location.nativeElement;
- componentEl.parentNode.removeChild(componentEl);
- if (this._contentRef.componentRef) {
- this._contentRef.componentRef.destroy();
- }
- this._componentRef.destroy();
- if (this._viewContainerRef && this._contentRef.viewRef) {
- this._viewContainerRef.remove(this._viewContainerRef.indexOf(this._contentRef.viewRef));
- }
- if (this._contentRef.viewRef) {
- this._contentRef.viewRef.destroy();
- }
- this._contentRef = null;
- this._componentRef = null;
- this._removeGlobalListener();
- this.onHidden.emit();
- return this;
- }
- /**
- * @return {?}
- */
- toggle() {
- if (this.isShown) {
- this.hide();
- return;
- }
- this.show();
- }
- /**
- * @return {?}
- */
- dispose() {
- if (this.isShown) {
- this.hide();
- }
- this._unsubscribePositioning();
- if (this._unregisterListenersFn) {
- this._unregisterListenersFn();
- }
- }
- /**
- * @param {?} listenOpts
- * @return {?}
- */
- listen(listenOpts) {
- this.triggers = listenOpts.triggers || this.triggers;
- this._listenOpts.outsideClick = listenOpts.outsideClick;
- this._listenOpts.outsideEsc = listenOpts.outsideEsc;
- listenOpts.target = listenOpts.target || this._elementRef.nativeElement;
- /** @type {?} */
- const hide = (this._listenOpts.hide = (/**
- * @return {?}
- */
- () => listenOpts.hide ? listenOpts.hide() : void this.hide()));
- /** @type {?} */
- const show = (this._listenOpts.show = (/**
- * @param {?} registerHide
- * @return {?}
- */
- (registerHide) => {
- listenOpts.show ? listenOpts.show(registerHide) : this.show(registerHide);
- registerHide();
- }));
- /** @type {?} */
- const toggle = (/**
- * @param {?} registerHide
- * @return {?}
- */
- (registerHide) => {
- this.isShown ? hide() : show(registerHide);
- });
- this._unregisterListenersFn = listenToTriggersV2(this._renderer, {
- target: listenOpts.target,
- triggers: listenOpts.triggers,
- show,
- hide,
- toggle
- });
- return this;
- }
- /**
- * @return {?}
- */
- _removeGlobalListener() {
- if (this._globalListener) {
- this._globalListener();
- this._globalListener = null;
- }
- }
- /**
- * @param {?} vRef
- * @param {?} template
- * @return {?}
- */
- attachInline(vRef,
- /* tslint:disable-next-line: no-any*/
- template) {
- this._inlineViewRef = vRef.createEmbeddedView(template);
- return this;
- }
- /**
- * @return {?}
- */
- _registerOutsideClick() {
- if (!this._componentRef || !this._componentRef.location) {
- return;
- }
- // why: should run after first event bubble
- if (this._listenOpts.outsideClick) {
- /** @type {?} */
- const target = this._componentRef.location.nativeElement;
- setTimeout((/**
- * @return {?}
- */
- () => {
- this._globalListener = registerOutsideClick(this._renderer, {
- targets: [target, this._elementRef.nativeElement],
- outsideClick: this._listenOpts.outsideClick,
- hide: (/**
- * @return {?}
- */
- () => this._listenOpts.hide())
- });
- }));
- }
- if (this._listenOpts.outsideEsc) {
- /** @type {?} */
- const target = this._componentRef.location.nativeElement;
- this._globalListener = registerEscClick(this._renderer, {
- targets: [target, this._elementRef.nativeElement],
- outsideEsc: this._listenOpts.outsideEsc,
- hide: (/**
- * @return {?}
- */
- () => this._listenOpts.hide())
- });
- }
- }
- /**
- * @return {?}
- */
- getInnerComponent() {
- return this._innerComponent;
- }
- /**
- * @private
- * @return {?}
- */
- _subscribePositioning() {
- if (this._zoneSubscription || !this.attachment) {
- return;
- }
- this.onShown.subscribe((/**
- * @return {?}
- */
- () => {
- this._posService.position({
- element: this._componentRef.location,
- target: this._elementRef,
- attachment: this.attachment,
- appendToBody: this.container === 'body'
- });
- }));
- this._zoneSubscription = this._ngZone.onStable.subscribe((/**
- * @return {?}
- */
- () => {
- if (!this._componentRef) {
- return;
- }
- this._posService.calcPosition();
- }));
- }
- /**
- * @private
- * @return {?}
- */
- _unsubscribePositioning() {
- if (!this._zoneSubscription) {
- return;
- }
- this._zoneSubscription.unsubscribe();
- this._zoneSubscription = null;
- }
- /**
- * @private
- * @param {?} content
- * @param {?=} context
- * @param {?=} initialState
- * @return {?}
- */
- _getContentRef(
- /* tslint:disable-next-line: no-any*/
- content,
- /* tslint:disable-next-line: no-any*/
- context,
- /* tslint:disable-next-line: no-any*/
- initialState) {
- if (!content) {
- return new ContentRef([]);
- }
- if (content instanceof TemplateRef) {
- if (this._viewContainerRef) {
- /** @type {?} */
- const _viewRef = this._viewContainerRef
- .createEmbeddedView(content, context);
- _viewRef.markForCheck();
- return new ContentRef([_viewRef.rootNodes], _viewRef);
- }
- /** @type {?} */
- const viewRef = content.createEmbeddedView({});
- this._applicationRef.attachView(viewRef);
- return new ContentRef([viewRef.rootNodes], viewRef);
- }
- if (typeof content === 'function') {
- /** @type {?} */
- const contentCmptFactory = this._componentFactoryResolver.resolveComponentFactory(content);
- /** @type {?} */
- const modalContentInjector = Injector.create({
- providers: this._providers,
- parent: this._injector
- });
- /** @type {?} */
- const componentRef = contentCmptFactory.create(modalContentInjector);
- Object.assign(componentRef.instance, initialState);
- this._applicationRef.attachView(componentRef.hostView);
- return new ContentRef([[componentRef.location.nativeElement]], componentRef.hostView, componentRef);
- }
- return new ContentRef([[this._renderer.createText(`${content}`)]]);
- }
- }
- /**
- * @fileoverview added by tsickle
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
- */
- class ComponentLoaderFactory {
- /**
- * @param {?} _componentFactoryResolver
- * @param {?} _ngZone
- * @param {?} _injector
- * @param {?} _posService
- * @param {?} _applicationRef
- */
- constructor(_componentFactoryResolver, _ngZone, _injector, _posService, _applicationRef) {
- this._componentFactoryResolver = _componentFactoryResolver;
- this._ngZone = _ngZone;
- this._injector = _injector;
- this._posService = _posService;
- this._applicationRef = _applicationRef;
- }
- /**
- *
- * @template T
- * @param {?} _elementRef
- * @param {?} _viewContainerRef
- * @param {?} _renderer
- * @return {?}
- */
- createLoader(_elementRef, _viewContainerRef, _renderer) {
- return new ComponentLoader(_viewContainerRef, _renderer, _elementRef, this._injector, this._componentFactoryResolver, this._ngZone, this._applicationRef, this._posService);
- }
- }
- ComponentLoaderFactory.decorators = [
- { type: Injectable }
- ];
- /** @nocollapse */
- ComponentLoaderFactory.ctorParameters = () => [
- { type: ComponentFactoryResolver },
- { type: NgZone },
- { type: Injector },
- { type: PositioningService },
- { type: ApplicationRef }
- ];
- export { BsComponentRef, ComponentLoader, ComponentLoaderFactory, ContentRef };
- //# sourceMappingURL=ngx-bootstrap-component-loader.js.map
|