Files
wp-multisite-waas/assets/js/wubox.min.js
David Stone d88e50df38 Prep Plugin for release on WordPress.org (#23)
* Update translation text domain
* Escape everything that should be escaped.
* Add nonce checks where needed.
* Sanitize all inputs.
* Apply Code style changes across the codebase.
* Correct many deprecation notices.
* Optimize load order of many filters.
* Add Proper Build script
* Use emojii flags
* Fix i18n deprecation  notice for translating too early
* Put all scripts in footer and load async
2025-04-14 11:36:46 -06:00

57 lines
9.6 KiB
JavaScript

(()=>{let l=(r,s,c,u,m)=>{let w="",h="",v="",y="",b="",g="",B="",t=!1;if(m){var n=document.querySelectorAll(`a[rel="${m}"]`);for(let e=0;e<n.length&&""===g;e++)n[e].href!==c?t?(y=n[e].title,b=n[e].href,g="<span id='WUB_next'>&nbsp;&nbsp;<a href='#'>"+wuboxL10n.next+"</a></span>"):(w=n[e].title,h=n[e].href,v="<span id='WUB_prev'>&nbsp;&nbsp;<a href='#'>"+wuboxL10n.prev+"</a></span>"):(t=!0,B=wuboxL10n.image+" "+(e+1)+" "+wuboxL10n.of+" "+n.length)}let p=new Image;p.onload=()=>{p.onload=null,t=document.documentElement,e=window.innerWidth||self.innerWidth||t&&t.clientWidth||document.body.clientWidth,t=window.innerHeight||self.innerHeight||t&&t.clientHeight||document.body.clientHeight;var e={width:e,height:t},t=e.width-150,e=e.height-150;let n=p.width,d=p.height,i=(n>t?(d*=t/n,n=t,d>e&&(n*=e/d,d=e)):d>e&&(n*=e/d,d=e,n>t)&&(d*=t/n,n=t),f(r,n,d),r.insertAdjacentHTML("beforeend",`
<a href='' id='WUB_ImageOff'>
<span class='screen-reader-text'>${wuboxL10n.close}</span>
<img id='WUB_Image' src='${c}' width='${n}' height='${d}' alt='${u}'/>
</a>
<div id='WUB_caption'>
${u}
<div id='WUB_secondLine'>
${B+v+g}
</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>
`),null!=(e=document.getElementById("WUB_closeWindowButton"))&&e.addEventListener("click",W),()=>{r.innerHTML="",l(),_(w,h,m)}),o=()=>{r.innerHTML="",l(),_(y,b,m)},a=(null!=(t=document.getElementById("WUB_prev"))&&t.addEventListener("click",i),null!=(e=document.getElementById("WUB_next"))&&e.addEventListener("click",o),e=>{"Escape"===e.key?W():"ArrowRight"===e.key&&g?o():"ArrowLeft"===e.key&&v&&i()}),l=()=>{window.removeEventListener("keydown",a),document.body.removeEventListener("wubox:unload",l)};window.addEventListener("keydown",a),document.body.addEventListener("wubox:unload",l),null!=(t=document.getElementById("WUB_ImageOff"))&&t.addEventListener("click",W),s()},p.src=c},r=(e,t,n,d,i,o)=>{var a,d=d.split("WUB_");null!=(a=document.getElementById("WUB_load"))&&a.remove(),o.modal?(t.removeEventListener("click",W),e.insertAdjacentHTML("beforeend",`
<iframe
id='WUB_iframeContent'
frameborder='0'
hspace='0'
allowtransparency='true'
src='${d[0]}'
name='WUB_iframeContent${Math.round(1e3*Math.random())}'
style='width:${o.width+29}px; height:${o.height+17}px;'>${wuboxL10n.noiframes}
</iframe>`),null!=(a=document.getElementById("WUB_iframeContent"))&&a.addEventListener("load",m)):(e.insertAdjacentHTML("beforeend",`
<div id='WUB_title'>
<div id='WUB_ajaxWindowTitle'>${i}</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='${d[0]}'
name='WUB_iframeContent${Math.round(1e3*Math.random())}'
style='width:${o.width+29}px; height:${o.height+17}px;'
>
${wuboxL10n.noiframes}
</iframe>`),null!=(t=document.getElementById("WUB_iframeContent"))&&t.addEventListener("load",m)),f(e,o.width,o.height),n()},s=(e,t,n,d)=>("visible"!==e.style.visibility?d.modal?(t.removeEventListener("click",W),e.insertAdjacentHTML("beforeend",`<div id='WUB_ajaxContent' class='WUB_modal' style='width:${d.width}px; height:${d.height}px;'></div>`)):e.insertAdjacentHTML("beforeend",`
<div id='WUB_title'>
<div id='WUB_ajaxWindowTitle'>${n}</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:${d.width}px; height:${d.height}px;'></div>`):((t=document.getElementById("WUB_ajaxContent")).style.width=d.width+"px",t.style.height=d.height+"px",t.scrollTop=0,t.innerHTML=n),document.getElementById("WUB_ajaxContent")),c=(t,e,n,d,i,o)=>{let a=s(t,e,i,o);e=d+(d.includes("?")?"&":"?")+"random="+(new Date).getTime();fetch(e,{headers:{"X-Requested-With":"XMLHttpRequest"}}).then(e=>e.text()).then(e=>{a.innerHTML=e,f(t,o.width,o.height),n()})},u=(e,t,n,d,i)=>{let o=s(e,t,d,i),a=document.getElementById(i.inlineId),l=(o.insertAdjacentElement("beforeend",null==a?void 0:a.children[0]),()=>{null!=a&&a.insertAdjacentElement("afterbegin",o.children[0]),document.body.removeEventListener("wubox:unload",l)});document.body.addEventListener("wubox:unload",l),f(e,i.width,i.height),n()},t=d=>async e=>{e.preventDefault();var t=d.querySelector("textarea[data-editor]"),n=t?d.querySelector('input[name="'+t.id+'"]'):null,n=(t&&n&&(n.value=t.value),wu_block_ui(d)),t=(window["wu_"+d.getAttribute("id")+"_errors"]&&(window["wu_"+d.getAttribute("id")+"_errors"].errors=[]),e.submitter.value),e=new FormData(d),t=(e.append("submit",t),await fetch(d.getAttribute("action"),{method:"POST",body:e,headers:{"X-Requested-With":"XMLHttpRequest"}}).then(e=>e.text()).then(e=>e?JSON.parse(e):null));null===t||null===t.data?(n.unblock(),W()):(t.success||(n.unblock(),e=d.getAttribute("id"),window["wu_"+e+"_errors"]&&(window["wu_"+e+"_errors"].errors=t.data),null!=(e=document.querySelector('[data-wu-app="'+e+'_errors"]'))&&e.setAttribute("tabindex","-1"),null!=e&&e.focus()),"object"==typeof t.data.tables&&(n.unblock(),W(),Object.keys(t.data.tables).forEach(e=>{window[e].update()})),"string"==typeof t.data.redirect_url&&(window.location.href=t.data.redirect_url),"object"==typeof t.data.send&&window[t.data.send.scope][t.data.send.function_name](t.data.send.data,W))};function f(e,t,n){e.style.marginLeft="-"+t/2+"px",e.style.marginTop="-"+n/2+"px"}function n(d,i,o){let a=parseFloat(getComputedStyle(d).opacity),l=null;requestAnimationFrame(function e(t){var t=t-(l=l||t),n=Math.max(a-t/i,0);d.style.opacity=n.toString(),t<i?requestAnimationFrame(e):(d.style.display="none",o&&o())})}function m(){var e;null!=(e=document.getElementById("WUB_load"))&&e.remove(),document.getElementById("WUB_window").style.visibility="visible",document.body.dispatchEvent(new Event("wubox:iframe:loaded"))}function _(e,t,n){var d=document.getElementById("WUB_overlay")||document.createElement("div");d.id="WUB_overlay";let i=document.getElementById("WUB_window")||document.createElement("div"),o=(i.id="WUB_window",document.body.insertAdjacentElement("beforeend",d),document.body.insertAdjacentElement("beforeend",i),d.addEventListener("click",W),document.body.classList.add("modal-open"),document.createElement("div"));o.id="WUB_load",o.style.display="block";var a=()=>{i.style.visibility="visible",document.body.dispatchEvent(new Event("wubox:load")),o.remove(),w()};document.body.insertAdjacentElement("beforeend",o);!!t.split("?")[0].toLowerCase().match(/\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$/)?l(i,a,t,e,n):(n=t.replace(/^[^\?]+\??/,""),n=new URLSearchParams(n),n={width:parseInt(n.get("width")||"")||630,height:parseInt(n.get("height")||"")||440,modal:!!n.get("modal")||!1,inlineId:n.get("inlineId")||""},t.includes("WUB_iframe")?r(i,d,a,t,e,n):t.includes("WUB_inline")?u(i,d,a,e,n):c(i,d,a,t,e,n),null!=(d=document.getElementById("WUB_closeWindowButton"))&&d.addEventListener("click",W));a=document.getElementById("WUB_closeWindowButton"),t=null==a?void 0:a.querySelector(".wutb-close-icon");t&&(t.offsetWidth||t.offsetHeight||t.getClientRects().length)&&a.focus()}let d=e=>{document.body.addEventListener("wubox:iframe:loaded",()=>{var e;null!=(e=document.getElementById("WUB_window"))&&e.classList.remove("wubox-loading")}),document.body.addEventListener("wubox:load",()=>{var e=document.querySelector("#WUB_ajaxContent .wu_form");e&&((e=e).addEventListener("submit",t(e)),wu_initialize_editors())})},i=e=>{e.preventDefault();e=e.currentTarget;_(e.title||e.name||"",e.href||e.alt,e.rel||!1),e.blur()},o=(e,t=!1,n=!1)=>{document.querySelectorAll(e).forEach(e=>{e.removeEventListener("click",i),e.addEventListener("click",i)}),t&&d(),n&&(t={childList:!0,subtree:!0},new MutationObserver(()=>{o(e,!1,!1)}).observe(document.body,t))},W=()=>{var e;null!=(e=document.getElementById("WUB_ImageOff"))&&e.removeEventListener("click",W),null!=(e=document.getElementById("WUB_closeWindowButton"))&&e.removeEventListener("click",W),document.body.classList.remove("modal-open"),null!=(e=document.getElementById("WUB_load"))&&e.remove(),n(document.getElementById("WUB_window"),200),n(document.getElementById("WUB_overlay"),150,()=>{document.body.dispatchEvent(new Event("wubox:unload")),document.querySelectorAll("#WUB_window, #WUB_overlay, #WUB_HideSelect").forEach(e=>e.remove()),document.body.dispatchEvent(new Event("wubox:removed"))})},w=()=>{var e,t,n,d=document.querySelector("#WUB_ajaxContent .wu_form");d&&(wu_initialize_editors(),e=document.getElementById("WUB_ajaxContent"),t=document.getElementById("WUB_window"),e.style.height="100vh",n=window.innerHeight-120,n=d.offsetHeight>=n?n:d.offsetHeight+1,t.style.transition="margin 200ms",e.style.height=n+"px",t.style.marginTop="-"+n/2+"px")};window.wubox={init:o,show:_,remove:W,refresh:w,width:e=>{var t=document.getElementById("WUB_ajaxContent"),n=document.getElementById("WUB_window");t&&(t.style.transition="width 150ms",n.style.transition="margin 150ms",t.style.width=e+"px",n.style.marginLeft="-"+e/2+"px",n.style.width=e+"px",setTimeout(()=>{w()},150))}},window.addEventListener("DOMContentLoaded",()=>{window.wubox.init(".wubox",!0,!0)})})();