Skip to content

Instantly share code, notes, and snippets.

@mmpataki
Last active May 14, 2021 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmpataki/8abf53397ac22df4331116f1d334da22 to your computer and use it in GitHub Desktop.
Save mmpataki/8abf53397ac22df4331116f1d334da22 to your computer and use it in GitHub Desktop.
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