-
Notifications
You must be signed in to change notification settings - Fork 0
/
litelightbox.min.js
1 lines (1 loc) · 2.72 KB
/
litelightbox.min.js
1
!function(){const e=document.createElement("dialog");if(e.id="llb-dialog","function"!=typeof e.showModal)return void console.warn("[LiteLightbox] The <dialog> API is not supported by this browser.");const t=document.createElement("form");t.method="dialog",t.id="llb-dialogform";const o=document.createElement("div");o.id="llb-top-right-controls-wrapper",t.appendChild(o);const n=document.createElement("button");n.id="llb-closebtn",n.textContent="Close image dialog.",o.appendChild(n);const l=document.createElement("a");l.id="llb-openbtn",l.textContent="Open full image",o.appendChild(l);const i=document.createElement("button");i.id="llb-prevbtn",i.textContent="Previous image",t.appendChild(i);const d=document.createElement("button");d.id="llb-nextbtn",d.textContent="Next image",t.appendChild(d),e.appendChild(t);const c=document.createElement("figure");c.id="llb-figure";const a=document.createElement("figcaption");a.id="llb-figcaption",c.appendChild(a),e.appendChild(c),document.body.appendChild(e);for(const e of[...document.querySelectorAll("[data-llb-src]")])e.addEventListener("click",function(t){return t.preventDefault(),s(e),!1});const r={keyDown:!1,goPrev:null,goNext:null};function s(t){let o=t.dataset,n=o.llbSrc;"href"===n&&(n=t.href);const u=document.createElement("img");u.src=n,u.setAttribute("data-llb-removeme","");const p=t.firstElementChild;"llbAlt"in o?u.alt=o.llbAlt:p&&"alt"in p&&(u.alt=p.alt),c.prepend(u);const f=t.nextElementSibling;if(f&&"llbCaption"in f.dataset)for(const e of[...f.cloneNode(!0).childNodes])a.appendChild(e);if(l.href=n,"llbGallery"in o){const e=[...document.querySelectorAll("[data-llb-gallery]")];let o=!1,n=null,l=null;for(const i of e)if(i!==t){if(o){l=i;break}n=i}else o=!0;i.className=n?"shown":"",d.className=l?"shown":"",i.onclick=r.goPrev=n?e=>(e&&e.preventDefault(),m(),s(n),!1):null,d.onclick=r.goNext=l?e=>(e&&e.preventDefault(),m(),s(l),!1):null}else i.className="",d.className="",i.onclick=r.goPrev=null,d.onclick=r.goNext=null;document.body.classList.add("llb-modal"),e.showModal(),window.history.pushState({},"","#lightbox")}function m(){for([...e.querySelectorAll("[data-llb-removeme]")].forEach(e=>e.remove());a.childNodes.length;)a.childNodes[0].remove();l.href=""}window.addEventListener("popstate",t=>{e.close()}),window.addEventListener("keydown",e=>{e.isComposing||229===e.keyCode||r.keyDown||("ArrowLeft"===e.key&&"function"==typeof r.goPrev&&(r.goPrev(),r.keyDown=!0),"ArrowRight"===e.key&&"function"==typeof r.goNext&&(r.goNext(),r.keyDown=!0))}),window.addEventListener("keyup",e=>{e.isComposing||229===e.keyCode||"ArrowLeft"!==e.key&&"ArrowRight"!==e.key||(r.keyDown=!1)}),e.addEventListener("close",()=>{m(),document.body.classList.remove("llb-modal"),"#lightbox"==location.hash&&window.history.back()})}();