{"version":3,"file":"main.js","sources":["../js/spa.js"],"sourcesContent":["const wrapper = document.getElementById('page')\n\nfunction executeScript (script) {\n\tvar newScript = document.createElement('script')\n\n\tArray.from(script.attributes).forEach(attr => {\n\t\tnewScript.setAttribute(attr.name, attr.value)\n\t})\n\n\tvar scriptText = document.createTextNode(script.innerHTML)\n\tnewScript.appendChild(scriptText)\n\n\tscript.parentNode.replaceChild(newScript, script)\n}\n\nasync function update (page) {\n\tvar uri = `${page}?delta`\n\tvar answer = await fetch(uri)\n\tvar html = await answer.text()\n\n\tif (typeof window.onPageUnload === 'function') {\n\t\twindow.onPageUnload()\n\t\twindow.onPageUnload = null\n\t}\n\n\tsetInnerHTML(wrapper, html)\n\thistory.pushState(null, '', page)\n}\n\nasync function onLinkClick (ev) {\n\tev.preventDefault()\n\n\tvar link = ev.target\n\n\tif (!link.getAttribute('href')) {\n\t\treturn\n\t}\n\n\t// Should be a left click on an .\n\tif (ev.button != 0) {\n\t\treturn\n\t}\n\n\t// Should be a same-origin navigation:\n\t// a link within the site.\n\tif (link.origin != document.location.origin) {\n\t\treturn\n\t}\n\n\tupdate(link.pathname)\n}\n\nfunction upgradeLinks (element) {\n\tArray.from(element.querySelectorAll('a'))\n\t\t.filter(e => e.getAttribute('target') != '_blank')\n\t\t\t.forEach(link => link.onclick = onLinkClick)\n}\n\nasync function onFormSubmit (ev) {\n\tev.preventDefault()\n\n\tvar form = ev.target\n\n\tvar uri = form.getAttribute('action') ||\n\t\tdocument.location.pathname + document.location.search\n\n\tvar data = new FormData(form)\n\n\tvar method = form.getAttribute('method') || 'GET'\n\tvar body\n\n\tif (method == 'GET') {\n\t\tvar args = {}\n\n\t\tfor (let [key, value] of data.entries()) {\n\t\t\tvar pattern = `/:${key}`\n\n\t\t\tif (uri.includes(pattern)) {\n\t\t\t\turi = uri.replace(pattern, `/${encodeURIComponent(value)}`)\n\t\t\t} else {\n\t\t\t\targs[key] = value\n\t\t\t}\n\t\t}\n\n\t\tvar qs = new URLSearchParams(args).toString()\n\t\turi = `${uri}?${qs}`\n\t\tvar action = `${uri}&delta`\n\n\t} else {\n\t\tvar args = Object.fromEntries(data.entries())\n\n\t\tvar action = uri.includes('?') ?\n\t\t\t`${uri}&delta` : `${uri}?delta`\n\n\t\tbody = JSON.stringify(args)\n\t}\n\n\tvar answer = await fetch(action, { body, method })\n\tvar text = await answer.text()\n\n\tsetInnerHTML(wrapper, text)\n\n\tif (uri.endsWith('?')) {\n\t\turi = uri.substring(0, uri.length - 1)\n\t}\n\n\thistory.pushState(null, '', uri)\n}\n\nfunction upgradeForms (element) {\n\tvar forms = element.querySelectorAll('form')\n\tforms.forEach(form => form.onsubmit = onFormSubmit)\n}\n\nfunction setInnerHTML (element, html) {\n\telement.innerHTML = html;\n\n\tvar scripts = element.querySelectorAll('script')\n\tscripts.forEach(executeScript)\n\n\tif (typeof window.onPageLoad === 'function') {\n\t\twindow.onPageLoad()\n\t\twindow.onPageLoad = null\n\t}\n\n\tupgradeForms(element)\n\tupgradeLinks(element)\n}\n\nexport default () => {\n\tupgradeForms(wrapper)\n\tupgradeLinks(wrapper)\n\n\twindow.onpopstate = ev => {\n\t\tupdate(document.location.pathname)\n\t\tev.preventDefault()\n\t}\n\n\tif (typeof window.onPageLoad === 'function') {\n\t\twindow.onPageLoad()\n\t\twindow.onPageLoad = null\n\t}\n}\n"],"names":["wrapper","document","getElementById","executeScript","script","newScript","createElement","Array","from","attributes","forEach","attr","setAttribute","name","value","scriptText","createTextNode","innerHTML","appendChild","parentNode","replaceChild","async","update","page","uri","answer","fetch","html","text","window","onPageUnload","setInnerHTML","history","pushState","onLinkClick","ev","preventDefault","link","target","getAttribute","button","origin","location","pathname","upgradeLinks","element","querySelectorAll","filter","e","onclick","onFormSubmit","body","form","search","data","FormData","method","args","key","entries","pattern","includes","replace","encodeURIComponent","action","URLSearchParams","toString","Object","fromEntries","JSON","stringify","endsWith","substring","length","upgradeForms","onsubmit","onPageLoad","onpopstate"],"mappings":"yBAAA,MAAMA,E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