forked from mtrpcic/pathjs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
path.js
187 lines (177 loc) · 5.87 KB
/
path.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
var current = null,
root = null,
rescue = null,
previous = null,
defined = {};
var Path = {
map: function (path) {
if (defined.hasOwnProperty(path)) {
return defined[path];
} else {
return new Route(path);
}
},
root: function (path) {
root = path;
},
rescue: function (fn) {
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){
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 = false;//('state' in window.history);
Path.history.initial.URL = location.href;
window.onpopstate = Path.history.popState;
} else {
if(Path.history.fallback){
for(route in defined){
if(route.charAt(0) != "#"){
defined["#"+route] = defined[route];
defined["#"+route].path = "#"+route;
}
}
Path.listen();
}
}
}
},
match: function (path, parameterize) {
var params = {}, route = null, possible_routes, slice, i, j, compare;
for (route in defined) {
if (route !== null && route !== undefined) {
route = 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 (current !== passed_route) {
previous = current;
current = passed_route;
matched_route = Path.match(passed_route, true);
if (previous) {
previous_route = Path.match(previous);
if (previous_route !== null && previous_route.do_exit !== null) {
previous_route.do_exit();
}
}
if (matched_route !== null) {
matched_route.run();
return true;
} else {
if (rescue !== null) {
rescue();
}
}
}
},
listen: function () {
var fn = function(){ Path.dispatch(location.hash); }
if (location.hash === '' || location.hash === '#') {
if (root !== null) {
location.hash = root;
}
}
window.onhashchange = fn;
if(location.hash !== "") {
Path.dispatch(location.hash);
}
}
};
function Route(path) {
this.path = path;
this.action = null;
this.do_enter = [];
this.do_exit = null;
this.params = {};
defined[path] = this;
}
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 (defined[this.path].hasOwnProperty("do_enter")) {
if (defined[this.path].do_enter.length > 0) {
for (i = 0; i < defined[this.path].do_enter.length; i++) {
result = defined[this.path].do_enter[i].call(this);
if (result === false) {
halt_execution = true;
break;
}
}
}
}
if (!halt_execution) {
defined[this.path].action();
}
}
};
if (typeof module === 'object' && module && module.exports)
module.exports = Path;