-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
router.js
285 lines (218 loc) · 6.73 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
/**
@module ember
@submodule ember-routing
*/
var Router = requireModule("router");
var get = Ember.get, set = Ember.set;
var defineProperty = Ember.defineProperty;
var DefaultView = Ember._MetamorphView;
require("ember-routing/system/dsl");
function setupLocation(router) {
var location = get(router, 'location'),
rootURL = get(router, 'rootURL'),
options = {};
if (typeof rootURL === 'string') {
options.rootURL = rootURL;
}
if ('string' === typeof location) {
options.implementation = location;
location = set(router, 'location', Ember.Location.create(options));
}
}
/**
The `Ember.Router` class manages the application state and URLs. Refer to
the [routing guide](http://emberjs.com/guides/routing/) for documentation.
@class Router
@namespace Ember
@extends Ember.Object
*/
Ember.Router = Ember.Object.extend({
location: 'hash',
init: function() {
this.router = this.constructor.router;
this._activeViews = {};
setupLocation(this);
},
url: Ember.computed(function() {
return get(this, 'location').getURL();
}),
startRouting: function() {
this.router = this.router || this.constructor.map(Ember.K);
var router = this.router,
location = get(this, 'location'),
container = this.container,
self = this;
setupRouter(this, router, location);
container.register('view:default', DefaultView);
container.register('view:toplevel', Ember.View.extend());
location.onUpdateURL(function(url) {
self.handleURL(url);
});
this.handleURL(location.getURL());
},
didTransition: function(infos) {
var appController = this.container.lookup('controller:application'),
path = routePath(infos);
if (!('currentPath' in appController)) {
defineProperty(appController, 'currentPath');
}
set(appController, 'currentPath', path);
this.notifyPropertyChange('url');
if (get(this, 'namespace').LOG_TRANSITIONS) {
Ember.Logger.log("Transitioned into '" + path + "'");
}
},
handleURL: function(url) {
this.router.handleURL(url);
this.notifyPropertyChange('url');
},
/**
Transition to another route via the `routeTo` event which
will by default be handled by ApplicationRoute.
@method routeTo
@param {TransitionEvent} transitionEvent
*/
routeTo: function(transitionEvent) {
var handlerInfos = this.router.currentHandlerInfos;
if (handlerInfos) {
transitionEvent.sourceRoute = handlerInfos[handlerInfos.length - 1].handler;
}
this.send('routeTo', transitionEvent);
},
transitionTo: function(name) {
var args = [].slice.call(arguments);
doTransition(this, 'transitionTo', args);
},
replaceWith: function() {
var args = [].slice.call(arguments);
doTransition(this, 'replaceWith', args);
},
generate: function() {
var url = this.router.generate.apply(this.router, arguments);
return this.location.formatURL(url);
},
isActive: function(routeName) {
var router = this.router;
return router.isActive.apply(router, arguments);
},
send: function(name, context) {
this.router.trigger.apply(this.router, arguments);
},
hasRoute: function(route) {
return this.router.hasRoute(route);
},
/**
@private
Resets the state of the router by clearing the current route
handlers and deactivating them.
@method reset
*/
reset: function() {
this.router.reset();
},
_lookupActiveView: function(templateName) {
var active = this._activeViews[templateName];
return active && active[0];
},
_connectActiveView: function(templateName, view) {
var existing = this._activeViews[templateName];
if (existing) {
existing[0].off('willDestroyElement', this, existing[1]);
}
var disconnect = function() {
delete this._activeViews[templateName];
};
this._activeViews[templateName] = [view, disconnect];
view.one('willDestroyElement', this, disconnect);
}
});
Ember.Router.reopenClass({
defaultFailureHandler: {
setup: function(error) {
Ember.Logger.error('Error while loading route:', error);
// Using setTimeout allows us to escape from the Promise's try/catch block
setTimeout(function() { throw error; });
}
}
});
function getHandlerFunction(router) {
var seen = {}, container = router.container,
DefaultRoute = container.resolve('route:basic');
return function(name) {
var routeName = 'route:' + name,
handler = container.lookup(routeName);
if (seen[name]) { return handler; }
seen[name] = true;
if (!handler) {
if (name === 'loading') { return {}; }
if (name === 'failure') { return router.constructor.defaultFailureHandler; }
container.register(routeName, DefaultRoute.extend());
handler = container.lookup(routeName);
if (get(router, 'namespace.LOG_ACTIVE_GENERATION')) {
Ember.Logger.info("generated -> " + routeName, { fullName: routeName });
}
}
if (name === 'application') {
// Inject default `routeTo` handler.
handler.events = handler.events || {};
handler.events.routeTo = handler.events.routeTo || Ember.TransitionEvent.defaultHandler;
}
handler.routeName = name;
return handler;
};
}
function routePath(handlerInfos) {
var path = [];
for (var i=1, l=handlerInfos.length; i<l; i++) {
var name = handlerInfos[i].name,
nameParts = name.split(".");
path.push(nameParts[nameParts.length - 1]);
}
return path.join(".");
}
function setupRouter(emberRouter, router, location) {
var lastURL;
router.getHandler = getHandlerFunction(emberRouter);
var doUpdateURL = function() {
location.setURL(lastURL);
};
router.updateURL = function(path) {
lastURL = path;
Ember.run.once(doUpdateURL);
};
if (location.replaceURL) {
var doReplaceURL = function() {
location.replaceURL(lastURL);
};
router.replaceURL = function(path) {
lastURL = path;
Ember.run.once(doReplaceURL);
};
}
router.didTransition = function(infos) {
emberRouter.didTransition(infos);
};
}
function doTransition(router, method, args) {
var passedName = args[0], name;
if (!router.router.hasRoute(args[0])) {
name = args[0] = passedName + '.index';
} else {
name = passedName;
}
Ember.assert("The route " + passedName + " was not found", router.router.hasRoute(name));
router.router[method].apply(router.router, args);
router.notifyPropertyChange('url');
}
Ember.Router.reopenClass({
map: function(callback) {
var router = this.router = new Router();
var dsl = Ember.RouterDSL.map(function() {
this.resource('application', { path: "/" }, function() {
callback.call(this);
});
});
router.map(dsl.generate());
return router;
}
});