-
Notifications
You must be signed in to change notification settings - Fork 192
/
router.js
167 lines (129 loc) · 4.36 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
import Qs from 'qs';
import PathToRegexp from 'path-to-regexp';
SharedRouter = class {
constructor() {
this._routes = [];
this._routesMap = {};
// holds onRoute callbacks
this._onRouteCallbacks = [];
this.env = {};
this.env.trailingSlash = new Meteor.EnvironmentVariable();
}
route(pathDef, options, group) {
if (!/^\/.*/.test(pathDef)) {
const message = "route's path must start with '/'";
throw new Error(message);
}
options = options || {};
const currentRoute = new Route(this, pathDef, options, group);
currentRoute._init();
this._routes.push(currentRoute);
if (options.name) {
this._routesMap[options.name] = currentRoute;
}
this._triggerRouteRegister(currentRoute);
return currentRoute;
}
// XXX this function needs to be cleaned up if possible by removing `if (this.isServer)`
// and `if (this.isClient)` if possible
path(pathDef, fields = {}, queryParams = {}) {
if (this._routesMap[pathDef]) {
pathDef = this._routesMap[pathDef].path;
}
let newPath = '';
// Prefix the path with the router global prefix
if (this._basePath) {
newPath += `/${this._basePath}/`;
}
// Encode query params
queryParams = this._encodeValues(queryParams);
const toPath = PathToRegexp.compile(pathDef);
newPath += toPath(fields);
// If we have one optional parameter in path definition e.g.
// /:category?
// and the parameter isn't present, path will be an empty string.
// We have this check as a value for path is required by e.g. FlowRouter.go()
if (!newPath) {
newPath = '/';
}
// Replace multiple slashes with single slash
newPath = newPath.replace(/\/\/+/g, '/');
// remove trailing slash
// but keep the root slash if it's the only one
newPath = newPath.match(/^\/{1}$/) ? newPath : newPath.replace(/\/$/, '');
// explictly asked to add a trailing slash
if (this.env.trailingSlash.get() && _.last(newPath) !== '/') {
newPath += '/';
}
const strQueryParams = Qs.stringify(queryParams || {});
if (strQueryParams) {
newPath += `?${strQueryParams}`;
}
return newPath;
}
go() {
// client only
}
watchPathChange() {
// client only
}
group(options) {
return new Group(this, options);
}
url(...args) {
// We need to remove the leading base path, or "/", as it will be inserted
// automatically by `Meteor.absoluteUrl` as documented in:
// http://docs.meteor.com/#/full/meteor_absoluteurl
const completePath = this.path(...args);
const basePath = this._basePath || '/';
const pathWithoutBase = completePath.replace(RegExp(`^${basePath}`), '');
return Meteor.absoluteUrl(pathWithoutBase);
}
// For client:
// .current is not reactive on the client
// This is by design. use .getParam() instead
// If you really need to watch the path change, use .watchPathChange()
current() {
// We can't trust outside, that's why we clone this
// Anyway, we can't clone the whole object since it has non-jsonable values
// That's why we clone what's really needed.
const context = _.clone(this._getCurrentRouteContext());
context.queryParams = EJSON.clone(context.queryParams);
context.params = EJSON.clone(context.params);
return context;
}
onRouteRegister(cb) {
this._onRouteCallbacks.push(cb);
}
_encodeValues(obj) {
const newObj = {};
Object.keys(obj).forEach(key => {
const value = obj[key];
newObj[key] = typeof value !== 'undefined' ? encodeURIComponent(value) : value;
});
return newObj;
}
_triggerRouteRegister(currentRoute) {
// We should only need to send a safe set of fields on the route
// object.
// This is not to hide what's inside the route object, but to show
// these are the public APIs
const routePublicApi = _.pick(currentRoute, 'name', 'pathDef', 'path');
const omittingOptionFields = [
'triggersEnter', 'triggersExit', 'name', 'action'
];
routePublicApi.options = _.omit(currentRoute.options, omittingOptionFields);
this._onRouteCallbacks.forEach((cb) => {
cb(routePublicApi);
});
}
_getCurrentRouteContext() {
throw new Error('Not implemented');
}
_init() {
throw new Error('Not implemented');
}
withTrailingSlash(fn) {
return this.env.trailingSlash.withValue(true, fn);
}
};