-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
notification-container.component.ts
248 lines (229 loc) · 8.07 KB
/
notification-container.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
/**
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/
import { Direction } from '@angular/cdk/bidi';
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, ViewEncapsulation } from '@angular/core';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { NotificationConfig, NzConfigService } from 'ng-zorro-antd/core/config';
import { toCssPixel } from 'ng-zorro-antd/core/util';
import { NzMNContainerComponent } from 'ng-zorro-antd/message';
import { NzNotificationData, NzNotificationDataOptions, NzNotificationPlacement } from './typings';
const NZ_CONFIG_MODULE_NAME = 'notification';
const NZ_NOTIFICATION_DEFAULT_CONFIG: Required<NotificationConfig> = {
nzTop: '24px',
nzBottom: '24px',
nzPlacement: 'topRight',
nzDuration: 4500,
nzMaxStack: 7,
nzPauseOnHover: true,
nzAnimate: true,
nzDirection: 'ltr'
};
@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
selector: 'nz-notification-container',
exportAs: 'nzNotificationContainer',
preserveWhitespaces: false,
template: `
<div
class="ant-notification ant-notification-topLeft"
[class.ant-notification-rtl]="dir === 'rtl'"
[style.top]="top"
[style.left]="'0px'"
>
<nz-notification
*ngFor="let instance of topLeftInstances"
[instance]="instance"
[placement]="'topLeft'"
(destroyed)="remove($event.id, $event.userAction)"
></nz-notification>
</div>
<div
class="ant-notification ant-notification-topRight"
[class.ant-notification-rtl]="dir === 'rtl'"
[style.top]="top"
[style.right]="'0px'"
>
<nz-notification
*ngFor="let instance of topRightInstances"
[instance]="instance"
[placement]="'topRight'"
(destroyed)="remove($event.id, $event.userAction)"
></nz-notification>
</div>
<div
class="ant-notification ant-notification-bottomLeft"
[class.ant-notification-rtl]="dir === 'rtl'"
[style.bottom]="bottom"
[style.left]="'0px'"
>
<nz-notification
*ngFor="let instance of bottomLeftInstances"
[instance]="instance"
[placement]="'bottomLeft'"
(destroyed)="remove($event.id, $event.userAction)"
></nz-notification>
</div>
<div
class="ant-notification ant-notification-bottomRight"
[class.ant-notification-rtl]="dir === 'rtl'"
[style.bottom]="bottom"
[style.right]="'0px'"
>
<nz-notification
*ngFor="let instance of bottomRightInstances"
[instance]="instance"
[placement]="'bottomRight'"
(destroyed)="remove($event.id, $event.userAction)"
></nz-notification>
</div>
<div
class="ant-notification ant-notification-top"
[class.ant-notification-rtl]="dir === 'rtl'"
[style.top]="top"
[style.left]="'50%'"
[style.transform]="'translateX(-50%)'"
>
<nz-notification
*ngFor="let instance of topInstances"
[instance]="instance"
[placement]="'top'"
(destroyed)="remove($event.id, $event.userAction)"
></nz-notification>
</div>
<div
class="ant-notification ant-notification-bottom"
[class.ant-notification-rtl]="dir === 'rtl'"
[style.bottom]="bottom"
[style.left]="'50%'"
[style.transform]="'translateX(-50%)'"
>
<nz-notification
*ngFor="let instance of bottomInstances"
[instance]="instance"
[placement]="'bottom'"
(destroyed)="remove($event.id, $event.userAction)"
></nz-notification>
</div>
`
})
export class NzNotificationContainerComponent extends NzMNContainerComponent {
dir: Direction = 'ltr';
bottom?: string | null;
top?: string | null;
override config!: Required<NotificationConfig>; // initialized by parent class constructor
override instances: Array<Required<NzNotificationData>> = [];
topLeftInstances: Array<Required<NzNotificationData>> = [];
topRightInstances: Array<Required<NzNotificationData>> = [];
bottomLeftInstances: Array<Required<NzNotificationData>> = [];
bottomRightInstances: Array<Required<NzNotificationData>> = [];
topInstances: Array<Required<NzNotificationData>> = [];
bottomInstances: Array<Required<NzNotificationData>> = [];
constructor(cdr: ChangeDetectorRef, nzConfigService: NzConfigService) {
super(cdr, nzConfigService);
const config = this.nzConfigService.getConfigForComponent(NZ_CONFIG_MODULE_NAME);
this.dir = config?.nzDirection || 'ltr';
}
override create(notification: NzNotificationData): Required<NzNotificationData> {
const noti = this.onCreate(notification);
const key = noti.options.nzKey;
const notificationWithSameKey = this.instances.find(
msg => msg.options.nzKey === (notification.options as Required<NzNotificationDataOptions>).nzKey
);
if (key && notificationWithSameKey) {
this.replaceNotification(notificationWithSameKey, noti);
} else {
if (this.instances.length >= this.config.nzMaxStack) {
this.instances = this.instances.slice(1);
}
this.instances = [...this.instances, noti];
}
this.readyInstances();
return noti;
}
protected override onCreate(instance: NzNotificationData): Required<NzNotificationData> {
instance.options = this.mergeOptions(instance.options);
instance.onClose = new Subject<boolean>();
instance.onClick = new Subject<MouseEvent>();
return instance as Required<NzNotificationData>;
}
protected subscribeConfigChange(): void {
this.nzConfigService
.getConfigChangeEventForComponent(NZ_CONFIG_MODULE_NAME)
.pipe(takeUntil(this.destroy$))
.subscribe(() => {
this.updateConfig();
const config = this.nzConfigService.getConfigForComponent(NZ_CONFIG_MODULE_NAME);
if (config) {
const { nzDirection } = config;
this.dir = nzDirection || this.dir;
}
});
}
protected updateConfig(): void {
this.config = {
...NZ_NOTIFICATION_DEFAULT_CONFIG,
...this.config,
...this.nzConfigService.getConfigForComponent(NZ_CONFIG_MODULE_NAME)
};
this.top = toCssPixel(this.config.nzTop!);
this.bottom = toCssPixel(this.config.nzBottom!);
this.cdr.markForCheck();
}
private replaceNotification(old: NzNotificationData, _new: NzNotificationData): void {
old.title = _new.title;
old.content = _new.content;
old.template = _new.template;
old.type = _new.type;
old.options = _new.options;
}
protected override readyInstances(): void {
const instancesMap: Record<NzNotificationPlacement, Array<Required<NzNotificationData>>> = {
topLeft: [],
topRight: [],
bottomLeft: [],
bottomRight: [],
top: [],
bottom: []
};
this.instances.forEach(m => {
const placement = m.options.nzPlacement;
switch (placement) {
case 'topLeft':
instancesMap.topLeft.push(m);
break;
case 'topRight':
instancesMap.topRight.push(m);
break;
case 'bottomLeft':
instancesMap.bottomLeft.push(m);
break;
case 'bottomRight':
instancesMap.bottomRight.push(m);
break;
case 'top':
instancesMap.top.push(m);
break;
case 'bottom':
instancesMap.bottom.push(m);
break;
default:
instancesMap.topRight.push(m);
}
});
this.topLeftInstances = instancesMap.topLeft;
this.topRightInstances = instancesMap.topRight;
this.bottomLeftInstances = instancesMap.bottomLeft;
this.bottomRightInstances = instancesMap.bottomRight;
this.topInstances = instancesMap.top;
this.bottomInstances = instancesMap.bottom;
this.cdr.detectChanges();
}
protected override mergeOptions(options?: NzNotificationDataOptions): NzNotificationDataOptions {
const { nzDuration, nzAnimate, nzPauseOnHover, nzPlacement } = this.config;
return { nzDuration, nzAnimate, nzPauseOnHover, nzPlacement, ...options };
}
}