forked from googlearchive/more-routing
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmore-route.html
193 lines (162 loc) · 5.05 KB
/
more-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
<!--
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="routing.html">
<link rel="import" href="more-route-context-aware.html">
<script>
Polymer({
is: 'more-route',
behaviors: [
MoreRouting.ContextAware,
],
properties: {
/**
* The name of this route. Behavior differs based on the presence of
* `path` during _declaration_.
*
* If `path` is present during declaration, it is registered via `name`.
*
* Otherwise, this `more-route` becomes a `reference` to the route with
* `name`. Changing `name` will update which route is referenced.
*/
name: {
type: String,
observer: '_nameChanged',
},
/**
* A path expression used to parse parameters from the window's URL.
*/
path: {
type: String,
obserer: '_pathChanged',
},
/**
* Whether this route should become a context for the element that
* contains it.
*/
context: {
type: Boolean,
},
/**
* The underlying `MoreRouting.Route` object that is being wrapped.
*
* @type {MoreRouting.Route}
*/
route: {
type: Object,
readOnly: true,
notify: true,
observer: '_routeChanged',
},
/**
* The full path expression for this route, including routes this is
* nested within.
*/
fullPath: {
type: String,
readOnly: true,
notify: true,
},
/**
* Param values matching the current URL, or an empty object if not
* `active`.
*/
params: {
type: Object,
// readOnly: true,
notify: true,
},
/**
* Whether the route matches the current URL.
*/
active: {
type: Boolean,
readOnly: true,
notify: true,
},
},
routingReady: function() {
this._identityChanged();
},
_nameChanged: function(newName, oldName) {
if (oldName) {
console.error('Changing the `name` property is not supported for', this);
return;
}
this._identityChanged();
},
_pathChanged: function(newPath, oldPath) {
if (oldPath) {
console.error('Changing the `path` property is not supported for', this);
return;
}
this._identityChanged();
},
_identityChanged: function() {
if (!this.routingIsReady) return;
if (this.name && this.path) {
this._setRoute(MoreRouting.registerNamedRoute(this.name, this.path, this.parentRoute));
} else if (this.name) {
this._setRoute(MoreRouting.getRouteByName(this.name));
} else if (this.path) {
this._setRoute(MoreRouting.getRouteByPath(this.path, this.parentRoute));
} else {
this._setRoute(null);
}
},
_routeChanged: function() {
this._observeRoute();
this._setFullPath(this.route.fullPath);
// this._setParams(this.route.params);
this.params = this.route.params;
this._setActive(this.route.active);
// @see MoreRouting.ContextAware
this.moreRouteContext = this.route;
if (this.context) {
var parent = Polymer.dom(this).parentNode;
if (parent.nodeType !== Node.ELEMENT_NODE) {
parent = parent.host;
}
if (parent.nodeType === Node.ELEMENT_NODE) {
parent.moreRouteContext = this.route;
} else {
console.warn('Unable to determine parent element for', this, '- not setting a context');
}
}
},
_observeRoute: function() {
// TODO(nevir) https://github.com/Polymore/more-routing/issues/24
if (this._routeListener) {
this._routeListener.close();
this._routeListener = null;
}
if (this._paramListener) {
this._paramListener.close();
this._paramListener = null;
}
if (!this.route) return;
this._routeListener = this.route.__subscribe(function() {
this._setActive(this.route && this.route.active);
}.bind(this));
this._paramListener = this.route.params.__subscribe(function(key, value) {
// https://github.com/Polymer/polymer/issues/1505
this.notifyPath('params.' + key, value);
}.bind(this));
},
urlFor: function(params) {
return this.route.urlFor(params);
},
navigateTo: function(params) {
return this.route.navigateTo(params);
},
isCurrentUrl: function(params) {
return this.route.isCurrentUrl(params);
},
});
</script>