-
Notifications
You must be signed in to change notification settings - Fork 2
/
bix.min.js
7 lines (7 loc) · 1.7 KB
/
bix.min.js
1
2
3
4
5
6
7
/*!
* Bix - A JavaScript Routing Library
* Copyright (C) 2012 Dafydd Francis
* MIT Licensed
*//**
* A private namespace to set things up against the global object.
*/(function(){var a=function(a){this.urls=a,this.error=function(a){console.log("Bix. Routing Error: ",a)},this.root="/",this.forceHash=!1,this.hashBang=!0};a.prototype.route=function(a,b){var c,d=[],e=this.urls[a];a=a.split("/"),b=(b||"/").split("/");if(b.length!==a.length)return null;for(c=1;c<b.length;c++){var f=a[c];if(f[0]===":")d.push(b[c]);else if(f!==b[c])return null}return e.apply(null,d)},a.prototype.matchUrl=function(a){var b,c=this,d=this.urls;for(b in d)if(d.hasOwnProperty(b)){var e=c.route(b,a);if(e!==null)return e}c.error({status:404})},a.prototype.pushStates=function(){var a,b=document.getElementsByTagName("a"),c=this,d=function(){var a=this.hash.split("/");return a.shift(),a=c.root+a.join("/"),window.history.pushState({},null,a),c.matchUrl(a.replace(new RegExp("^"+c.root),"/")),!1};for(a=0;a<b.length;a++){var e=b[a];e.hash.length>0&&(b[a].onclick=d)}window.onpopstate=function(){c.matchUrl(window.location.pathname.replace(new RegExp("^"+c.root),"/"))}},a.prototype.hashchange=function(){var a=this;window.onhashchange=function(){a.matchUrl(window.location.hash)};if(location.pathname===a.root)window.onhashchange();else{var b=a.hashBang?"#!":"#";b+=location.pathname.replace(new RegExp("^"+this.root),"/"),window.onhashchange=function(){},location.hash=b,location.pathname=a.root}},window.Bix=function(b){b=b||{};var c=new a(b);return{add:function(a,b){c.urls[a]=b},run:function(){!c.forceHash&&window.history.pushState!==undefined?c.pushStates():c.hashchange()},config:function(a){var b;for(b in a)a.hasOwnProperty(b)&&(c[b]=a[b])}}}})();