Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Avantica authored and Avantica committed Jun 19, 2012
1 parent 0bf21b8 commit a20a7a5
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 0 deletions.
52 changes: 52 additions & 0 deletions demo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<!-- Always force latest IE rendering engine (even in intranet) & Chrome Frame
Remove this if you use the .htaccess -->
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title>Route32 Demo</title>
<meta name="description" content="Example Implementation of Route32 jQuery Anchor Routing Plugin" />
<meta name="author" content="Rolando Garro<[email protected]>" />
<meta name="viewport" content="width=device-width; initial-scale=1.0" />
<!-- Replace favicon.ico & apple-touch-icon.png in the root of your domain and delete these references -->
<link rel="shortcut icon" href="/favicon.ico" />
<link rel="apple-touch-icon" href="/apple-touch-icon.png" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="jquery.route32.js"></script>
<script type="text/javascript">
$(document).ready(function(){

var mynumber = 0;

function increase(){
mynumber = mynumber +1;
}

var router = new Route32({
'automatic':true
});

router.add('#/caribean/',function(){
increase();
$("#arrivalPlace").html("Guapiles by the highway " + mynumber);
});

router.add('#/centralvalley/',function(){
increase();
$("#arrivalPlace").html("SanJose Caribenos Station" + mynumber);
});

router.drive();

});
</script>
</head>
<body>
<ul>
<li><a class="nav" href="#/caribean/">Caribean</a></li>
<li><a class="nav" href="#/centralvalley/">Central Valley</a></li>
</ul>
<div id="arrivalPlace"></div>
</body>
</html>
105 changes: 105 additions & 0 deletions jquery.route32.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* Route32 --## Simple Anchor Location Router ##--
* executes callback on location hash change that matches declared routes.
* Intenteded to be use as a piece on JavaScript MVC Apps
*
* @author Rolando Garro <[email protected]>
* @requires jQuery
*/
//BEGIN
//Requires jQuery
if(typeof jQuery != "undefined"){
/* begin Route32 */
function Route32(options){
//Settings
var settings = $.extend({
'automatic':true,
'selector':'.nav'
},options);
//array of hashes containing hashsregexpstr,callbackfunc pairs
var routes = [];

var activeHash = '';

//methods
var methods = {
//initial method
'init':function(){
window.onhashchange = function(evt){
activeHash = "#" + evt.newURL.split("#")[1];
//activeHash = methods.getHashValue();
};
},
//verifies is string is a valid location hash
'isValidHash':function(hashStr){
return true;
},
'isValidCallbackfunc':function(callbackfunc){
if(typeof callbackfunc == "function"){
return true;
}else{
return false;
}
},
'getHashValue':function(evt){
//return window.location.hash;
return "#" + evt.newURL.split("#")[1]
},
'executeCurrent':function(){
$.each(routes,function(index,value){
if(value.hash == activeHash){
value.callback();
}
});
}
};
//adds routes
this.add = function(hashRegexpStr,callbackfunc){
if(methods.isValidHash(hashRegexpStr) && methods.isValidCallbackfunc(callbackfunc)){
routes.push({hash:hashRegexpStr,callback:callbackfunc});
}else{
alert('route should be a valid hash string, callback function pair.');
}
};

//starts driving
this.drive = function(){
if(routes.length > 0){
if(settings.automatic){
//start listening location changes
window.onhashchange = function(evt){
activeHash = methods.getHashValue(evt);
methods.executeCurrent();
};
}else{
//listen selector click
$(settings.selector).live('click',function(){
var turn = true;
window.onhashchange = function(evt){
activeHash = methods.getHashValue(evt);
turn = false;
methods.executeCurrent();
};
if(turn){
methods.executeCurrent();
}
});
}
}else{
alert('use add method to add routes');
}
};
//executes actual route arbitrarily
this.again = function(){
methods.executeCurrent();
};

methods.init();

return this;
};
/* end Route32 */
}else{
alert("jQuery is required to ride Route32.");
}
//END

0 comments on commit a20a7a5

Please sign in to comment.