-
Notifications
You must be signed in to change notification settings - Fork 3
/
pjax.min.js
2 lines (2 loc) · 6.44 KB
/
pjax.min.js
1
2
/* Pjax.js 0.7.0 | Copyright (c) 2024 Guilherme Nascimento ([email protected]) | Released under the MIT license */
!function(){"use strict";var e,t,r,n,a,o,i="undefined"!=typeof window?window:{},c=i.document||{},l=i.history,s=!!i.DOMParser,f=!!i.FormData,d=!!i.URLSearchParams,p={},u=c.implementation,h=i.location,m=h?h.protocol+"//"+h.host:"",v=!1,g=i.Element&&i.Element.prototype,x=[].slice,T=":not([data-pjax-ignore=true])",y=!!(g&&l.pushState&&(s||u&&u.createHTMLDocument)),L=[{attr:"containers",cfg:"containers"},{attr:"replace",cfg:"replace"},{attr:"update-head",cfg:"updateHead"},{attr:"insertion",cfg:"insertion"},{attr:"loader",cfg:"loader"},{attr:"scroll-left",cfg:"scrollLeft"},{attr:"scroll-top",cfg:"scrollTop"},{attr:"done",cfg:"done"},{attr:"fail",cfg:"fail"}];if(s)try{s=!!((e=_("<html><head><title>1</title></head><body>1</body></html>")).head&&e.title&&e.body)}catch(j){s=!1}if(d)try{(e=new FormData).append("a","1"),d="a=1"===P(e)}catch(S){d=!1}function $(e,t,r){x.call(t?e.querySelectorAll(t):e).forEach(r),r=o}function _(e){if(s)return new DOMParser().parseFromString(e,"text/html");var t=u.createHTMLDocument("");return/^\s*<(\!doctype|html)[^>]*>/i.test(e)?(t.documentElement.innerHTML=e,t.body&&t.head||(t=u.createHTMLDocument("")).write(e)):t.body.innerHTML=e,t}function E(e,t,r,n){var a=p[e];if(a)for(var o=0,i=a.length;o<i;o++)a[o](t,r,n)}function H(e,t,r){if("function"==typeof t){p[e]||(p[e]=[]);var n=p[e];if(r){var a=n.indexOf(t);a>-1&&n.splice(a,1)}else n.push(t)}}function M(e,t,r,n){var a,s,f,d,u,h,m=r.containers,v=t&&_(t),g=v&&v.body;if(!g||!g.querySelectorAll(m.join(",")).length)return"No such containers";p.dom&&E("dom",e,v);var x=r.insertion,T=r.scrollLeft>-1?+r.scrollLeft:i.scrollX||i.pageXOffset,y=r.scrollTop>-1?+r.scrollTop:i.scrollY||i.pageYOffset;r.updateHead&&v.head&&(a=v,d=[],u=c.head,$(a.head.children,o,function(e){"TITLE"!==e.tagName&&d.push(e.outerHTML)}),$(u.children,o,function(e){("TITLE"===e.tagName||C(e,"resource"))&&(-1===(s=d.indexOf(e.outerHTML))?e.parentNode.removeChild(e):d.splice(s,1))}),(f=a.createElement("div")).innerHTML=d.join(""),$(f.children,o,function(e){C(e,"resource")||u.appendChild(e)}),f=d=o),c.title=v.title||"";for(var L=m.length-1;L>=0;L--)(h=g.querySelector(m[L]))&&$(c,m[L],function(e){if("append"===x||"prepend"===x){var t=c.createDocumentFragment();$(h.childNodes,o,function(e){t.appendChild(e)}),"append"===x?e.appendChild(t):e.insertBefore(t,e.firstChild),t=o}else e.innerHTML=h.innerHTML});var j={pjaxUrl:e,pjaxData:t,pjaxConfig:r};1===n?l.pushState(j,"",e):2===n&&l.replaceState(j,"",e),i.scrollTo(T,y),v=m=o}function C(e,t){var r=e.getAttribute("data-pjax-"+t);if("true"===r||"false"===r)return"true"===r;if(!r)return"";if(!isNaN(r))return parseFloat(r);if(r.length>3){var n=r[0],a=r[r.length-1];if("["===n&&"]"===a||"{"===n&&"}"===a)try{r=JSON.parse(r)}catch(o){console.error("Invalid value in data-pjax-"+t+" attribute on element:",e)}}return r}function b(e){var t,n,a=JSON.parse(JSON.stringify(r));if(e)for(var o=L.length-1;o>=0;o--)""!==(n=C(e,(t=L[o]).attr))&&(a[t.cfg]=n);return a}function N(e,t,r,o,i,c,l,s){t.loader&&(n&&clearTimeout(n),a.className+=" pjax-end",n=setTimeout(function(){n=0,a.className+=" pjax-hide"},1e3)),s||(s=M(e,c,t,r)),E(s?"fail":"done",e,l,s),E("then",e),i&&Function(i).call(o)}function D(){t&&t.abort()}function X(e,r,i,l,s,f){if(D(),E("initiate",e,f),f.loader&&function e(){if(n){clearTimeout(n),n=0,a.className="pjax-loader pjax-hide",setTimeout(e,20);return}a||((a=c.createElement("div")).innerHTML='<div class="pjax-progress"></div>',c.body.appendChild(a)),a.className="pjax-loader pjax-start",n=setTimeout(function(){n=0,a.className+=" pjax-inload"},10)}(),p.handler)return E("handler",{url:e,method:i,element:s,replace:2===r},function(t){N(e,f,r,s,f.done,t,0,o)},function(t){N(e,f,r,s,f.fail,"",-1,t)});var d,u,h=e,m=f.headers;for(var v in m["X-PJAX-Container"]=f.containers.join(","),m["X-PJAX"]="true",f.proxy&&(h=f.proxy+encodeURIComponent(h)),f.nocache&&(h=(d=h,u="_="+ +new Date,d+(-1===d.indexOf("?")?"?":"&")+u)),(t=new XMLHttpRequest).open(i,h,!0),m)t.setRequestHeader(v,m[v]);t.onreadystatechange=function(){if(4===t.readyState){var n=t.status;if(n>=200&&n<300){var a=t.getResponseHeader("X-PJAX-Container");a&&(f.containers=a.split(",")),N(t.getResponseHeader("X-PJAX-URL")||e,f,r,s,f.done,t.responseText,n,o)}else N(e,f,r,s,f.fail,"",n,"HTTP Error ("+n+")")}},t.send(l||"")}function A(e){if(0===e.button){for(var t,n=e.target;n.tagName&&"A"!==n.tagName;)n=n.parentNode;(t=n.href)&&n.matches(r.linkSelector)&&G(n.target,t)&&U("GET",t,o,n,e,b(n))}}function O(e){var t=e.target,n=t.action;if(f&&t.matches(r.formSelector)&&G(t.target,n)){var a=new FormData(t),i=String(t.method).toUpperCase(),c=t.enctype,l=b(t);if("POST"!==i&&(i="GET"),"GET"===i||"multipart/form-data"!==c){if(!d)return;a=P(a),"GET"===i?n=n.replace(/\?[\s\S]+/,"")+"?"+a:"text/plain"===c&&(a=a.replace(/&/g,"\n"),l.headers["Content-Type"]=c)}U(i,n,a,t,e,l),a=o}}function P(e){return new URLSearchParams(e)+""}function U(e,t,r,n,a,o){a.preventDefault();var i=o.replace||t===h+""?2:1;X(t,i,e,r,n,o)}function k(e){e.state&&e.state.pjaxUrl&&(D(),M(e.state.pjaxUrl,e.state.pjaxData,e.state.pjaxConfig,o),E("history",e.state.pjaxUrl,e.state))}function q(){if(!v){v=!0;var e=h+"";l.replaceState({pjaxUrl:e,pjaxData:c.documentElement.outerHTML,pjaxConfig:r},"",e),i.addEventListener("popstate",k),r.linkSelector&&c.addEventListener("click",A),r.formSelector&&c.addEventListener("submit",O)}}function R(){r&&v&&(c.removeEventListener("click",A),c.removeEventListener("submit",O),i.removeEventListener("popstate",k),v=!1,r=o)}function G(e,t){return(!(e=e.toLowerCase())||e===i.name||"_self"===e||"_top"===e&&w===i.top||"_parent"===e&&w===i.parent)&&(t===m||0===t.indexOf(m))}g&&!g.matches&&(g.matches=g.matchesSelector||g.mozMatchesSelector||g.msMatchesSelector||g.oMatchesSelector||g.webkitMatchesSelector||function(e){for(var t=(this.document||this.ownerDocument).querySelectorAll(e),r=t.length;--r>=0&&t[r]!==this;);return r>-1}),i.Pjax={supported:y,remove:R,start:function e(t){if(y&&/^https?:$/.test(h.protocol)){if(R(),r={linkSelector:"a",formSelector:"form",containers:["#pjax-container"],replace:!1,updateHead:!0,insertion:o,proxy:o,scrollLeft:0,scrollTop:0,nocache:!1,loader:!0,headers:{}},t){for(var n in r)n in t&&(r[n]=t[n]);t.linkSelector+=T,t.formSelector+=T,t=o}/^(interactive|complete)$/.test(c.readyState)?q():c.addEventListener("DOMContentLoaded",q)}},request:function(e,t){v&&X(e,t.replace?2:1,t.method,t.data,o,r)},on:function(e,t){H(e,t)},off:function(e,t){H(e,t,!0)}}}();