UNPKG

mvdom

Version:

deprecated - Moved to dom-native package

241 lines 6.65 kB
import { asNodeArray } from './utils'; export function first(el_or_selector, selector) { if (!selector && typeof el_or_selector !== "string") { const el = el_or_selector; const firstElementChild = el.firstElementChild; if (!firstElementChild && el.firstChild) { if (el.firstChild.nodeType === 1) { return el.firstChild; } else { return next(el.firstChild); } } return firstElementChild; } else { return _execQuerySelector(false, el_or_selector, selector); } } export function all(el, selector) { const nodeList = _execQuerySelector(true, el, selector); return (nodeList != null) ? asNodeArray(nodeList) : []; } export function next(el, selector) { return _sibling(true, el, selector); } export function prev(el, selector) { return _sibling(false, el, selector); } export function closest(el, selector) { return (el) ? el.closest(selector) : null; } export function append(refEl, newEl, position) { let parentEl; let nextSibling = null; let result; if (typeof newEl === 'string') { newEl = frag(newEl); } if (newEl instanceof Array) { result = newEl; const fragment = document.createDocumentFragment(); for (const elItem of newEl) { fragment.appendChild(elItem); } newEl = fragment; } else if (newEl instanceof DocumentFragment) { result = [...newEl.children]; } else { result = newEl; } position = (position) ? position : "last"; if (position === "last" || position === "first" || position === "empty") { parentEl = refEl; } else if (position === "before" || position === "after") { parentEl = refEl.parentNode; if (!parentEl) { throw new Error("mvdom ERROR - The referenceElement " + refEl + " does not have a parentNode. Cannot insert " + position); } } if (position === "first") { nextSibling = first(refEl); } else if (position === "before") { nextSibling = refEl; } else if (position === "after") { nextSibling = next(refEl); } if (nextSibling) { parentEl.insertBefore(newEl, nextSibling); } else { if (position === "empty") { if (parentEl instanceof HTMLElement) { parentEl.innerHTML = ''; } else if (parentEl instanceof DocumentFragment) { while (parentEl.lastChild) { parentEl.removeChild(parentEl.lastChild); } } } parentEl.appendChild(newEl); } return result; } export function frag(html) { html = (html) ? html.trim() : null; const template = document.createElement("template"); if (html) { template.innerHTML = html; } return template.content; } export function style(el, style) { if (el == null) return el; if (el instanceof HTMLElement) { _styleEl(el, style); } else if (el instanceof Array) { for (const elItem of el) { _styleEl(elItem, style); } } return el; } function _styleEl(el, style) { for (const name of Object.keys(style)) { el.style[name] = style[name]; } } export function className(els, keyValues) { if (els instanceof Array) { for (const el of els) { _setClassName(el, keyValues); } } else { _setClassName(els, keyValues); } return els; } function _setClassName(el, keyValues) { for (const name of Object.keys(keyValues)) { const val = keyValues[name]; if (val === null || val === false) { el.classList.remove(name); } else if (val !== undefined) { el.classList.add(name); } } } export function attr(els, arg, val) { if (val !== undefined) { if (typeof arg !== 'string') { throw new Error(`attr - attr(els, name, value) must have name as string and not: ${arg}`); } const name = arg; if (els instanceof Array) { for (const el of els) { _setAttribute(el, name, val); } } else { _setAttribute(els, name, val); } return els; } else if (typeof arg === 'string' || arg instanceof Array) { return _attrGet(els, arg); } else { return _attrSet(els, arg); } } export function _attrSet(els, arg) { if (els instanceof Array) { for (const el of els) { _setAttributes(el, arg); } } else { _setAttributes(els, arg); } return els; } function _setAttributes(el, nameValueObject) { for (const name of Object.keys(nameValueObject)) { _setAttribute(el, name, nameValueObject[name]); } } function _setAttribute(el, name, val) { const txtVal = (typeof val !== 'boolean') ? val : (val === true) ? '' : null; if (txtVal !== null) { el.setAttribute(name, txtVal); } else { el.removeAttribute(name); } } export function _attrGet(els, arg) { if (els instanceof Array) { const ells = els; return ells.map(el => { const r = _getAttrEl(el, arg); return r; }); } else { const r = _getAttrEl(els, arg); return r; } } export function _getAttrEl(el, names) { if (names instanceof Array) { return names.map(n => { return el.getAttribute(n); }); } else { return el.getAttribute(names); } } export function elem(...names) { if (names.length === 1) { return document.createElement(names[0]); } else { return names.map(n => { return document.createElement(n); }); } } function _sibling(next, el, selector) { const sibling = (next) ? 'nextSibling' : 'previousSibling'; let tmpEl = (el) ? el[sibling] : null; while (tmpEl != null && tmpEl != document) { if (tmpEl.nodeType === 1 && (!selector || tmpEl.matches(selector))) { return tmpEl; } tmpEl = tmpEl[sibling]; } return null; } function _execQuerySelector(all, elOrSelector, selector) { let el = null; if (elOrSelector == null) { return null; } if (typeof selector === "undefined") { selector = elOrSelector; el = document; } else { el = elOrSelector; } return (all) ? el.querySelectorAll(selector) : el.querySelector(selector); } //# sourceMappingURL=dom.js.map