-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
progress.js
170 lines (141 loc) · 3.88 KB
/
progress.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
/*
Copyright © 2018 Andrew Powell, Matheus Gonçalves da Silva
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of this Source Code Form.
*/
const { addCss, addHtml } = require('./util');
const ns = 'wps-progress';
const css = `
#${ns}{
width: 200px;
height: 200px;
position: fixed;
right: 5%;
top: 5%;
transition: opacity .25s ease-in-out;
z-index: 2147483645;
}
#${ns}-bg {
fill: #282d35;
}
#${ns}-fill {
fill: rgba(0, 0, 0, 0);
stroke: rgb(186, 223, 172);
stroke-dasharray: 219.99078369140625;
stroke-dashoffset: -219.99078369140625;
stroke-width: 10;
transform: rotate(90deg)translate(0px, -80px);
}
#${ns}-percent {
font-family: 'Open Sans';
font-size: 18px;
fill: #ffffff;
}
#${ns}-percent-value {
dominant-baseline: middle;
text-anchor: middle;
}
#${ns}-percent-super {
fill: #bdc3c7;
font-size: .45em;
baseline-shift: 10%;
}
.${ns}-noselect {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
cursor: default;
}
@keyframes ${ns}-fade {
0% {
opacity: 1;
transform: scale(1);
-webkit-transform: scale(1);
}
100% {
opacity: 0;
transform: scale(0);
-webkit-transform: scale(0);
}
}
.${ns}-disappear {
animation: ${ns}-fade .3s;
animation-fill-mode:forwards;
animation-delay: .5s;
}
.${ns}-hidden {
display: none;
}
/* Put google web font at the end, or you'll see FOUC in Firefox */
@import url('https://fonts.googleapis.com/css?family=Open+Sans:400,700');
`;
const html = `
<svg id="${ns}" class="${ns}-noselect ${ns}-hidden" x="0px" y="0px" viewBox="0 0 80 80">
<circle id="${ns}-bg" cx="50%" cy="50%" r="35"></circle>
<path id="${ns}-fill" d="M5,40a35,35 0 1,0 70,0a35,35 0 1,0 -70,0" />
<text id="${ns}-percent" x="50%" y="51%"><tspan id="${ns}-percent-value">0</tspan><tspan id="${ns}-percent-super">%</tspan></text>
</svg>
`;
let hideOnPageVisible = false;
const update = (percent) => {
const max = -219.99078369140625;
const value = document.querySelector(`#${ns}-percent-value`);
const track = document.querySelector(`#${ns}-fill`);
const offset = ((100 - percent) / 100) * max;
track.setAttribute('style', `stroke-dashoffset: ${offset}`);
value.innerHTML = percent.toString();
};
const reset = (svg) => {
svg.classList.add(`${ns}-disappear`);
};
const init = (options, socket) => {
if (options.firstInstance) {
document.addEventListener('DOMContentLoaded', () => {
addCss(css);
addHtml(html);
// Reset progress to zero after disappear animation
const svg = document.querySelector(`#${ns}`);
svg.addEventListener('animationend', () => {
update(0);
svg.classList.add(`${ns}-hidden`);
});
});
document.addEventListener('visibilitychange', () => {
if (!document.hidden && hideOnPageVisible) {
const svg = document.querySelector(`#${ns}`);
reset(svg);
hideOnPageVisible = false;
}
});
}
socket.addEventListener('message', (message) => {
const { action, data } = JSON.parse(message.data);
if (action !== 'progress') {
return;
}
const percent = Math.floor(data.percent * 100);
const svg = document.querySelector(`#${ns}`);
if (!svg) {
return;
}
// we can safely call this even if it doesn't have the class
svg.classList.remove(`${ns}-disappear`, `${ns}-hidden`);
if (data.percent === 1) {
if (document.hidden) {
hideOnPageVisible = true;
} else {
reset(svg);
}
} else {
hideOnPageVisible = false;
}
update(percent);
});
};
module.exports = { init };