Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added unload function execution capabilities #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 81 additions & 13 deletions lib/routie.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
(function(w) {

var currentRoute = null;
var routes = [];
var map = {};
var reference = "routie";
var oldReference = w[reference];

var Route = function(path, name) {
var Route = function(path, name, unloadFunction) {
this.name = name;
this.path = path;
this.keys = [];
this.fns = [];
this.params = {};
this.plainParams = [];
this.regex = pathToRegexp(this.path, this.keys, false, false);

this.unloadFunction = unloadFunction;
};

Route.prototype.addHandler = function(fn) {
this.fns.push(fn);
};

Route.prototype.addUnload = function(fn){
this.unloadFunction = fn;
};

Route.prototype.removeHandler = function(fn) {
for (var i = 0, c = this.fns.length; i < c; i++) {
Expand All @@ -30,10 +35,33 @@
};

Route.prototype.run = function(params) {
for (var i = 0, c = this.fns.length; i < c; i++) {
var that = this;
window.onbeforeunload = null;
if(that.unloadFunction !== null){
window.onbeforeunload = function(ev){return that.runUnload(that.plainParams)};
}

for (var i = 0, c = this.fns.length; i < c; i++) {
this.fns[i].apply(this, params);
}
};

Route.prototype.runHashUnload = function(params){
if(this.unloadFunction === null){
return true;
} else {
var text = this.unloadFunction.apply(this, params);
if(text !== null && text !== '' && text !== undefined && typeof text === "string"){
return confirm(text);
} else {
return true;
}
}
};

Route.prototype.runUnload = function(params){
return this.unloadFunction.apply(this, params);
};

Route.prototype.match = function(path, params){
var m = this.regex.exec(path);
Expand All @@ -51,7 +79,7 @@
}
params.push(val);
}

this.plainParams = params;
return true;
};

Expand Down Expand Up @@ -85,25 +113,39 @@
return new RegExp('^' + path + '$', sensitive ? '' : 'i');
};

var addHandler = function(path, fn) {
var addHandler = function(path, fn, unloadDialogs) {
var s = path.split(' ');
var name = (s.length == 2) ? s[0] : null;
path = (s.length == 2) ? s[1] : s[0];

if (!map[path]) {
map[path] = new Route(path, name);
map[path] = new Route(path, name, matchUnloadFunction(path, unloadDialogs));
routes.push(map[path]);
}
map[path].addHandler(fn);
};

var routie = function(path, fn) {
var matchUnloadFunction = function(path, unloadDialogs){
var unloadFunction = null;

if(!unloadDialogs){
return unloadFunction;
}

if(unloadDialogs.hasOwnProperty(path)){
unloadFunction = unloadDialogs[path];
}

return unloadFunction;
};

var routie = function(path, fn, unload) {
if (typeof fn == 'function') {
addHandler(path, fn);
addHandler(path, fn, unload);
routie.reload();
} else if (typeof path == 'object') {
} else if (typeof path == 'object') {;
for (var p in path) {
addHandler(p, path[p]);
addHandler(p, path[p], fn);
}
routie.reload();
} else if (typeof fn === 'undefined') {
Expand Down Expand Up @@ -163,13 +205,39 @@
var checkRoute = function(hash, route) {
var params = [];
if (route.match(hash, params)) {
route.run(params);
return true;
// runit is a parameter which flags wether the Route should run or not.
// when the hash of the currentRoute is the same as the window.hash then we don't have to run the route again
var runit = false;
if(!currentRoute){
runit = true;
} else {
if(currentRoute.hash !== getHash()){ // avoid unload function execution when coming back from return false unload
runit = true;
}
}

if(runit){
currentRoute = route;
route.hash = hash;
route.run(params);
return true;
} else {
return false;
}
}
return false;
};

var hashChanged = routie.reload = function() {
if(currentRoute){
if(currentRoute.hash !== getHash()){ // avoid unload function execution when coming back from return false unload
if(!currentRoute.runHashUnload(currentRoute.plainParams)){
window.location.replace('#' + currentRoute.toURL(currentRoute.params));
return false;
}
}
}

var hash = getHash();
for (var i = 0, c = routes.length; i < c; i++) {
var route = routes[i];
Expand Down