forked from googlearchive/more-routing
-
Notifications
You must be signed in to change notification settings - Fork 1
/
route.html
226 lines (196 loc) · 6.58 KB
/
route.html
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
<!--
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<link rel="import" href="emitter.html">
<link rel="import" href="params.html">
<script>
(function(scope) {
var MoreRouting = scope.MoreRouting = scope.MoreRouting || {};
MoreRouting.Route = Route;
// Note that this can differ from the part separator defined by the driver. The
// driver's separator is used when parsing/generating URLs given to the client,
// whereas this one is for route definitions.
var PART_SEPARATOR = '/';
var PARAM_SENTINEL = ':';
var SEPARATOR_CLEANER = /\/\/+/g;
/**
* TODO(nevir): Docs.
*/
function Route(path, parent) {
// For `MoreRouting.Emitter`; Emits changes for `active`.
this.__listeners = [];
this.path = path;
this.parent = parent;
this.fullPath = path;
this.compiled = this._compile(this.path);
this.active = false;
this.driver = null;
var params = MoreRouting.Params(namedParams(this.compiled), this.parent && this.parent.params);
params.__subscribe(this._navigateToParams.bind(this));
Object.defineProperty(this, 'params', {
get: function() { return params; },
set: function() { throw new Error('Route#params cannot be overwritten'); },
});
this.parts = [];
this.children = [];
// Param values matching the current URL, or an empty object if not `active`.
//
// To make data "binding" easy, `Route` guarantees that `params` will always
// be the same object; just make a reference to it.
if (this.parent) {
this.parent.children.push(this);
this.fullPath = this.parent.fullPath + this.fullPath;
this.depth = this.parent.depth + this.compiled.length;
this.numParams = this.parent.numParams + countParams(this.compiled);
} else {
this.depth = this.compiled.length;
this.numParams = countParams(this.compiled);
}
}
Route.prototype = Object.create(MoreRouting.Emitter);
Object.defineProperty(Route.prototype, 'active', {
get: function() {
return this._active;
},
set: function(value) {
if (value !== this._active);
this._active = value;
this.__notify('active', value);
},
});
Route.isPath = function isPath(pathOrName) {
return pathOrName.indexOf(PART_SEPARATOR) === 0;
};
Route.joinPath = function joinPath(paths) {
var joined = Array.prototype.join.call(arguments, PART_SEPARATOR);
joined = joined.replace(SEPARATOR_CLEANER, PART_SEPARATOR);
var minLength = joined.length - PART_SEPARATOR.length;
if (joined.substr(minLength) === PART_SEPARATOR) {
joined = joined.substr(0, minLength);
}
return joined;
};
Route.prototype.urlFor = function urlFor(params) {
return this.driver.urlForParts(this.partsForParams(params));
};
Route.prototype.navigateTo = function navigateTo(params) {
return this.driver.navigateToParts(this.partsForParams(params));
}
Route.prototype.isCurrentUrl = function isCurrentUrl(params) {
if (!this.active) return false;
var currentKeys = Object.keys(this.params);
for (var i = 0, key; key = currentKeys[i]; i++) {
if (this.params[key] !== String(params[key])) {
return false;
}
}
return true;
};
// Driver Interface
Route.prototype.partsForParams = function partsForParams(params, silent) {
var parts = this.parent && this.parent.partsForParams(params, silent) || [];
for (var i = 0, config; config = this.compiled[i]; i++) {
if (config.type === 'static') {
parts.push(config.part);
} else if (config.type === 'param') {
var value
if (params && config.name in params) {
value = params[config.name];
} else {
value = this.params[config.name];
}
if (value === undefined) {
if (silent) {
return null;
} else {
throw new Error('Missing param "' + config.name + '" for route ' + this);
}
}
parts.push(value);
}
}
return parts;
};
/**
* Called by the driver whenever it has detected a change to the URL.
*
* @param {Array.<String>|null} parts The parts of the URL, or null if the
* route should be disabled.
*/
Route.prototype.processPathParts = function processPathParts(parts) {
this.parts = parts;
this.active = this.matchesPathParts(parts);
// We don't want to notify of these changes; they'd be no-op noise.
this.params.__silent = true;
if (this.active) {
var keys = Object.keys(this.params);
for (var i = 0; i < keys.length; i++) {
delete this.params[keys[i]];
}
for (var i = 0, config; config = this.compiled[i]; i++) {
if (config.type === 'param') {
this.params[config.name] = parts[i];
}
}
} else {
for (key in this.params) {
this.params[key] = undefined;
}
}
delete this.params.__silent;
};
Route.prototype.matchesPathParts = function matchesPathParts(parts) {
if (!parts) return false;
if (parts.length < this.compiled.length) return false;
for (var i = 0, config; config = this.compiled[i]; i++) {
if (config.type === 'static' && parts[i] !== config.part) {
return false;
}
}
return true;
};
Route.prototype.toString = function toString() {
return this.path;
};
// Internal Implementation
Route.prototype._compile = function _compile(rawPath) {
// Not strictly required, but helps us stay consistent w/ `getRoute`, etc.
if (rawPath.indexOf(PART_SEPARATOR) !== 0) {
throw new Error('Route paths must begin with a path separator; got: "' + rawPath + '"');
}
var path = rawPath.substr(PART_SEPARATOR.length);
if (path === '') return [];
return path.split(PART_SEPARATOR).map(function(part) {
// raw fragment.
if (part.substr(0, 1) == PARAM_SENTINEL) {
return {type: 'param', name: part.substr(1)};
} else {
return {type: 'static', part: part};
}
});
};
Route.prototype._navigateToParams = function _navigateToParams() {
var parts = this.partsForParams(this.params, true);
if (!parts) return;
this.driver.navigateToParts(parts);
};
function countParams(compiled) {
return compiled.reduce(function(count, part) {
return count + (part.type === 'param' ? 1 : 0);
}, 0);
}
function namedParams(compiled) {
var result = [];
compiled.forEach(function(part) {
if (part.type === 'static') return;
result.push(part.name);
});
return result;
}
})(window);
</script>