4 is: 'more-route-selector',
7 MoreRouting.ContextAware,
13 * The attribute to read route expressions from (on children).
21 * The routes managed by this element (inferred from the items that are
22 * defined by the selector it targets).
31 * The selected `MoreRouting.Route` object, or `null`.
33 * @type {MoreRouting.Route}
43 * The index of the selected route (relative to `routes`). -1 when there
54 * The _full_ path expression of the selected route, or `null`.
63 * The params of the selected route, or an empty object if no route.
74 * @event more-route-selected fires when a new route is selected.
76 * newRoute: MoreRouting.Route, oldRoute: MoreRouting.Route,
77 * newIndex: number, oldIndex: number,
78 * newPath: ?string, oldPath: ?string,
79 * newParams: Object, oldParams: Object,
83 attached: function() {
84 this._managedSelector = this._findTargetSelector();
85 if (!this._managedSelector) {
86 console.warn(this, 'was built without a selector to manage. It will do nothing.');
90 this._managedSelector.addEventListener(
91 'selected-item-changed', this._onSelectedItemChanged.bind(this));
96 * Handle a change in selected item, driven by the targeted selector.
98 * Note that this will fail if a route is chosen that requires params not
99 * defined by the current URL.
101 _onSelectedItemChanged: function(event) {
102 if (this._settingSelection) return;
103 var route = this._routeForItem(event.detail.value);
108 _updateRoutes: function() {
110 if (this._managedSelector) {
111 routes = this._managedSelector.items.map(this._routeForItem.bind(this));
113 this._setRoutes(routes);
116 _onMoreRouteChange: function(event) {
117 if (!this._managedSelector) return;
121 var index = this.routes.indexOf(event.detail.newRoute);
122 var attrForSelected = this._managedSelector.attrForSelected;
123 if (!attrForSelected) {
126 var item = this._managedSelector.items[index];
128 selected = item[attrForSelected] || item.getAttribute(attrForSelected);
131 // Make sure that we don't turn around and re-navigate
132 this._settingSelection = true;
133 this._managedSelector.select(selected);
134 this._settingSelection = false;
137 _findTargetSelector: function() {
138 var children = Polymer.dom(this).children;
139 if (children.length !== 1) {
140 console.error(this, 'expects only a single selector child');
144 var child = children[0];
145 if ('selected' in child && 'items' in child) {
148 console.error(this, 'can only manage children that are selectors');
153 _routeForItem: function(item) {
154 if (!item) return null;
155 if (item.moreRouteContext && item.moreRouteContext instanceof MoreRouting.Route) {
156 return item.moreRouteContext;
159 if (!item.hasAttribute(this.routeAttribute)) {
160 console.warn(item, 'is missing a context route or "' + this.routeAttribute + '" attribute');
163 var expression = item.getAttribute(this.routeAttribute);
164 var route = MoreRouting.getRoute(expression, this.parentRoute);
165 // Associate the route w/ its element while we're here.
166 item.moreRouteContext = route;