This repository was archived by the owner on Jul 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrosshatch.min.js
15 lines (14 loc) · 3.7 KB
/
crosshatch.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var Crosshatch=function(){var version=0.4,urls={},history=[],can_manage_history=("pushState"in window.history)&&navigator.userAgent.match(/Android/)===null,base_path=window.location.pathname,view=undefined,previous=undefined,online=false,beforeLoad=function(){},afterLoad=function(){},transition_delay=0;var setTitle=function(_title){document.title=_title;};var _encode=function(_string){_string=encodeURIComponent(_string);_string=_string.replace(/%20/g,"+");_string=_string.replace(/%2F/g,"/");return _string;};var _decode=function(_string){_string=_string.replace(/\//g,"%2F");_string=_string.replace(/\+/g,"%20");_string=decodeURIComponent(_string);return _string;};var setLocation=function(_location){if(!online){return false;}
_location=_location.replace("%21","!");if(_location.indexOf("#!")===0){_location=_location.substring(2);}
if(_location==="previous"){if(window.history.length>2){window.history.back();}else{document.location.href="#";}}else{if(_location===""){_location="#";}else if(_location!=="#"){_location=_location.replace(/#!\//,"/");_location=_encode(_location);_location="#!"+_location;}
document.location.href=_location;}};var replaceLocation=function(_location){if(can_manage_history){window.history.replaceState(_location,"",_location);}else{document.location.href=_location;}
loader();};var pushLocation=function(_location){if(can_manage_history){window.history.pushState(_location,"",_location);}else{document.location.href=_location;}
loader();};var isLoggedIn=function(){return true;};var sendToLogin=function(view,_url){console.warn("login required. what do I do?\nPass a function to setSendToLogin, e.g.\nCrosshatch.setSendToLogin(function(_view, _url) {\n\t// do login stuff here.\n});\nto configure this properly.");};var router=function(_config){urls[_config.url]={login_required:_config.login_required===true,pattern:_config.pattern,controller:_config.controller};};var loader=function(_force){if(!online){return false;}
if(_force===undefined){_force=false;}
setTimeout(function(){var _url=window.location.hash.replace(/#!\//,"/");var _previous=history[history.length-1];if(_previous!==undefined){_previous=_decode(_previous);}
if(_url===_previous&&_force){return false;}
beforeLoad(_url,_previous);var i;for(i in urls){if(_url.match(urls[i].pattern)){if(view!==undefined){previous=view;}
view=urls[i];view.url=i;view.uri=_url;if(view.login_required&&!isLoggedIn()){sendToLogin(view,_url);}else{view.controller(view,_url);}
break;}}
history.push(_url);afterLoad(_url,_previous);},transition_delay);};var ready=function(_callback){online=true;loader();if(typeof _callback==="function"){_callback();}};var navigation_interval,navigation_interval_timeout=500;if("onhashchange"in window&&navigator.appVersion.indexOf("MSIE 7.")<0&&navigator.appVersion.indexOf("MSIE 6.")<0){window.onhashchange=loader;}else{navigation_interval=setInterval(loader,navigation_interval_timeout);}
return{version:version,urls:urls,history:history,route:router,ready:ready,load:loader,start:function(){history=[];ready();},reload:function(){loader(true);},encode:_encode,decode:_decode,setIsLoggedIn:function(_function){isLoggedIn=_function;},setSendToLogin:function(_function){sendToLogin=_function;},getCurrentView:function(){return view;},getCurrentLocation:function(){if(view!==undefined){return view.url;}return undefined;},getPreviousLocation:function(){if(previous!==undefined){return previous.uri;}return"#";},historyLength:function(){return history.length;},clearHistory:function(){history=[];},beforeLoad:function(_beforeLoad){beforeLoad=_beforeLoad;},afterLoad:function(_afterLoad){afterLoad=_afterLoad;},setTransitionDelay:function(_delay){transition_delay=_delay;},setTitle:setTitle,setLocation:setLocation,replaceLocation:replaceLocation,pushLocation:pushLocation}}();