-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.jbridge-1.0.min.js
7 lines (7 loc) · 4.36 KB
/
jquery.jbridge-1.0.min.js
1
2
3
4
5
6
7
/**
* @project: jBridge, the missing piece
* @version: 1.0.3
* @description: an easy, abstract and versatile AJAX + History API site manager
* @author: Laser Design Studio http://laserdesignstudio.it
*/
(function(e){jQuery.jBridge=function(t){var n={},r=e.Deferred().resolve().promise(),i=0,s=e({});var o=e.extend({menuAnchors:"nav.main a",menuAnchorsContainer:null,internalAnchors:'a[href^="/"]',onUnload:n.bypass,onLoad:n.bypass,onError:n.bypass,onHashChange:null,debug:false,requestJsonp:false,additionalRequestHeaders:{}},t);var u=e("body"),a=e(o.menuAnchors);var f=function(t,r){if(e.isFunction(t))return[t];var i=[];for(var s in t)if(r.indexOf(s)==0)i.push(t[s]);if(i.length==0)i.push(n.bypass);return i};n.getPathname=function(){return document.location.pathname.replace(/#.*$/,"")};n.LOG_MESSAGE=0;n.LOG_WARN=1;n.LOG_ERROR=2;n.log=function(e,t){methods=["info","warn","error"];t=t?t:0;if(o.debug)console[methods[t]]("jBridge: "+e)};n.replaceAppend=function(e){history.replaceState(history.state,"",n.getPathname()+e)};n.hold=function(s){n.log("on hold");if(r.state()!="resolved"||r.state()!="rejected"){i=s?s:1;r=e.Deferred();return r.promise()}};n.release=function(){if(r.state()=="resolved"||r.state()=="rejected")return;if(i>0)i--;n.log("stack length -> "+i);if(i==0){n.log("released");r.resolve();s.dequeue("op")}};n.getPromise=function(){return r.promise()};n.bypass=function(){s.dequeue("op")};e(document).on("click",o.internalAnchors,function(t){t.preventDefault();var r=e(this),i=r.attr("href");if(i==n.getPathname())return false;n.log("click on internal anchor: "+i);n.load(i)});e(document).on("click","a[href^=#]",function(t){c=true;if(e(this).attr("href")=="#")return;if(o.onHashChange===null)return;var r=e(this).attr("href");n.log("click on hash anchor: "+r);o.onHashChange(r);return false});var l=false,c=false;window.onpopstate=function(){if(c){c=false;return false}if(!l)return false;n.log("onpopstate triggered");n.load();return false};e(window).one("load",function(){var e=n.getPathname(),t=f(o.onLoad,e);h();if(location.hash&&o.onHashChange!==null){n.log("found hashFragment onload, calling onHashChange");o.onHashChange(location.hash)}s.queue("op",t).dequeue("op");setTimeout(function(){l=true},0)});var h=function(){var t=n.getPathname(),r=a;if(o.menuAnchorsContainer===null)a.removeClass("active");else a.parent(o.menuAnchorsContainer).removeClass("active");r=r.filter(function(n){return t.indexOf(e(this).attr("href"))!=-1});if(o.menuAnchorsContainer!==null)r=r.parents(o.menuAnchorsContainer);if(!r.length)n.log("couldn't find any menu item to activate, check menu settings or your menu markup.",n.LOG_WARN);else r.addClass("active")};n.replaceContent=function(t){var r=e(t["title"]).filter("title").text();e("head > title").text(r);delete t["title"];n.appendStyles(t["stylesheets"]);delete t["stylesheets"];n.appendScripts(t["scripts"]);delete t["scripts"];n.log("replacing sections");var i=0;e.each(t,function(t,n){i++;e("#"+t).html(e.parseHTML(n,document,true))});n.log("replaced "+i+" section"+(i>1?"s":""))};n.appendStyles=function(t){var r=e(t);if(!r.length)return;n.log(r.length+" stylesheet"+(r.length>1?"s":"")+" found, checking existance");e.each(r,function(t,r){var i=e(r),s=i.attr("href"),o=s.split("/").slice(-1)[0];if(e('link[href$="'+o+'"]').length)return;n.log("appending stylesheet -> "+s);e("head").append(i)})};n.appendScripts=function(t){var r=e(e.trim(t));if(!r.length)return n.bypass();n.log(r.length+" script"+(r.length>1?"s":"")+" found, checking existance");n.hold(r.filter(":not([async])").length+r.filter('[src*="bridge.release"]').length);e.each(r,function(t,r){var i=e(r),s=i.attr("src");if(e('script[src="'+s+'"]').length){if(s.match("bridge.release"))n.release();return n.release()}n.log("appending script -> "+s);i=e("<script />");e("body").append(i);i.one("load",n.release);i.attr("src",s)})};n.requestPage=function(){n.log("requesting page: "+window.location);var t={url:window.location,dataType:o.requestJsonp?"jsonp":"json",headers:o.additionalRequestHeaders,success:function(e){n.log("successfully retrieved page: "+window.location);n.replaceContent(e)},error:function(e,t,n){s.clearQueue("op").queue("op",[o.onError.bind(this,arguments)]).dequeue()}};n.hold();e.ajax(t)};n.load=function(e){if(e)history.pushState({route:e},"",e);var t=n.getPathname();var r=f(o.onUnload,t);var i=f(o.onLoad,t);h();s.clearQueue("op").queue("op",r.concat([n.requestPage]).concat(i)).dequeue("op")};return n}})(jQuery)