-
Notifications
You must be signed in to change notification settings - Fork 407
/
progress_bar.js
127 lines (107 loc) · 2.91 KB
/
progress_bar.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
import { unindent, getMetaContent } from "../../util"
export const ProgressBarID = "turbo-progress-bar"
export class ProgressBar {
static animationDuration = 300 /*ms*/
static get defaultCSS() {
return unindent`
.turbo-progress-bar {
position: fixed;
display: block;
top: 0;
left: 0;
height: 3px;
background: #0076ff;
z-index: 2147483647;
transition:
width ${ProgressBar.animationDuration}ms ease-out,
opacity ${ProgressBar.animationDuration / 2}ms ${ProgressBar.animationDuration / 2}ms ease-in;
transform: translate3d(0, 0, 0);
}
`
}
hiding = false
value = 0
visible = false
constructor() {
this.stylesheetElement = this.createStylesheetElement()
this.progressElement = this.createProgressElement()
this.installStylesheetElement()
this.setValue(0)
}
show() {
if (!this.visible) {
this.visible = true
this.installProgressElement()
this.startTrickling()
}
}
hide() {
if (this.visible && !this.hiding) {
this.hiding = true
this.fadeProgressElement(() => {
this.uninstallProgressElement()
this.stopTrickling()
this.visible = false
this.hiding = false
})
}
}
setValue(value) {
this.value = value
this.refresh()
}
// Private
installStylesheetElement() {
document.head.insertBefore(this.stylesheetElement, document.head.firstChild)
}
installProgressElement() {
this.progressElement.style.width = "0"
this.progressElement.style.opacity = "1"
document.documentElement.insertBefore(this.progressElement, document.body)
this.refresh()
}
fadeProgressElement(callback) {
this.progressElement.style.opacity = "0"
setTimeout(callback, ProgressBar.animationDuration * 1.5)
}
uninstallProgressElement() {
if (this.progressElement.parentNode) {
document.documentElement.removeChild(this.progressElement)
}
}
startTrickling() {
if (!this.trickleInterval) {
this.trickleInterval = window.setInterval(this.trickle, ProgressBar.animationDuration)
}
}
stopTrickling() {
window.clearInterval(this.trickleInterval)
delete this.trickleInterval
}
trickle = () => {
this.setValue(this.value + Math.random() / 100)
}
refresh() {
requestAnimationFrame(() => {
this.progressElement.style.width = `${10 + this.value * 90}%`
})
}
createStylesheetElement() {
const element = document.createElement("style")
element.id = ProgressBarID
element.type = "text/css"
element.textContent = ProgressBar.defaultCSS
if (this.cspNonce) {
element.nonce = this.cspNonce
}
return element
}
createProgressElement() {
const element = document.createElement("div")
element.className = "turbo-progress-bar"
return element
}
get cspNonce() {
return getMetaContent("csp-nonce")
}
}