itty-bitty/docs/render.js

95 lines
3.3 KiB
JavaScript
Raw Permalink Normal View History

2022-07-02 21:27:59 +00:00
// window.el = function (tagName, attrs, ...children) {
// let l = document.createElement(tagName);
// Object.entries(attrs).forEach(([k,v]) => l[k] = v);
// children.forEach((c) => l.appendChild(typeof c == "string" ? document.createTextNode(c) : c));
// return l;
// }
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 {
2023-06-21 14:34:50 +00:00
if (typeof attrs[prop] === 'function' || prop == "className") {
node[prop] = attrs[prop];
} else {
node.setAttribute(prop, attrs[prop]);
}
2022-07-02 21:27:59 +00:00
}
}
}
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;
};
2023-05-29 15:58:38 +00:00
el.trust = function (html) {
if (!html?.length) return undefined;
var template = document.createElement('template');
template.innerHTML = html;
return Array.from(template.content.childNodes);
}
2022-07-02 21:27:59 +00:00
window.el = el;
2022-02-15 23:28:50 +00:00
2022-02-17 17:54:22 +00:00
function async(u, c) {
var d = document, t = 'script',
o = d.createElement(t),
s = d.getElementsByTagName(t)[0];
o.src = '//' + u;
if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
s.parentNode.insertBefore(o, s);
2022-02-15 23:28:50 +00:00
}
2022-08-17 04:47:10 +00:00
function loadScript(src, callback, type = "module") {
let promise = new Promise((resolve, reject) => {
document.head.appendChild(el("script", { src, type, onload:resolve}));
})
return callback ? promise.then(callback) : promise;
}
2022-06-11 03:48:44 +00:00
function loadSyle(href, callback) {
2022-06-17 01:49:38 +00:00
let promise = new Promise((resolve, reject) => {
document.head.appendChild(el("link", { type: "text/css", rel: "stylesheet", href, onload:resolve}));
})
2022-08-17 04:47:10 +00:00
return callback ? promise.then(callback) : promise;
2022-02-15 23:28:50 +00:00
}
2022-06-17 01:49:38 +00:00
2022-06-09 05:53:37 +00:00
function renderScriptContent(data, origin) {
2022-07-02 21:27:59 +00:00
var base = el('base', {href: data.script});
2022-02-15 23:28:50 +00:00
document.head.appendChild(base);
2022-07-27 12:04:38 +00:00
window.script = data.script
2022-06-09 05:53:37 +00:00
window.params = data;
window.params.origin = origin;
2022-07-02 21:27:59 +00:00
console.log("🖊Rendering with", {script:data.script, params:data})
2022-06-09 05:53:37 +00:00
loadScript(data.script);
}
2022-02-15 23:28:50 +00:00
2022-06-09 05:53:37 +00:00
window.addEventListener("message", function(e) {
renderScriptContent(e.data, e.origin);
2022-02-15 23:28:50 +00:00
}, false);
2022-06-15 14:50:03 +00:00
function QRCodeURL(url, options) {
2022-07-31 20:05:20 +00:00
if (url.length > 2953) return undefined;
2022-06-15 14:50:03 +00:00
let size = options?.size ?? 547;
let errorCorrection = options?.correction ?? 'L';
let margin = options?.margin?.toString() || "1";
return `https://chart.googleapis.com/chart?cht=qr&chs=${size}x${size}&chld=${errorCorrection}|${margin}&choe=UTF-8&chl=${encodeURIComponent(url || location.href)}`;
}