-
Notifications
You must be signed in to change notification settings - Fork 157
/
jhash-2.1.min.js
executable file
·19 lines (19 loc) · 4.6 KB
/
jhash-2.1.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* jHash v2.1 - http://jhash.codeplex.com - Copyright (c) 2013 Chris Pietschmann */
(function(window){var ie_documentMode=window.document.documentMode;var hashChangeSupported=('onhashchange'in window)&&(ie_documentMode===undefined||ie_documentMode>7);var jHash=window.jHash={jhash:"2.1",change:function(handler){if(hashChangeSupported){attachEvent(window,"hashchange",handler);}else{eventHandlers.push(handler);}},unbind:function(handler){var i=0,len=0;if(hashChangeSupported){detachEvent(window,"hashchange",handler);}else{arrayRemove(eventHandlers,eventHandlers.indexOf(handler));}
return this;},val:function(name,value){var ho=jHash.query();if(arguments.length===2){ho[name.toLowerCase()]=(value===null?'':value);return this.set(this.root(),ho);}else if(arguments.length===1&&typeof(name)==='string'){return ho[name.toLowerCase()];}else if(typeof(name)==='object'){return this.set(this.root(),name);}
return ho;},root:function(value){if(value===undefined){return parseHashRoot(window.location.hash);}
return this.set(value,this.val());},set:function(root,query){var fullHashValue=null;if(root===null&&query===null){fullHashValue=null;}else if(query!==undefined){var rootValue=root;var queryValue=typeof(query)==="string"?query:objectToHash(query);if(queryValue.length>0){rootValue+='?';}
fullHashValue=rootValue+queryValue;}else{fullHashValue=root;}
window.location.hash=fullHashValue;return this;},remove:function(name){var ho=jHash.query();ho[name.toLowerCase()]=undefined;return this.set(this.root(),ho);},query:function(){return hashToObject(window.location.hash);},clear:function(){window.location.hash='';return this;},clearQuery:function(){this.set(this.root());return this;},clearRoot:function(){this.set('',this.query());return this;},_routes:{},_routeHandlerRegistered:false,_defaultRoute:null,route:function(route,handler){this._routes[route]=handler;if(!this._routeHandlerRegistered){this._routeHandlerRegistered=true;jHash.change(routeHandler);}},processRoute:function(){routeHandler();},defaultRoute:function(root,query){if(arguments.length==0){return this._defaultRoute;}
this._defaultRoute={root:root,query:query};}};if(!hashChangeSupported){window.setInterval(function(){var currentHash=window.location.hash;if(previousHashValue!==currentHash){for(var i in eventHandlers){eventHandlers[i].call(window);}}
previousHashValue=currentHash;},200);}
var eventHandlers=[],previousHashValue=window.location.hash,attachEvent=function(element,evtName,handler){if(element.addEventListener){element.addEventListener(evtName,handler,false);}else if(element.attachEvent){element.attachEvent("on"+evtName,handler);}else{element["on"+evtName]+=handler;}},detachEvent=function(element,evtName,handler){if(element.removeEventListener){element.removeEventListener(evtName,handler,false);}else if(element.detachEvent){element.detachEvent("on"+evtName,handler);}else{element["on"+evtName]-=handler;}},arrayRemove=function(array,from,to){var rest=array.slice((to||from)+1||array.length);array.length=from<0?array.length+from:from;return array.push.apply(array,rest);},hashToObject=function(hash){var obj={},pair=null,strHash=hash.substring(0,hash.length);if(hash!==null&&hash!==undefined){if(strHash.indexOf("#")===0){strHash=strHash.substring(1,strHash.length);}
var queryIndex=strHash.indexOf("?");if(queryIndex>-1){strHash=strHash.substring(queryIndex+1,strHash.length);var parts=strHash.split("&");for(var i in parts){pair=parts[i].split("=");obj[pair[0].toString().toLowerCase()]=pair[1];}}}
return obj;},objectToHash=function(object){var s="";for(var i in object){if(object[i]!==undefined){if(s.length>0){s+="&";}
s+=i.toString()+"="+object[i].toString();}}
return s;},parseHashRoot=function(hash){var strHash=hash.substring(0,hash.length);if(strHash.indexOf("#")>-1){strHash=strHash.substring(1,strHash.length);}
if(strHash.indexOf("?")>-1){strHash=strHash.substring(0,strHash.indexOf("?"));}
return strHash;},ROUTE_REPLACE="([^\/]+)",ROUTE_MATCH=/{([\w\d]+)}/g,getRouteMatches=function(route,root){var pathRegex=new RegExp(route.replace(ROUTE_MATCH,ROUTE_REPLACE)+"$")
return root.match(pathRegex);},routeHandler=function(){var root=jHash.root();var defaultRoute=jHash.defaultRoute();if((root||'').length===0&&defaultRoute){jHash.set(defaultRoute.root,defaultRoute.query);return;}
for(var r in jHash._routes){if(typeof(r)==="string"){var matches=getRouteMatches(r,root);if(matches!==null){var handler=jHash._routes[r];var context={};var routeParts=r.match(ROUTE_MATCH);if(routeParts!==null){for(var rp=0;rp<routeParts.length;rp++){var routePartName=routeParts[rp].substring(1).substring(0,routeParts[rp].length-2);context[routePartName]=matches[rp+1];}}
handler.call(context);return;}}}};})(window);