forked from minddust/jquery-pjaxr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.pjaxr.min.js
executable file
·2 lines (2 loc) · 6.09 KB
/
jquery.pjaxr.min.js
1
2
/*! jquery.pjaxr v1.1.0 | Copyright (c) 2013-2014 Stephan Groß | MIT license | minddust.com */
!function(e){"use strict";function t(e,t){return this.on("click.pjaxr",e,function(e){o(e,t)})}function r(t){return e.isReady?e(document).one("pjaxr:done",t):e(document).ready(t)}function a(t){return e.isReady||e(document).ready(t),e(document).on("pjaxr:done",t)}function n(r,a){if("string"==typeof r){var n=document.createElement("A");n.href=r,r=n}if("A"!==r.tagName.toUpperCase())throw"$.fn.pjaxr requires an anchor element";if((location.protocol===r.protocol&&location.hostname===r.hostname||":"===r.protocol&&""===r.hostname)&&(!r.hash||r.href.replace(r.hash,"")!==location.href.replace(location.hash,""))&&r.href!==location.href+"#"){var o=e.isFunction(r.href)?r.href():r.href,i=o;-1==i.indexOf("pjaxr")&&(i+=-1==o.indexOf("?")?"?pjaxr":"&pjaxr");var c={url:i,plainUrl:o,type:"GET",dataType:"html"},h=t.options=e.extend(!0,{},e.ajaxSettings,c,e.fn.pjaxr.defaults,a);y||(y=e("body").data("pjaxr-namespace")||"");var m;h.beforeSend=function(e,t){return u("pjaxr:beforeSend",[e,t])?(e.setRequestHeader("X-PJAX","true"),e.setRequestHeader("X-PJAX-NAMESPACE",y),t.timeout>0&&(m=setTimeout(function(){u("pjaxr:timeout",[e,h])&&e.abort("timeout")},t.timeout),t.timeout=0),!0):!1},t.state||(t.state={id:d(),namespace:y,url:window.location.href,title:document.title},window.history.replaceState(t.state,t.state.title,t.state.url));var x=t.xhr;x&&x.readyState<4&&(x.onreadystatechange=e.noop,x.abort()),x=t.xhr=e.ajax(h),x.readyState>0&&(u("pjaxr:start",[h]),u("pjaxr:send",[h])),x.done(function(r,a,n){u("pjaxr:success",[h]);var o="function"==typeof h.version?h.version():h.version,i=n.getResponseHeader("X-PJAX-Version");if(o&&i&&o!==i)return void f(h.plainUrl);var c=r.match(/<pjaxr-head>([\s\S.]*)<\/pjaxr-head>/i),m=r.match(/<pjaxr-body>([\s\S.]*)<\/pjaxr-body>/i);if(!c&&!m)return void f(h.plainUrl);u("pjaxr:success",[r,a,n,h]),document.activeElement.blur();var x=d();if(c)var v=e(l(c[0])),j=s("forward",v.children(),null,null),w=j[0],b=j[1],g=j[2];if(m)var S=e(l(m[0])),A=p(S.children()),T=A[0],P=A[1];var _=r.match(/<pjaxr-namespace>([\s\S.]*)<\/pjaxr-namespace>/i);_&&(y=e(l(_[0].replace(/(\r\n|\n|\r|\s|\t)/gm,""))).html()),e(document).find("input[autofocus], textarea[autofocus]").last().focus(),"number"==typeof h.scrollTo&&e(window).scrollTop(h.scrollTo),e.extend(t.state,{head_revert:c?b:null,head_remove:c?g:null,body_revert:m?P:null}),(h.push||h.replace)&&window.history.replaceState(t.state,t.state.title,t.state.url),t.state={id:x,namespace:y,url:h.plainUrl,title:document.title,head_apply:c?w:null,body_apply:m?T:null},h.push?window.history.pushState(t.state,t.state.title,t.state.url):h.replace&&window.history.replaceState(t.state,t.state.title,t.state.url),u("pjaxr:done",[r,a,n,h])}),x.fail(function(e,t,r){"abort"!==t&&u("pjaxr:fail",[e,t,r,h])&&f(h.plainUrl)}),x.always(function(){m&&clearTimeout(m),u("pjaxr:always",[h]),u("pjaxr:end",[h])})}}function o(e,t){var r=e.currentTarget;if("A"!==r.tagName.toUpperCase())throw"$.fn.pjaxr requires an anchor element";if(!(e.which>1||e.metaKey||e.ctrlKey||e.shiftKey||e.altKey||e.isDefaultPrevented())){if(!u("pjaxr:click"))return void e.preventDefault();n(r,t),e.preventDefault()}}function i(t,r){var a=[],n=[],o=[];return t&&t.length>0&&e.each(t,function(t,i){var s=e(i);if(s.is("title"))document.title=s.text();else if(s.is("meta")){var p,u=s.attr("name"),l=s.attr("property");u?p=e('head > meta[name="'+u+'"]'):l&&(p=e('head > meta[property="'+l+'"]')),void 0!==p&&(p.length>0?(o.push(c(p)),p.remove()):n.push(c(s)),r===!0&&(e("head").append(s),a.push(c(s))))}else if(s.is("link")){var d=s.attr("href");if(d){var f=e('head > link[href="'+d+'"]');f.length>0?(o.push(c(f)),f.remove()):n.push(c(s)),r===!0&&(e("head").append(s),a.push(c(s)))}}else if(s.is("script")){var h=s.attr("src");if(h){var m=e('head > script[src="'+h+'"]');m.length>0?(o.push(c(m)),m.remove()):n.push(c(s)),r===!0&&(e("head").append(s),a.push(c(s)))}}else s.is("style")&&r===!0&&(e("head").append(s),a.push(c(s)),n.push(c(s)))}),[a,n,o]}function s(t,r,a,n){var o=[],s=[],p=[];if("forward"===t)e("head > [data-remove-on-pjaxr]").each(function(){var t=e(this);p.push(c(t)),t.remove()});else if("back"===t){var u=i(a,!1),l=i(n,!0);e.extend(s,u[1]),e.extend(p,l[2])}var d=i(r,!0);return e.extend(o,d[0]),e.extend(s,d[1]),e.extend(p,d[2]),[o,s,p]}function p(t){var r=[],a=[];return t&&t.length>0&&e.each(t,function(t,n){var o=e(n),i=o.attr("id");if(i){var s=e("#"+i);if(s.length>0){a.push(c(s));try{s.html(o.html())}catch(p){window.console&&console.error(p)}r.push(c(s))}}}),[r,a]}function u(t,r){var a=e.Event(t);return e(document).trigger(a,r),!a.isDefaultPrevented()}function l(t){return e.parseHTML(t,document,!0)}function c(e){return e.clone().wrap("<p>").parent().html()}function d(){return(new Date).getTime()}function f(e){window.location.href=e}function h(e){var r=e.state;if(r){if(j&&w==r.url)return;u("pjaxr:start",[t.options]),document.title=r.title;var a=t.state.id<r.id?"forward":"back";s(a,r.head_apply,r.head_revert,r.head_remove);var n="forward"===a?r.body_apply:r.body_revert;n&&n.length>0&&p(n),t.state=r,y=r.namespace,document.body.offsetHeight,u("pjaxr:end",[t.options])}j=!1}function m(){return e("meta").filter(function(){return"X-PJAX-VERSION"===String(e(this).attr("http-equiv")).toUpperCase()}).attr("content")}function x(){e.fn.pjaxr=t,e.fn.pjaxrReady=r,e.fn.pjaxrAlways=a,e.fn.pjaxr.click=o,e.fn.pjaxr.request=n,e.fn.pjaxr.enable=e.noop,e.fn.pjaxr.disable=v,e.fn.pjaxr.defaults={timeout:650,push:!0,replace:!1,scrollTo:0,version:m},e(window).on("popstate.pjaxr",h)}function v(){e.fn.pjaxr=function(){return this},e.fn.pjaxrReady=function(t){return e(document).ready(t)},e.fn.pjaxrAlways=function(t){return e(document).ready(t)},e.fn.pjaxr.enable=x,e.fn.pjaxr.disable=e.noop,e(window).off("popstate.pjaxr",h)}var y,j=!0,w=window.location.href,b=window.history.state;b&&(t.state=b),"state"in window.history&&(j=!1),e.inArray("state",e.event.props)<0&&e.event.props.push("state"),e.support.pjaxr=window.history&&window.history.pushState&&window.history.replaceState&&!navigator.userAgent.match(/((iPod|iPhone|iPad).+\bOS\s+[1-4]|WebApps\/.+CFNetwork)/),e.support.pjaxr?x():v()}(jQuery);