Skip to content

Instantly share code, notes, and snippets.

@mmpataki

mmpataki/render.js

Last active May 14, 2021
Embed
What would you like to do?
render
function render(name, spec, elemCreated, container) {
let e;
if (!spec.preBuilt) {
e = document.createElement(spec.ele);
spec.iden && elemCreated(spec.iden, e)
if (spec.text) e.innerHTML = spec.text;
if (spec.classList) {
e.classList = `${name}-` + spec.classList.split(/\s+/).join(` ${name}-`)
}
spec.attribs && Object.keys(spec.attribs).forEach(key => {
e[key] = spec.attribs[key]
})
spec.styles && Object.keys(spec.styles).forEach(key => {
e.style[key] = spec.styles[key]
})
spec.evnts && Object.keys(spec.evnts).forEach(key => {
e.addEventListener(key, spec.evnts[key])
})
if (spec.children) {
if (spec.children instanceof Function) {
spec.children().map(x => e.appendChild(x))
}
else spec.children.forEach(child => render(name, child, elemCreated, e))
}
} else {
e = spec.ele;
}
if (container) {
let lbl;
if (spec.label || spec.postlabel) {
let rgid = "id_" + Math.random();
e.id = rgid
lbl = document.createElement('label')
lbl.innerHTML = spec.label || spec.postlabel
lbl.setAttribute('for', rgid)
}
if (spec.label) container.appendChild(lbl)
container.appendChild(e)
if (spec.postlabel) container.appendChild(lbl)
return container;
}
return e;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment