-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.njax.min.js
1 lines (1 loc) · 7.52 KB
/
jquery.njax.min.js
1
!function(t,e,r,n){"use strict";var a,o,i,l={pushState:!0,target:"#content",scrollToTarget:!1,scrollSpeed:200,insert:"insert",noScripts:!1,format:"json",fragment:null,elements:null,filterResponse:null,partial:null,title:r.title,timeoutTime:6e4,start:function(e){t.noop(e)},end:function(e){t.noop(e)},error:function(e){t.noop(e)},success:function(e){t.noop(e)},timeout:function(e){t.noop(e)}},s=t(e),u=t("head"),c=t(r.body),f=c.add("html"),p=!1,h=e.history,d=!0,g={},m={},j={},x={},v=[],y=null,T={title:r.title},b=function(r,l,s){if(!p)return void(e.location.href=r);if(!r)throw new TypeError("No url given for njax request");if(l=l||t.njax.defaults.target,"string"!=typeof l)throw new TypeError('Njax request requires target to be a string selector, "'+typeof l+'" given.');var u=t(l).eq(0);if(!u.length)throw new TypeError('Njax request requires a selector for an existing target, "'+l+'" given.');s=t.extend(!0,{},t.njax.defaults,s),a&&a.readyState<4&&(a.onreadystatechange=t.noop,a.abort());var c=F();return a=t.ajax({url:r,dataType:"html"===s.format?"html":"json",beforeSend:function(t,e){return t.setRequestHeader("X-NJAX","true"),t.setRequestHeader("X-NJAX-Format",s.format),s.fragment&&t.setRequestHeader("X-NJAX-Fragment",s.fragment),s.partial&&t.setRequestHeader("X-NJAX-Partial",s.partial),k(u,"njax:start",[r,s,t,e],s.start)?(t.abort(),!1):(o=null,void(s.timeoutTime>0&&(i=setTimeout(function(){t.abort(),k(u,"njax:timeout",[r,s,t,e],s.timeout)},s.timeoutTime))))},success:function(a,i,f){d=!0;var p={$content:t(),url:f.getResponseHeader("X-NJAX-Display-URL")||r,title:f.getResponseHeader("X-NJAX-Title")||s.title,partial:f.getResponseHeader("X-NJAX-Partial")||null,data:t.parseJSON(f.getResponseHeader("X-NJAX-Data"))||{},css:[],js:[]};if("html"===s.format){p.$content=t(t.trim(a));var h=K(p.$content,"title");h.length&&(p.title=h.text()),K(p.$content,'link[rel="stylesheet"]').each(function(){var e=t(this),r=e.attr("href");return r?(p.css.push({url:r,media:e.attr("media")||"all"}),void e.remove()):!0}),K(p.$content,"script[src]").each(function(){var e=t(this),r=e.attr("src");return r?(p.js.push({url:r}),void e.remove()):!0})}else p.$content=t(t.trim(a.content)),p.js=a.js||[],p.css=a.css||[];if("function"==typeof s.filterResponse)p.$content=s.filterResponse(p.$content);else if(s.elements){var g=p.$content.filter(s.elements);g.length||(g=p.$content.find(s.elements)),p.$content=g}else if(s.fragment){var m=p.$content.filter(s.fragment);m.length||(m=p.$content.find(s.fragment)),p.$content=m.html()||""}if(R(t.extend(!0,{},{target:l},c)),s.pushState&&E(p.url,p.title,{target:l,options:{partial:s.partial,fragment:s.fragment}}),X(),"append"===s.insert)u.append(p.$content);else if("prepend"===s.insert)u.prepend(p.$content);else if("replace"===s.insert){var j=p.$content;u.replaceWith(j),u=j}else u.html(p.$content);$(p.css);var x=function(){k(u,"njax:success",[p,a,r,s,f,i],s.success)};s.noScripts?x():S(p.js,x),e._gaq!==n&&e._gaq.push(["_trackPageview"]),s.scrollToTarget&&I(u,s.scrollSpeed,s.scrollToTarget),o={url:r,target:l,options:s},d=!1},error:function(e,n,a){"abort"!==a&&(d=!0,R(t.extend(!0,{},{target:l},c)),s.pushState&&E(r,s.title,{target:l,options:{partial:s.partial,fragment:s.fragment}}),k(u,"njax:error",[r,s,e,n,a],s.error),d=!1)},complete:function(t,e){clearTimeout(i),d=!1,k(u,"njax:end",[r,s,t,e],s.end)}})},$=function(e){t.each(e,function(e,r){return m[r.url]!==n?!0:void(m[r.url]=t("<link />",{rel:"stylesheet",href:r.url,media:r.media}).appendTo(u))})},S=function(e,a){a=a||t.noop;var o=[],i=!1,l=function(e,r){0===t.inArray(e,o)?(o.shift(),y=e,j[e]={url:e,local:!0},t.globalEval(r),y=null):setTimeout(function(){l(e,r)},50),o.length||a.apply(this)};t.each(e,function(e,a){if(!J(a.url)){if(j[a.url]!==n)return w(a.url),!0;var s=r.createElement("script");return r.body.appendChild(s),s.onload=function(){l(a.url,"false")},s.src=a.url,i=!0,o.push(a.url),!0}return j[a.url]!==n?(w(a.url),!0):(i=!0,o.push(a.url),void t.ajax({url:a.url,dataType:"text",success:function(t){l(a.url,t)},error:function(){l(a.url,"")}}))}),i||a.apply(this)},w=function(e){if(x[e]===n)return 0;var r=0;return t.each(x[e],function(t,e){v.push(e),e.load(),r++}),r},X=function(){t.each(v,function(t,e){e.unload()}),v=[]},q=function(e,r){var a={load:"function"==typeof e?e:t.noop,unload:"function"==typeof r?r:t.noop},o=!1;if(!y){var i=t("script[src]").last();i.length&&(y=i.attr("src"),o=!0)}y&&(x[y]===n&&(x[y]=[]),x[y].push(a)),o&&(y=null),v.push(a),a.load()},N=function(){o||e.location.reload(),b(o.url,o.target,o.options)},E=function(e,n,a){n=t.trim(n).length?n:null,a=a||{};var o=t.extend(!0,{},{id:P(),scrollTop:s.scrollTop(),url:e,title:n},a);return h.pushState(o,n,e),r.title=n,T=o},R=function(n){var a=t.extend(!0,{},T,{id:T.id||P(),scrollTop:s.scrollTop(),url:e.location.pathname+e.location.search},n);h.replaceState(a,T.title),r.title=T.title,T=a},k=function(e,r,n,a){"function"==typeof a&&e.on(r+".njaxtrigger",a);var o=t.Event(r,{relatedTarget:e});return e.trigger(o,n),e.off(r+".njaxtrigger"),o.isDefaultPrevented()},A=function(t){var r=t.currentTarget;return t.which>1||t.metaKey||t.ctrlKey||t.altKey||t.shiftKey?!1:e.location.protocol!==r.protocol||e.location.host!==r.host?!1:r.hash&&r.href.replace(r.hash,"")===e.location.href.replace(e.location.hash,"")?!1:"#"===r.href||r.href===e.location.href+"#"?!1:!0},J=function(t){var r=H(t);return r.protocol===e.location.protocol&&r.host===e.location.host},H=function(t){var e=r.createElement("a");return e.href=t,e},P=function(){for(var t="0";;)if(t=(new Date).getTime()+"",g[t]===n)break;return t},F=function(){return"function"==typeof t.njax.globalState?t.njax.globalState.apply(e):t.njax.globalState},D=function(){return t.njax.defaults.partial},K=function(t,e){var r=t.find(e);return r=r.add(t.filter(e))},I=function(t,e,r){e=e===n?200:e,r=parseInt(r,10)||0,t.length&&f.animate({scrollTop:t.offset().top-r},e)};t.njax=function(){if(!arguments.length)throw new Error("$.njax() requires at least one argument!");"function"==typeof arguments[0]?q.apply(e,arguments):"string"==typeof arguments[0]&&b.apply(e,arguments)},t.fn.njax=function(e,r){if(!p)return!0;var n=t(this);if(!n.is("a[href]"))throw new TypeError('$().njax() can only be called on "a" elements that have a href attribute.');if("object"!=typeof e)throw new TypeError("$().njax() requires the original event (usually click) to be passed as first argument.");if(!A(e))return!0;r=r||{};var a=n.data("njax")||{},o=n.attr("href")||!1,i=n.data("njaxTarget")||a.target||r.target||t.njax.defaults.target;return i&&o?(a=t.extend({},t.njax.defaults,r,a),a.fragment=n.data("njaxFragment")||a.fragment,a.partial=n.data("njaxPartial")||a.partial,a.format=n.data("njaxFormat")||a.format,b(o,i,a),!1):!0},t.njax.defaults=l,t.njax.isNjaxEvent=A,t.njax.reload=N,t.njax.refresh=N,t.njax.partialForTarget=D,t.njax.globalState={},p=e.history&&e.history.pushState&&e.history.replaceState&&!navigator.userAgent.match(/((iPod|iPhone|iPad)\.+\bOS\s+[1-4]|WebApps\/\.+CFNetwork)/),p&&t(function(){R(),t('link[rel="stylesheet"]').each(function(){var e=t(this),r=e.attr("href");r&&(m[r]=e)}),t("script[src]").each(function(){var e=t(this).attr("src");e&&j[e]===n&&(j[e]={url:e,local:J(e)})}),c.on("click","a[data-njax]",function(e){return t(this).njax(e)}),s.on("popstate.njax",function(r){if(!d&&null!==r.originalEvent.state){var n=r.originalEvent.state,a=parseInt(T.id,10)<=parseInt(n.id,10)?"forward":"back",o="back"===a?n.target:T.target,i=t(o).eq(0);if(!p||!i.length)return e.location=n.url,!1;if(k(i,"njax:history",[a,n]))return!1;T=n,b(n.url,o,{pushState:!1,partial:t.njax.partialForTarget(o),success:function(){s.scrollTop("back"===a?n.scrollTop:0)}})}})})}(jQuery,window,document);