-
Notifications
You must be signed in to change notification settings - Fork 30
/
Router.js
405 lines (348 loc) · 12.4 KB
/
Router.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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*!
* Ext.ux.Router
* http://github.com/brunotavares/Ext.ux.Router
*
* Copyright 2012 Bruno Tavares
* Released under the MIT license
* Check MIT-LICENSE.txt
*/
/*
* @class Ext.ux.Router
* @extend Ext.app.Controller
*
* Enables routing engine for Ext JS 4 MVC architecture. Responsible for parsing URI Token and fire a dispatch action
* process. Uses Ext.History internally to detect URI Token changes, providing browser history navigation capabilities.
*
* Ext.application({
* name: 'MyApp',
* ...
* paths: {
* 'Ext.ux': 'app/ux'
* },
* routes: {
* '/': 'home#index',
* 'users': 'users#list',
* 'users/:id/edit': 'users#edit'
* }
* });
*
* Given the routing example above, we would develop controllers specifying their correspondents actions.
*
* Ext.define('AM.controller.Users', {
* extend: 'Ext.app.Controller',
* views: ['user.List', 'user.Edit'],
* stores: ['Users'],
* models: ['User'],
*
* //actions
* list: function()
* {
* //TODO: show users list
* },
*
* edit: function(params)
* {
* //TODO: show user form
* }
* });
*
* @docauthor Bruno Tavares
*/
Ext.define('Ext.ux.Router', {
singleton: true,
alternateClassName: 'Ext.Router',
mixins: {
observable: 'Ext.util.Observable'
},
requires: [
'Ext.util.History',
'Ext.app.Application'
],
// @private
constructor: function() {
var me = this;
me.ready = false;
me.routes = [];
me.mixins.observable.constructor.call(me);
},
/**
* Processes the routes for the given app and initializes Ext.History. Also parses
* the initial token, generally main, home, index, etc.
* @private
*/
init: function(app) {
var me = this,
history = Ext.History;
if (!app || !app.routes) {
return;
}
me.processRoutes(app);
if (me.ready) {
return;
}
me.ready = true;
me.addEvents(
/**
* @event routemissed
* Fires when no route is found for a given URI Token
* @param {String} uri The URI Token
*/
'routemissed',
/**
* @event beforedispatch
* Fires before loading controller and calling its action. Handlers can return false to cancel the dispatch
* process.
* @param {String} uri URI Token.
* @param {Object} match Route that matched the URI Token.
* @param {Object} params The params appended to the URI Token.
*/
'beforedispatch',
/**
* @event dispatch
* Fires after loading controller and calling its action.
* @param {String} uri URI Token.
* @param {Object} match Route that matched the URI Token.
* @param {Object} params The params appended to the URI Token.
* @param {Object} controller The controller handling the action.
*/
'dispatch'
);
history.init();
history.on('change', me.parse, me);
Ext.onReady(function() {
me.parse(history.getToken());
});
},
/**
* Convert routes string definied in Ext.Application into structures objects.
* @private
*/
processRoutes: function(app) {
var key,
appRoutes = app.routes;
for (key in appRoutes) {
if (appRoutes.hasOwnProperty(key)) {
this.routeMatcher(app, key, appRoutes[key]);
}
}
},
/**
* Creates a matcher for a route config, based on
* {@link https://github.com/cowboy/javascript-route-matcher javascript-route-matcher}
* @private
*/
routeMatcher: function(app, route, rules) {
var routeObj, action,
me = this,
routes = me.routes,
reRoute = route,
reParam = /([:*])(\w+)/g,
reEscape= /([-.+?\^${}()|\[\]\/\\])/g,
names = [];
if (rules.regex) {
routeObj = {
app : app,
route : route,
regex : rules.regex,
controller : rules.controller,
action : rules.action
};
delete rules.controller;
delete rules.action;
routeObj.rules = rules;
}
else {
reRoute = reRoute.replace(reEscape, "\\$1").replace(reParam, function(_, mode, name) {
names.push(name);
return mode === ":" ? "([^/]*)" : "(.*)";
});
routeObj = {
app : app,
route : route,
names : names,
matcher : new RegExp("^" + reRoute + "$"),
manageArgs : route.indexOf('?') !== -1
};
if (Ext.isString(rules)) {
action = rules.split('#');
routeObj.controller = action[0];
routeObj.action = action[1];
routeObj.rules = undefined;
}
else {
routeObj.controller = rules.controller;
routeObj.action = rules.action;
delete rules.controller;
delete rules.action;
routeObj.rules = rules;
}
}
//<debug error>
if (!routeObj.controller && Ext.isDefined(Ext.global.console)) {
Ext.global.console.error("[Ext.ux.Router] Config 'controller' can't be undefined", route, rules);
}
if (!routeObj.action && Ext.isDefined(Ext.global.console)) {
Ext.global.console.error("[Ext.ux.Router] Config 'action' can't be undefined", route, rules);
}
//</debug>
routes.push(routeObj);
},
/**
* Receives a url token and goes trough each of of the defined route objects searching
* for a match.
* @private
*/
parse: function(token) {
var route, matches, params, names, j, param, value, rules,
tokenArgs, tokenWithoutArgs,
me = this,
routes = me.routes,
i = 0,
len = routes.length;
token = token||"";
tokenWithoutArgs = token.split('?');
tokenArgs = tokenWithoutArgs[1];
tokenWithoutArgs = tokenWithoutArgs[0];
for (; i < len; i++) {
route = routes[i];
if (route.regex) {
matches = token.match(route.regex);
if (matches) {
matches = matches.slice(1);
if (me.dispatch(token, route, matches)) {
return { captures: matches };
}
}
}
else {
matches = route.manageArgs ? token.match(route.matcher) : tokenWithoutArgs.match(route.matcher);
// special index rule
if (tokenWithoutArgs === '' && route.route === '/' || tokenWithoutArgs === '/' && route.route === '') {
matches = [];
}
if (matches) {
params = {};
names = route.names;
rules = route.rules;
j = 0;
while (j < names.length) {
param = names[j++];
value = matches[j];
if (rules && param in rules && !this.validateRule(rules[param], value)) {
matches = false;
break;
}
params[param] = value;
}
if (tokenArgs && !route.manageArgs) {
params = Ext.applyIf(params, Ext.Object.fromQueryString(tokenArgs));
}
if (matches && me.dispatch(token, route, params)) {
return params;
}
}
}
}
me.fireEvent('routemissed', token);
return false;
},
/**
* Each route can have rules, and this function ensures these rules. They could be Functions,
* Regular Expressions or simple string strong comparisson.
* @private
*/
validateRule: function(rule, value) {
if (Ext.isFunction(rule)) {
return rule(value);
}
else if (Ext.isFunction(rule.test)) {
return rule.test(value);
}
return rule === value;
},
/**
* Tries to dispatch a route to the controller action. Fires the 'beforedispatch' and
* 'dispatch' events.
* @private
*/
dispatch: function(token, route, params) {
var controller,
me = this;
if (me.fireEvent('beforedispatch', token, route, params) === false) {
return false;
}
controller = me.getController(route);
if (!controller) {
return false;
}
//<debug error>
if (!controller[route.action] && Ext.isDefined(Ext.global.console)) {
Ext.global.console.error("[Ext.ux.Router] Controller action not found ", route.controller, route.action);
return false;
}
//</debug>
controller[route.action].call(controller, params, token, route);
me.fireEvent('dispatch', token, route, params, controller);
return true;
},
/**
* Redirects the page to other URI.
* @param {String} uri URI Token
* @param {Boolean} [preventDuplicates=true] When true, if the passed token matches the current token
* it will not save a new history step. Set to false if the same state can be saved more than once
* at the same history stack location.
*/
redirect: function(token, preventDup) {
var history = Ext.History;
if (preventDup !== false && history.getToken() === token) {
this.parse(token);
}
else {
history.add(token);
}
},
/**
* Utility method that receives a route and returns the controller instance.
* Controller name could be either the regular name (e.g. UserSettings), a
* string to be capitalized (e.g. userSettings -> UserSettings) or even separated
* by namespace (e.g. user.Settings).
*/
getController: function(route) {
var controllerFullName, controllerCapitalized,
app = route.app,
classMgr = Ext.ClassManager,
controller = route.controller;
// try regular name
controllerFullName = app.getModuleClassName(controller, 'controller');
if (!classMgr.get(controllerFullName)) {
// try capitalized
controller = Ext.String.capitalize(controller);
controllerFullName = app.getModuleClassName(controller, 'controller');
if (!classMgr.get(controllerFullName)) {
//<debug>
if (Ext.isDefined(Ext.global.console)) {
Ext.global.console.warn("[Ext.ux.Router] Controller not found ", route.controller);
}
//</debug>
return false;
}
// fix controller name
route.controller = controller;
}
return app.getController(controller);
}
},
function() {
/*
* Patch Ext.Application to auto-initialize Router
*/
Ext.override(Ext.app.Application, {
enableRouter: true,
onBeforeLaunch: function() {
this.callOverridden();
if(this.enableRouter){
Ext.ux.Router.init(this);
}
}
});
});