/* This file is a set of utility functions used in the manipulation of HTML pages There is nothing specific to Dweb at all here, feel free to copy and modify. */ function urlsFrom(url) { /* Convert to urls, url: Array of urls, or string representing url or representing array of urls return: Array of strings representing url */ if (typeof(url) === "string") { if (url[0] === '[') url = JSON.parse(url); else if (url.includes(',')) url = url.split(','); else url = [ url ]; } if (!Array.isArray(url)) throw new Error(`Unparsable url: ${url}`); return url; } function elementFrom(el) { /* Make sure we have an element, if passed a string then find the element with that id. el: Element or string being the id of an element. returns: element. */ return (typeof(el) === "string") ? document.getElementById(el) : el; } async function p_resolveobj(url) { /* Asynchronously find an object url: An object, or a url representing an object, or an array of urls. returns: Object throws: Error if can't resolve object */ try { if (typeof url === 'string') url = [ url ]; if (Array.isArray(url)) url = await Dweb.SmartDict.p_fetch(url, verbose); return url; } catch(err) { console.log("p_resolveobj: Cannot resolve",url); throw err; } } function form2dict(frm) { /* Convert a form into a dictionary its mindblowing that Javascript even at EC6 doesnt have this ! */ let res = {}; /* Find the element if we are given a string */ /* Note this is not the usual getElementById since forms have their own array */ let el_form = (typeof frm === "string") ? document.forms.namedItem(frm) : frm; for (let el of el_form.elements) { if (el.type !== "submit") { res[el.name] = el.value; } } return res; } function togglevis(el, displayvis) { /* Toggle the visibility of an item el element, its id, or an array of elements displayvis is one of "inline" "block" "inlineblock" */ if (Array.isArray(el)) { el.map((e) => togglevis(e, displayvis)) } else { el = elementFrom(el); el.style.display = (el.style && el.style.display === "none" ? displayvis : "none"); } } function setstatus(msg) { // Display the message in a Status DIV (usually top right corner, but could be anywhere example wants it) document.getElementById("status").innerHTML=msg; } function deletechildren(el, keeptemplate) { /* Remove all children from a node :param el: An HTML element, or a string with id of an HTML element */ if (typeof keeptemplate === "undefined") keeptemplate=true; el = (typeof(el) === "string") ? document.getElementById(el) : el; // Carefull - this deletes from the end, because template if it exists will be firstChild while (el.lastChild && !(keeptemplate && el.lastChild.classList && el.lastChild.classList.contains("template"))) { // Note that deletechildren is also used on Span's to remove the children before replacing with text. el.removeChild(el.lastChild); } return el; // For chaining } function replacetext(el, text) { /* Replace the text of el with text, removing all other children :param el: An HTML element, or a string with id of an HTML element */ el = elementFrom(el); //console.log("replacetext",text,el.constructor.name) // Uncomment to get class name of different things want to edit if (el instanceof HTMLImageElement) { el.src = text; } else { deletechildren(el); return el.appendChild(document.createTextNode(text)) } } function replacetexts(el, ...dict) { /* Replace the text of all inner nodes of el from the dict Note this intentionally doesnt allow html as the values of the dict since probably from a network call and could be faked as "bad" html Rules apply to el or any children. Adds an inner text node with oo.yyy Replaces with ooo.aaa.bbb Replaces with ooo.aaa.bbb Has y replecated withe each of ooo.aaa[] Has value replaced with ooo.ccc (typically used for