-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathdom.js
More file actions
157 lines (137 loc) · 3.85 KB
/
dom.js
File metadata and controls
157 lines (137 loc) · 3.85 KB
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
import { isFn } from '../util/core.js';
/** @type {Record<string, Element>} */
const cacheNode = {};
/**
* Get Node
* @param {String|Element} el A DOM element
* @param {Boolean} noCache Flag to use or not use the cache
* @return {Element} The found node element
*/
export function getNode(el, noCache = false) {
if (typeof el === 'string') {
if (typeof window.Vue !== 'undefined') {
return find(el);
}
el = noCache ? find(el) : cacheNode[el] || (cacheNode[el] = find(el));
}
return el;
}
/**
*
* @param {*} el the targt element or the selector
* @param {*} content the content to be rendered as HTML
* @param {*} replace To replace the content (true) or insert instead (false) , default is false
*/
/**
* @param {string|Element} el
* @param {string} content
* @param {boolean} [replace]
*/
export function setHTML(el, content, replace) {
const node = getNode(el);
if (node) {
node[replace ? 'outerHTML' : 'innerHTML'] = content;
}
}
export const $ = document;
export const body = $.body;
export const head = $.head;
/**
* Find the first matching element
* @param {string|Element} el The root element on which to perform the query
* from, or a query string to query from `document`.
* @param {string} [query] The query string to use on `el` if `el` is an
* element.
* @returns {Element} The found DOM element
* @example
* find('nav') => document.querySelector('nav')
* find(nav, 'a') => nav.querySelector('a')
*/
export function find(el, query = ':is()') {
return /** @type {Element} */ (
typeof el !== 'string' ? el.querySelector(query) : $.querySelector(el)
);
}
/**
* Find all matching elements
* @param {string|Element} el The root element on which to perform the query
* from, or a query string to query from `document`.
* @param {string} [query] The query string to use on `el` if `el` is an
* element.
* @returns {Array<Element>} An array of DOM elements
* @example
* findAll('a') => Array.from(document.querySelectorAll('a'))
* findAll(nav, 'a') => Array.from(nav.querySelectorAll('a'))
*/
export function findAll(el, query = ':is()') {
return Array.from(
typeof el !== 'string'
? el.querySelectorAll(query)
: $.querySelectorAll(el),
);
}
/**
* @param {string} node
* @param {string} [tpl]
* @returns {HTMLElement}
*/
export function create(node, tpl) {
const element = $.createElement(node);
if (tpl) {
element.innerHTML = tpl;
}
return element;
}
/**
* @param {Element} target
* @param {Element} el
*/
export function appendTo(target, el) {
return target.appendChild(el);
}
/**
* @param {Element} target
* @param {Element} el
*/
export function before(target, el) {
return target.insertBefore(el, target.children[0]);
}
/**
* @param {any} el
* @param {any} type
* @param {any} [handler]
*/
export function on(el, type, handler) {
isFn(type)
? window.addEventListener(el, type)
: el.addEventListener(type, handler);
}
/**
* @param {any} el
* @param {any} type
* @param {any} [handler]
*/
export function off(el, type, handler) {
isFn(type)
? window.removeEventListener(el, type)
: el.removeEventListener(type, handler);
}
/**
* @param {string} content
*/
export function style(content) {
appendTo(head, /** @type {Element} */ (create('style', content)));
}
/**
* Fork https://github.com/bendrucker/document-ready/blob/master/index.js
* @param {(event: Event) => void} callback The callbacack to be called when the page is loaded
* @returns {number|void} If the page is already loaded returns the result of the setTimeout callback,
* otherwise it only attaches the callback to the DOMContentLoaded event
*/
export function documentReady(callback, doc = document) {
const state = doc.readyState;
if (state === 'complete' || state === 'interactive') {
return setTimeout(callback, 0);
}
doc.addEventListener('DOMContentLoaded', callback);
}