Lettersail: online English school for kids on MODX 3 (templates, chunks, snippets, plugins, scripts)
This commit is contained in:
commit
3799cf8de5
96 files changed
+4423
No files matched your search
@@ -0,0 +1,61 @@
|
||||
(()=>{
|
||||
const ui=JSON.parse(document.querySelector("[data-ui]").textContent);
|
||||
const paths=ui.docs.map(url=>new URL(url,location.href).pathname);
|
||||
const match=link=>{try{const url=new URL(link.href,location.href);return url.origin===location.origin&&paths.includes(url.pathname)}catch(e){return false}};
|
||||
let box=null,body=null;
|
||||
const cache=new Map();
|
||||
const build=()=>{
|
||||
box=document.createElement("dialog");
|
||||
box.className="docbox";
|
||||
box.setAttribute("data-lenis-prevent","");
|
||||
box.setAttribute("aria-labelledby","docbox-title");
|
||||
box.innerHTML='<button class="docbox__close" type="button" aria-label="'+ui.close+'"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M6 6L18 18M18 6L6 18"/></svg></button><div class="docbox__body"></div>';
|
||||
body=box.querySelector(".docbox__body");
|
||||
body.tabIndex=-1;
|
||||
box.querySelector(".docbox__close").addEventListener("click",()=>box.close());
|
||||
box.addEventListener("click",event=>{if(event.target===box)box.close()});
|
||||
document.body.append(box);
|
||||
};
|
||||
const load=async path=>{
|
||||
if(cache.has(path))return cache.get(path);
|
||||
const response=await fetch(path,{credentials:"same-origin"});
|
||||
if(!response.ok)throw new Error(response.status);
|
||||
const doc=new DOMParser().parseFromString(await response.text(),"text/html");
|
||||
const main=doc.querySelector(".doc__main");
|
||||
if(!main)throw new Error("no content");
|
||||
const title=main.querySelector(".doc__title");
|
||||
if(title)title.id="docbox-title";
|
||||
const html=main.innerHTML;
|
||||
cache.set(path,html);
|
||||
return html;
|
||||
};
|
||||
const open=async path=>{
|
||||
if(!box)build();
|
||||
body.innerHTML=await load(path);
|
||||
body.scrollTop=0;
|
||||
if(!box.open)box.showModal();
|
||||
body.focus({preventScroll:true});
|
||||
};
|
||||
const wanted=new URLSearchParams(location.search).get("doc");
|
||||
if(wanted&&paths.includes(wanted)&&window.HTMLDialogElement){
|
||||
history.replaceState(null,"",location.pathname);
|
||||
open(wanted).catch(()=>{});
|
||||
}
|
||||
document.addEventListener("click",event=>{
|
||||
const button=event.target.closest("[data-doc]");
|
||||
if(!button||!window.HTMLDialogElement)return;
|
||||
event.preventDefault();
|
||||
open(button.dataset.doc).catch(()=>{location.href=button.dataset.doc});
|
||||
});
|
||||
document.addEventListener("click",async event=>{
|
||||
if(event.defaultPrevented||event.button!==0||event.metaKey||event.ctrlKey||event.shiftKey||event.altKey)return;
|
||||
const link=event.target.closest("a[href]");
|
||||
if(!link||!match(link)||!window.HTMLDialogElement)return;
|
||||
event.preventDefault();
|
||||
try{
|
||||
await open(new URL(link.href,location.href).pathname);
|
||||
}catch(e){
|
||||
location.href=link.href;
|
||||
}
|
||||
});
|
||||
})();
|
||||
Reference in new issue
Block a user