58 lines
14 KiB
JavaScript
58 lines
14 KiB
JavaScript
(() => {
|
|
"use strict";const createImageBox=(boxWindow,loaded,url,caption,imageGroup)=>{let prevCaption="",prevURL="",prevHTML="",nextCaption="",nextURL="",nextHTML="",imageCount="",foundURL=!1;if(imageGroup){const tempArray=document.querySelectorAll(`a[rel="${imageGroup}"]`);for(let index=0;index<tempArray.length&&nextHTML==="";index++)tempArray[index].href!==url?foundURL?(nextCaption=tempArray[index].title,nextURL=tempArray[index].href,nextHTML="<span id='WUB_next'> <a href='#'>"+wuboxL10n.next+"</a></span>"):(prevCaption=tempArray[index].title,prevURL=tempArray[index].href,prevHTML="<span id='WUB_prev'> <a href='#'>"+wuboxL10n.prev+"</a></span>"):(foundURL=!0,imageCount=wuboxL10n.image+" "+(index+1)+" "+wuboxL10n.of+" "+tempArray.length)}const imgPreloader=new Image;imgPreloader.onload=()=>{var _a,_b,_c,_d;imgPreloader.onload=null;const pagesize=getPageSize(),x=pagesize.width-150,y=pagesize.height-150;let imageWidth=imgPreloader.width,imageHeight=imgPreloader.height;imageWidth>x?(imageHeight=imageHeight*(x/imageWidth),imageWidth=x,imageHeight>y&&(imageWidth=imageWidth*(y/imageHeight),imageHeight=y)):imageHeight>y&&(imageWidth=imageWidth*(y/imageHeight),imageHeight=y,imageWidth>x&&(imageHeight=imageHeight*(x/imageWidth),imageWidth=x)),setBoxPosition(boxWindow,imageWidth,imageHeight),boxWindow.insertAdjacentHTML("beforeend",`
|
|
<a href='' id='WUB_ImageOff'>
|
|
<span class='screen-reader-text'>${wuboxL10n.close}</span>
|
|
<img id='WUB_Image' src='${url}' width='${imageWidth}' height='${imageHeight}' alt='${caption}'/>
|
|
</a>
|
|
<div id='WUB_caption'>
|
|
${caption}
|
|
<div id='WUB_secondLine'>
|
|
${imageCount+prevHTML+nextHTML}
|
|
</div>
|
|
</div>
|
|
<div id='WUB_closeWindow'>
|
|
<button type='button' id='WUB_closeWindowButton'>
|
|
<span class='screen-reader-text'>${wuboxL10n.close}</span>
|
|
<span class='wutb-close-icon'></span>
|
|
</button>
|
|
</div>
|
|
`),(_a=document.getElementById("WUB_closeWindowButton"))==null||_a.addEventListener("click",removeBox);const goPrev=()=>{boxWindow.innerHTML="",unloadKeydownEvent(),showBox(prevCaption,prevURL,imageGroup)},goNext=()=>{boxWindow.innerHTML="",unloadKeydownEvent(),showBox(nextCaption,nextURL,imageGroup)};(_b=document.getElementById("WUB_prev"))==null||_b.addEventListener("click",goPrev),(_c=document.getElementById("WUB_next"))==null||_c.addEventListener("click",goNext);const keydownEvent=e=>{e.key==="Escape"?removeBox():e.key==="ArrowRight"&&nextHTML?goNext():e.key==="ArrowLeft"&&prevHTML&&goPrev()},unloadKeydownEvent=()=>{window.removeEventListener("keydown",keydownEvent),document.body.removeEventListener("wubox:unload",unloadKeydownEvent)};window.addEventListener("keydown",keydownEvent),document.body.addEventListener("wubox:unload",unloadKeydownEvent),(_d=document.getElementById("WUB_ImageOff"))==null||_d.addEventListener("click",removeBox),loaded()},imgPreloader.src=url},createIframeBox=(boxWindow,boxOverlay,loaded,url,caption,params)=>{var _a,_b,_c;const urlNoQuery=url.split("WUB_");(_a=document.getElementById("WUB_load"))==null||_a.remove(),params.modal?(boxOverlay.removeEventListener("click",removeBox),boxWindow.insertAdjacentHTML("beforeend",`
|
|
<iframe
|
|
id='WUB_iframeContent'
|
|
frameborder='0'
|
|
hspace='0'
|
|
allowtransparency='true'
|
|
src='${urlNoQuery[0]}'
|
|
name='WUB_iframeContent${Math.round(Math.random()*1e3)}'
|
|
style='width:${params.width+29}px; height:${params.height+17}px;'>${wuboxL10n.noiframes}
|
|
</iframe>`),(_b=document.getElementById("WUB_iframeContent"))==null||_b.addEventListener("load",showBoxIframe)):(boxWindow.insertAdjacentHTML("beforeend",`
|
|
<div id='WUB_title'>
|
|
<div id='WUB_ajaxWindowTitle'>${caption}</div>
|
|
<div id='WUB_closeAjaxWindow'>
|
|
<button type='button' id='WUB_closeWindowButton'>
|
|
<span class='screen-reader-text'>${wuboxL10n.close}</span><span class='wutb-close-icon'></span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
<iframe
|
|
id='WUB_iframeContent'
|
|
frameborder='0'
|
|
hspace='0'
|
|
allowtransparency='true'
|
|
src='${urlNoQuery[0]}'
|
|
name='WUB_iframeContent${Math.round(Math.random()*1e3)}'
|
|
style='width:${params.width+29}px; height:${params.height+17}px;'
|
|
>
|
|
${wuboxL10n.noiframes}
|
|
</iframe>`),(_c=document.getElementById("WUB_iframeContent"))==null||_c.addEventListener("load",showBoxIframe)),setBoxPosition(boxWindow,params.width,params.height),loaded()},baseAjaxElement=(boxWindow,boxOverlay,caption,params)=>{if(boxWindow.style.visibility!=="visible")params.modal?(boxOverlay.removeEventListener("click",removeBox),boxWindow.insertAdjacentHTML("beforeend",`<div id='WUB_ajaxContent' class='WUB_modal' style='width:${params.width}px; height:${params.height}px;'></div>`)):boxWindow.insertAdjacentHTML("beforeend",`
|
|
<div id='WUB_title'>
|
|
<div id='WUB_ajaxWindowTitle'>${caption}</div>
|
|
<div id='WUB_closeAjaxWindow'>
|
|
<button type='button' id='WUB_closeWindowButton'>
|
|
<span class='screen-reader-text'>${wuboxL10n.close}</span>
|
|
<span class='wutb-close-icon'></span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
<div id='WUB_ajaxContent' style='width:${params.width}px; height:${params.height}px;'></div>`);else{const ajaxContent=document.getElementById("WUB_ajaxContent");ajaxContent.style.width=params.width+"px",ajaxContent.style.height=params.height+"px",ajaxContent.scrollTop=0,ajaxContent.innerHTML=caption}return document.getElementById("WUB_ajaxContent")},createAjaxBox=(boxWindow,boxOverlay,loaded,url,caption,params)=>{const ajaxContent=baseAjaxElement(boxWindow,boxOverlay,caption,params),load_url=url+(url.includes("?")?"&":"?")+"random="+new Date().getTime();fetch(load_url,{headers:{"X-Requested-With":"XMLHttpRequest"}}).then(response=>response.text()).then(html=>{ajaxContent.innerHTML=html,setBoxPosition(boxWindow,params.width,params.height),loaded()})},createInlineBox=(boxWindow,boxOverlay,loaded,caption,params)=>{const ajaxContent=baseAjaxElement(boxWindow,boxOverlay,caption,params),element=document.getElementById(params.inlineId);ajaxContent.insertAdjacentElement("beforeend",element==null?void 0:element.children[0]);const unloadAction=()=>{element==null||element.insertAdjacentElement("afterbegin",ajaxContent.children[0]),document.body.removeEventListener("wubox:unload",unloadAction)};document.body.addEventListener("wubox:unload",unloadAction),setBoxPosition(boxWindow,params.width,params.height),loaded()},formSubmit=form=>async event=>{event.preventDefault();const textArea=form.querySelector("textarea[data-editor]"),textAreaInput=textArea?form.querySelector('input[name="'+textArea.id+'"]'):null;textArea&&textAreaInput&&(textAreaInput.value=textArea.value);const blocked_form=wu_block_ui(form);window["wu_"+form.getAttribute("id")+"_errors"]&&(window["wu_"+form.getAttribute("id")+"_errors"].errors=[]);const submitButton=event.submitter.value,formData=new FormData(form);formData.append("submit",submitButton);const response=await fetch(form.getAttribute("action"),{method:"POST",body:formData,headers:{"X-Requested-With":"XMLHttpRequest"}}).then(response2=>response2.text()).then(txt=>txt?JSON.parse(txt):null);if(response===null||response.data===null){blocked_form.unblock(),removeBox();return}if(!response.success){blocked_form.unblock();const formId=form.getAttribute("id");window["wu_"+formId+"_errors"]&&(window["wu_"+formId+"_errors"].errors=response.data);const formApp=document.querySelector('[data-wu-app="'+formId+'_errors"]');formApp==null||formApp.setAttribute("tabindex","-1"),formApp==null||formApp.focus()}typeof response.data.tables=="object"&&(blocked_form.unblock(),removeBox(),Object.keys(response.data.tables).forEach(key=>{window[key].update()})),typeof response.data.redirect_url=="string"&&(window.location.href=response.data.redirect_url),typeof response.data.send=="object"&&window[response.data.send.scope][response.data.send.function_name](response.data.send.data,removeBox)};function getPageSize(){const de=document.documentElement,width=window.innerWidth||self.innerWidth||de&&de.clientWidth||document.body.clientWidth,height=window.innerHeight||self.innerHeight||de&&de.clientHeight||document.body.clientHeight;return{width,height}}function setBoxPosition(boxWindow,width,height){boxWindow.style.marginLeft="-"+width/2+"px",boxWindow.style.marginTop="-"+height/2+"px"}function fadeOutEffect(element,duration,callback){const startOpacity=parseFloat(getComputedStyle(element).opacity);let startTime=null;function step(timestamp){startTime||(startTime=timestamp);const progress=timestamp-startTime,opacity=Math.max(startOpacity-progress/duration,0);element.style.opacity=opacity.toString(),progress<duration?requestAnimationFrame(step):(element.style.display="none",callback&&callback())}requestAnimationFrame(step)}function showBoxIframe(){var _a;(_a=document.getElementById("WUB_load"))==null||_a.remove();const boxWindow=document.getElementById("WUB_window");boxWindow.style.visibility="visible",document.body.dispatchEvent(new Event("wubox:iframe:loaded"))}function showBox(caption,url,imageGroup){var _a;const boxOverlay=document.getElementById("WUB_overlay")||document.createElement("div");boxOverlay.id="WUB_overlay";const boxWindow=document.getElementById("WUB_window")||document.createElement("div");boxWindow.id="WUB_window",document.body.insertAdjacentElement("beforeend",boxOverlay),document.body.insertAdjacentElement("beforeend",boxWindow),boxOverlay.addEventListener("click",removeBox),document.body.classList.add("modal-open");const loader=document.createElement("div");loader.id="WUB_load",loader.style.display="block";const loaded=()=>{boxWindow.style.visibility="visible",document.body.dispatchEvent(new Event("wubox:load")),loader.remove(),refreshBox()};document.body.insertAdjacentElement("beforeend",loader);const baseURL=url.split("?")[0],urlString=/\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$/;if(!!baseURL.toLowerCase().match(urlString))createImageBox(boxWindow,loaded,url,caption,imageGroup);else{const queryString=url.replace(/^[^\?]+\??/,""),searchParams=new URLSearchParams(queryString),params={width:parseInt(searchParams.get("width")||"")||630,height:parseInt(searchParams.get("height")||"")||440,modal:!!searchParams.get("modal")||!1,inlineId:searchParams.get("inlineId")||""};url.includes("WUB_iframe")?createIframeBox(boxWindow,boxOverlay,loaded,url,caption,params):url.includes("WUB_inline")?createInlineBox(boxWindow,boxOverlay,loaded,caption,params):createAjaxBox(boxWindow,boxOverlay,loaded,url,caption,params),(_a=document.getElementById("WUB_closeWindowButton"))==null||_a.addEventListener("click",removeBox)}const closeBoxWindowButton=document.getElementById("WUB_closeWindowButton"),closeIcon=closeBoxWindowButton==null?void 0:closeBoxWindowButton.querySelector(".wutb-close-icon");closeIcon&&(closeIcon.offsetWidth||closeIcon.offsetHeight||closeIcon.getClientRects().length)&&closeBoxWindowButton.focus()}function initForm(form){form.addEventListener("submit",formSubmit(form))}const setBodyListeners=domChunk=>{document.body.addEventListener("wubox:iframe:loaded",()=>{var _a;(_a=document.getElementById("WUB_window"))==null||_a.classList.remove("wubox-loading")}),document.body.addEventListener("wubox:load",()=>{const form=document.querySelector("#WUB_ajaxContent .wu_form");form&&(initForm(form),wu_initialize_editors())})},onClickEvent=event=>{event.preventDefault();const target=event.currentTarget,caption=target.title||target.name||"",url=target.href||target.alt,imageGroup=target.rel||!1;showBox(caption,url,imageGroup),target.blur()},initBox=(domChunk,addGlobalListeners=!1,addMutationObserver=!1)=>{if(document.querySelectorAll(domChunk).forEach(el=>{el.removeEventListener("click",onClickEvent),el.addEventListener("click",onClickEvent)}),addGlobalListeners&&setBodyListeners(),addMutationObserver){const observerOptions={childList:!0,subtree:!0};new MutationObserver(()=>{initBox(domChunk,!1,!1)}).observe(document.body,observerOptions)}},removeBox=()=>{var _a,_b,_c;(_a=document.getElementById("WUB_ImageOff"))==null||_a.removeEventListener("click",removeBox),(_b=document.getElementById("WUB_closeWindowButton"))==null||_b.removeEventListener("click",removeBox),document.body.classList.remove("modal-open"),(_c=document.getElementById("WUB_load"))==null||_c.remove(),fadeOutEffect(document.getElementById("WUB_window"),200),fadeOutEffect(document.getElementById("WUB_overlay"),150,()=>{document.body.dispatchEvent(new Event("wubox:unload")),document.querySelectorAll("#WUB_window, #WUB_overlay, #WUB_HideSelect").forEach(el=>el.remove()),document.body.dispatchEvent(new Event("wubox:removed"))})},refreshBox=()=>{const form=document.querySelector("#WUB_ajaxContent .wu_form");if(!form)return;wu_initialize_editors();const content=document.getElementById("WUB_ajaxContent"),boxWindow=document.getElementById("WUB_window");content.style.height="100vh";const max_height=window.innerHeight-120,height=form.offsetHeight>=max_height?max_height:form.offsetHeight+1;boxWindow.style.transition="margin 200ms",content.style.height=height+"px",boxWindow.style.marginTop="-"+height/2+"px"},setBoxWidth=width=>{const content=document.getElementById("WUB_ajaxContent"),boxWindow=document.getElementById("WUB_window");content&&(content.style.transition="width 150ms",boxWindow.style.transition="margin 150ms",content.style.width=width+"px",boxWindow.style.marginLeft="-"+width/2+"px",boxWindow.style.width=width+"px",setTimeout(()=>{refreshBox()},150))};window.wubox={init:initBox,show:showBox,remove:removeBox,refresh:refreshBox,width:setBoxWidth};window.addEventListener("DOMContentLoaded",()=>{window.wubox.init(".wubox",!0,!0)});
|
|
})() |