forked from mtrpcic/pathjs
-
Notifications
You must be signed in to change notification settings - Fork 3
/
path.js
220 lines (209 loc) · 7.4 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
var Path = {
'version': "0.8.4.1",
'map': function (path) {
if (Path.routes.defined.hasOwnProperty(path)) {
return Path.routes.defined[path];
} 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;
}
}
},
'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;
if (window.location.hash === "") {
if (Path.routes.root !== null) {
Path.history.pushState({}, "", Path.routes.root);
}
}
} 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();
}
}
}
},
'match': function (path, parameterize) {
function matchPathToRoute(path, route) {
var tokens = route.split('/');
var re = /\/?([^\/]+)/;
var isMatch = true;
var params = {};
var match, routePart;
for (;
isMatch &&
tokens.length &&
path.length &&
(match = re.exec(path)) ;
path = path.substring(match[0].length)) {
var routePart = tokens.shift();
if (routePart.charAt(0) == ':') {
// keep this param
params[routePart.substring(1)] = match[1];
} else if (routePart !== match[1]) {
// non-parameterized part, they must match
isMatch = false;
} // else just continue
}
return (isMatch && (0 === tokens.length) && (0 === path.length)) ?
params : false;
}
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;
params = matchPathToRoute(compare, slice);
if (params) {
if (parameterize) {
route.params = params;
}
return route;
} else {
params = {};
}
}
}
}
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 (matched_route !== null) {
matched_route.run();
return true;
} else {
if (Path.routes.rescue !== null) {
Path.routes.rescue();
}
}
}
},
'listen': function () {
var fn = function() { Path.dispatch(window.location.hash); };
if (window.location.hash === "") {
if (Path.routes.root !== null) {
window.location.hash = Path.routes.root;
}
}
// 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 (window.location.hash !== "") {
Path.dispatch(window.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;
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);
if (result === false) {
halt_execution = true;
break;
}
}
}
}
if (!halt_execution) {
Path.routes.defined[this.path].action(this.params);
}
}
};