-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogress-bar.js
153 lines (131 loc) · 3.5 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
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
// @ts-ignore
const css = `
#wrapper {
width: 100%;
height: 100%;
contain: strict;
background-color: rgba(255, 255, 255, 0.2);
cursor: pointer;
}
#indicator {
width: 100%;
height: 100%;
transform: none;
will-change: transform;
background-color: currentColor;
pointer-events: none;
}
`;
const ErrorMessages = {
NO_DURATION: 'You need to set duration before you can start/stop the progress bar.'
};
export default class ProgressBar extends HTMLElement {
/**
* Renders the progress bar to a shadow DOM, caches references to the wrapper
* && indicator elements and adds a click handler to the wrapper element for
* handling the seeking.
*/
connectedCallback () {
const sDOM = this.attachShadow({ mode: 'closed' });
sDOM.innerHTML = `
<style>${css}</style>
<div id="wrapper">
<div id="indicator"></div>
</div>
`;
this.wrapper = sDOM.querySelector('#wrapper');
this.indicator = sDOM.querySelector('#indicator');
this.wrapper.addEventListener('click', event => {
if (event instanceof MouseEvent) {
this.handleClick(event);
}
}, false);
}
/**
* Handle clicking the progress bar wrapper. Calculate the percentate
* to seek to and emits a progress-bar:seek CustomEvent to be listened
* to outside this component to react to the click.
*
* @param {MouseEvent} event
*/
handleClick (event) {
const percent = this.getMousePositionAsPercent(event);
const detail = { percent };
document.dispatchEvent(new CustomEvent('progress-bar:seek', { detail }));
this.currentTime = this.animationDuration * percent;
}
/**
* Calculates where in the progress bar the person clicked
* in percent.
*
* @param {MouseEvent} mouse
*/
getMousePositionAsPercent (mouse) {
const boundRect = this.wrapper.getBoundingClientRect();
const offset = {
left: Math.abs((boundRect.left + window.pageXOffset) - mouse.pageX),
width: Math.round(boundRect.width)
};
return offset.left / offset.width;
}
/**
* Setting this recrates the animation with the new duraton and pauses the animation.
*/
set duration (duration) {
this.animationDuration = duration * 1000;
/** @type {Keyframe[]} */
const keyframes = ([
{
transform: 'translateX(-100%)'
},
{
transform: 'translateX(0%)'
}
]);
this.animation = this.indicator.animate(keyframes, {
duration: this.animationDuration,
iterations: 1
});
this.animation.pause();
}
/**
* Setting this stops the animations, updates the playback rate and plays it again.
*/
set playbackRate (playbackRate) {
this.stop();
this.animation.playbackRate = playbackRate;
this.start();
}
/**
* Set current time with miliseconds.
* @memberof ProgressBar
*/
set currentTime (currentTime) {
if (this.animation) {
this.animation.currentTime = currentTime;
}
}
/**
* Starts the animation if duration is defined.
*
* @throws Will throw an error if duration is not set.
*/
start () {
if (!this.animation) {
throw new Error(ErrorMessages.NO_DURATION);
}
this.animation.play();
}
/**
* Stops the animation if duration is defined.
*
* @throws Will throw an error if duration is not set.
*/
stop () {
if (!this.animation) {
throw new Error(ErrorMessages.NO_DURATION);
}
this.animation.pause();
}
}
customElements.define('progress-bar', ProgressBar);