* Creates element with given tag, attributes and children. * @param {string} type * @param {object} attributes * @param {string|HTMLElement|HTMLElement[]} children * @return {HTMLElement}
(type, attributes = {}, children = null)
| 17 | * @return {HTMLElement} |
| 18 | */ |
| 19 | static createElement (type, attributes = {}, children = null) { |
| 20 | const $element = document.createElement(type) |
| 21 | |
| 22 | // set element attributes |
| 23 | Object.keys(attributes).forEach(name => { |
| 24 | const value = attributes[name] |
| 25 | if (nonStandardAttributes[name] === undefined) { |
| 26 | // lowercase event attributes |
| 27 | const attributeName = |
| 28 | name.indexOf('on') === 0 |
| 29 | ? name.toLowerCase() |
| 30 | : name |
| 31 | $element[attributeName] = value |
| 32 | } else { |
| 33 | // set non-standard attribute |
| 34 | $element.setAttribute(nonStandardAttributes[name], value) |
| 35 | } |
| 36 | }) |
| 37 | |
| 38 | // append children |
| 39 | if (children !== null) { |
| 40 | switch (Object.prototype.toString.call(children)) { |
| 41 | case '[object String]': |
| 42 | $element.innerText = children |
| 43 | break |
| 44 | case '[object Array]': |
| 45 | children.forEach($child => $child && $element.appendChild($child)) |
| 46 | break |
| 47 | default: |
| 48 | $element.appendChild(children) |
| 49 | } |
| 50 | } |
| 51 | return $element |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * View constructor |
no test coverage detected