forked from googlearchive/more-routing
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmore-route-selection.html
197 lines (171 loc) · 5.4 KB
/
more-route-selection.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
<!--
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="../polymer/polymer.html">
<link rel="import" href="more-route-context-aware.html">
<link rel="import" href="route.html">
<!--
TODO(nevir): Document.
-->
<script>
Polymer({
is: 'more-route-selection',
behaviors: [
MoreRouting.ContextAware,
],
properties: {
/**
* Routes to select from, as either a path expression or route name.
*
* You can either specify routes via this attribute, or as child nodes
* to this element, but not both.
*
* @type {String|Array<string|MoreRouting.Route>}
*/
routes: {
type: String,
observer: '_routesChanged',
},
/**
* The selected `MoreRouting.Route` object, or `null`.
*
* @type {MoreRouting.Route}
*/
selectedRoute: {
type: Object,
value: null,
readOnly: true,
notify: true,
},
/**
* The index of the selected route (relative to `routes`). -1 when there
* is no active route.
*/
selectedIndex: {
type: Number,
value: -1,
readOnly: true,
notify: true,
},
/**
* The _full_ path expression of the selected route, or `null`.
*/
selectedPath: {
type: String,
readOnly: true,
notify: true,
},
/**
* The params of the selected route, or an empty object if no route.
*/
selectedParams: {
type: Object,
readOnly: true,
notify: true,
},
},
/**
* @event more-route-change fires when a new route is selected.
* @detail {{
* newRoute: MoreRouting.Route, oldRoute: MoreRouting.Route,
* newIndex: number, oldIndex: number,
* newPath: ?string, oldPath: ?string,
* newParams: Object, oldParams: Object,
* }}
*/
routingReady: function() {
this._routesChanged();
},
_routesChanged: function() {
if (!this.routingIsReady) return;
var routes = this.routes || [];
if (typeof routes === 'string') {
routes = routes.split(/\s+/);
}
this._routeInfo = this._sortIndexes(routes.map(function(route, index) {
return {
model: MoreRouting.getRoute(route, this.parentRoute),
index: index,
};
}.bind(this)));
this._observeRoutes();
this._evaluate();
},
/**
* Tracks changes to the routes.
*/
_observeRoutes: function() {
if (this._routeListeners) {
for (var i = 0, listener; listener = this._routeListeners[i]; i++) {
listener.close();
}
}
this._routeListeners = this._routeInfo.map(function(routeInfo) {
return routeInfo.model.__subscribe(this._evaluate.bind(this));
}.bind(this));
},
_evaluate: function() {
var newIndex = -1;
var newRoute = null;
var oldIndex = this.selectedIndex;
for (var i = 0, routeInfo; routeInfo = this._routeInfo[i]; i++) {
if (routeInfo.model && routeInfo.model.active) {
newIndex = routeInfo.index;
newRoute = routeInfo.model;
break;
}
}
if (newIndex === oldIndex) return;
var oldRoute = this.selectedRoute;
var oldPath = this.selectedPath;
var oldParams = this.selectedParams;
var newPath = newRoute ? newRoute.fullPath : null;
var newParams = newRoute ? newRoute.params : {};
this._setSelectedRoute(newRoute);
this._setSelectedIndex(newIndex);
this._setSelectedPath(newPath);
this._setSelectedParams(newParams);
this.fire('more-route-change', {
newRoute: newRoute, oldRoute: oldRoute,
newIndex: newIndex, oldIndex: oldIndex,
newPath: newPath, oldPath: oldPath,
newParams: newParams, oldParams: oldParams,
});
},
/**
* We want the most specific routes to match first, so we must create a
* mapping of indexes within `routes` that map
*/
_sortIndexes: function(routeInfo) {
return routeInfo.sort(function(a, b) {
if (!a.model) {
return 1;
} else if (!b.model) {
return -1;
// Routes with more path parts are most definitely more specific.
} else if (a.model.depth < b.model.depth) {
return 1;
} if (a.model.depth > b.model.depth) {
return -1;
} else {
// Also, routes with fewer params are more specific. For example
// `/users/foo` is more specific than `/users/:id`.
if (a.model.numParams < b.model.numParams) {
return -1;
} else if (a.model.numParams > b.model.numParams) {
return 1;
} else {
// Equally specific; we fall back to the default (and hopefully
// stable) sort order.
return 0;
}
}
});
},
});
</script>