1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | import * as pair from './pair.js'
|
10 | import * as map from './map.js'
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | export const doc = (typeof document !== 'undefined' ? document : {})
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | export const createElement = name => doc.createElement(name)
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | export const createDocumentFragment = () => doc.createDocumentFragment()
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | export const createTextNode = text => doc.createTextNode(text)
|
37 |
|
38 |
|
39 | export const domParser = (typeof DOMParser !== 'undefined' ? new DOMParser() : null)
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | export const emitCustomEvent = (el, name, opts) => el.dispatchEvent(new CustomEvent(name, opts))
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | export const setAttributes = (el, attrs) => {
|
56 | pair.forEach(attrs, (key, value) => {
|
57 | if (value === false) {
|
58 | el.removeAttribute(key)
|
59 | } else if (value === true) {
|
60 | el.setAttribute(key, '')
|
61 | } else {
|
62 |
|
63 | el.setAttribute(key, value)
|
64 | }
|
65 | })
|
66 | return el
|
67 | }
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | export const setAttributesMap = (el, attrs) => {
|
76 | attrs.forEach((value, key) => { el.setAttribute(key, value) })
|
77 | return el
|
78 | }
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | export const fragment = children => {
|
86 | const fragment = createDocumentFragment()
|
87 | for (let i = 0; i < children.length; i++) {
|
88 | appendChild(fragment, children[i])
|
89 | }
|
90 | return fragment
|
91 | }
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | export const append = (parent, nodes) => {
|
100 | appendChild(parent, fragment(nodes))
|
101 | return parent
|
102 | }
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | export const remove = el => el.remove()
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | export const addEventListener = (el, name, f) => el.addEventListener(name, f)
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | export const removeEventListener = (el, name, f) => el.removeEventListener(name, f)
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | export const addEventListeners = (node, listeners) => {
|
133 | pair.forEach(listeners, (name, f) => addEventListener(node, name, f))
|
134 | return node
|
135 | }
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 | export const removeEventListeners = (node, listeners) => {
|
144 | pair.forEach(listeners, (name, f) => removeEventListener(node, name, f))
|
145 | return node
|
146 | }
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | export const element = (name, attrs = [], children = []) =>
|
156 | append(setAttributes(createElement(name), attrs), children)
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 | export const canvas = (width, height) => {
|
164 | const c = (createElement('canvas'))
|
165 | c.height = height
|
166 | c.width = width
|
167 | return c
|
168 | }
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | export const text = createTextNode
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 | export const pairToStyleString = pair => `${pair.left}:${pair.right};`
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 | export const pairsToStyleString = pairs => pairs.map(pairToStyleString).join('')
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 | export const mapToStyleString = m => map.map(m, (value, key) => `${key}:${value};`).join('')
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 | export const querySelector = (el, query) => el.querySelector(query)
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 | export const querySelectorAll = (el, query) => el.querySelectorAll(query)
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 | export const getElementById = id => (doc.getElementById(id))
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 | const _parse = html => domParser.parseFromString(`<html><body>${html}</body></html>`, 'text/html').body
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 | export const parseFragment = html => fragment( (_parse(html).childNodes))
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 | export const parseElement = html => (_parse(html).firstElementChild)
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 | export const replaceWith = (oldEl, newEl) => oldEl.replaceWith(newEl)
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 | export const insertBefore = (parent, el, ref) => parent.insertBefore(el, ref)
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 | export const appendChild = (parent, child) => parent.appendChild(child)
|
266 |
|
267 | export const ELEMENT_NODE = doc.ELEMENT_NODE
|
268 | export const TEXT_NODE = doc.TEXT_NODE
|
269 | export const CDATA_SECTION_NODE = doc.CDATA_SECTION_NODE
|
270 | export const COMMENT_NODE = doc.COMMENT_NODE
|
271 | export const DOCUMENT_NODE = doc.DOCUMENT_NODE
|
272 | export const DOCUMENT_TYPE_NODE = doc.DOCUMENT_TYPE_NODE
|
273 | export const DOCUMENT_FRAGMENT_NODE = doc.DOCUMENT_FRAGMENT_NODE
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 | export const checkNodeType = (node, type) => node.nodeType === type
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 | export const isParentOf = (parent, child) => {
|
286 | let p = child.parentNode
|
287 | while (p && p !== parent) {
|
288 | p = p.parentNode
|
289 | }
|
290 | return p === parent
|
291 | }
|