From 3927bf209bfd5a8791dd56b2ec9e5f76d4a52875 Mon Sep 17 00:00:00 2001 From: Maxime Lafarie Date: Wed, 7 Aug 2024 00:22:29 +0200 Subject: [PATCH] fix: series of blocking issues --- .../components/ngx-smart-modal.component.ts | 4 ++-- .../services/ngx-smart-modal-stack.service.ts | 21 ++++++++----------- .../lib/services/ngx-smart-modal.service.ts | 15 ++++++------- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/projects/ngx-smart-modal/src/lib/components/ngx-smart-modal.component.ts b/projects/ngx-smart-modal/src/lib/components/ngx-smart-modal.component.ts index 35c3969..71201c0 100644 --- a/projects/ngx-smart-modal/src/lib/components/ngx-smart-modal.component.ts +++ b/projects/ngx-smart-modal/src/lib/components/ngx-smart-modal.component.ts @@ -258,9 +258,9 @@ export class NgxSmartModalComponent implements OnInit, OnDestroy, AfterViewCheck /** * Retrieve the data attached to the modal instance */ - public getData(): unknown { + public getData(): T { this.assignComponentDataToModalData(this._componentRef); - return this._data; + return this._data as T; } /** diff --git a/projects/ngx-smart-modal/src/lib/services/ngx-smart-modal-stack.service.ts b/projects/ngx-smart-modal/src/lib/services/ngx-smart-modal-stack.service.ts index 5cd5909..afe9dd9 100644 --- a/projects/ngx-smart-modal/src/lib/services/ngx-smart-modal-stack.service.ts +++ b/projects/ngx-smart-modal/src/lib/services/ngx-smart-modal-stack.service.ts @@ -70,14 +70,16 @@ export class NgxSmartModalStackService { * * @returns the opened modal with highest z-index. */ - public getTopOpenedModal(): NgxSmartModalComponent { - if (!this.getOpenedModals().length) { - throw new Error('No modal is opened'); + public getTopOpenedModal(): NgxSmartModalComponent | undefined { + const openedModals = this.getOpenedModals(); + + if (!openedModals.length) { + return undefined; } - return this.getOpenedModals() + return openedModals .map((o: ModalInstance) => o.modal) - .reduce((highest, item) => item.layerPosition > highest.layerPosition ? item : highest, this.getOpenedModals()[0].modal); + .reduce((highest, item) => item.layerPosition > highest.layerPosition ? item : highest, openedModals[0].modal); } /** @@ -107,13 +109,8 @@ export class NgxSmartModalStackService { * @param id The modal identifier. * @returns the removed modal instance. */ - public removeModal(id: string): undefined | ModalInstance { + public removeModal(id: string): ModalInstance | undefined { const i: number = this._modalStack.findIndex((o: any) => o.id === id); - if (i < 0) { - return; - } - - const modalInstance = this._modalStack.splice(i, 1)[0]; - return modalInstance; + return i >= 0 ? this._modalStack.splice(i, 1)[0] : undefined; } } diff --git a/projects/ngx-smart-modal/src/lib/services/ngx-smart-modal.service.ts b/projects/ngx-smart-modal/src/lib/services/ngx-smart-modal.service.ts index 5d8dd73..17f16c2 100644 --- a/projects/ngx-smart-modal/src/lib/services/ngx-smart-modal.service.ts +++ b/projects/ngx-smart-modal/src/lib/services/ngx-smart-modal.service.ts @@ -17,7 +17,6 @@ export class NgxSmartModalService { private _appRef: ApplicationRef, private _injector: Injector, private _modalStack: NgxSmartModalStackService, - private applicationRef: ApplicationRef, @Inject(DOCUMENT) private _document: any, @Inject(PLATFORM_ID) private _platformId: any ) { @@ -116,7 +115,7 @@ export class NgxSmartModalService { * * @returns the opened modal with highest z-index. */ - public getTopOpenedModal(): NgxSmartModalComponent { + public getTopOpenedModal(): NgxSmartModalComponent | undefined { return this._modalStack.getTopOpenedModal(); } @@ -209,7 +208,7 @@ export class NgxSmartModalService { * Close the latest opened modal */ public closeLatestModal(): void { - this.getTopOpenedModal().close(); + this.getTopOpenedModal()?.close(); } /** @@ -426,7 +425,7 @@ export class NgxSmartModalService { if (content instanceof TemplateRef) { const viewRef = content.createEmbeddedView(null as any); - this.applicationRef.attachView(viewRef); + this._appRef.attachView(viewRef); return [viewRef.rootNodes]; } @@ -442,7 +441,7 @@ export class NgxSmartModalService { try { const modal = this.getTopOpenedModal(); - if (!modal.escapable) { + if (!modal?.escapable) { return false; } @@ -474,7 +473,7 @@ export class NgxSmartModalService { try { const modal = this.getTopOpenedModal(); - if (!modal.nsmDialog.first.nativeElement.contains(document.activeElement)) { + if (modal && !modal.nsmDialog.first.nativeElement.contains(document.activeElement)) { event.preventDefault(); event.stopPropagation(); modal.nsmDialog.first.nativeElement.focus(); @@ -498,6 +497,8 @@ export class NgxSmartModalService { return; } - this._document.body.removeChild(modal.elementRef.nativeElement); + if (modal.elementRef.nativeElement && this._document.body.contains(modal.elementRef.nativeElement)) { + this._document.body.removeChild(modal.elementRef.nativeElement); + } } }