Repository navigation
Expand file tree
/
Copy pathroute.js
More file actions
313 lines (272 loc) · 8.15 KB
/
Copy pathroute.js
File metadata and controls
313 lines (272 loc) · 8.15 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Generated by CoffeeScript 1.11.1
(function() {
var $, Path, Route, Spine, escapeRegExp, hashStrip, namedParam, splatParam,
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty,
slice = [].slice;
Spine = this.Spine || require('spine');
$ = Spine.$;
hashStrip = /^#*/;
namedParam = /:([\w\d]+)/g;
splatParam = /\*([\w\d]+)/g;
escapeRegExp = /[-[\]{}()+?.,\\^$|#\s]/g;
Path = (function(superClass) {
extend(Path, superClass);
function Path(path, callback) {
var match;
this.names = [];
this.path = path;
this.callback = callback;
if (typeof path === 'string') {
namedParam.lastIndex = 0;
while ((match = namedParam.exec(path)) !== null) {
this.names.push(match[1]);
}
splatParam.lastIndex = 0;
while ((match = splatParam.exec(path)) !== null) {
this.names.push(match[1]);
}
path = path.replace(escapeRegExp, '\\$&').replace(namedParam, '([^\/]*)').replace(splatParam, '(.*?)');
this.route = new RegExp("^" + path + "$");
} else {
this.route = path;
}
}
Path.prototype.match = function(path, options) {
var i, j, len, match, param, params;
if (options == null) {
options = {};
}
if (!(match = this.route.exec(path))) {
return false;
}
options.match = match;
params = match.slice(1);
if (this.names.length) {
for (i = j = 0, len = params.length; j < len; i = ++j) {
param = params[i];
options[this.names[i]] = param;
}
}
Route.trigger('before', this);
return this.callback.call(null, options) !== false;
};
return Path;
})(Spine.Module);
Route = (function(superClass) {
var ref;
extend(Route, superClass);
Route.extend(Spine.Events);
Route.historySupport = ((ref = window.history) != null ? ref.pushState : void 0) != null;
Route.options = {
trigger: true,
history: false,
shim: false,
replace: false,
redirect: false
};
Route.routers = [];
Route.setup = function(options) {
if (options == null) {
options = {};
}
this.options = $.extend({}, this.options, options);
if (this.options.history) {
this.history = this.historySupport && this.options.history;
}
if (this.options.shim) {
return;
}
if (this.history) {
$(window).on('popstate', this.change);
} else {
$(window).on('hashchange', this.change);
}
return this.change();
};
Route.off = function() {
var unbindResult;
unbindResult = Spine.Events.off.apply(this, arguments);
if (arguments.length > 0) {
return unbindResult;
}
if (this.options.shim) {
return;
}
if (this.history) {
return $(window).off('popstate', this.change);
} else {
return $(window).off('hashchange', this.change);
}
};
Route.navigate = function() {
var args, lastArg, options, path, routes;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
options = {};
lastArg = args[args.length - 1];
if (typeof lastArg === 'object') {
options = args.pop();
} else if (typeof lastArg === 'boolean') {
options.trigger = args.pop();
}
options = $.extend({}, this.options, options);
path = args.join('/');
if (this.path === path) {
return;
}
this.path = path;
if (options.trigger) {
this.trigger('navigate', this.path);
routes = this.matchRoutes(this.path, options);
if (!routes.length) {
if (typeof options.redirect === 'function') {
return options.redirect.apply(this, [this.path, options]);
} else {
if (options.redirect === true) {
this.redirect(this.path);
}
}
}
}
if (options.shim) {
return true;
} else if (this.history && options.replace) {
return history.replaceState({}, document.title, this.path);
} else if (this.history) {
return history.pushState({}, document.title, this.path);
} else {
return window.location.hash = this.path;
}
};
Route.create = function() {
var router;
router = new this;
this.routers.push(router);
return router;
};
Route.add = function(path, callback) {
return this.router.add(path, callback);
};
Route.prototype.add = function(path, callback) {
var key, results, value;
if (typeof path === 'object' && !(path instanceof RegExp)) {
results = [];
for (key in path) {
value = path[key];
results.push(this.add(key, value));
}
return results;
} else {
return this.routes.push(new Path(path, callback));
}
};
Route.prototype.destroy = function() {
var r;
this.routes.length = 0;
return this.constructor.routers = (function() {
var j, len, ref1, results;
ref1 = this.constructor.routers;
results = [];
for (j = 0, len = ref1.length; j < len; j++) {
r = ref1[j];
if (r !== this) {
results.push(r);
}
}
return results;
}).call(this);
};
Route.getPath = function() {
var path;
if (this.history) {
path = window.location.pathname;
if (path.substr(0, 1) !== '/') {
path = '/' + path;
}
} else {
path = window.location.hash;
path = path.replace(hashStrip, '');
}
return path;
};
Route.getHost = function() {
return window.location.protocol + "//" + window.location.host;
};
Route.change = function() {
var path;
path = Route.getPath();
if (path === Route.path) {
return;
}
Route.path = path;
return Route.matchRoutes(Route.path);
};
Route.matchRoutes = function(path, options) {
var j, len, match, matches, ref1, router;
matches = [];
ref1 = this.routers.concat([this.router]);
for (j = 0, len = ref1.length; j < len; j++) {
router = ref1[j];
match = router.matchRoute(path, options);
if (match) {
matches.push(match);
}
}
if (matches.length) {
this.trigger('change', matches, path);
}
return matches;
};
Route.redirect = function(path) {
return window.location = path;
};
function Route() {
this.routes = [];
}
Route.prototype.matchRoute = function(path, options) {
var j, len, ref1, route;
ref1 = this.routes;
for (j = 0, len = ref1.length; j < len; j++) {
route = ref1[j];
if (route.match(path, options)) {
return route;
}
}
};
Route.prototype.trigger = function() {
var args, ref1;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
args.splice(1, 0, this);
return (ref1 = this.constructor).trigger.apply(ref1, args);
};
return Route;
})(Spine.Module);
Route.router = new Route;
Route.prototype.unbind = Route.prototype.off;
Spine.Controller.include({
route: function(path, callback) {
if (this.router instanceof Spine.Route) {
return this.router.add(path, this.proxy(callback));
} else {
return Spine.Route.add(path, this.proxy(callback));
}
},
routes: function(routes) {
var key, results, value;
results = [];
for (key in routes) {
value = routes[key];
results.push(this.route(key, value));
}
return results;
},
navigate: function() {
return Spine.Route.navigate.apply(Spine.Route, arguments);
}
});
Route.Path = Path;
Spine.Route = Route;
if (typeof module !== "undefined" && module !== null) {
module.exports = Route;
}
}).call(this);