-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelfDestruct.js
276 lines (252 loc) · 7.71 KB
/
SelfDestruct.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/**
* Enchanced timer which allows for pause, resetting, and determining (approximate) time left.
*/
class Timer {
#id;
#startedAt;
#msRemaining;
#isRunning;
#callback;
#delay;
constructor(callback, delay) {
this.#msRemaining = delay;
this.#callback = callback;
this.#delay = delay;
this.start();
}
get timeLeft() {
if (this.#isRunning) {
this.pause();
this.start();
}
return this.#msRemaining;
}
get isRunning() {
return this.#isRunning;
}
pause() {
this.#isRunning = false;
clearTimeout(this.#id);
this.#msRemaining -= new Date() - this.#startedAt;
}
reset() {
this.stop();
this.#msRemaining = this.#delay;
}
start() {
this.#isRunning = true;
this.#startedAt = new Date();
this.#id = setTimeout(this.#callback, this.#msRemaining);
}
stop() {
this.#isRunning = false;
clearTimeout(this.#id);
this.#msRemaining = 0;
}
}
class SelfDestruct extends HTMLElement {
/**
* Build a horizontal progress bar.
*/
buildBar(percentage) {
percentage = Math.min(1, Math.max(0, percentage)); // ensure 0 <= percentage <= 1
return `<span class="self-destruct-progress-bar" style="width:${percentage * 100}%;"></span>`;
}
/**
* Build an SVG pie chart.
*/
buildPie(percentage) {
percentage = Math.min(1, Math.max(0, percentage)); // ensure 0 <= percentage <= 1
const x = Math.cos(2 * Math.PI * percentage);
const y = Math.sin(2 * Math.PI * percentage);
const pie = percentage > 0 && percentage < 1 ? `<path d="M1 0A1 1 0 ${percentage > 0.5 ? 1 : 0} 1 ${x} ${y}L0 0" class="self-destruct-svg-fg" fill="#f009" transform="rotate(-90)"/>` : '';
return `<svg width="1em" height="1em" class="self-destruct-svg" viewBox="-1 -1 2 2" aria-hidden="true"><circle r="1" cx="0" cy="0" class="self-destruct-svg-bg" fill="#0000"/>${pie}</svg>`;
}
constructor() {
super();
this.defaultTtl = 5000; // default time to live in ms (seconds * 1000)
this.reduceMotion = window.matchMedia(`(prefers-reduced-motion: reduce)`).matches;
this.progress = null;
}
/**
* Runs when the element is added to the DOM.
*/
connectedCallback() {
// how long until self-destruction?
this.ttl = this.hasAttribute('ttl') ? this.parseTime(this.getAttribute('ttl')) : this.defaultTtl;
// allow animation?
this.animate = !this.reduceMotion && !this.hasAttribute('no-animation') && this.ttl >= 250;
// show a progress indicator?
this.progressType = this.getAttribute('progress') ?? false;
// build the progress indicator if more than 1 second remaining and progress type is specified
if (this.ttl > 1000 && this.progressType) {
this.initProgress();
}
// init the countdown
this.initTimer();
// add event handling
this.addEventListener('click', this);
this.addEventListener('mouseenter', this);
this.addEventListener('mouseleave', this);
}
/**
* Remove any progress indicator.
*/
destroyProgress() {
this.updater && clearInterval(this.updater);
this.progressContainer && this.progressContainer.remove();
}
/**
* Handle the self-destruction.
*/
destruct() {
this.destroyProgress();
this.timer && this.timer.stop();
this.remove();
}
/**
* Runs when the element is removed from the DOM.
*/
disconnectedCallback() {
// stop the countdown timer
if (this.timer) {
this.timer.stop();
this.timer = null;
}
// remove the progress indicator
this.destroyProgress();
// remove event handling
this.removeEventListener('click', this);
this.removeEventListener('mouseenter', this);
this.removeEventListener('mouseleave', this);
}
/**
* Delegate events to class methods.
*/
handleEvent(ev) {
return this['on' + ev.type] && this['on' + ev.type](ev);
}
/**
* Build the specified progress indicator.
*/
initProgress() {
// figure out where to add the progress indicator
let el = this.childElementCount ? this.firstElementChild : this;
const useTarget = this.hasAttribute('progress-target') && this.querySelector(this.getAttribute('progress-target'));
if (useTarget) {
el = this.querySelector(this.getAttribute('progress-target'));
el.textContent = ''; // clear any existing content
}
// build the progress indicator container and fill it with the initial state
this.progressContainer = document.createElement('span');
this.progressContainer.className = 'self-destruct-progress';
if (this.progressType === 'pie') {
this.progressContainer.innerHTML = this.buildPie(1);
this.progress = this.progressContainer;
} else if (this.progressType === 'bar') {
this.progressContainer.innerHTML = this.buildBar(1);
this.progress = this.progressContainer.querySelector('.self-destruct-progress-bar');
el = this;
} else if (this.progressType === 'seconds') {
this.progressContainer.textContent = this.ttl;
this.progress = this.progressContainer;
}
// add the progress indicator to the main element
el.appendChild(this.progressContainer);
// start the progress indicator updating
this.updater = setInterval(this.updateProgress.bind(this), 50);
}
/**
* Start the timer.
*/
initTimer() {
const el = this; // cache 'this' to avoid scope issues
if (el.ttl > 0) {
const ttl = el.animate ? el.ttl - 250 : el.ttl; // shave off 250ms for fade out if animating
el.timer = new Timer(() => {
if (el.animate) {
el.style.transition = `opacity 250ms ease`;
el.style.opacity = '0';
el.timer = new Timer(el.destruct.bind(el), 250);
} else {
el.destruct();
}
}, ttl);
}
}
/**
* Click event handler.
*/
onclick(ev) {
const type = this.getAttribute('on-click');
if (ev.target.matches('.self-destruct') || type === 'destroy') {
this.destruct();
} else if (type === 'pause' && this.timer.isRunning) {
this.timer.pause();
} else if (type === 'pause' && !this.timer.isRunning) {
this.timer.start();
} else if (type === 'prevent') {
this.preventDestruct();
} else if (type === 'reset') {
this.timer.reset();
this.timer.start();
}
}
/**
* Mouse enter event handler.
*/
onmouseenter(ev) {
const type = this.getAttribute('on-hover');
if (type === 'pause') {
this.timer.pause();
} else if (type === 'prevent') {
this.preventDestruct();
} else if (type === 'reset') {
this.timer.reset();
}
}
/**
* Mouse leave event handler.
*/
onmouseleave(ev) {
const type = this.getAttribute('on-hover');
if (type === 'pause' || type === 'reset') {
this.timer.start();
}
}
/**
* Parse time into ms.
* E.g., .2s, 0.3s, 1s, 5.1s, 80ms, 100
*/
parseTime(text) {
const parts = text.match(/((?:\d?\.)?\d+) ?(m?s)?/);
let r = parts ? parseFloat(parts[1]) : this.defaultTtl;
if (parts && parts[2] === 's') {
r *= 1000;
}
return r;
}
/**
* Prevent the self-destruct from happening.
*/
preventDestruct() {
this.timer.stop();
this.updater && clearInterval(this.updater);
this.destroyProgress();
}
/**
* Update the progress indicator.
*/
updateProgress() {
if (this.progressType === 'pie') {
this.progressContainer.innerHTML = this.buildPie(this.timer.timeLeft / this.ttl);
} else if (this.progressType === 'bar') {
this.progress.style.width = String(this.timer.timeLeft / this.ttl * 100) + '%';
} else if (this.progressType === 'seconds') {
this.progressContainer.textContent = Math.ceil(this.timer.timeLeft / 1000);
}
}
}
if ("customElements" in window) {
window.customElements.define('self-destruct', SelfDestruct);
}