| 181 | |
| 182 | export class DOM { |
| 183 | static element(type, options) { |
| 184 | const node = document.createElement(type); |
| 185 | if (options === undefined) return node; |
| 186 | if (typeof options === 'string') { |
| 187 | // Old behaviour: options = class string |
| 188 | node.className = options; |
| 189 | } else if (Array.isArray(options)) { |
| 190 | // Old behaviour: options = class array |
| 191 | DOM.addClasses(node, options); |
| 192 | } else { |
| 193 | // New behaviour: options = attribute dict |
| 194 | for (const [key, value] of Object.entries(options)) { |
| 195 | if (key == 'className') { |
| 196 | node.className = value; |
| 197 | } else if (key == 'classList') { |
| 198 | DOM.addClasses(node, value); |
| 199 | } else if (key == 'textContent') { |
| 200 | node.textContent = value; |
| 201 | } else if (key == 'children') { |
| 202 | for (const child of value) { |
| 203 | node.appendChild(child); |
| 204 | } |
| 205 | } else { |
| 206 | node.setAttribute(key, value); |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | return node; |
| 211 | } |
| 212 | |
| 213 | static addClasses(node, classes) { |
| 214 | const classList = node.classList; |