-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
nz-modal.component.ts
430 lines (364 loc) · 14.9 KB
/
nz-modal.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import { Overlay, OverlayRef } from '@angular/cdk/overlay';
import { DOCUMENT } from '@angular/common';
import {
AfterViewInit,
Component,
ComponentFactoryResolver,
ComponentRef,
ElementRef,
EventEmitter,
Inject,
Injector,
Input,
OnChanges,
OnDestroy,
OnInit,
Output,
Renderer2,
SimpleChanges,
TemplateRef,
Type,
ViewChild,
ViewContainerRef
} from '@angular/core';
import { Observable, Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { NzMeasureScrollbarService } from '../core/services/nz-measure-scrollbar.service';
import { InputBoolean } from '../core/util/convert';
import { NzI18nService } from '../i18n/nz-i18n.service';
import ModalUtil from './modal-util';
import { NzModalControlService } from './nz-modal-control.service';
import { NzModalRef } from './nz-modal-ref.class';
import { ModalButtonOptions, ModalOptions, ModalType, OnClickCallback } from './nz-modal.type';
export const MODAL_ANIMATE_DURATION = 200; // Duration when perform animations (ms)
interface ClassMap {
[ index: string ]: boolean;
}
type AnimationState = 'enter' | 'leave' | null;
@Component({
selector : 'nz-modal',
templateUrl: './nz-modal.component.html'
})
// tslint:disable-next-line:no-any
export class NzModalComponent<T = any, R = any> extends NzModalRef<T, R> implements OnInit, OnChanges, AfterViewInit, OnDestroy, ModalOptions {
private unsubscribe$ = new Subject<void>();
// tslint:disable-next-line:no-any
locale: any = {};
@Input() nzModalType: ModalType = 'default';
@Input() nzContent: string | TemplateRef<{}> | Type<T>; // [STATIC] If not specified, will use <ng-content>
@Input() nzComponentParams: object; // [STATIC] ONLY avaliable when nzContent is a component
@Input() nzFooter: string | TemplateRef<{}> | Array<ModalButtonOptions<T>>; // [STATIC] Default Modal ONLY
@Input() nzGetContainer: HTMLElement | OverlayRef | (() => HTMLElement | OverlayRef) = () => this.overlay.create(); // [STATIC]
@Input() @InputBoolean() nzVisible: boolean = false;
@Output() nzVisibleChange = new EventEmitter<boolean>();
@Input() nzZIndex: number = 1000;
@Input() nzWidth: number | string = 520;
@Input() nzWrapClassName: string;
@Input() nzClassName: string;
@Input() nzStyle: object;
@Input() nzIconType: string = 'question-circle'; // Confirm Modal ONLY
@Input() nzTitle: string | TemplateRef<{}>;
@Input() @InputBoolean() nzClosable: boolean = true;
@Input() @InputBoolean() nzMask: boolean = true;
@Input() @InputBoolean() nzMaskClosable: boolean = true;
@Input() nzMaskStyle: object;
@Input() nzBodyStyle: object;
@Output() nzAfterOpen = new EventEmitter<void>(); // Trigger when modal open(visible) after animations
@Output() nzAfterClose = new EventEmitter<R>(); // Trigger when modal leave-animation over
get afterOpen(): Observable<void> { // Observable alias for nzAfterOpen
return this.nzAfterOpen.asObservable();
}
get afterClose(): Observable<R> { // Observable alias for nzAfterClose
return this.nzAfterClose.asObservable();
}
// --- Predefined OK & Cancel buttons
@Input() nzOkText: string;
get okText(): string {
return this.nzOkText || this.locale.okText;
}
@Input() nzOkType = 'primary';
@Input() @InputBoolean() nzOkLoading: boolean = false;
@Input() @Output() nzOnOk: EventEmitter<T> | OnClickCallback<T> = new EventEmitter<T>();
@ViewChild('autoFocusButtonOk', { read: ElementRef }) autoFocusButtonOk: ElementRef; // Only aim to focus the ok button that needs to be auto focused
@Input() nzCancelText: string;
get cancelText(): string {
return this.nzCancelText || this.locale.cancelText;
}
@Input() @InputBoolean() nzCancelLoading: boolean = false;
@Input() @Output() nzOnCancel: EventEmitter<T> | OnClickCallback<T> = new EventEmitter<T>();
@ViewChild('modalContainer') modalContainer: ElementRef;
@ViewChild('bodyContainer', { read: ViewContainerRef }) bodyContainer: ViewContainerRef;
get hidden(): boolean {
return !this.nzVisible && !this.animationState;
} // Indicate whether this dialog should hidden
maskAnimationClassMap: object;
modalAnimationClassMap: object;
transformOrigin = '0px 0px 0px'; // The origin point that animation based on
private contentComponentRef: ComponentRef<T>; // Handle the reference when using nzContent as Component
private animationState: AnimationState; // Current animation state
private container: HTMLElement | OverlayRef;
constructor(
private overlay: Overlay,
private i18n: NzI18nService,
private renderer: Renderer2,
private cfr: ComponentFactoryResolver,
private elementRef: ElementRef,
private viewContainer: ViewContainerRef,
private nzMeasureScrollbarService: NzMeasureScrollbarService,
private modalControl: NzModalControlService,
@Inject(DOCUMENT) private document: any) { // tslint:disable-line:no-any
super();
}
ngOnInit(): void {
this.i18n.localeChange.pipe(takeUntil(this.unsubscribe$)).subscribe(() => this.locale = this.i18n.getLocaleData('Modal'));
if (this.isComponent(this.nzContent)) {
this.createDynamicComponent(this.nzContent as Type<T>); // Create component along without View
}
if (this.isModalButtons(this.nzFooter)) { // Setup default button options
this.nzFooter = this.formatModalButtons(this.nzFooter as Array<ModalButtonOptions<T>>);
}
// Place the modal dom to elsewhere
this.container = typeof this.nzGetContainer === 'function' ? this.nzGetContainer() : this.nzGetContainer;
if (this.container instanceof HTMLElement) {
this.container.appendChild(this.elementRef.nativeElement);
} else if (this.container instanceof OverlayRef) { // NOTE: only attach the dom to overlay, the view container is not changed actually
this.container.overlayElement.appendChild(this.elementRef.nativeElement);
}
// Register modal when afterOpen/afterClose is stable
this.modalControl.registerModal(this);
}
// [NOTE] NOT available when using by service!
// Because ngOnChanges never be called when using by service,
// here we can't support "nzContent"(Component) etc. as inputs that initialized dynamically.
// BUT: User also can change "nzContent" dynamically to trigger UI changes (provided you don't use Component that needs initializations)
ngOnChanges(changes: SimpleChanges): void {
if (changes.nzVisible) {
this.handleVisibleStateChange(this.nzVisible, !changes.nzVisible.firstChange); // Do not trigger animation while initializing
}
}
ngAfterViewInit(): void {
// If using Component, it is the time to attach View while bodyContainer is ready
if (this.contentComponentRef) {
this.bodyContainer.insert(this.contentComponentRef.hostView);
}
if (this.autoFocusButtonOk) {
(this.autoFocusButtonOk.nativeElement as HTMLButtonElement).focus();
}
}
ngOnDestroy(): void {
if (this.container instanceof OverlayRef) {
this.container.dispose();
}
this.unsubscribe$.next();
this.unsubscribe$.complete();
}
open(): void {
this.changeVisibleFromInside(true);
}
close(result?: R): void {
this.changeVisibleFromInside(false, result);
}
destroy(result?: R): void { // Destroy equals Close
this.close(result);
}
triggerOk(): void {
this.onClickOkCancel('ok');
}
triggerCancel(): void {
this.onClickOkCancel('cancel');
}
getInstance(): NzModalComponent {
return this;
}
getContentComponentRef(): ComponentRef<T> {
return this.contentComponentRef;
}
getContentComponent(): T {
return this.contentComponentRef && this.contentComponentRef.instance;
}
getElement(): HTMLElement {
return this.elementRef && this.elementRef.nativeElement;
}
onClickMask($event: MouseEvent): void {
if (
this.nzMask &&
this.nzMaskClosable &&
($event.target as HTMLElement).classList.contains('ant-modal-wrap') &&
this.nzVisible
) {
this.onClickOkCancel('cancel');
}
}
isModalType(type: ModalType): boolean {
return this.nzModalType === type;
}
private onClickCloseBtn(): void {
if (this.nzVisible) {
this.onClickOkCancel('cancel');
}
}
private onClickOkCancel(type: 'ok' | 'cancel'): void {
const trigger = { 'ok': this.nzOnOk, 'cancel': this.nzOnCancel }[ type ];
const loadingKey = { 'ok': 'nzOkLoading', 'cancel': 'nzCancelLoading' }[ type ];
if (trigger instanceof EventEmitter) {
trigger.emit(this.getContentComponent());
} else if (typeof trigger === 'function') {
const result = trigger(this.getContentComponent());
const caseClose = (doClose: boolean | void | {}) => (doClose !== false) && this.close(doClose as R); // Users can return "false" to prevent closing by default
if (isPromise(result)) {
this[ loadingKey ] = true;
const handleThen = (doClose) => {
this[ loadingKey ] = false;
caseClose(doClose);
};
(result as Promise<void>).then(handleThen).catch(handleThen);
} else {
caseClose(result);
}
}
}
private isNonEmptyString(value: {}): boolean {
return typeof value === 'string' && value !== '';
}
private isTemplateRef(value: {}): boolean {
return value instanceof TemplateRef;
}
private isComponent(value: {}): boolean {
return value instanceof Type;
}
private isModalButtons(value: {}): boolean {
return Array.isArray(value) && value.length > 0;
}
// Do rest things when visible state changed
private handleVisibleStateChange(visible: boolean, animation: boolean = true, closeResult?: R): Promise<void> {
if (visible) { // Hide scrollbar at the first time when shown up
this.changeBodyOverflow(1);
}
return Promise
.resolve(animation && this.animateTo(visible))
.then(() => { // Emit open/close event after animations over
if (visible) {
this.nzAfterOpen.emit();
} else {
this.nzAfterClose.emit(closeResult);
this.changeBodyOverflow(); // Show/hide scrollbar when animation is over
}
});
// .then(() => this.changeBodyOverflow());
}
// Lookup a button's property, if the prop is a function, call & then return the result, otherwise, return itself.
private getButtonCallableProp(options: ModalButtonOptions<T>, prop: string): {} {
const value = options[ prop ];
const args = [];
if (this.contentComponentRef) {
args.push(this.contentComponentRef.instance);
}
return typeof value === 'function' ? value.apply(options, args) : value;
}
// On nzFooter's modal button click
private onButtonClick(button: ModalButtonOptions<T>): void {
const result = this.getButtonCallableProp(button, 'onClick'); // Call onClick directly
if (isPromise(result)) {
button.loading = true;
(result as Promise<{}>).then(() => button.loading = false).catch(() => button.loading = false);
}
}
// Change nzVisible from inside
private changeVisibleFromInside(visible: boolean, closeResult?: R): Promise<void> {
if (this.nzVisible !== visible) {
// Change nzVisible value immediately
this.nzVisible = visible;
this.nzVisibleChange.emit(visible);
return this.handleVisibleStateChange(visible, true, closeResult);
}
return Promise.resolve();
}
private changeAnimationState(state: AnimationState): void {
this.animationState = state;
if (state) {
this.maskAnimationClassMap = {
[ `fade-${state}` ] : true,
[ `fade-${state}-active` ]: true
};
this.modalAnimationClassMap = {
[ `zoom-${state}` ] : true,
[ `zoom-${state}-active` ]: true
};
} else {
this.maskAnimationClassMap = this.modalAnimationClassMap = null;
}
}
private animateTo(isVisible: boolean): Promise<void> {
if (isVisible) { // Figure out the lastest click position when shows up
window.setTimeout(() => this.updateTransformOrigin()); // [NOTE] Using timeout due to the document.click event is fired later than visible change, so if not postponed to next event-loop, we can't get the lastest click position
}
this.changeAnimationState(isVisible ? 'enter' : 'leave');
return new Promise((resolve) => window.setTimeout(() => { // Return when animation is over
this.changeAnimationState(null);
resolve();
}, MODAL_ANIMATE_DURATION));
}
private formatModalButtons(buttons: Array<ModalButtonOptions<T>>): Array<ModalButtonOptions<T>> {
return buttons.map((button) => {
const mixedButton = {
...{
type : 'default',
size : 'default',
autoLoading: true,
show : true,
loading : false,
disabled : false
},
...button
};
// if (mixedButton.autoLoading) { mixedButton.loading = false; } // Force loading to false when autoLoading=true
return mixedButton;
});
}
/**
* Create a component dynamically but not attach to any View (this action will be executed when bodyContainer is ready)
* @param component Component class
*/
private createDynamicComponent(component: Type<T>): void {
const factory = this.cfr.resolveComponentFactory(component);
const childInjector = Injector.create({
providers: [ { provide: NzModalRef, useValue: this } ],
parent : this.viewContainer.parentInjector
});
this.contentComponentRef = factory.create(childInjector);
if (this.nzComponentParams) {
Object.assign(this.contentComponentRef.instance, this.nzComponentParams);
}
// Do the first change detection immediately (or we do detection at ngAfterViewInit, multi-changes error will be thrown)
this.contentComponentRef.changeDetectorRef.detectChanges();
}
// Update transform-origin to the last click position on document
private updateTransformOrigin(): void {
const modalElement = this.modalContainer.nativeElement as HTMLElement;
const lastPosition = ModalUtil.getLastClickPosition();
if (lastPosition) {
this.transformOrigin = `${lastPosition.x - modalElement.offsetLeft}px ${lastPosition.y - modalElement.offsetTop}px 0px`;
}
// else {
// this.transformOrigin = '0px 0px 0px';
// }
}
/**
* Take care of the body's overflow to decide the existense of scrollbar
* @param plusNum The number that the openModals.length will increase soon
*/
private changeBodyOverflow(plusNum: number = 0): void {
const openModals = this.modalControl.openModals;
if (openModals.length + plusNum > 0) {
this.renderer.setStyle(this.document.body, 'padding-right', `${this.nzMeasureScrollbarService.scrollBarWidth}px`);
this.renderer.setStyle(this.document.body, 'overflow', 'hidden');
} else {
this.renderer.removeStyle(this.document.body, 'padding-right');
this.renderer.removeStyle(this.document.body, 'overflow');
}
}
}
////////////
function isPromise(obj: {} | void): boolean {
return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof (obj as Promise<{}>).then === 'function' && typeof (obj as Promise<{}>).catch === 'function';
}