Skip to content

Commit

Permalink
fix: series of blocking issues
Browse files Browse the repository at this point in the history
  • Loading branch information
maximelafarie committed Aug 6, 2024
1 parent 90616b5 commit 3927bf2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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>(): T {
this.assignComponentDataToModalData(this._componentRef);
return this._data;
return this._data as T;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -209,7 +208,7 @@ export class NgxSmartModalService {
* Close the latest opened modal
*/
public closeLatestModal(): void {
this.getTopOpenedModal().close();
this.getTopOpenedModal()?.close();
}

/**
Expand Down Expand Up @@ -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];
}

Expand All @@ -442,7 +441,7 @@ export class NgxSmartModalService {
try {
const modal = this.getTopOpenedModal();

if (!modal.escapable) {
if (!modal?.escapable) {
return false;
}

Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
}
}

0 comments on commit 3927bf2

Please sign in to comment.