-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpjax.min.js
1 lines (1 loc) · 13.6 KB
/
pjax.min.js
1
var Pjax=function(){"use strict";var t=function(t,e,o){return t instanceof HTMLCollection||t instanceof NodeList||t instanceof Array?Array.prototype.forEach.call(t,e,o):e.call(o,t)},e=t,o=function(t,o,n,s){(o="string"==typeof o?o.split(" "):o).forEach((function(o){e(t,(function(t){t.addEventListener(o,n,s)}))}))},n=o,s={outerHTML:function(t,e){t.outerHTML=e.outerHTML,this.onSwitch()},innerHTML:function(t,e){t.innerHTML=e.innerHTML,""===e.className?t.removeAttribute("class"):t.className=e.className,this.onSwitch()},switchElementsAlt:function(t,e){if(t.innerHTML=e.innerHTML,e.hasAttributes())for(var o=e.attributes,n=0;n<o.length;n++)t.attributes.setNamedItem(o[n].cloneNode());this.onSwitch()},replaceNode:function(t,e){t.parentNode.replaceChild(e,t),this.onSwitch()},sideBySide:function(t,e,o,s){var i=Array.prototype.forEach,r=[],a=[],l=document.createDocumentFragment(),c="animationend webkitAnimationEnd MSAnimationEnd oanimationend",u=0,h=function(t){t.target===t.currentTarget&&--u<=0&&r&&(r.forEach((function(t){t.parentNode&&t.parentNode.removeChild(t)})),a.forEach((function(t){t.className=t.className.replace(t.getAttribute("data-pjax-classes"),""),t.removeAttribute("data-pjax-classes")})),a=null,r=null,this.onSwitch())}.bind(this);s=s||{},i.call(t.childNodes,(function(t){r.push(t),t.classList&&!t.classList.contains("js-Pjax-remove")&&(t.hasAttribute("data-pjax-classes")&&(t.className=t.className.replace(t.getAttribute("data-pjax-classes"),""),t.removeAttribute("data-pjax-classes")),t.classList.add("js-Pjax-remove"),s.callbacks&&s.callbacks.removeElement&&s.callbacks.removeElement(t),s.classNames&&(t.className+=" "+s.classNames.remove+" "+(o.backward?s.classNames.backward:s.classNames.forward)),u++,n(t,c,h,!0))})),i.call(e.childNodes,(function(t){if(t.classList){var e="";s.classNames&&(e=" js-Pjax-add "+s.classNames.add+" "+(o.backward?s.classNames.forward:s.classNames.backward)),s.callbacks&&s.callbacks.addElement&&s.callbacks.addElement(t),t.className+=e,t.setAttribute("data-pjax-classes",e),a.push(t),l.appendChild(t),u++,n(t,c,h,!0)}})),t.className=e.className,t.appendChild(l)}},i=s;function r(){window._gaq&&_gaq.push(["_trackPageview"]),window.ga&&ga("send","pageview",{page:location.pathname,title:document.title})}var a,l,c,u,h,d,f,p,m,w,v,g,y,b,E,x,A,R,T,j,N,P,S,q=(a=0,function(){var t="pjax"+(new Date).getTime()+"_"+a;return a++,t}),L=t,U=function(t,e,o){(e="string"==typeof e?e.split(" "):e).forEach((function(e){var n;(n=document.createEvent("HTMLEvents")).initEvent(e,!0,!0),n.eventName=e,o&&Object.keys(o).forEach((function(t){n[t]=o[t]})),L(t,(function(t){var e=!1;t.parentNode||t===document||t===window||(e=!0,document.body.appendChild(t)),t.dispatchEvent(n),e&&t.parentNode.removeChild(t)}))}))},H=function(t){if(null===t||"object"!=typeof t)return t;var e=t.constructor();for(var o in t)t.hasOwnProperty(o)&&(e[o]=t[o]);return e};function k(){if(h)return u;h=1;var t="data-pjax-state";return u=function(e){switch(e.tagName.toLowerCase()){case"a":e.hasAttribute(t)||this.attachLink(e);break;case"form":e.hasAttribute(t)||this.attachForm(e);break;default:throw"Pjax can only be applied on <a> or <form> submit"}}}function M(){return E?b:(E=1,b=function(){})}function C(){return T?R:(T=1,R=function(t,e,o){var n=new RegExp("([?&])"+e+"=.*?(&|$)","i"),s=-1!==t.indexOf("?")?"&":"?";return t.match(n)?t.replace(n,"$1"+e+"="+o+"$2"):t+s+e+"="+o})}var O=t,X=function(t){return(t=t||{}).elements=t.elements||"a[href], form[action]",t.selectors=t.selectors||["title",".js-Pjax"],t.switches=t.switches||{},t.switchesOptions=t.switchesOptions||{},t.history=void 0===t.history||t.history,t.analytics="function"==typeof t.analytics||!1===t.analytics?t.analytics:r,t.scrollTo=void 0===t.scrollTo?0:t.scrollTo,t.scrollRestoration=void 0===t.scrollRestoration||t.scrollRestoration,t.cacheBust=void 0===t.cacheBust||t.cacheBust,t.debug=t.debug||!1,t.timeout=t.timeout||0,t.currentUrlFullReload=void 0!==t.currentUrlFullReload&&t.currentUrlFullReload,t.switches.head||(t.switches.head=i.switchElementsAlt),t.switches.body||(t.switches.body=i.switchElementsAlt),t},D=q,F=o,B=U,I=H,J=function(t,e,o){for(var n=0;n<e.length;n++)for(var s=t.querySelectorAll(e[n]),i=0;i<s.length;i++)if(s[i].contains(o))return!0;return!1},K=function(t){if(null==t)return null;for(var e=Object(t),o=1;o<arguments.length;o++){var n=arguments[o];if(null!=n)for(var s in n)Object.prototype.hasOwnProperty.call(n,s)&&(e[s]=n[s])}return e},_=function(t){this.state={numPendingSwitches:0,href:null,options:null},this.options=X(t),this.log("Pjax options",this.options),this.options.scrollRestoration&&"scrollRestoration"in history&&(history.scrollRestoration="manual",F(window,"beforeunload",(function(){history.scrollRestoration="auto"}))),this.maxUid=this.lastUid=D(),this.parseDOM(document),F(window,"popstate",function(t){if(t.state){var e=I(this.options);e.url=t.state.url,e.title=t.state.title,e.history=!1,e.scrollPos=t.state.scrollPos,t.state.uid<this.lastUid?e.backward=!0:e.forward=!0,this.lastUid=t.state.uid,this.loadUrl(t.state.url,e)}}.bind(this))};return _.switches=s,_.prototype={log:(c||(c=1,l=function(){this.options.debug&&console&&("function"==typeof console.log?console.log.apply(console,arguments):console.log&&console.log(arguments))}),l),getElements:function(t){return t.querySelectorAll(this.options.elements)},parseDOM:function(t){var e=k();O(this.getElements(t),e,this)},refresh:function(t){this.parseDOM(t||document)},reload:function(){window.location.reload()},attachLink:function(){if(f)return d;f=1;var t=o,e=H,n="data-pjax-state",s=function(t,o){if(!i(o)){var s=e(this.options),r=function(t,e){if(e.which>1||e.metaKey||e.ctrlKey||e.shiftKey||e.altKey)return"modifier";if(t.protocol!==window.location.protocol||t.host!==window.location.host)return"external";if(t.hash&&t.href.replace(t.hash,"")===window.location.href.replace(location.hash,""))return"anchor";if(t.href===window.location.href.split("#")[0]+"#")return"anchor-empty"}(t,o);if(r)t.setAttribute(n,r);else{if(o.preventDefault(),this.options.currentUrlFullReload&&t.href===window.location.href.split("#")[0])return t.setAttribute(n,"reload"),void this.reload();t.setAttribute(n,"load"),s.triggerElement=t,this.loadUrl(t.href,s)}}},i=function(t){return t.defaultPrevented||!1===t.returnValue};return d=function(e){var o=this;e.setAttribute(n,""),t(e,"click",(function(t){s.call(o,e,t)})),t(e,"keyup",function(t){13===t.keyCode&&s.call(o,e,t)}.bind(this))}}(),attachForm:function(){if(m)return p;m=1;var t=o,e=H,n="data-pjax-state",s=function(t,o){if(!i(o)){var s=e(this.options);s.requestOptions={requestUrl:t.getAttribute("action")||window.location.href,requestMethod:t.getAttribute("method")||"GET"};var r=document.createElement("a");r.setAttribute("href",s.requestOptions.requestUrl);var a=function(t,e){if(t.protocol!==window.location.protocol||t.host!==window.location.host)return"external";if(t.hash&&t.href.replace(t.hash,"")===window.location.href.replace(location.hash,""))return"anchor";if(t.href===window.location.href.split("#")[0]+"#")return"anchor-empty";if(e.currentUrlFullReload&&t.href===window.location.href.split("#")[0])return"reload"}(r,s);a?t.setAttribute(n,a):(o.preventDefault(),"multipart/form-data"===t.enctype?s.requestOptions.formData=new FormData(t):s.requestOptions.requestParams=function(t){for(var e=[],o=t.elements,n=0;n<o.length;n++){var s=o[n],i=s.tagName.toLowerCase();if(s.name&&void 0!==s.attributes&&"button"!==i){var r=s.attributes.type;if(!r||"checkbox"!==r.value&&"radio"!==r.value||s.checked){var a=[];if("select"===i)for(var l,c=0;c<s.options.length;c++)(l=s.options[c]).selected&&!l.disabled&&a.push(l.hasAttribute("value")?l.value:l.text);else a.push(s.value);for(var u=0;u<a.length;u++)e.push({name:encodeURIComponent(s.name),value:encodeURIComponent(a[u])})}}}return e}(t),t.setAttribute(n,"submit"),s.triggerElement=t,this.loadUrl(r.href,s))}},i=function(t){return t.defaultPrevented||!1===t.returnValue};return p=function(e){var o=this;e.setAttribute(n,""),t(e,"submit",(function(t){s.call(o,e,t)}))}}(),forEachSelectors:function(e,o,n){return function(){if(v)return w;v=1;var e=t;return w=function(t,o,n,s){s=s||document,t.forEach((function(t){e(s.querySelectorAll(t),o,n)}))}}().bind(this)(this.options.selectors,e,o,n)},switchSelectors:function(e,o,n,i){return function(){if(y)return g;y=1;var e=t,o=s;return g=function(t,n,s,i,r,a){var l=[];s.forEach((function(s){var c=i.querySelectorAll(s),u=r.querySelectorAll(s);if(this.log&&this.log("Pjax switch",s,c,u),c.length!==u.length)throw"DOM doesn’t look the same on new loaded page: ’"+s+"’ - new "+c.length+", old "+u.length;e(c,(function(e,i){var r=u[i];this.log&&this.log("newEl",e,"oldEl",r);var c=t[s]?t[s].bind(this,r,e,a,n[s]):o.outerHTML.bind(this,r,e,a);l.push(c)}),this)}),this),this.state.numPendingSwitches=l.length,l.forEach((function(t){t()}))},g}().bind(this)(this.options.switches,this.options.switchesOptions,e,o,n,i)},latestChance:function(t){window.location=t},onSwitch:function(){B(window,"resize scroll"),this.state.numPendingSwitches--,0===this.state.numPendingSwitches&&this.afterAllSwitches()},loadContent:function(t,e){if("string"==typeof t){var o=document.implementation.createHTMLDocument("pjax"),n=t.match(/<html[^>]+>/gi);if(n&&n.length&&(n=n[0].match(/\s?[a-z:]+(?:=['"][^'">]+['"])*/gi)).length&&(n.shift(),n.forEach((function(t){var e=t.trim().split("=");1===e.length?o.documentElement.setAttribute(e[0],!0):o.documentElement.setAttribute(e[0],e[1].slice(1,-1))}))),o.documentElement.innerHTML=t,this.log("load content",o.documentElement.attributes,o.documentElement.innerHTML.length),document.activeElement&&J(document,this.options.selectors,document.activeElement))try{document.activeElement.blur()}catch(t){}this.switchSelectors(this.options.selectors,o,document,e)}else B(document,"pjax:complete pjax:error",e)},abortRequest:function(){if(A)return x;A=1;var t=M();return x=function(e){e&&e.readyState<4&&(e.onreadystatechange=t,e.abort())}}(),doRequest:function(){if(N)return j;N=1;var t=C();return j=function(e,o,n){var s,i=(o=o||{}).requestOptions||{},r=(i.requestMethod||"GET").toUpperCase(),a=i.requestParams||null,l=i.formData||null,c=null,u=new XMLHttpRequest,h=o.timeout||0;if(u.onreadystatechange=function(){4===u.readyState&&(200===u.status?n(u.responseText,u,e,o):0!==u.status&&n(null,u,e,o))},u.onerror=function(t){console.log(t),n(null,u,e,o)},u.ontimeout=function(){n(null,u,e,o)},a&&a.length)switch(s=a.map((function(t){return t.name+"="+t.value})).join("&"),r){case"GET":e=e.split("?")[0],e+="?"+s;break;case"POST":c=s}else l&&(c=l);return o.cacheBust&&(e=t(e,"t",Date.now())),u.open(r,e,!0),u.timeout=h,u.setRequestHeader("X-Requested-With","XMLHttpRequest"),u.setRequestHeader("X-PJAX","true"),u.setRequestHeader("X-PJAX-Selectors",JSON.stringify(o.selectors)),c&&"POST"===r&&!l&&u.setRequestHeader("Content-Type","application/x-www-form-urlencoded"),u.send(c),u}}(),handleResponse:function(){if(S)return P;S=1;var t=H,e=q,o=U;return P=function(n,s,i,r){if((r=t(r||this.options)).request=s,!1!==n){var a=window.history.state||{};window.history.replaceState({url:a.url||window.location.href,title:a.title||document.title,uid:a.uid||e(),scrollPos:[document.documentElement.scrollLeft||document.body.scrollLeft,document.documentElement.scrollTop||document.body.scrollTop]},document.title,window.location.href);var l=i;s.responseURL?i!==s.responseURL&&(i=s.responseURL):s.getResponseHeader("X-PJAX-URL")?i=s.getResponseHeader("X-PJAX-URL"):s.getResponseHeader("X-XHR-Redirected-To")&&(i=s.getResponseHeader("X-XHR-Redirected-To"));var c=document.createElement("a");c.href=l;var u=c.hash;c.href=i,u&&!c.hash&&(c.hash=u,i=c.href),this.state.href=i,this.state.options=r;try{this.loadContent(n,r)}catch(t){if(o(document,"pjax:error",r),this.options.debug)throw t;return console&&console.error&&console.error("Pjax switch fail: ",t),this.latestChance(i)}}else o(document,"pjax:complete pjax:error",r)}}(),loadUrl:function(t,e){e="object"==typeof e?K({},this.options,e):I(this.options),this.log("load href",t,e),this.abortRequest(this.request),B(document,"pjax:send",e),this.request=this.doRequest(t,e,this.handleResponse.bind(this))},executeScripts:function(t){t.forEach((function(t){var e=t.text||t.textContent||t.innerHTML||"",o=document.createElement("script");t.id&&(o.id=t.id),t.className&&(o.className=t.className),t.type&&(o.type=t.type),t.src&&(o.src=t.src,o.async=!1),void 0!==t.dataset.pjax&&(o.dataset.pjax=""),""!==e&&o.appendChild(document.createTextNode(e)),t.parentNode.replaceChild(o,t)}))},afterAllSwitches:function(){var t=Array.prototype.slice.call(document.querySelectorAll("[autofocus]")).pop();t&&document.activeElement!==t&&t.focus(),this.options.selectors.forEach((function(t){O(document.querySelectorAll(t),(function(t){}))}));var e=this.state;if(e.options.history&&(window.history.state||(this.lastUid=this.maxUid=D(),window.history.replaceState({url:window.location.href,title:document.title,uid:this.maxUid,scrollPos:[0,0]},document.title)),this.lastUid=this.maxUid=D(),window.history.pushState({url:e.href,title:e.options.title,uid:this.maxUid,scrollPos:[0,0]},e.options.title,e.href)),this.forEachSelectors((function(t){this.parseDOM(t)}),this),B(document,"pjax:complete pjax:success",e.options),"function"==typeof e.options.analytics&&e.options.analytics(),e.options.history){var o=document.createElement("a");if(o.href=this.state.href,o.hash){var n=o.hash.slice(1);n=decodeURIComponent(n);var s=0,i=document.getElementById(n)||document.getElementsByName(n)[0];if(i&&i.offsetParent)do{s+=i.offsetTop,i=i.offsetParent}while(i);window.scrollTo(0,s)}else!1!==e.options.scrollTo&&(e.options.scrollTo.length>1?window.scrollTo(e.options.scrollTo[0],e.options.scrollTo[1]):window.scrollTo(0,e.options.scrollTo))}else e.options.scrollRestoration&&e.options.scrollPos&&window.scrollTo(e.options.scrollPos[0],e.options.scrollPos[1]);this.state={numPendingSwitches:0,href:null,options:null}}},_}();