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