-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathcircleMixin.js
179 lines (166 loc) · 5.83 KB
/
circleMixin.js
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
import { emptyRadius, fillRadius, radius } from "@/components/Circle/radiusCalculation";
import { isValidNumber } from "@/utils";
const wait = (ms = 400) => new Promise((resolve) => setTimeout(() => resolve(), ms));
export default {
name: "CircleMixin",
props: {
options: {
type: Object,
required: true,
},
},
data: () => ({
isInitialized: false,
isAnimationPlaying: false,
}),
computed: {
progress() {
return parseFloat(this.options.progress || 0);
},
progressOffset() {
const offset = this.circumference - (this.progress / 100) * this.circumference;
if (Math.abs(this.circumference - offset) < 1) return this.circumference - 0.5;
return offset;
},
radius() {
return radius(this.options);
},
fillRadius() {
return fillRadius(this.options.linePosition, this.options.thickness, this.radius);
},
emptyRadius() {
return emptyRadius(this.options);
},
emptyFillRadius() {
return fillRadius(this.options.emptyLinePosition, this.options.emptyThickness, this.emptyRadius);
},
dataIsAvailable() {
return isValidNumber(this.progress) && !this.options.noData;
},
animationClass() {
return [
this.isAnimationPlaying && "ep-animation-playing",
`animation__${
!this.options.loading && this.dataIsAvailable && this.isInitialized ? this.options.animation.type : "none"
}`,
];
},
animationDuration() {
return `${this.options.animation.duration}ms`;
},
color() {
return Array.isArray(this.options.color.colors)
? `url(#ep-progress-gradient-${this.options.uid})`
: this.options.color;
},
emptyColor() {
return Array.isArray(this.options.emptyColor.colors)
? `url(#ep-empty-gradient-${this.options.uid})`
: this.options.emptyColor;
},
colorFill() {
return Array.isArray(this.options.colorFill.colors)
? `url(#ep-progress-fill-gradient-${this.options.uid})`
: this.options.colorFill;
},
emptyColorFill() {
return Array.isArray(this.options.emptyColorFill.colors)
? `url(#ep-empty-fill-gradient-${this.options.uid})`
: this.options.emptyColorFill;
},
angle() {
return isValidNumber(this.options.angle) ? this.options.angle : -90;
},
transformOrigin() {
return "50% 50%";
},
emptyDasharray() {
if (!this.options.dash.count || !this.options.dash.spacing) {
return this.options.dash;
}
return `${2 * Math.PI * this.emptyRadius * this.getDashPercent()},
${2 * Math.PI * this.emptyRadius * this.getDashSpacingPercent()}`.trim();
},
strokeDashOffset() {
return this.dataIsAvailable && !this.options.loading && this.isInitialized
? this.progressOffset
: this.circumference;
},
styles() {
return {
transition: `${this.animationDuration}, opacity 0.3s`,
strokeDashoffset: this.strokeDashOffset,
transitionTimingFunction: "ease-in-out",
transformOrigin: this.transformOrigin,
opacity: this.options.loading || !this.dataIsAvailable ? 0 : 1,
"--ep-circumference": this.circumference,
"--ep-negative-circumference": this.getNegativeCircumference(),
"--ep-double-circumference": this.getDoubleCircumference(),
"--ep-stroke-offset": this.progressOffset,
"--ep-loop-stroke-offset": this.getLoopOffset(),
"--ep-bounce-out-stroke-offset": this.getBounceOutOffset(),
"--ep-bounce-in-stroke-offset": this.getBounceInOffset(),
"--ep-reverse-stroke-offset": this.getReverseOffset(),
"--ep-loading-stroke-offset": this.circumference * 0.2,
"animation-duration": this.animationDuration,
};
},
isLoading() {
return (this.options.determinate || this.options.loading) && this.dataIsAvailable;
},
},
methods: {
getDashSpacingPercent() {
return this.options.dash.spacing / this.options.dash.count;
},
getDashPercent() {
return (1 - this.options.dash.spacing) / this.options.dash.count;
},
/* Animations helper Methods */
getNegativeCircumference() {
return this.circumference * -1;
},
getDoubleCircumference() {
return this.circumference * 2;
},
getLoopOffset() {
return this.getNegativeCircumference() - (this.circumference - this.progressOffset);
},
getReverseOffset() {
return this.getDoubleCircumference() + this.progressOffset;
},
getBounceOutOffset() {
return this.progressOffset < 100 ? 0 : this.progressOffset - 100;
},
getBounceInOffset() {
return this.circumference - this.progressOffset < 100 ? this.progressOffset : this.progressOffset + 100;
},
toggleIsAnimationPlaying() {
this.$nextTick(() => {
this.isAnimationPlaying = !this.isAnimationPlaying;
});
},
},
async mounted() {
const circle = this.$refs.circleProgress;
if (circle) {
// this is only required for older MacOS/IOS versions and Safari. On Apple system the transition is triggered
// right after initial animation causing the progress line rendered twice. So we track animation state to
// add/remove CSS transition properties
circle.addEventListener("animationstart", this.toggleIsAnimationPlaying, false);
circle.addEventListener("animationend", this.toggleIsAnimationPlaying, false);
}
if (!this.options.loading) {
// await initial delay before applying animations
await wait(this.options.animation.delay);
}
this.isInitialized = true;
},
unmounted() {
const circle = this.$refs.circleProgress;
if (circle) {
circle.removeEventListener("animationstart", this.toggleIsAnimationPlaying, false);
circle.removeEventListener("animationend", this.toggleIsAnimationPlaying, false);
}
},
};