Skip to content

Commit

Permalink
feat(modal): add new dismissAll method
Browse files Browse the repository at this point in the history
Close #1963
Closes #2633
  • Loading branch information
divdavem authored and pkozlowski-opensource committed Aug 24, 2018
1 parent fd76e4b commit daf6645
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/modal/modal-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
Injectable,
Injector,
Inject,
ComponentFactory,
ComponentFactoryResolver,
ComponentRef,
TemplateRef
Expand All @@ -22,6 +21,7 @@ import {NgbActiveModal, NgbModalRef} from './modal-ref';
export class NgbModalStack {
private _windowAttributes = ['ariaLabelledBy', 'backdrop', 'centered', 'keyboard', 'size', 'windowClass'];
private _backdropAttributes = ['backdropClass'];
private _modalRefs: NgbModalRef[] = [];

constructor(
private _applicationRef: ApplicationRef, private _injector: Injector, @Inject(DOCUMENT) private _document,
Expand All @@ -45,6 +45,7 @@ export class NgbModalStack {
let windowCmptRef: ComponentRef<NgbModalWindow> = this._attachWindowComponent(moduleCFR, containerEl, contentRef);
let ngbModalRef: NgbModalRef = new NgbModalRef(windowCmptRef, contentRef, backdropCmptRef, options.beforeDismiss);

this._registerModalRef(ngbModalRef);
ngbModalRef.result.then(revertPaddingForScrollBar, revertPaddingForScrollBar);
activeModal.close = (result: any) => { ngbModalRef.close(result); };
activeModal.dismiss = (reason: any) => { ngbModalRef.dismiss(reason); };
Expand All @@ -57,6 +58,8 @@ export class NgbModalStack {
return ngbModalRef;
}

dismissAll(reason?: any) { this._modalRefs.forEach(ngbModalRef => ngbModalRef.dismiss(reason)); }

private _attachBackdrop(moduleCFR: ComponentFactoryResolver, containerEl: any): ComponentRef<NgbModalBackdrop> {
let backdropFactory = moduleCFR.resolveComponentFactory(NgbModalBackdrop);
let backdropCmptRef = backdropFactory.create(this._injector);
Expand Down Expand Up @@ -125,4 +128,15 @@ export class NgbModalStack {
this._applicationRef.attachView(componentRef.hostView);
return new ContentRef([[componentRef.location.nativeElement]], componentRef.hostView, componentRef);
}

private _registerModalRef(ngbModalRef: NgbModalRef) {
const unregisterModalRef = () => {
const index = this._modalRefs.indexOf(ngbModalRef);
if (index > -1) {
this._modalRefs.splice(index, 1);
}
};
this._modalRefs.push(ngbModalRef);
ngbModalRef.result.then(unregisterModalRef, unregisterModalRef);
}
}
19 changes: 19 additions & 0 deletions src/modal/modal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,24 @@ describe('ngb-modal', () => {
expect(fixture.nativeElement).not.toHaveModal();
});

it('should dismiss with dismissAll', () => {
fixture.componentInstance.open('foo');
fixture.detectChanges();
expect(fixture.nativeElement).toHaveModal('foo');

fixture.componentInstance.dismissAll('dismissAllArg');
fixture.detectChanges();
expect(fixture.nativeElement).not.toHaveModal();
});

it('should not throw when dismissAll called with no active modal', () => {
expect(fixture.nativeElement).not.toHaveModal();

fixture.componentInstance.dismissAll();
fixture.detectChanges();
expect(fixture.nativeElement).not.toHaveModal();
});

it('should not throw when dismiss called multiple times', () => {
const modalRef = fixture.componentInstance.open('foo');
modalRef.result.catch(NOOP);
Expand Down Expand Up @@ -786,6 +804,7 @@ class TestComponent {
this.openedModal.close('ok');
}
}
dismissAll(reason?: any) { this.modalService.dismissAll(reason); }
openTpl(options?: Object) { return this.modalService.open(this.tplContent, options); }
openCmpt(cmptType: any, options?: Object) { return this.modalService.open(cmptType, options); }
openDestroyableTpl(options?: Object) { return this.modalService.open(this.tplDestroyableContent, options); }
Expand Down
7 changes: 7 additions & 0 deletions src/modal/modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,11 @@ export class NgbModal {
const combinedOptions = Object.assign({}, this._config, options);
return this._modalStack.open(this._moduleCFR, this._injector, content, combinedOptions);
}

/**
* Dismiss all currently displayed modal windows with the supplied reason.
*
* @since 3.1.0
*/
dismissAll(reason?: any) { this._modalStack.dismissAll(reason); }
}

0 comments on commit daf6645

Please sign in to comment.