This repository was archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathrouter.ats
More file actions
224 lines (181 loc) · 5.48 KB
/
router.ats
File metadata and controls
224 lines (181 loc) · 5.48 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
import {Grammar} from './grammar';
import {Pipeline} from './pipeline';
/**
* @name Router
* @where shared
* @description
* The router is responsible for mapping URLs to components.
*
* You can see the state of the router by inspecting the read-only field `router.navigating`.
* This may be useful for showing a spinner, for instance.
*/
class Router {
constructor(grammar:Grammar, pipeline:Pipeline, parent, name) {
this.name = name;
this.parent = parent || null;
this.navigating = false;
this.outlets = {};
this.children = {};
this.registry = grammar;
this.pipeline = pipeline;
}
/**
* @description
* Constructs a child router.
* You probably don't need to use this unless you're writing a reusable component.
*/
childRouter(name = 'default') {
if (!this.children[name]) {
this.children[name] = new ChildRouter(this, name);
}
return this.children[name];
}
/**
* @description
* Register an object to notify of route changes.
* You probably don't need to use this unless you're writing a reusable component.
*/
registerOutlet(view, name = 'default') {
this.outlets[name] = view;
return this.renavigate();
}
/**
* @description
* Update the routing configuation and trigger a navigation.
*
* ```js
* router.config({ path: '/', component: '/user' });
* ```
*
* For more, see the [configuration](configuration) guide.
*/
config(mapping) {
this.registry.config(this.name, mapping);
return this.renavigate();
}
/**
* @description Navigate to a URL or Component.
* Returns the cannonical URL for the route navigated to.
*/
navigate(url) {
if (this.navigating) {
return Promise.resolve();
}
this.lastNavigationAttempt = url;
var instruction = this.recognize(url) || this.recognizeComponent(url);
if (!instruction) {
return Promise.reject();
}
this._startNavigating();
instruction.router = this;
return this.pipeline.process(instruction)
.then(() => this._finishNavigating(), () => this._finishNavigating())
.then(() => instruction.canonicalUrl);
}
_startNavigating() {
this.navigating = true;
}
_finishNavigating() {
this.navigating = false;
}
makeDescendantRouters(instruction) {
this.traverseInstructionSync(instruction, (instruction, childInstruction) => {
childInstruction.router = instruction.router.childRouter(childInstruction.component);
});
}
traverseInstructionSync(instruction, fn) {
forEach(instruction.outlets,
(childInstruction, outletName) => fn(instruction, childInstruction));
forEach(instruction.outlets,
(childInstruction) => this.traverseInstructionSync(childInstruction, fn));
}
traverseInstruction(instruction, fn) {
if (!instruction) {
return Promise.resolve();
}
return mapObjAsync(instruction.outlets,
(childInstruction, outletName) => boolToPromise(fn(childInstruction, outletName)))
.then(() => mapObjAsync(instruction.outlets, (childInstruction, outletName) => {
return childInstruction.router.traverseInstruction(childInstruction, fn);
}));
}
/*
* given a instruction obj
* update outlets accordingly
*/
activateOutlets(instruction) {
return this.queryOutlets((outlet, name) => {
return outlet.activate(instruction.outlets[name]);
})
.then(() => mapObjAsync(instruction.outlets, (instruction) => {
return instruction.router.activateOutlets(instruction);
}));
}
/*
* given a instruction obj
* update outlets accordingly
*/
canDeactivateOutlets(instruction) {
return this.traverseOutlets((outlet, name) => {
return boolToPromise(outlet.canDeactivate(instruction.outlets[name]));
});
}
traverseOutlets(fn) {
return this.queryOutlets(fn)
.then(() => mapObjAsync(this.children, (child) => child.traverseOutlets(fn)));
}
queryOutlets(fn) {
return mapObjAsync(this.outlets, fn);
}
recognize(url) {
return this.registry.recognize(url);
}
recognizeComponent(name) {
return this.registry.recognizeComponent(name);
}
/**
* @description Navigates to either the last URL successfully naviagted to,
* or the last URL requested if the router has yet to successfully navigate.
* You shouldn't need to use this API very often.
*/
renavigate() {
var renavigateDestination = this.previousUrl || this.lastNavigationAttempt;
if (!this.navigating && renavigateDestination) {
return this.navigate(renavigateDestination);
} else {
return Promise.resolve();
}
}
/**
* @description generate a URL from a component name and optional map of parameters.
* The URL is relative to the app's base href.
*/
generate(name:string, params) {
return this.registry.generate(name, params);
}
}
export class RootRouter extends Router {
constructor(grammar:Grammar, pipeline:Pipeline) {
super(grammar, pipeline, null, '/');
}
}
class ChildRouter extends Router {
constructor(parent, name) {
super(parent.registry, parent.pipeline, parent, name);
this.parent = parent;
}
}
function forEach(obj, fn) {
Object.keys(obj).forEach(key => fn(obj[key], key));
}
function mapObjAsync(obj, fn) {
return Promise.all(mapObj(obj, fn));
}
function mapObj(obj, fn) {
var result = [];
Object.keys(obj).forEach(key => result.push(fn(obj[key], key)));
return result;
}
function boolToPromise (value) {
return value ? Promise.resolve(value) : Promise.reject();
}