-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathdsl.js
195 lines (150 loc) · 5.54 KB
/
dsl.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
import { assign } from 'ember-utils';
import { assert, deprecate } from 'ember-debug';
let uuid = 0;
class DSL {
constructor(name, options) {
this.parent = name;
this.enableLoadingSubstates = options && options.enableLoadingSubstates;
this.matches = [];
this.explicitIndex = undefined;
this.options = options;
}
route(name, options = {}, callback) {
let dummyErrorRoute = `/_unused_dummy_error_path_route_${name}/:error`;
if (arguments.length === 2 && typeof options === 'function') {
callback = options;
options = {};
}
assert(`'${name}' cannot be used as a route name.`, (() => {
if (options.overrideNameAssertion === true) { return true; }
return ['array', 'basic', 'object', 'application'].indexOf(name) === -1;
})());
if (this.enableLoadingSubstates) {
createRoute(this, `${name}_loading`, { resetNamespace: options.resetNamespace });
createRoute(this, `${name}_error`, { resetNamespace: options.resetNamespace, path: dummyErrorRoute });
}
if (callback) {
let fullName = getFullName(this, name, options.resetNamespace);
let dsl = new DSL(fullName, this.options);
createRoute(dsl, 'loading');
createRoute(dsl, 'error', { path: dummyErrorRoute });
callback.call(dsl);
createRoute(this, name, options, dsl.generate());
} else {
createRoute(this, name, options);
}
}
push(url, name, callback, serialize) {
let parts = name.split('.');
if (this.options.engineInfo) {
let localFullName = name.slice(this.options.engineInfo.fullName.length + 1);
let routeInfo = assign({ localFullName }, this.options.engineInfo);
if (serialize) {
routeInfo.serializeMethod = serialize;
}
this.options.addRouteForEngine(name, routeInfo);
} else if (serialize) {
throw new Error(`Defining a route serializer on route '${name}' outside an Engine is not allowed.`);
}
if (url === '' || url === '/' || parts[parts.length - 1] === 'index') { this.explicitIndex = true; }
this.matches.push(url, name, callback);
}
resource(name, options = {}, callback) {
if (arguments.length === 2 && typeof options === 'function') {
callback = options;
options = {};
}
options.resetNamespace = true;
deprecate('this.resource() is deprecated. Use this.route(\'name\', { resetNamespace: true }, function () {}) instead.', false, { id: 'ember-routing.router-resource', until: '3.0.0' });
this.route(name, options, callback);
}
generate() {
let dslMatches = this.matches;
if (!this.explicitIndex) {
this.route('index', { path: '/' });
}
return match => {
for (let i = 0; i < dslMatches.length; i += 3) {
match(dslMatches[i]).to(dslMatches[i + 1], dslMatches[i + 2]);
}
};
}
mount(_name, options = {}) {
let engineRouteMap = this.options.resolveRouteMap(_name);
let name = _name;
if (options.as) {
name = options.as;
}
let fullName = getFullName(this, name, options.resetNamespace);
let engineInfo = {
name: _name,
instanceId: uuid++,
mountPoint: fullName,
fullName
};
let path = options.path;
if (typeof path !== 'string') {
path = `/${name}`;
}
let callback;
let dummyErrorRoute = `/_unused_dummy_error_path_route_${name}/:error`;
if (engineRouteMap) {
let shouldResetEngineInfo = false;
let oldEngineInfo = this.options.engineInfo;
if (oldEngineInfo) {
shouldResetEngineInfo = true;
this.options.engineInfo = engineInfo;
}
let optionsForChild = assign({ engineInfo }, this.options);
let childDSL = new DSL(fullName, optionsForChild);
createRoute(childDSL, 'loading');
createRoute(childDSL, 'error', { path: dummyErrorRoute });
engineRouteMap.class.call(childDSL);
callback = childDSL.generate();
if (shouldResetEngineInfo) {
this.options.engineInfo = oldEngineInfo;
}
}
let localFullName = 'application';
let routeInfo = assign({ localFullName }, engineInfo);
if (this.enableLoadingSubstates) {
// These values are important to register the loading routes under their
// proper names for the Router and within the Engine's registry.
let substateName = `${name}_loading`;
let localFullName = `application_loading`;
let routeInfo = assign({ localFullName }, engineInfo);
createRoute(this, substateName, { resetNamespace: options.resetNamespace });
this.options.addRouteForEngine(substateName, routeInfo);
substateName = `${name}_error`;
localFullName = `application_error`;
routeInfo = assign({ localFullName }, engineInfo);
createRoute(this, substateName, { resetNamespace: options.resetNamespace, path: dummyErrorRoute });
this.options.addRouteForEngine(substateName, routeInfo);
}
this.options.addRouteForEngine(fullName, routeInfo);
this.push(path, fullName, callback);
}
}
export default DSL;
function canNest(dsl) {
return dsl.parent !== 'application';
}
function getFullName(dsl, name, resetNamespace) {
if (canNest(dsl) && resetNamespace !== true) {
return `${dsl.parent}.${name}`;
} else {
return name;
}
}
function createRoute(dsl, name, options = {}, callback) {
let fullName = getFullName(dsl, name, options.resetNamespace);
if (typeof options.path !== 'string') {
options.path = `/${name}`;
}
dsl.push(options.path, fullName, callback, options.serialize);
}
DSL.map = callback => {
let dsl = new DSL();
callback.call(dsl);
return dsl;
};