AlkantarClanX12
Current Path : /home/thanudqk/128shen.com/wp-content/themes/thegem/js/ |
Current File : //home/thanudqk/128shen.com/wp-content/themes/thegem/js/thegem-delay-javascript.min.js |
class TheGemDelayJavaScript{constructor(){this.triggerEvents=["keydown","mousedown","mousemove","touchmove","touchstart","touchend","wheel"],this.userEventHandler=this.triggerListener.bind(this),this.touchStartHandler=this.onTouchStart.bind(this),this.touchMoveHandler=this.onTouchMove.bind(this),this.touchEndHandler=this.onTouchEnd.bind(this),this.clickHandler=this.onClick.bind(this),this.interceptedClicks=[],this.delayedScripts={normal:[],async:[],defer:[]},this.allJQueries=[]}addUserInteractionListener(e){document.hidden?e.triggerListener():(this.triggerEvents.forEach(t=>window.addEventListener(t,e.userEventHandler,{passive:!0})),window.addEventListener("touchstart",e.touchStartHandler,{passive:!0}),window.addEventListener("mousedown",e.touchStartHandler),document.addEventListener("visibilitychange",e.userEventHandler))}removeUserInteractionListener(){this.triggerEvents.forEach(e=>window.removeEventListener(e,this.userEventHandler,{passive:!0})),document.removeEventListener("visibilitychange",this.userEventHandler)}onTouchStart(e){"HTML"!==e.target.tagName&&(window.addEventListener("touchend",this.touchEndHandler),window.addEventListener("mouseup",this.touchEndHandler),window.addEventListener("touchmove",this.touchMoveHandler,{passive:!0}),window.addEventListener("mousemove",this.touchMoveHandler),e.target.addEventListener("click",this.clickHandler),this.renameDOMAttribute(e.target,"onclick","thegem-onclick"))}onTouchMove(e){window.removeEventListener("touchend",this.touchEndHandler),window.removeEventListener("mouseup",this.touchEndHandler),window.removeEventListener("touchmove",this.touchMoveHandler,{passive:!0}),window.removeEventListener("mousemove",this.touchMoveHandler),e.target.removeEventListener("click",this.clickHandler),this.renameDOMAttribute(e.target,"thegem-onclick","onclick")}onTouchEnd(e){window.removeEventListener("touchend",this.touchEndHandler),window.removeEventListener("mouseup",this.touchEndHandler),window.removeEventListener("touchmove",this.touchMoveHandler,{passive:!0}),window.removeEventListener("mousemove",this.touchMoveHandler)}onClick(e){e.target.removeEventListener("click",this.clickHandler),this.renameDOMAttribute(e.target,"thegem-onclick","onclick"),this.interceptedClicks.push(e),e.preventDefault(),e.stopPropagation(),e.stopImmediatePropagation()}replayClicks(){window.removeEventListener("touchstart",this.touchStartHandler,{passive:!0}),window.removeEventListener("mousedown",this.touchStartHandler),this.interceptedClicks.forEach(e=>{e.target.dispatchEvent(new MouseEvent("click",{view:e.view,bubbles:!0,cancelable:!0}))})}renameDOMAttribute(e,t,i){e.hasAttribute&&e.hasAttribute(t)&&(event.target.setAttribute(i,event.target.getAttribute(t)),event.target.removeAttribute(t))}triggerListener(){this.removeUserInteractionListener(this),"loading"===document.readyState?document.addEventListener("DOMContentLoaded",this.startLoadingScripts.bind(this)):this.startLoadingScripts()}async startLoadingScripts(){this.delayEventListeners(),this.delayJQueryReady(this),this.handleDocumentWrite(),this.registerAllDelayedScripts(),this.preloadAllScripts(),await this.loadScriptsFromList(this.delayedScripts.normal),await this.loadScriptsFromList(this.delayedScripts.defer),await this.loadScriptsFromList(this.delayedScripts.async);try{await this.triggerDOMContentLoaded(),await this.triggerWindowLoad()}catch(e){}window.dispatchEvent(new Event("thegem-allScriptsLoaded")),this.replayClicks()}registerAllDelayedScripts(){document.querySelectorAll("script[type=thegemdelayscript]").forEach(e=>{e.hasAttribute("src")?e.hasAttribute("async")&&!1!==e.async?this.delayedScripts.async.push(e):e.hasAttribute("defer")&&!1!==e.defer||"module"===e.getAttribute("data-thegem-type")?this.delayedScripts.defer.push(e):this.delayedScripts.normal.push(e):this.delayedScripts.normal.push(e)})}async transformScript(e){return await this.requestAnimFrame(),new Promise(t=>{let i=document.createElement("script");[...e.attributes].forEach(e=>{let t=e.nodeName;"type"!==t&&("data-thegem-type"===t&&(t="type"),i.setAttribute(t,e.nodeValue))}),e.hasAttribute("src")?(i.addEventListener("load",t),i.addEventListener("error",t)):(i.text=e.text,t());try{e.parentNode.replaceChild(i,e)}catch(r){t()}})}async loadScriptsFromList(e){let t=e.shift();return t?(await this.transformScript(t),this.loadScriptsFromList(e)):Promise.resolve()}preloadAllScripts(){var e=document.createDocumentFragment();[...this.delayedScripts.normal,...this.delayedScripts.defer,...this.delayedScripts.async].forEach(t=>{let i=t.getAttribute("src");if(i){let r=document.createElement("link");r.href=i,r.rel="preload",r.as="script",e.appendChild(r)}}),document.head.appendChild(e)}delayEventListeners(){let e={};function t(t,i){!function(t){function i(i){return e[t].eventsToRewrite.indexOf(i)>=0?"thegem-"+i:i}e[t]||(e[t]={originalFunctions:{add:t.addEventListener,remove:t.removeEventListener},eventsToRewrite:[]},t.addEventListener=function(){arguments[0]=i(arguments[0]),e[t].originalFunctions.add.apply(t,arguments)},t.removeEventListener=function(){arguments[0]=i(arguments[0]),e[t].originalFunctions.remove.apply(t,arguments)})}(t),e[t].eventsToRewrite.push(i)}function i(e,t){let i=e[t];Object.defineProperty(e,t,{get:()=>i||function(){},set(r){e["thegem"+t]=i=r}})}t(document,"DOMContentLoaded"),t(window,"DOMContentLoaded"),t(window,"load"),t(window,"pageshow"),t(document,"readystatechange"),i(document,"onreadystatechange"),i(window,"onload"),i(window,"onpageshow")}delayJQueryReady(e){let t=window.jQuery;Object.defineProperty(window,"jQuery",{get:()=>t,set(i){if(i&&i.fn&&!e.allJQueries.includes(i)){i.fn.ready=i.fn.init.prototype.ready=function(t){e.domReadyFired?t.bind(document)(i):document.addEventListener("thegem-DOMContentLoaded",()=>t.bind(document)(i))};let r=i.fn.on;i.fn.on=i.fn.init.prototype.on=function(){if(this[0]===window){function e(e){return e.split(" ").map(e=>"load"===e||0===e.indexOf("load.")?"thegem-jquery-load":e).join(" ")}"string"==typeof arguments[0]||arguments[0]instanceof String?arguments[0]=e(arguments[0]):"object"==typeof arguments[0]&&Object.keys(arguments[0]).forEach(t=>{delete Object.assign(arguments[0],{[e(t)]:arguments[0][t]})[t]})}return r.apply(this,arguments),this},e.allJQueries.push(i)}t=i}})}async triggerDOMContentLoaded(){this.domReadyFired=!0,await this.requestAnimFrame(),document.dispatchEvent(new Event("thegem-DOMContentLoaded")),await this.requestAnimFrame(),window.dispatchEvent(new Event("thegem-DOMContentLoaded")),await this.requestAnimFrame(),document.dispatchEvent(new Event("thegem-readystatechange")),await this.requestAnimFrame(),document.thegemonreadystatechange&&document.thegemonreadystatechange()}async triggerWindowLoad(){await this.requestAnimFrame(),window.dispatchEvent(new Event("thegem-load")),await this.requestAnimFrame(),window.thegemonload&&window.thegemonload(),await this.requestAnimFrame(),this.allJQueries.forEach(e=>e(window).trigger("thegem-jquery-load")),window.dispatchEvent(new Event("thegem-pageshow")),await this.requestAnimFrame(),window.thegemonpageshow&&window.thegemonpageshow()}handleDocumentWrite(){let e=new Map;document.write=document.writeln=function(t){let i=document.currentScript,r=document.createRange(),n=i.parentElement,a=e.get(i);void 0===a&&(a=i.nextSibling,e.set(i,a));let s=document.createDocumentFragment();r.setStart(s,0),s.appendChild(r.createContextualFragment(t)),n.insertBefore(s,a)}}async requestAnimFrame(){return document.hidden?new Promise(e=>setTimeout(e)):new Promise(e=>requestAnimationFrame(e))}static init(){let e=new TheGemDelayJavaScript;e.addUserInteractionListener(e)}}TheGemDelayJavaScript.init();