fictioneer/js/utility.min.js
Tetrakern 4e857dfe79 Remove deprecated property
Why did I even use that?
2023-06-15 13:11:15 +02:00

1 line
6.7 KiB
JavaScript

const _$=document.querySelector.bind(document),_$$=document.querySelectorAll.bind(document),_$$$=document.getElementById.bind(document);async function fcn_ajaxPost(e={},t=null,n={}){t||(t=fictioneer_ajax.ajax_url),final_headers={"Content-Type":"application/x-www-form-urlencoded","Cache-Control":"no-cache"},final_headers={...final_headers,...n},e={nonce:fcn_getNonce(),...e};const c=await fetch(t,{method:"POST",credentials:"same-origin",headers:final_headers,mode:"same-origin",body:new URLSearchParams(e)});return c.ok?c.json():Promise.reject(c)}async function fcn_ajaxGet(e={},t=null,n={}){t=t||fictioneer_ajax.ajax_url,t=fcn_buildUrl(e={nonce:fcn_getNonce(),...e},t),final_headers={"Content-Type":"application/x-www-form-urlencoded","Cache-Control":"no-cache"},final_headers={...final_headers,...n};const c=await fetch(t,{method:"GET",credentials:"same-origin",headers:final_headers,mode:"same-origin"});return c.ok?c.json():Promise.reject(c)}function fcn_evaluateAsBoolean(e,t=!1){if("boolean"==typeof e)return e;if(void 0===e)return t;if(e instanceof HTMLInputElement&&"checkbox"===e.getAttribute("type"))return e.checked;e instanceof HTMLElement&&(e.hasAttribute("value")?e=e.value:e.hasAttribute("data-value")&&(e=e.dataset.value));const n=String(e),c=parseInt(e);return"true"===n||"1"===n||1===c||"false"!==n&&"0"!==n&&0!==c&&t}function fcn_copyToClipboard(e,t=!1){t=t||__("Copied to clipboard!","fictioneer"),navigator.clipboard&&(navigator.clipboard.writeText(e),t&&fcn_showNotification(t,2))}function fcn_isValidJSONString(e){try{JSON.parse(e)}catch(e){return!1}return!0}function fcn_removeItemOnce(e,t){var n=e.indexOf(t);return n>-1&&e.splice(n,1),e}function fcn_clamp(e,t,n){return Math.min(Math.max(n,e),t)}function fcn_updateThemeColor(e=!1){const t=fcn_siteSettings.darken?fcn_siteSettings.darken:0,n=fcn_siteSettings.saturation?fcn_siteSettings.saturation:0,c=fcn_siteSettings["hue-rotate"]?fcn_siteSettings["hue-rotate"]:0,o=t>=0?1+Math.pow(t,2):1-Math.pow(t,2),i=n>=0?1+Math.pow(n,2):1-Math.pow(n,2);let a=fcn_cssVars.getPropertyValue("--theme-color-base").trim().split(" ");a=`hsl(${(parseInt(a[0])+c)%360}deg ${(parseInt(a[1])*i).toFixed(2)}% ${(parseInt(a[2])*o).toFixed(2)}%)`,_$("meta[name=theme-color]").setAttribute("content",e||a)}function fcn_offset(e){const t=e.getBoundingClientRect();return{top:t.top+window.scrollY,left:t.left+window.scrollX}}function fcn_throttle(e,t,n){var c,o,i,a=null,l=0;n||(n={});var r=function(){l=!1===n.leading?0:Date.now(),a=null,i=e.apply(c,o),a||(c=o=null)};return function(){var s=Date.now();l||!1!==n.leading||(l=s);var u=t-(s-l);return c=this,o=arguments,u<=0||u>t?(a&&(clearTimeout(a),a=null),l=s,i=e.apply(c,o),a||(c=o=null)):a||!1===n.trailing||(a=setTimeout(r,u)),i}}function fcn_bindEventToAnimationFrame(e,t,n=window){var c=!1;n.addEventListener(e,(function(){c||(c=!0,requestAnimationFrame((()=>{n.dispatchEvent(new CustomEvent(t)),c=!1})))}))}var fcn_lastClicked;function fcn_toggleLastClicked(e){const t=!e.classList.contains("last-clicked");e.classList.toggle("last-clicked",t),e.closest(".watch-last-clicked")?.classList.toggle("has-last-clicked",t),fcn_lastClicked&&fcn_lastClicked!=e&&fcn_lastClicked.classList.remove("last-clicked"),fcn_lastClicked=e}function fcn_cleanTextSelectionFromButtons(e){return e=(e=(e=(e=(e=(e=e.replace(/[\r\n]{2,}/g,"__$__")).replace(new RegExp("(__Bookmark|__Quote|__Link)","g"),"")).replace(new RegExp("(__Bookmark|__Quote|__TTS|__Link)","g"),"")).replace(new RegExp("(__Bookmark|__Quote|__Suggestion|__TTS|__Link)","g"),"")).replace(new RegExp("(__Bookmark|__Quote|__Suggestion|__Link)","g"),"")).replace(/[__$]{1,}/g,"\n\n").replace(/^[\r\n]+|[\r\n]+$/g,"")}function fcn_deleteCookie(e){document.cookie=e+"=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/"}function fcn_deleteAllCookies(){localStorage.clear(),document.cookie.split(";").forEach((e=>{document.cookie=e.replace(/^ +/,"").replace(/=.*/,"=;expires="+(new Date).toUTCString()+";path=/")}))}function fcn_setCookie(e,t,n=30){const c=new Date;c.setTime(c.getTime()+24*n*60*60*1e3);const o="expires="+c.toUTCString();document.cookie=e+"="+encodeURIComponent(t)+";"+o+";SameSite=Strict;path=/"}function fcn_getCookie(e){const t=e+"=",n=document.cookie.split(";");for(var c=0;c<n.length;c++){const e=n[c].trim();if(0==e.indexOf(t))return decodeURIComponent(e.substring(t.length,e.length))}return null}function fcn_isValidUrl(e){return!!e&&null!=e.match(/^(https?:\/\/)/)}_$("body").addEventListener("click",(e=>{!["BUTTON","A"].includes(e.target.tagName)&&e.target.closest(".toggle-last-clicked")||null!==e.target.closest(".escape-last-click")||fcn_lastClicked&&e.currentTarget!=fcn_lastClicked&&(fcn_lastClicked.closest(".watch-last-clicked")?.classList.remove("has-last-clicked"),fcn_lastClicked.classList.remove("last-clicked"),fcn_lastClicked=null)})),_$("body").addEventListener("keydown",(e=>{27==e.keyCode&&fcn_lastClicked&&(fcn_lastClicked.closest(".watch-last-clicked")?.classList.remove("has-last-clicked"),fcn_lastClicked.classList.remove("last-clicked"),fcn_lastClicked=null,document.activeElement?.blur())}));var fcn_defaultNonce=_$$$("fictioneer-nonce")?.value??0;function fcn_getNonce(){return _$$$("fictioneer-ajax-nonce")?.value??fcn_defaultNonce}function fcn_matchFingerprint(e){return"undefined"!=typeof fcn_fingerprint&&fcn_fingerprint.fingerprint===e}function fcn_buildUrl(e={},t=null){return t=t?new URL(t):new URL(window.location.protocol+"//"+window.location.host+window.location.pathname),e&&Object.keys(e).forEach((n=>{t.searchParams.append(n,e[n])})),t}function fcn_buildErrorNotice(e,t=!1){const n=document.createElement("div");let c=e;return t&&(n.id=t),n.classList="notice _warning","object"==typeof e&&(c="",e.status&&(c=`${e.status}: `),e.statusText&&(c+=e.statusText),c||(c="Unknown error.")),n.innerHTML=`<i class="fa-solid fa-triangle-exclamation"></i><div>${fcn_sanitizeHTML(c)}</div>`,n}function fcn_sanitizeHTML(e){const t=document.createElement("div");return t.innerText=e instanceof HTMLElement?e.innerHTML:e,t.innerHTML}function fcn_resizeInput(e,t=null){t=t||e.value.length,e.style.width=.88*t+2+"ch"}function fcn_ariaCheckedUpdate(e){const t=e.closest('[role="checkbox"][aria-checked]');if(t){const n=fcn_evaluateAsBoolean(e);t.ariaChecked=n}}function fcn_scrollToAnchor(e){return _$(`[name="${e.getAttribute("href").replace("#","")}"]`).scrollIntoView({behavior:"smooth",block:"center"}),!1}function fcn_detectScreenCollision(e){const t=e.getBoundingClientRect(),n=window.innerHeight??document.documentElement.clientHeight,c=(e.closest(".popup-menu-toggle")?.clientHeight??32)+16,o=n-t.bottom-e.clientHeight,i=t.top-e.clientHeight,a=[];return t.top<=50&&o>50+c&&a.push("top"),t.bottom>=n-50&&i>50+c&&a.push("bottom"),a}function fcn_scrollTo(e){window.scrollTo({top:e.getBoundingClientRect().top+window.scrollY-64,behavior:"smooth"})}