forked from mymth/vanillajs-datepicker
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathdom.js
55 lines (49 loc) · 1.35 KB
/
dom.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
let range = null;
export function parseHTML(html) {
if (range == null) { range = document.createRange() }
return range.createContextualFragment(html);
}
// equivalent to jQuery's :visble
export function isVisible(el) {
return !!(el.offsetWidth || el.offsetHeight || el.getClientRects().length);
}
export function hideElement(el) {
if (el.style.display === 'none') {
return;
}
// back up the existing display setting in data-style-display
if (el.style.display) {
el.dataset.styleDisplay = el.style.display;
}
el.style.display = 'none';
}
export function showElement(el) {
if (el.style.display !== 'none') {
return;
}
if (el.dataset.styleDisplay) {
// restore backed-up dispay property
el.style.display = el.dataset.styleDisplay;
delete el.dataset.styleDisplay;
} else {
el.style.display = '';
}
}
export function emptyChildNodes(el) {
if (el.firstChild) {
el.removeChild(el.firstChild);
emptyChildNodes(el);
}
}
export function replaceChildNodes(el, newChildNodes) {
emptyChildNodes(el);
if (newChildNodes instanceof DocumentFragment) {
el.appendChild(newChildNodes);
} else if (typeof newChildNodes === 'string') {
el.appendChild(parseHTML(newChildNodes));
} else if (typeof newChildNodes.forEach === 'function') {
newChildNodes.forEach((node) => {
el.appendChild(node);
});
}
}