-
Notifications
You must be signed in to change notification settings - Fork 210
Expand file tree
/
Copy pathprogress.component.ts
More file actions
executable file
·191 lines (174 loc) · 6.15 KB
/
Copy pathprogress.component.ts
File metadata and controls
executable file
·191 lines (174 loc) · 6.15 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
import { AfterViewInit, Component, ContentChildren, Input, OnChanges, QueryList, SimpleChanges, TemplateRef } from '@angular/core';
import { merge } from 'lodash-es';
import { ProgressTemplateDirective } from './progress-template.directive';
import { IGradientColor, IProgressItem, ShowContentConfig } from './progress.types';
@Component({
selector: 'd-progress',
templateUrl: './progress.component.html',
styleUrls: ['./progress.component.scss'],
preserveWhitespaces: false,
standalone: false
})
export class ProgressComponent implements OnChanges, AfterViewInit {
static ID_SEED = 0;
@Input() isDynamic = true;
@Input() percentage = 0;
@Input() percentageText: string;
/**
* @deprecated
* 用strokeColor替换
*/
@Input() barbgcolor: string;
@Input() strokeColor: string | IGradientColor[] = '';
/**
* @deprecated
* 统一用strokeWidth
*/
@Input() set height(value) {
this.strokeWidth = parseInt(value, 10);
}
@Input() strokeWidth = 14; // 进度条的线条宽度
/**
* @deprecated
* 用type类型替换
*/
@Input() set isCircle(value: boolean) {
this.type = value ? 'circle' : 'line';
}
@Input() type: 'line' | 'circle' = 'line';
@Input() showContent: boolean | ShowContentConfig;
@Input() multiProgressConfig: IProgressItem[] = [];
@ContentChildren(ProgressTemplateDirective) templates: QueryList<ProgressTemplateDirective>;
id: number;
pathString: string;
trailPath: { [key: string]: string };
progressData: IProgressItem[] = [];
gradientColor: IGradientColor[] = [];
isGradient = false;
showContentConfig = {
showInnerContent: false,
showOuterContent: false,
showCenterContent: true,
};
centerTemplate: TemplateRef<any>;
outerTemplate: TemplateRef<any>;
get content() {
return this.percentageText ? this.percentageText : `${this.percentage}%`;
}
constructor() {
this.id = ProgressComponent.ID_SEED++;
}
ngOnChanges(changes: SimpleChanges): void {
const { isCircle, multiProgressConfig, percentage, showContent, strokeColor, type } = changes;
const hasChanged = [percentage, strokeColor, type, isCircle].some((change) => change);
if (multiProgressConfig || (this.multiProgressConfig?.length && percentage) || (!this.multiProgressConfig?.length && hasChanged)) {
this.render();
}
if (showContent) {
this.showContentConfig =
typeof this.showContent === 'boolean'
? {
showInnerContent: this.showContent,
showOuterContent: this.showContent,
showCenterContent: false,
}
: { ...this.showContentConfig, ...this.showContent };
}
}
ngAfterViewInit(): void {
// 兼容之前line中自定义模板对应右侧模板,circle对应中间模板
if (this.templates?.length) {
const restItem = [];
this.templates.forEach((item) => {
const pos = item.position || item.dPosition;
const name = pos && `${pos}Template`;
if (name) {
this[name] = item.template;
} else {
restItem.push(item);
}
});
if (restItem.length) {
this.outerTemplate = this.outerTemplate || restItem[0].template;
this.centerTemplate = this.centerTemplate || restItem[restItem.length - 1]?.template;
}
}
}
render(): void {
const data = this.multiProgressConfig?.length
? [...this.multiProgressConfig]
: [
{
color: (this.checkStrokeColor() || this.barbgcolor) ?? '',
percentage: this.percentage ?? 0,
percentageText: this.percentageText ?? '',
content: this.content ?? '',
},
];
if (this.type === 'line') {
this.checkSumOfPercentages(data, (sum: number, item: IProgressItem) => {
item.width = sum;
});
}
if (this.type === 'circle') {
this.setCircleProgress(data);
}
// 展示数据倒序输出,短的后输出,显示层级高
data.reverse();
if (this.isDynamic) {
// merge方式覆盖数据,不改变数据源,避免重渲染dom导致动画失效
merge(this.progressData, data);
} else {
this.progressData = [...data];
}
}
setCircleProgress(data: IProgressItem[]): void {
const radius = 50 - this.strokeWidth / 2;
const beginPositionY = -radius;
const endPositionY = radius * -2;
const len = Math.PI * 2 * radius;
this.pathString = `M 50,50 m 0,${beginPositionY}
a ${radius},${radius} 0 1 1 0,${-endPositionY}
a ${radius},${radius} 0 1 1 0,${endPositionY}`;
this.trailPath = {
strokeDasharray: `${len}px ${len}px`,
strokeDashoffset: `0`,
transition: 'stroke-dashoffset .3s ease 0s, stroke-dasharray .3s ease 0s, stroke .3s',
};
this.checkSumOfPercentages(data, (sum: number, item: IProgressItem) => {
item.strokePath = {
stroke: item.color || this.checkStrokeColor() || this.barbgcolor || (null as any),
strokeDasharray: `${(sum / 100) * len}px ${len}px`,
strokeDashoffset: `0`,
transition: 'stroke-dashoffset .3s ease 0s, stroke-dasharray .3s ease 0s, stroke .3s, stroke-width .06s ease .3s',
};
});
}
checkSumOfPercentages(data: IProgressItem[], func: Function) {
const result = data.reduce((sum: number, item: IProgressItem, index: number) => {
sum += Number(item.percentage);
sum = sum > 100 || (index > 0 && index === data.length - 1 && this.percentage === 100) ? 100 : sum;
func(sum, item);
return sum;
}, 0);
this.percentage = this.percentage >= 100 ? 100 : result;
}
checkStrokeColor(): string {
if (Array.isArray(this.strokeColor)) {
this.isGradient = true;
this.gradientColor = this.strokeColor;
if (this.type === 'line') {
const colors = this.strokeColor.map((item) => `${item.color} ${item.percentage}`).join(', ');
const result = `linear-gradient(to right, ${colors})`;
return this.strokeColor.length ? result : '';
}
if (this.type === 'circle') {
return `url(#devui-progress-gradient-${this.id}`;
}
} else {
this.isGradient = false;
this.strokeColor = this.strokeColor || '';
return this.strokeColor;
}
}
}