Files
blog/assets/js/jquery.pjax.min.js

7 lines
8.2 KiB
JavaScript
Raw Normal View History

/*!
* Copyright 2012, Chris Wanstrath
* Released under the MIT License
* https://github.com/defunkt/jquery-pjax
*/
!function(t){function e(e,a,r){return r=g(a,r),this.on("click.pjax",e,(function(e){var a=r;a.container||((a=t.extend({},r)).container=t(this).attr("data-pjax")),n(e,a)}))}function n(e,n,a){a=g(n,a);var i=e.currentTarget,o=t(i);if("A"!==i.tagName.toUpperCase())throw"$.fn.pjax or $.pjax.click requires an anchor element";if(!(e.which>1||e.metaKey||e.ctrlKey||e.shiftKey||e.altKey||location.protocol!==i.protocol||location.hostname!==i.hostname||i.href.indexOf("#")>-1&&x(i)==x(location)||e.isDefaultPrevented())){var s={url:i.href,container:o.attr("data-pjax"),target:i},c=t.extend({},s,a),u=t.Event("pjax:click");o.trigger(u,[c]),u.isDefaultPrevented()||(r(c),e.preventDefault(),o.trigger("pjax:clicked",[c]))}}function a(e,n,a){a=g(n,a);var i=e.currentTarget,o=t(i);if("FORM"!==i.tagName.toUpperCase())throw"$.pjax.submit requires a form element";var s={type:(o.attr("method")||"GET").toUpperCase(),url:o.attr("action"),container:o.attr("data-pjax"),target:i};if("GET"!==s.type&&void 0!==window.FormData)s.data=new FormData(i),s.processData=!1,s.contentType=!1;else{if(o.find(":file").length)return;s.data=o.serializeArray()}r(t.extend({},s,a)),e.preventDefault()}function r(e){e=t.extend(!0,{},t.ajaxSettings,r.defaults,e),t.isFunction(e.url)&&(e.url=e.url());var n=v(e.url).hash,a=t.type(e.container);if("string"!==a)throw"expected string value for 'container' option; got "+a;var i,s=e.context=t(e.container);if(!s.length)throw"the container selector '"+e.container+"' did not match anything";function c(n,a,r){r||(r={}),r.relatedTarget=e.target;var i=t.Event(n,r);return s.trigger(i,a),!i.isDefaultPrevented()}e.data||(e.data={}),t.isArray(e.data)?e.data.push({name:"_pjax",value:e.container}):e.data._pjax=e.container,e.beforeSend=function(t,a){if("GET"!==a.type&&(a.timeout=0),t.setRequestHeader("X-PJAX","true"),t.setRequestHeader("X-PJAX-Container",e.container),!c("pjax:beforeSend",[t,a]))return!1;a.timeout>0&&(i=setTimeout((function(){c("pjax:timeout",[t,e])&&t.abort("timeout")}),a.timeout),a.timeout=0);var r=v(a.url);n&&(r.hash=n),e.requestUrl=m(r)},e.complete=function(t,n){i&&clearTimeout(i),c("pjax:complete",[t,n,e]),c("pjax:end",[t,e])},e.error=function(t,n,a){var r=w("",t,e),i=c("pjax:error",[t,n,a,e]);"GET"==e.type&&"abort"!==n&&i&&o(r.url)},e.success=function(a,i,u){var l=r.state,p="function"==typeof t.pjax.defaults.version?t.pjax.defaults.version():t.pjax.defaults.version,d=u.getResponseHeader("X-PJAX-Version"),h=w(a,u,e),m=v(h.url);if(n&&(m.hash=n,h.url=m.href),p&&d&&p!==d)o(h.url);else if(h.contents){if(r.state={id:e.id||f(),url:h.url,title:h.title,container:e.container,fragment:e.fragment,timeout:e.timeout},(e.push||e.replace)&&window.history.replaceState(r.state,h.title,h.url),t.contains(s,document.activeElement))try{document.activeElement.blur()}catch(t){}h.title&&(document.title=h.title),c("pjax:beforeReplace",[h.contents,e],{state:r.state,previousState:l}),s.html(h.contents);var x=s.find("input[autofocus], textarea[autofocus]").last()[0];x&&document.activeElement!==x&&x.focus(),function(e){if(!e)return;var n=t("script[src]");e.each((function(){var e=this.src;if(!n.filter((function(){return this.src===e})).length){var a=document.createElement("script"),r=t(this).attr("type");r&&(a.type=r),a.src=t(this).attr("src"),document.head.appendChild(a)}}))}(h.scripts);var g=e.scrollTo;if(n){var y=decodeURIComponent(n.slice(1)),j=document.getElementById(y)||document.getElementsByName(y)[0];j&&(g=t(j).offset().top)}"number"==typeof g&&t(window).scrollTop(g),c("pjax:success",[a,i,u,e])}else o(h.url)},r.state||(r.state={id:f(),url:window.location.href,title:document.title,container:e.container,fragment:e.fragment,timeout:e.timeout},window.history.replaceState(r.state,document.title)),d(r.xhr),r.options=e;var u,l,p=r.xhr=t.ajax(e);return p.readyState>0&&(e.push&&!e.replace&&(u=r.state.id,l=[e.container,h(s)],b[u]=l,E.push(u),S(T,0),S(E,r.defaults.maxCacheLength),window.history.pushState(null,"",e.requestUrl)),c("pjax:start",[p,e]),c("pjax:send",[p,e])),r.xhr}function i(e,n){var a={url:window.location.href,push:!1,replace:!0,scrollTo:!1};retur