itty-bitty/docs/js/util.js
Nicholas Jitkoff 45b99ababc Update contact
2023-06-06 21:05:48 +02:00

45 lines
1.5 KiB
JavaScript

const el = (selector, ...args) => {
var attrs = (args[0] && typeof args[0] === 'object' && !Array.isArray(args[0]) && !(args[0] instanceof HTMLElement)) ? args.shift() : {};
let classes = selector.split(".");
if (classes.length > 0) selector = classes.shift();
if (classes.length) attrs.className = classes.join(" ")
let id = selector.split("#");
if (id.length > 0) selector = id.shift();
if (id.length) attrs.id = id[0];
var node = document.createElement(selector.length > 0 ? selector : "div");
for (let prop in attrs) {
if (attrs.hasOwnProperty(prop) && attrs[prop] != undefined) {
if (prop.indexOf("data-") == 0) {
let dataProp = prop.substring(5).replace(/-([a-z])/g, function(g) { return g[1].toUpperCase(); });
node.dataset[dataProp] = attrs[prop];
} else {
if (typeof attrs[prop] === 'function' || prop == "className") {
node[prop] = attrs[prop];
} else {
node.setAttribute(prop, attrs[prop]);
}
}
}
}
const append = (child) => {
if (Array.isArray(child)) return child.forEach(append);
if (typeof child == "string") child = document.createTextNode(child);
if (child) node.appendChild(child);
};
args.forEach(append);
return node;
};
el.trust = function (html) {
if (!html?.length) return undefined;
var template = document.createElement('template');
template.innerHTML = html;
return Array.from(template.content.childNodes);
}
const m = el;
window.el = el;