diff options
Diffstat (limited to 'ka.js')
| -rw-r--r-- | ka.js | 48 |
1 files changed, 48 insertions, 0 deletions
@@ -0,0 +1,48 @@ +var doc = document; +var elAttr = (tag, name) => tag.getAttribute(name); +function el$Attr(name, value) { + this.setAttribute(name, value); + return this; +} +function el$OnClick(callback) { + this.onclick = callback; + return this; +} +function tag(name, attrs, ...children) { + const el = doc.createElement(name); + el.attr = elAttr; + el.$attr = el$Attr; + el.$onclick = el$OnClick; + for (const child of children) { + el.appendChild((typeof(child) === 'string') ? doc.createTextNode(child) : child); + } + for (const [attr, value] of Object.entries(attrs||{})) { + el.$attr(attr, value); + } + return el; +} + +const TRIVIAL = "h1,h2,h3,p,a,div,span,select"; +for (let name of TRIVIAL.split(",")) { + window[name] = (...children) => tag(name, null, ...children); +} + +var ahref = (href, ...children) => tag("a", {href}, ...children); +var divcls = (klass, ...children) => tag("div", {class: klass}, ...children); +var img = (src) => tag("img", {src}); +var input = (type) => tag("input", {type}); + +function router(root, routes, onreload) { + function reload() { + let h = doc.location.hash.substr(1) || "/"; + if (!(h in routes)) { + h = ""; + } + root.replaceChildren(routes[h]()); + onreload && onreload(h); + }; + reload(); + window.addEventListener("hashchange", reload); + root.reload = reload; +} + |
