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 basic bower reg file. Made PathJS AMD module compatible #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "mtrpcic/pathjs",
"version": "0.8.4",
"main": "path/to/main.css",
"description": "PathJS is a lightweight, client-side routing library that allows you to create "single page" applications using Hashbangs and/or HTML5 pushState.",
"license": "MIT",
"ignore": [
".jshintrc",
"**/*.txt"
]
}
361 changes: 191 additions & 170 deletions path.js
Original file line number Diff line number Diff line change
@@ -1,192 +1,213 @@
var Path = {
'version': "0.8.4",
'map': function (path) {
if (Path.routes.defined.hasOwnProperty(path)) {
return Path.routes.defined[path];
(function(window) {
(function(factory) {
if (typeof define === 'function' && define.amd) {
define(factory);
} else {
return new Path.core.route(path);
}
},
'root': function (path) {
Path.routes.root = path;
},
'rescue': function (fn) {
Path.routes.rescue = fn;
},
'history': {
'initial':{}, // Empty container for "Initial Popstate" checking variables.
'pushState': function(state, title, path){
if(Path.history.supported){
if(Path.dispatch(path)){
history.pushState(state, title, path);
}
} else {
if(Path.history.fallback){
window.location.hash = "#" + path;
// No module loader (plain <script> tag) - put directly in global namespace
window.Path = factory();
}
})(function() {

var Path = {
'version': "0.8.4",
'map': function(path) {
if (Path.routes.defined.hasOwnProperty(path)) {
return Path.routes.defined[path];
} else {
return new Path.core.route(path);
}
}
},
'popState': function(event){
var initialPop = !Path.history.initial.popped && location.href == Path.history.initial.URL;
Path.history.initial.popped = true;
if(initialPop) return;
Path.dispatch(document.location.pathname);
},
'listen': function(fallback){
Path.history.supported = !!(window.history && window.history.pushState);
Path.history.fallback = fallback;
},
'root': function(path) {
Path.routes.root = path;
},
'rescue': function(fn) {
Path.routes.rescue = fn;
},
'history': {
'initial': {}, // Empty container for "Initial Popstate" checking variables.
'pushState': function(state, title, path) {
if (Path.history.supported) {
if (Path.dispatch(path)) {
history.pushState(state, title, path);
}
} else {
if (Path.history.fallback) {
window.location.hash = "#" + path;
}
}
},
'popState': function(event) {
var initialPop = !Path.history.initial.popped && location.href == Path.history.initial.URL;
Path.history.initial.popped = true;
if (initialPop) return;
Path.dispatch(document.location.pathname);
},
'listen': function(fallback) {
Path.history.supported = !! (window.history && window.history.pushState);
Path.history.fallback = fallback;

if(Path.history.supported){
Path.history.initial.popped = ('state' in window.history), Path.history.initial.URL = location.href;
window.onpopstate = Path.history.popState;
} else {
if(Path.history.fallback){
for(route in Path.routes.defined){
if(route.charAt(0) != "#"){
Path.routes.defined["#"+route] = Path.routes.defined[route];
Path.routes.defined["#"+route].path = "#"+route;
if (Path.history.supported) {
Path.history.initial.popped = ('state' in window.history), Path.history.initial.URL = location.href;
window.onpopstate = Path.history.popState;
} else {
if (Path.history.fallback) {
for (route in Path.routes.defined) {
if (route.charAt(0) != "#") {
Path.routes.defined["#" + route] = Path.routes.defined[route];
Path.routes.defined["#" + route].path = "#" + route;
}
}
Path.listen();
}
}
Path.listen();
}
}
}
},
'match': function (path, parameterize) {
var params = {}, route = null, possible_routes, slice, i, j, compare;
for (route in Path.routes.defined) {
if (route !== null && route !== undefined) {
route = Path.routes.defined[route];
possible_routes = route.partition();
for (j = 0; j < possible_routes.length; j++) {
slice = possible_routes[j];
compare = path;
if (slice.search(/:/) > 0) {
for (i = 0; i < slice.split("/").length; i++) {
if ((i < compare.split("/").length) && (slice.split("/")[i].charAt(0) === ":")) {
params[slice.split('/')[i].replace(/:/, '')] = compare.split("/")[i];
compare = compare.replace(compare.split("/")[i], slice.split("/")[i]);
},
'match': function(path, parameterize) {
var params = {}, route = null,
possible_routes, slice, i, j, compare;
for (route in Path.routes.defined) {
if (route !== null && route !== undefined) {
route = Path.routes.defined[route];
possible_routes = route.partition();
for (j = 0; j < possible_routes.length; j++) {
slice = possible_routes[j];
compare = path;
if (slice.search(/:/) > 0) {
for (i = 0; i < slice.split("/").length; i++) {
if ((i < compare.split("/").length) && (slice.split("/")[i].charAt(0) === ":")) {
params[slice.split('/')[i].replace(/:/, '')] = compare.split("/")[i];
compare = compare.replace(compare.split("/")[i], slice.split("/")[i]);
}
}
}
if (slice === compare) {
if (parameterize) {
route.params = params;
}
return route;
}
}
}
}
return null;
},
'dispatch': function(passed_route) {
var previous_route, matched_route;
if (Path.routes.current !== passed_route) {
Path.routes.previous = Path.routes.current;
Path.routes.current = passed_route;
matched_route = Path.match(passed_route, true);

if (Path.routes.previous) {
previous_route = Path.match(Path.routes.previous);
if (previous_route !== null && previous_route.do_exit !== null) {
previous_route.do_exit();
}
}
if (slice === compare) {
if (parameterize) {
route.params = params;

if (matched_route !== null) {
matched_route.run();
return true;
} else {
if (Path.routes.rescue !== null) {
Path.routes.rescue();
}
return route;
}
}
}
}
return null;
},
'dispatch': function (passed_route) {
var previous_route, matched_route;
if (Path.routes.current !== passed_route) {
Path.routes.previous = Path.routes.current;
Path.routes.current = passed_route;
matched_route = Path.match(passed_route, true);
},
'listen': function() {
var fn = function() {
Path.dispatch(location.hash);
}

if (Path.routes.previous) {
previous_route = Path.match(Path.routes.previous);
if (previous_route !== null && previous_route.do_exit !== null) {
previous_route.do_exit();
if (location.hash === "") {
if (Path.routes.root !== null) {
location.hash = Path.routes.root;
}
}
}

if (matched_route !== null) {
matched_route.run();
return true;
} else {
if (Path.routes.rescue !== null) {
Path.routes.rescue();
// The 'document.documentMode' checks below ensure that PathJS fires the right events
// even in IE "Quirks Mode".
if ("onhashchange" in window && (!document.documentMode || document.documentMode >= 8)) {
window.onhashchange = fn;
} else {
setInterval(fn, 50);
}
}
}
},
'listen': function () {
var fn = function(){ Path.dispatch(location.hash); }

if (location.hash === "") {
if (Path.routes.root !== null) {
location.hash = Path.routes.root;
if (location.hash !== "") {
Path.dispatch(location.hash);
}
},
'core': {
'route': function(path) {
this.path = path;
this.action = null;
this.do_enter = [];
this.do_exit = null;
this.params = {};
Path.routes.defined[path] = this;
}
},
'routes': {
'current': null,
'root': null,
'rescue': null,
'previous': null,
'defined': {}
}
}

// The 'document.documentMode' checks below ensure that PathJS fires the right events
// even in IE "Quirks Mode".
if ("onhashchange" in window && (!document.documentMode || document.documentMode >= 8)) {
window.onhashchange = fn;
} else {
setInterval(fn, 50);
}

if(location.hash !== "") {
Path.dispatch(location.hash);
}
},
'core': {
'route': function (path) {
this.path = path;
this.action = null;
this.do_enter = [];
this.do_exit = null;
this.params = {};
Path.routes.defined[path] = this;
}
},
'routes': {
'current': null,
'root': null,
'rescue': null,
'previous': null,
'defined': {}
}
};
Path.core.route.prototype = {
'to': function (fn) {
this.action = fn;
return this;
},
'enter': function (fns) {
if (fns instanceof Array) {
this.do_enter = this.do_enter.concat(fns);
} else {
this.do_enter.push(fns);
}
return this;
},
'exit': function (fn) {
this.do_exit = fn;
return this;
},
'partition': function () {
var parts = [], options = [], re = /\(([^}]+?)\)/g, text, i;
while (text = re.exec(this.path)) {
parts.push(text[1]);
}
options.push(this.path.split("(")[0]);
for (i = 0; i < parts.length; i++) {
options.push(options[options.length - 1] + parts[i]);
}
return options;
},
'run': function () {
var halt_execution = false, i, result, previous;
};
Path.core.route.prototype = {
'to': function(fn) {
this.action = fn;
return this;
},
'enter': function(fns) {
if (fns instanceof Array) {
this.do_enter = this.do_enter.concat(fns);
} else {
this.do_enter.push(fns);
}
return this;
},
'exit': function(fn) {
this.do_exit = fn;
return this;
},
'partition': function() {
var parts = [],
options = [],
re = /\(([^}]+?)\)/g,
text, i;
while (text = re.exec(this.path)) {
parts.push(text[1]);
}
options.push(this.path.split("(")[0]);
for (i = 0; i < parts.length; i++) {
options.push(options[options.length - 1] + parts[i]);
}
return options;
},
'run': function() {
var halt_execution = false,
i, result, previous;

if (Path.routes.defined[this.path].hasOwnProperty("do_enter")) {
if (Path.routes.defined[this.path].do_enter.length > 0) {
for (i = 0; i < Path.routes.defined[this.path].do_enter.length; i++) {
result = Path.routes.defined[this.path].do_enter[i].apply(this, null);
if (result === false) {
halt_execution = true;
break;
if (Path.routes.defined[this.path].hasOwnProperty("do_enter")) {
if (Path.routes.defined[this.path].do_enter.length > 0) {
for (i = 0; i < Path.routes.defined[this.path].do_enter.length; i++) {
result = Path.routes.defined[this.path].do_enter[i].apply(this, null);
if (result === false) {
halt_execution = true;
break;
}
}
}
}
if (!halt_execution) {
Path.routes.defined[this.path].action();
}
}
}
if (!halt_execution) {
Path.routes.defined[this.path].action();
}
}
};
};

return Path;
});
})(window);
Loading