-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathprogress.component.ts
More file actions
422 lines (374 loc) · 13.5 KB
/
Copy pathprogress.component.ts
File metadata and controls
422 lines (374 loc) · 13.5 KB
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
/**
* 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, Directionality } from '@angular/cdk/bidi';
import { NgTemplateOutlet } from '@angular/common';
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
Input,
OnChanges,
OnInit,
SimpleChanges,
ViewEncapsulation,
numberAttribute,
inject,
DestroyRef
} from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { NzConfigKey, onConfigChangeEventForComponent, WithConfig } from 'ng-zorro-antd/core/config';
import { NzOutletModule } from 'ng-zorro-antd/core/outlet';
import { NgStyleInterface } from 'ng-zorro-antd/core/types';
import { isNotNil, numberAttributeWithZeroFallback } from 'ng-zorro-antd/core/util';
import { NzIconModule } from 'ng-zorro-antd/icon';
import {
NzProgressCirclePath,
NzProgressColorGradient,
NzProgressFormatter,
NzProgressGapPositionType,
NzProgressGradientProgress,
NzProgressStatusType,
NzProgressStepItem,
NzProgressStrokeColorType,
NzProgressStrokeLinecapType,
NzProgressTypeType
} from './typings';
import { handleCircleGradient, handleLinearGradient } from './utils';
let gradientIdSeed = 0;
const NZ_CONFIG_MODULE_NAME: NzConfigKey = 'progress';
const statusIconNameMap = new Map([
['success', 'check'],
['exception', 'close']
]);
const statusColorMap = new Map([
['normal', '#108ee9'],
['exception', '#ff5500'],
['success', '#87d068']
]);
const defaultFormatter: NzProgressFormatter = (p: number): string => `${p}%`;
@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
selector: 'nz-progress',
exportAs: 'nzProgress',
imports: [NzIconModule, NzOutletModule, NgTemplateOutlet],
template: `
<ng-template #progressInfoTemplate>
@if (nzShowInfo) {
<span class="ant-progress-text">
@if ((status === 'exception' || status === 'success') && !nzFormat) {
<nz-icon [nzType]="icon" />
} @else {
<ng-container *nzStringTemplateOutlet="formatter; context: { $implicit: nzPercent }; let formatter">
{{ formatter(nzPercent) }}
</ng-container>
}
</span>
}
</ng-template>
<div
[class]="'ant-progress ant-progress-status-' + status"
[class.ant-progress-line]="nzType === 'line'"
[class.ant-progress-small]="nzSize === 'small'"
[class.ant-progress-default]="nzSize === 'default'"
[class.ant-progress-show-info]="nzShowInfo"
[class.ant-progress-circle]="isCircleStyle"
[class.ant-progress-steps]="isSteps"
[class.ant-progress-rtl]="dir === 'rtl'"
[class.ant-progress-indeterminate]="nzIndeterminate && nzType === 'line'"
>
@if (nzType === 'line') {
<div>
<!-- normal line style -->
@if (isSteps) {
<div class="ant-progress-steps-outer">
@for (step of steps; track $index) {
<div class="ant-progress-steps-item" [style]="step"></div>
}
<ng-template [ngTemplateOutlet]="progressInfoTemplate" />
</div>
} @else {
<div class="ant-progress-outer">
<div class="ant-progress-inner">
<div
class="ant-progress-bg"
[style.width.%]="nzIndeterminate ? 100 : nzPercent"
[style.border-radius]="nzStrokeLinecap === 'round' ? '100px' : '0'"
[style.background]="!isGradient ? nzStrokeColor : null"
[style.background-image]="isGradient ? lineGradient : null"
[style.height.px]="strokeWidth"
></div>
@if (nzSuccessPercent || nzSuccessPercent === 0) {
<div
class="ant-progress-success-bg"
[style.width.%]="nzSuccessPercent"
[style.border-radius]="nzStrokeLinecap === 'round' ? '100px' : '0'"
[style.height.px]="strokeWidth"
></div>
}
</div>
</div>
<ng-template [ngTemplateOutlet]="progressInfoTemplate" />
}
</div>
}
<!-- line progress -->
<!-- circle / dashboard progress -->
@if (isCircleStyle) {
<div
[style.width.px]="this.nzWidth"
[style.height.px]="this.nzWidth"
[style.fontSize.px]="this.nzWidth * 0.15 + 6"
class="ant-progress-inner"
[class.ant-progress-circle-gradient]="isGradient"
>
<svg class="ant-progress-circle " viewBox="0 0 100 100">
@if (isGradient) {
<defs>
<linearGradient [id]="'gradient-' + gradientId" x1="100%" y1="0%" x2="0%" y2="0%">
@for (i of circleGradient; track $index) {
<stop [attr.offset]="i.offset" [attr.stop-color]="i.color"></stop>
}
</linearGradient>
</defs>
}
<path
class="ant-progress-circle-trail"
stroke="#f3f3f3"
fill-opacity="0"
[attr.stroke-width]="strokeWidth"
[attr.d]="pathString"
[style]="trailPathStyle"
></path>
@for (p of progressCirclePath; track $index) {
<path
class="ant-progress-circle-path"
fill-opacity="0"
[attr.d]="pathString"
[attr.stroke-linecap]="nzStrokeLinecap"
[attr.stroke]="p.stroke"
[attr.stroke-width]="nzPercent ? strokeWidth : 0"
[style]="p.strokePathStyle"
></path>
}
</svg>
<ng-template [ngTemplateOutlet]="progressInfoTemplate" />
</div>
}
</div>
`
})
export class NzProgressComponent implements OnChanges, OnInit {
readonly _nzModuleName: NzConfigKey = NZ_CONFIG_MODULE_NAME;
private readonly cdr = inject(ChangeDetectorRef);
private readonly directionality = inject(Directionality);
private readonly destroyRef = inject(DestroyRef);
@Input() @WithConfig() nzShowInfo: boolean = true;
@Input() nzWidth = 132;
@Input() @WithConfig() nzStrokeColor?: NzProgressStrokeColorType = undefined;
@Input() @WithConfig() nzSize: 'default' | 'small' = 'default';
@Input() nzFormat?: NzProgressFormatter;
@Input({ transform: numberAttributeWithZeroFallback }) nzSuccessPercent?: number;
@Input({ transform: numberAttribute }) nzPercent: number = 0;
@Input({ transform: numberAttributeWithZeroFallback }) @WithConfig() nzStrokeWidth?: number;
@Input({ transform: numberAttributeWithZeroFallback }) @WithConfig() nzGapDegree?: number;
@Input() nzStatus?: NzProgressStatusType;
@Input() nzType: NzProgressTypeType = 'line';
@Input() @WithConfig() nzGapPosition: NzProgressGapPositionType = 'top';
@Input() @WithConfig() nzStrokeLinecap: NzProgressStrokeLinecapType = 'round';
@Input({ transform: numberAttribute }) nzSteps: number = 0;
@Input() nzIndeterminate: boolean = false;
steps: NzProgressStepItem[] = [];
/** Gradient style when `nzType` is `line`. */
lineGradient: string | null = null;
/** If user uses gradient color. */
isGradient = false;
/** If the linear progress is a step progress. */
isSteps = false;
/**
* Each progress whose `nzType` is circle or dashboard should have unique id to
* define `<linearGradient>`.
*/
gradientId = gradientIdSeed++;
/** Paths to rendered in the template. */
progressCirclePath: NzProgressCirclePath[] = [];
circleGradient?: Array<{ offset: string; color: string }>;
trailPathStyle: NgStyleInterface | null = null;
pathString?: string;
icon!: string;
dir: Direction = 'ltr';
get formatter(): NzProgressFormatter {
return this.nzFormat || defaultFormatter;
}
get status(): NzProgressStatusType {
return this.nzStatus || this.inferredStatus;
}
get strokeWidth(): number {
return this.nzStrokeWidth || (this.nzType === 'line' && this.nzSize !== 'small' ? 8 : 6);
}
get isCircleStyle(): boolean {
return this.nzType === 'circle' || this.nzType === 'dashboard';
}
private cachedStatus: NzProgressStatusType = 'normal';
private inferredStatus: NzProgressStatusType = 'normal';
constructor() {
onConfigChangeEventForComponent(NZ_CONFIG_MODULE_NAME, () => {
this.updateIcon();
this.setStrokeColor();
this.getCirclePaths();
});
}
ngOnChanges(changes: SimpleChanges): void {
const {
nzSteps,
nzGapPosition,
nzStrokeLinecap,
nzStrokeColor,
nzGapDegree,
nzType,
nzStatus,
nzPercent,
nzSuccessPercent,
nzStrokeWidth
} = changes;
if (nzStatus) {
this.cachedStatus = this.nzStatus || this.cachedStatus;
}
if (nzPercent || nzSuccessPercent) {
const fillAll = parseInt(this.nzPercent.toString(), 10) >= 100;
if (fillAll) {
if ((isNotNil(this.nzSuccessPercent) && this.nzSuccessPercent! >= 100) || this.nzSuccessPercent === undefined) {
this.inferredStatus = 'success';
}
} else {
this.inferredStatus = this.cachedStatus;
}
}
if (nzStatus || nzPercent || nzSuccessPercent || nzStrokeColor) {
this.updateIcon();
}
if (nzStrokeColor) {
this.setStrokeColor();
}
if (nzGapPosition || nzStrokeLinecap || nzGapDegree || nzType || nzPercent || nzStrokeColor || nzStrokeColor) {
this.getCirclePaths();
}
if (nzPercent || nzSteps || nzStrokeWidth) {
this.isSteps = this.nzSteps > 0;
if (this.isSteps) {
this.getSteps();
}
}
}
ngOnInit(): void {
this.directionality.change?.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(direction => {
this.dir = direction;
this.cdr.detectChanges();
});
this.dir = this.directionality.value;
}
private updateIcon(): void {
const ret = statusIconNameMap.get(this.status);
this.icon = ret ? ret + (this.isCircleStyle ? '-o' : '-circle-fill') : '';
}
/**
* Calculate step render configs.
*/
private getSteps(): void {
const current = Math.floor(this.nzSteps * (this.nzPercent / 100));
const stepWidth = this.nzSize === 'small' ? 2 : 14;
const steps = [];
for (let i = 0; i < this.nzSteps; i++) {
let color;
if (i <= current - 1) {
color = this.nzStrokeColor;
}
const stepStyle = {
backgroundColor: `${color}`,
width: `${stepWidth}px`,
height: `${this.strokeWidth}px`
};
steps.push(stepStyle);
}
this.steps = steps;
}
/**
* Calculate paths when the type is circle or dashboard.
*/
private getCirclePaths(): void {
if (!this.isCircleStyle) {
return;
}
const values = isNotNil(this.nzSuccessPercent) ? [this.nzSuccessPercent!, this.nzPercent] : [this.nzPercent];
// Calculate shared styles.
const radius = 50 - this.strokeWidth / 2;
const gapPosition = this.nzGapPosition || (this.nzType === 'circle' ? 'top' : 'bottom');
const len = Math.PI * 2 * radius;
const gapDegree = this.nzGapDegree || (this.nzType === 'circle' ? 0 : 75);
let beginPositionX = 0;
let beginPositionY = -radius;
let endPositionX = 0;
let endPositionY = radius * -2;
switch (gapPosition) {
case 'left':
beginPositionX = -radius;
beginPositionY = 0;
endPositionX = radius * 2;
endPositionY = 0;
break;
case 'right':
beginPositionX = radius;
beginPositionY = 0;
endPositionX = radius * -2;
endPositionY = 0;
break;
case 'bottom':
beginPositionY = radius;
endPositionY = radius * 2;
break;
default:
}
this.pathString = `M 50,50 m ${beginPositionX},${beginPositionY}
a ${radius},${radius} 0 1 1 ${endPositionX},${-endPositionY}
a ${radius},${radius} 0 1 1 ${-endPositionX},${endPositionY}`;
this.trailPathStyle = {
strokeDasharray: `${len - gapDegree}px ${len}px`,
strokeDashoffset: `-${gapDegree / 2}px`,
transition: 'stroke-dashoffset .3s ease 0s, stroke-dasharray .3s ease 0s, stroke .3s'
};
// Calculate styles for each path.
this.progressCirclePath = values
.map((value, index) => {
const isSuccessPercent = values.length === 2 && index === 0;
return {
stroke: this.isGradient && !isSuccessPercent ? `url(#gradient-${this.gradientId})` : null,
strokePathStyle: {
stroke: !this.isGradient
? isSuccessPercent
? statusColorMap.get('success')
: (this.nzStrokeColor as string)
: null,
transition:
'stroke-dashoffset .3s ease 0s, stroke-dasharray .3s ease 0s, stroke .3s, stroke-width .06s ease .3s',
strokeDasharray: `${((value || 0) / 100) * (len - gapDegree)}px ${len}px`,
strokeDashoffset: `-${gapDegree / 2}px`
}
};
})
.reverse();
}
private setStrokeColor(): void {
const color = this.nzStrokeColor;
const isGradient = (this.isGradient = !!color && typeof color !== 'string');
if (isGradient && !this.isCircleStyle) {
this.lineGradient = handleLinearGradient(color as NzProgressColorGradient);
} else if (isGradient && this.isCircleStyle) {
this.circleGradient = handleCircleGradient(this.nzStrokeColor as NzProgressGradientProgress);
} else {
this.lineGradient = null;
this.circleGradient = [];
}
}
}