-
Notifications
You must be signed in to change notification settings - Fork 0
/
when-do.mjs
132 lines (106 loc) · 2.8 KB
/
when-do.mjs
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
/**
* Remy Sharp
* MIT
*
* Designed initially for the purpose of moving through a conference schedule
* and also display elements, such as "buy buttons" for events
*
* 2023-07-14
* - supports show, hide and scroll into view
*/
const SHOW = 'show';
const HIDE = 'hide';
const SCROLL = 'scroll'; // special state that assumes it's visible, and when it ticks
function parse(s) {
const dates = s.split(',').map((_) => _.trim());
const isValid = (d) => d instanceof Date && !isNaN(d);
return dates.map((_) => {
const [a, b = Infinity] = _.split(/\s+/).map((_) => {
if (_.trim() === '') return undefined;
const d = new Date(_);
if (!isValid(d)) {
throw new Error(`Cannot parse the following timestamp: "${_}"`);
}
return d;
});
return { from: a.getTime(), until: b };
});
}
const whenables = [];
setInterval(() => {
for (let i = 0; i < whenables.length; i++) {
const when = whenables[i];
const inRange = when.inRange();
if (inRange) {
when[when.do]();
} else {
when[when.do === HIDE ? SHOW : HIDE]();
}
}
}, 1000);
class WhenDo extends HTMLElement {
state = undefined;
triggered = false;
constructor() {
const ref = super();
this.do = this.attributes.do.value || SHOW;
if (![SHOW, HIDE, SCROLL].includes(this.do)) {
throw new Error(
`when-do "do" property requires either "show", "hide" or "scroll"`
);
}
if (this.attributes.datetime?.value) {
this.dates = parse(this.attributes.datetime.value);
}
if (this.inRange()) {
this[this.do]();
} else {
this[this.do === HIDE ? SHOW : HIDE]();
}
whenables.push(ref);
}
inRange() {
const now = Date.now();
let res = false;
for (let i = 0; i < this.dates.length; i++) {
const { from, until } = this.dates[i];
if (now >= from && from < until) {
res = true;
break;
}
}
return res;
}
checkTriggered() {
if (this.triggered === false) {
this.triggered = true;
if (this.attributes.apply) {
this.classList.add(this.attributes.apply.value);
}
}
}
scroll() {
if (this.state === SCROLL) return; // nop
this.show();
if (this.firstElementChild) {
this.firstElementChild.scrollIntoView({ behavior: 'smooth' });
} else {
this.nextElementSibling.scrollIntoView({ behavior: 'smooth' });
}
this.state = SCROLL;
this.checkTriggered();
}
show() {
if (this.state === SHOW) return; // nop
this.setAttribute('style', 'display: contents');
this.state = SHOW;
this.checkTriggered();
}
hide() {
if (this.state === HIDE) return; // nop
this.setAttribute('style', 'display: none');
this.state = HIDE;
this.checkTriggered();
}
}
customElements.define('when-do', WhenDo);