-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelpers.js
76 lines (73 loc) · 2.24 KB
/
helpers.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
document.addEventListener('DOMContentLoaded', () => {
const hoverText = span('hover-text hidden', '');
document.addEventListener('mousemove', e => {
if (e.target.dataset.title) {
hoverText.textContent = e.target.dataset.title;
hoverText.classList.remove('hidden');
hoverText.style.left = e.clientX + 'px';
hoverText.style.top = e.clientY + 'px';
} else {
hoverText.classList.add('hidden');
}
});
document.body.innerHTML = '';
document.body.appendChild(hoverText);
init();
}, {once: true});
const dayNames = ['MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY'];
function humanTime(hours) {
return `${(hours + 11) % 12 + 1}:00 ${hours < 12 ? 'A' : 'P'}M`;
}
const renderer = {};
function createFragment(elems) {
const frag = document.createDocumentFragment();
elems.forEach(e => frag.appendChild(typeof e === 'string' ? document.createTextNode(e) : e));
return frag;
}
function span(classes = '', content = '', onclick = null, hoverText = '') {
const span = document.createElement(typeof onclick === 'string' ? 'a' : 'span');
if (Array.isArray(content)) {
span.appendChild(createFragment(content));
} else {
span.textContent = content;
}
span.className = classes;
if (hoverText) span.dataset.title = hoverText;
if (typeof onclick === 'string') {
span.href = onclick;
} else if (onclick) {
span.addEventListener('click', e => {
if (!span.classList.contains('disabled')) {
onclick();
}
});
span.tabIndex = 0;
}
return span;
}
function input(classes = '', label = '', value = '', hoverText = '') {
const input = document.createElement('input');
input.className = classes;
if (hoverText) input.dataset.title = hoverText;
if (label) input.placeholder = label;
if (value) input.value = value;
return input;
}
function disableBtn(btn, why) {
btn.classList.add('disabled');
btn.tabIndex = -1;
if (why) btn.dataset.title = why;
}
function enableBtn(btn) {
btn.classList.remove('disabled');
btn.tabIndex = 0;
btn.dataset.title = '';
}
function openDialog(listener) {
return () => {
renderer.closeBtn.classList.remove('disabled');
renderer.dialog.classList.remove('hidden');
renderer.dialogContent.innerHTML = '';
listener();
};
}