-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(module:modal): add afterOpen/afterAllClose/closeAll/openModals, …
…adjust the boolean props and changeBodyOverflow, complete testing. close #1162
- Loading branch information
1 parent
f5410cb
commit 6b96f47
Showing
12 changed files
with
446 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Injectable, Optional, SkipSelf } from '@angular/core'; | ||
import { Subject } from 'rxjs/Subject'; | ||
import { Subscription } from 'rxjs/Subscription'; | ||
|
||
import { NzModalRef } from './nz-modal-ref.class'; | ||
|
||
interface RegisteredMeta { | ||
modalRef: NzModalRef; | ||
afterOpenSubscription: Subscription; | ||
afterCloseSubscription: Subscription; | ||
} | ||
|
||
@Injectable() | ||
export class NzModalControlService { | ||
// Track singleton afterAllClose through over the injection tree | ||
get afterAllClose(): Subject<void> { | ||
return this.parentService ? this.parentService.afterAllClose : this.rootAfterAllClose; | ||
} | ||
// Track singleton openModals array through over the injection tree | ||
get openModals(): NzModalRef[] { | ||
return this.parentService ? this.parentService.openModals : this.rootOpenModals; | ||
} | ||
|
||
private rootOpenModals: NzModalRef[] = this.parentService ? null : []; | ||
private rootAfterAllClose: Subject<void> = this.parentService ? null : new Subject<void>(); | ||
|
||
private rootRegisteredMetaMap: Map<NzModalRef, RegisteredMeta> = this.parentService ? null : new Map(); | ||
private get registeredMetaMap(): Map<NzModalRef, RegisteredMeta> { // Registered modal for later usage | ||
return this.parentService ? this.parentService.registeredMetaMap : this.rootRegisteredMetaMap; | ||
} | ||
|
||
constructor( | ||
@Optional() @SkipSelf() private parentService: NzModalControlService) {} | ||
|
||
// Register a modal to listen its open/close | ||
registerModal(modalRef: NzModalRef): void { | ||
if (!this.hasRegistered(modalRef)) { | ||
const afterOpenSubscription = modalRef.afterOpen.subscribe(() => this.openModals.push(modalRef)); | ||
const afterCloseSubscription = modalRef.afterClose.subscribe(() => this.removeOpenModal(modalRef)); | ||
|
||
this.registeredMetaMap.set(modalRef, { modalRef, afterOpenSubscription, afterCloseSubscription }); | ||
} | ||
} | ||
|
||
// TODO: allow deregister modals | ||
// deregisterModal(modalRef: NzModalRef): void {} | ||
|
||
hasRegistered(modalRef: NzModalRef): boolean { | ||
return this.registeredMetaMap.has(modalRef); | ||
} | ||
|
||
// Close all registered opened modals | ||
closeAll(): void { | ||
let i = this.openModals.length; | ||
|
||
while (i--) { | ||
this.openModals[i].close(); | ||
} | ||
} | ||
|
||
private removeOpenModal(modalRef: NzModalRef): void { | ||
const index = this.openModals.indexOf(modalRef); | ||
|
||
if (index > -1) { | ||
this.openModals.splice(index, 1); | ||
|
||
if (!this.openModals.length) { | ||
this.afterAllClose.next(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.