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