| OLD | NEW |
| (Empty) |
| 1 | |
| 2 | |
| 3 Polymer({ | |
| 4 | |
| 5 is: 'more-route', | |
| 6 | |
| 7 behaviors: [ | |
| 8 MoreRouting.ContextAware, | |
| 9 ], | |
| 10 | |
| 11 properties: { | |
| 12 | |
| 13 /** | |
| 14 * The name of this route. Behavior differs based on the presence of | |
| 15 * `path` during _declaration_. | |
| 16 * | |
| 17 * If `path` is present during declaration, it is registered via `name`. | |
| 18 * | |
| 19 * Otherwise, this `more-route` becomes a `reference` to the route with | |
| 20 * `name`. Changing `name` will update which route is referenced. | |
| 21 */ | |
| 22 name: { | |
| 23 type: String, | |
| 24 observer: '_nameChanged', | |
| 25 }, | |
| 26 | |
| 27 /** | |
| 28 * A path expression used to parse parameters from the window's URL. | |
| 29 */ | |
| 30 path: { | |
| 31 type: String, | |
| 32 obserer: '_pathChanged', | |
| 33 }, | |
| 34 | |
| 35 /** | |
| 36 * Whether this route should become a context for the element that | |
| 37 * contains it. | |
| 38 */ | |
| 39 context: { | |
| 40 type: Boolean, | |
| 41 }, | |
| 42 | |
| 43 /** | |
| 44 * The underlying `MoreRouting.Route` object that is being wrapped. | |
| 45 * | |
| 46 * @type {MoreRouting.Route} | |
| 47 */ | |
| 48 route: { | |
| 49 type: Object, | |
| 50 readOnly: true, | |
| 51 notify: true, | |
| 52 observer: '_routeChanged', | |
| 53 }, | |
| 54 | |
| 55 /** | |
| 56 * The full path expression for this route, including routes this is | |
| 57 * nested within. | |
| 58 */ | |
| 59 fullPath: { | |
| 60 type: String, | |
| 61 readOnly: true, | |
| 62 notify: true, | |
| 63 }, | |
| 64 | |
| 65 /** | |
| 66 * Param values matching the current URL, or an empty object if not | |
| 67 * `active`. | |
| 68 */ | |
| 69 params: { | |
| 70 type: Object, | |
| 71 // readOnly: true, | |
| 72 notify: true, | |
| 73 }, | |
| 74 | |
| 75 /** | |
| 76 * Whether the route matches the current URL. | |
| 77 */ | |
| 78 active: { | |
| 79 type: Boolean, | |
| 80 readOnly: true, | |
| 81 notify: true, | |
| 82 }, | |
| 83 | |
| 84 }, | |
| 85 | |
| 86 routingReady: function() { | |
| 87 this._identityChanged(); | |
| 88 }, | |
| 89 | |
| 90 _nameChanged: function(newName, oldName) { | |
| 91 if (oldName) { | |
| 92 console.error('Changing the `name` property is not supported for', this)
; | |
| 93 return; | |
| 94 } | |
| 95 this._identityChanged(); | |
| 96 }, | |
| 97 | |
| 98 _pathChanged: function(newPath, oldPath) { | |
| 99 if (oldPath) { | |
| 100 console.error('Changing the `path` property is not supported for', this)
; | |
| 101 return; | |
| 102 } | |
| 103 this._identityChanged(); | |
| 104 }, | |
| 105 | |
| 106 _identityChanged: function() { | |
| 107 if (!this.routingIsReady) return; | |
| 108 | |
| 109 if (this.name && this.path) { | |
| 110 this._setRoute(MoreRouting.registerNamedRoute(this.name, this.path, this
.parentRoute)); | |
| 111 } else if (this.name) { | |
| 112 this._setRoute(MoreRouting.getRouteByName(this.name)); | |
| 113 } else if (this.path) { | |
| 114 this._setRoute(MoreRouting.getRouteByPath(this.path, this.parentRoute)); | |
| 115 } else { | |
| 116 this._setRoute(null); | |
| 117 } | |
| 118 }, | |
| 119 | |
| 120 _routeChanged: function() { | |
| 121 this._observeRoute(); | |
| 122 this._setFullPath(this.route.fullPath); | |
| 123 // this._setParams(this.route.params); | |
| 124 this.params = this.route.params; | |
| 125 this._setActive(this.route.active); | |
| 126 | |
| 127 // @see MoreRouting.ContextAware | |
| 128 this.moreRouteContext = this.route; | |
| 129 | |
| 130 if (this.context) { | |
| 131 var parent = Polymer.dom(this).parentNode; | |
| 132 if (parent.nodeType !== Node.ELEMENT_NODE) { | |
| 133 parent = parent.host; | |
| 134 } | |
| 135 | |
| 136 if (parent.nodeType === Node.ELEMENT_NODE) { | |
| 137 parent.moreRouteContext = this.route; | |
| 138 } else { | |
| 139 console.warn('Unable to determine parent element for', this, '- not se
tting a context'); | |
| 140 } | |
| 141 } | |
| 142 }, | |
| 143 | |
| 144 _observeRoute: function() { | |
| 145 // TODO(nevir) https://github.com/Polymore/more-routing/issues/24 | |
| 146 if (this._routeListener) { | |
| 147 this._routeListener.close(); | |
| 148 this._routeListener = null; | |
| 149 } | |
| 150 if (this._paramListener) { | |
| 151 this._paramListener.close(); | |
| 152 this._paramListener = null; | |
| 153 } | |
| 154 if (!this.route) return; | |
| 155 | |
| 156 this._routeListener = this.route.__subscribe(function() { | |
| 157 this._setActive(this.route && this.route.active); | |
| 158 }.bind(this)); | |
| 159 | |
| 160 this._paramListener = this.route.params.__subscribe(function(key, value) { | |
| 161 // https://github.com/Polymer/polymer/issues/1505 | |
| 162 this.notifyPath('params.' + key, value); | |
| 163 }.bind(this)); | |
| 164 }, | |
| 165 | |
| 166 urlFor: function(params) { | |
| 167 return this.route.urlFor(params); | |
| 168 }, | |
| 169 | |
| 170 navigateTo: function(params) { | |
| 171 return this.route.navigateTo(params); | |
| 172 }, | |
| 173 | |
| 174 isCurrentUrl: function(params) { | |
| 175 return this.route.isCurrentUrl(params); | |
| 176 }, | |
| 177 | |
| 178 }); | |
| 179 | |
| OLD | NEW |