Skip to content

Commit adbfe86

Browse files
authored
Merge pull request #237 from biig-io/feature/close-all-modals
Add close all modals feature, and add documentation about it
2 parents f7bb678 + 28ed958 commit adbfe86

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

src/app/doc/parts/api/api.component.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ export class AppComponent {
9090
type: 'void'
9191
}
9292
},
93+
{
94+
name: ['closeAll()'],
95+
description: 'Close all opened modals',
96+
params: [],
97+
return: {
98+
type: 'void'
99+
}
100+
},
93101
{
94102
name: ['toggle(id: string, force?: boolean)'],
95103
description: 'Toggle (open <strong>or</strong> close) a given modal',

src/ngx-smart-modal/src/services/ngx-smart-modal.service.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ export class NgxSmartModalService {
8383
return this._closeModal(this.get(id));
8484
}
8585

86+
/**
87+
* Close all opened modals
88+
*/
89+
public closeAll(): void {
90+
this.getOpenedModals().forEach((instance: ModalInstance) => {
91+
this._closeModal(instance.modal);
92+
});
93+
}
94+
8695
/**
8796
* Toggles a given modal
8897
* If the retrieved modal is opened it closes it, else it opens it.

src/ngx-smart-modal/tests/services/ngx-smart-modal.service.spec.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ describe('NgxSmartModalService', () => {
9191

9292
it('should add events ( _private and no browser )', inject([NgxSmartModalService], (service: NgxSmartModalService) => {
9393
spyOnProperty(service as any, 'isBrowser', 'get').and.returnValue(false);
94-
94+
9595
expect((service as any)._addEvents()).toBeFalsy();
9696
}));
9797

@@ -413,4 +413,48 @@ describe('NgxSmartModalService', () => {
413413
tick(500);
414414
expect((service as any)._trapFocusModal(event)).toBeFalsy();
415415
})));
416+
417+
it('should closeAll', fakeAsync(inject([NgxSmartModalService], (service: NgxSmartModalService) => {
418+
const firstFixture = TestBed.createComponent(NgxSmartModalComponent);
419+
const secFixture = TestBed.createComponent(NgxSmartModalComponent);
420+
const thirdFixture = TestBed.createComponent(NgxSmartModalComponent);
421+
const firstApp = firstFixture.debugElement.componentInstance;
422+
const secApp = secFixture.debugElement.componentInstance;
423+
const thirdApp = thirdFixture.debugElement.componentInstance;
424+
425+
firstApp.identifier = 'myFirstModal';
426+
secApp.identifier = 'mySecModal';
427+
thirdApp.identifier = 'myThirdModal';
428+
429+
service.addModal({ id: 'myFirstModal', modal: firstApp });
430+
service.addModal({ id: 'mySecModal', modal: secApp });
431+
service.addModal({ id: 'myThirdModal', modal: thirdApp });
432+
433+
spyOn(service, 'closeAll').and.callThrough();
434+
spyOn(service, 'getOpenedModals').and.callThrough();
435+
spyOn(service as any, '_closeModal').and.callThrough();
436+
437+
service.open(firstApp.identifier);
438+
service.open(secApp.identifier);
439+
service.open(thirdApp.identifier);
440+
441+
expect(firstApp.visible).toBeTruthy();
442+
expect(secApp.visible).toBeTruthy();
443+
expect(thirdApp.visible).toBeTruthy();
444+
445+
tick(501);
446+
447+
service.closeAll();
448+
449+
tick(501);
450+
451+
expect(service.closeAll).toHaveBeenCalled();
452+
expect(service.getOpenedModals).toHaveBeenCalled();
453+
expect(service['_closeModal']).toHaveBeenCalledWith(firstApp);
454+
expect(service['_closeModal']).toHaveBeenCalledWith(secApp);
455+
expect(service['_closeModal']).toHaveBeenCalledWith(thirdApp);
456+
expect(firstApp.visible).toBeFalsy();
457+
expect(secApp.visible).toBeFalsy();
458+
expect(thirdApp.visible).toBeFalsy();
459+
})));
416460
});

0 commit comments

Comments
 (0)