-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.pjax.min.js
1 lines (1 loc) · 8.05 KB
/
jquery.pjax.min.js
1
!function(t){function e(e,r,a){var o=this;return this.on("click.pjax",e,function(e){var i=t.extend({},v(r,a));i.container||(i.container=t(this).attr("data-pjax")||o),n(e,i)})}function n(e,n,r){r=v(n,r);var o=e.currentTarget;if("A"!==o.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!==o.protocol||location.hostname!==o.hostname||o.href.indexOf("#")>-1&&m(o)==m(location)||e.isDefaultPrevented())){var i={url:o.href,container:t(o).attr("data-pjax"),target:o},c=t.extend({},i,r),s=t.Event("pjax:click");t(o).trigger(s,[c]),s.isDefaultPrevented()||(a(c),e.preventDefault(),t(o).trigger("pjax:clicked",[c]))}}function r(e,n,r){r=v(n,r);var o=e.currentTarget;if("FORM"!==o.tagName.toUpperCase())throw"$.pjax.submit requires a form element";var i={type:o.method.toUpperCase(),url:o.action,container:t(o).attr("data-pjax"),target:o};if("GET"!==i.type&&void 0!==window.FormData)i.data=new FormData(o),i.processData=!1,i.contentType=!1;else{if(t(o).find(":file").length)return;i.data=t(o).serializeArray()}a(t.extend({},i,r)),e.preventDefault()}function a(e){function n(e,n,a){a||(a={}),a.relatedTarget=r;var o=t.Event(e,a);return c.trigger(o,n),!o.isDefaultPrevented()}e=t.extend(!0,{},t.ajaxSettings,a.defaults,e),t.isFunction(e.url)&&(e.url=e.url());var r=e.target,o=h(e.url).hash,c=e.context=x(e.container);e.data||(e.data={}),t.isArray(e.data)?e.data.push({name:"_pjax",value:c.selector}):e.data._pjax=c.selector;var s;e.beforeSend=function(t,r){if("GET"!==r.type&&(r.timeout=0),t.setRequestHeader("X-PJAX","true"),t.setRequestHeader("X-PJAX-Container",c.selector),!n("pjax:beforeSend",[t,r]))return!1;r.timeout>0&&(s=setTimeout(function(){n("pjax:timeout",[t,e])&&t.abort("timeout")},r.timeout),r.timeout=0);var a=h(r.url);a.hash=o,e.requestUrl=d(a.href)},e.complete=function(t,r){s&&clearTimeout(s),n("pjax:complete",[t,r,e]),n("pjax:end",[t,e])},e.error=function(t,r,a){var o=y("",t,e),c=n("pjax:error",[t,r,a,e]);"GET"==e.type&&"abort"!==r&&c&&i(o.url)},e.success=function(r,s,u){var p=a.state,f="function"==typeof t.pjax.defaults.version?t.pjax.defaults.version():t.pjax.defaults.version,d=u.getResponseHeader("X-PJAX-Version"),m=y(r,u,e),v=h(m.url);if(o&&(v.hash=o,m.url=v.href),f&&d&&f!==d)return void i(m.url);if(!m.contents)return void i(m.url);a.state={id:e.id||l(),url:m.url,title:m.title,container:c.selector,fragment:e.fragment,timeout:e.timeout},(e.push||e.replace)&&window.history.replaceState(a.state,m.title,m.url);try{document.activeElement.blur()}catch(x){}m.title&&(document.title=m.title),n("pjax:beforeReplace",[m.contents,e],{state:a.state,previousState:p}),c.html(m.contents);var g=c.find("input[autofocus], textarea[autofocus]").last()[0];g&&document.activeElement!==g&&g.focus(),w(m.scripts);var j=e.scrollTo;if(o){var b=decodeURIComponent(o.slice(1)),T=document.getElementById(b)||document.getElementsByName(b)[0];T&&(j=t(T).offset().top)}"number"==typeof j&&t(window).scrollTop(j),n("pjax:success",[r,s,u,e])},a.state||(a.state={id:l(),url:window.location.href,title:document.title,container:c.selector,fragment:e.fragment,timeout:e.timeout},window.history.replaceState(a.state,document.title)),u(a.xhr),a.options=e;var f=a.xhr=t.ajax(e);return f.readyState>0&&(e.push&&!e.replace&&(b(a.state.id,p(c)),window.history.pushState(null,"",e.requestUrl)),n("pjax:start",[f,e]),n("pjax:send",[f,e])),a.xhr}function o(e,n){var r={url:window.location.href,push:!1,replace:!0,scrollTo:!1};return a(t.extend(r,v(e,n)))}function i(t){window.history.replaceState(null,"",a.state.url),window.location.replace(t)}function c(e){A||u(a.xhr);var n=a.state,r=e.state;if(r&&r.container){if(A&&R==r.url)return;var o,c=r.container;if(n){if(n.id===r.id)return;o=n.id<r.id?"forward":"back","back"==o&&(c=n.container)}var s=t(c);if(s.length){var l=U[r.id];n&&T(o,n.id,p(s));var f=t.Event("pjax:popstate",{state:r,direction:o});s.trigger(f);var d={id:r.id,url:r.url,container:s,push:!1,fragment:r.fragment,timeout:r.timeout,scrollTo:!1};if(l){s.trigger("pjax:start",[null,d]),a.state=r,r.title&&(document.title=r.title);var h=t.Event("pjax:beforeReplace",{state:r,previousState:n});s.trigger(h,[l,d]),s.html(l),s.trigger("pjax:end",[null,d])}else a(d);s[0].offsetHeight}else i(location.href)}A=!1}function s(e){var n=t.isFunction(e.url)?e.url():e.url,r=e.type?e.type.toUpperCase():"GET",a=t("<form>",{method:"GET"===r?"GET":"POST",action:n,style:"display:none"});"GET"!==r&&"POST"!==r&&a.append(t("<input>",{type:"hidden",name:"_method",value:r.toLowerCase()}));var o=e.data;if("string"==typeof o)t.each(o.split("&"),function(e,n){var r=n.split("=");a.append(t("<input>",{type:"hidden",name:r[0],value:r[1]}))});else if(t.isArray(o))t.each(o,function(e,n){a.append(t("<input>",{type:"hidden",name:n.name,value:n.value}))});else if("object"==typeof o){var i;for(i in o)a.append(t("<input>",{type:"hidden",name:i,value:o[i]}))}t(document.body).append(a),a.submit()}function u(e){e&&e.readyState<4&&(e.onreadystatechange=t.noop,e.abort())}function l(){return(new Date).getTime()}function p(t){var e=t.clone();return e.find("script").each(function(){this.src||jQuery._data(this,"globalEval",!1)}),e.contents()}function f(t,e){return t.replace(new RegExp("[?&]"+e+"=[^&#]*"),"").replace(/[?&]($|#)/,"").replace(/[?&]/,"?")}function d(t){return t=f(t,"_pjax"),t=f(t,"_")}function h(t){var e=document.createElement("a");return e.href=t,e}function m(t){return t.href.replace(/#.*/,"")}function v(e,n){return e&&n?n.container=e:n=t.isPlainObject(e)?e:{container:e},n.container&&(n.container=x(n.container)),n}function x(e){if(e=t(e),e.length){if(""!==e.selector&&e.context===document)return e;if(e.attr("id"))return t("#"+e.attr("id"));throw"cant get selector for pjax container!"}throw"no pjax container for "+e.selector}function g(t,e){return t.filter(e).add(t.find(e))}function j(e){return t.parseHTML(e,document,!0)}function y(e,n,r){var a={},o=/<html/i.test(e),i=n.getResponseHeader("X-PJAX-URL");if(a.url=i?d(i):r.requestUrl,o)var c=t(j(e.match(/<head[^>]*>([\s\S.]*)<\/head>/i)[0])),s=t(j(e.match(/<body[^>]*>([\s\S.]*)<\/body>/i)[0]));else var c=s=t(j(e));if(0===s.length)return a;if(a.title=g(c,"title").last().text(),r.fragment){if("body"===r.fragment)var u=s;else var u=g(s,r.fragment).first();u.length&&(a.contents="body"===r.fragment?u:u.contents(),a.title||(a.title=u.attr("title")||u.data("title")))}else o||(a.contents=s);return a.contents&&(a.contents=a.contents.not(function(){return t(this).is("title")}),a.contents.find("title").remove(),a.scripts=g(a.contents,"script[src]").remove(),a.contents=a.contents.not(a.scripts)),a.title&&(a.title=t.trim(a.title)),a}function w(e){if(e){var n=t("script[src]");e.each(function(){var e=this.src,r=n.filter(function(){return this.src===e});if(!r.length){var a=document.createElement("script"),o=t(this).attr("type");o&&(a.type=o),a.src=t(this).attr("src"),document.head.appendChild(a)}})}}function b(t,e){U[t]=e,k.push(t),E(X,0),E(k,a.defaults.maxCacheLength)}function T(t,e,n){var r,o;U[e]=n,"forward"===t?(r=k,o=X):(r=X,o=k),r.push(e),(e=o.pop())&&delete U[e],E(r,a.defaults.maxCacheLength)}function E(t,e){for(;t.length>e;)delete U[t.shift()]}function S(){return t("meta").filter(function(){var e=t(this).attr("http-equiv");return e&&"X-PJAX-VERSION"===e.toUpperCase()}).attr("content")}function P(){t.fn.pjax=e,t.pjax=a,t.pjax.enable=t.noop,t.pjax.disable=C,t.pjax.click=n,t.pjax.submit=r,t.pjax.reload=o,t.pjax.defaults={timeout:650,push:!0,replace:!1,type:"GET",dataType:"html",scrollTo:0,maxCacheLength:20,version:S},t(window).on("popstate.pjax",c)}function C(){t.fn.pjax=function(){return this},t.pjax=s,t.pjax.enable=P,t.pjax.disable=t.noop,t.pjax.click=t.noop,t.pjax.submit=t.noop,t.pjax.reload=function(){window.location.reload()},t(window).off("popstate.pjax",c)}var A=!0,R=window.location.href,D=window.history.state;D&&D.container&&(a.state=D),"state"in window.history&&(A=!1);var U={},X=[],k=[];t.inArray("state",t.event.props)<0&&t.event.props.push("state"),t.support.pjax=window.history&&window.history.pushState&&window.history.replaceState&&!navigator.userAgent.match(/((iPod|iPhone|iPad).+\bOS\s+[1-4]\D|WebApps\/.+CFNetwork)/),t.support.pjax?P():C()}(jQuery);