forked from googlearchive/more-routing
-
Notifications
You must be signed in to change notification settings - Fork 1
/
more-route-selector.html
207 lines (179 loc) · 5.44 KB
/
more-route-selector.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
<!--
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="more-route-selection.html">
<!--
TODO(nevir): Document.
TODO(nevir): Support child addition/removal/reorder.
-->
<dom-module id="more-route-selector">
<template>
<more-route-selection
id="selection"
routes="[[routes]]"
selected-route="{{selectedRoute}}"
selected-index="{{selectedIndex}}"
selected-path="{{selectedPath}}"
selected-params="{{selectedParams}}"
on-more-route-change="_onMoreRouteChange">
</more-route-selection>
<content></content>
</template>
</dom-module>
<script>
Polymer({
is: 'more-route-selector',
behaviors: [
MoreRouting.ContextAware,
],
properties: {
/**
* The attribute to read route expressions from (on children).
*/
routeAttribute: {
type: String,
value: 'route',
},
/**
* The routes managed by this element (inferred from the items that are
* defined by the selector it targets).
*/
routes: {
type: Array,
readOnly: true,
notify: true,
},
/**
* 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-selected fires when a new route is selected.
* @param {{
* newRoute: MoreRouting.Route, oldRoute: MoreRouting.Route,
* newIndex: number, oldIndex: number,
* newPath: ?string, oldPath: ?string,
* newParams: Object, oldParams: Object,
* }}
*/
attached: function() {
this._managedSelector = this._findTargetSelector();
if (!this._managedSelector) {
console.warn(this, 'was built without a selector to manage. It will do nothing.');
return;
}
this._managedSelector.addEventListener(
'selected-item-changed', this._onSelectedItemChanged.bind(this));
this._updateRoutes();
},
/**
* Handle a change in selected item, driven by the targeted selector.
*
* Note that this will fail if a route is chosen that requires params not
* defined by the current URL.
*/
_onSelectedItemChanged: function(event) {
if (this._settingSelection) return;
var route = this._routeForItem(event.detail.value);
if (!route) return;
route.navigateTo();
},
_updateRoutes: function() {
var routes = [];
var _this = this;
setTimeout(function() {
if (_this._managedSelector) {
routes = _this._managedSelector.items.map(_this._routeForItem.bind(_this));
}
_this._setRoutes(routes);
}, 100);
},
_onMoreRouteChange: function(event) {
if (!this._managedSelector) return;
var selected = '';
var index = this.routes.indexOf(event.detail.newRoute);
var attrForSelected = this._managedSelector.attrForSelected;
if (!attrForSelected) {
selected = index;
} else {
var item = this._managedSelector.items[index];
if (item)
selected = item[attrForSelected] || item.getAttribute(attrForSelected);
}
// Make sure that we don't turn around and re-navigate
this._settingSelection = true;
this._managedSelector.select(selected);
this._settingSelection = false;
},
_findTargetSelector: function() {
var children = Polymer.dom(this).children;
if (children.length !== 1) {
console.error(this, 'expects only a single selector child');
return null;
}
var child = children[0];
if ('selected' in child && 'items' in child) {
return child;
} else {
console.error(this, 'can only manage children that are selectors');
return null;
}
},
_routeForItem: function(item) {
if (!item) return null;
if (item.moreRouteContext && item.moreRouteContext instanceof MoreRouting.Route) {
return item.moreRouteContext;
}
if (!item.hasAttribute(this.routeAttribute)) {
console.warn(item, 'is missing a context route or "' + this.routeAttribute + '" attribute');
return null;
}
var expression = item.getAttribute(this.routeAttribute);
var route = MoreRouting.getRoute(expression, this.parentRoute);
// Associate the route w/ its element while we're here.
item.moreRouteContext = route;
return route;
},
});
</script>