| OLD | NEW |
| 1 'use strict'; | 1 'use strict'; |
| 2 | 2 |
| 3 Polymer({ | 3 Polymer({ |
| 4 is: 'carbon-route', | 4 is: 'app-route', |
| 5 | 5 |
| 6 properties: { | 6 properties: { |
| 7 /** | 7 /** |
| 8 * The URL component managed by this element. | 8 * The URL component managed by this element. |
| 9 */ | 9 */ |
| 10 route: { | 10 route: { |
| 11 type: Object, | 11 type: Object, |
| 12 notify: true | 12 notify: true |
| 13 }, | 13 }, |
| 14 | 14 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 value: function() {return {path: null, prefix: null, __queryParams: null
};}, | 53 value: function() {return {path: null, prefix: null, __queryParams: null
};}, |
| 54 notify: true | 54 notify: true |
| 55 }, | 55 }, |
| 56 | 56 |
| 57 active: { | 57 active: { |
| 58 type: Boolean, | 58 type: Boolean, |
| 59 notify: true, | 59 notify: true, |
| 60 readOnly: true | 60 readOnly: true |
| 61 }, | 61 }, |
| 62 | 62 |
| 63 _skipMatch: { | 63 _queryParamsUpdating: { |
| 64 type: Boolean, | 64 type: Boolean, |
| 65 value: false | 65 value: false |
| 66 }, | 66 }, |
| 67 /** | 67 /** |
| 68 * @type {?string} | 68 * @type {?string} |
| 69 */ | 69 */ |
| 70 _matched: { | 70 _matched: { |
| 71 type: String, | 71 type: String, |
| 72 value: '' | 72 value: '' |
| 73 } | 73 } |
| 74 }, | 74 }, |
| 75 | 75 |
| 76 observers: [ | 76 observers: [ |
| 77 '__tryToMatch(route.path, pattern)', | 77 '__tryToMatch(route.path, pattern)', |
| 78 '__updatePathOnDataChange(data.*)', | 78 '__updatePathOnDataChange(data.*)', |
| 79 '__tailPathChanged(tail.path)', | 79 '__tailPathChanged(tail.path)', |
| 80 '__routeQueryParamsChanged(route.__queryParams)', | 80 '__routeQueryParamsChanged(route.__queryParams)', |
| 81 '__tailQueryParamsChanged(tail.__queryParams)', | 81 '__tailQueryParamsChanged(tail.__queryParams)', |
| 82 '__queryParamsChanged(queryParams.*)' | 82 '__queryParamsChanged(queryParams.*)' |
| 83 ], | 83 ], |
| 84 | 84 |
| 85 created: function() { | 85 created: function() { |
| 86 this.linkPaths('route.__queryParams', 'tail.__queryParams'); | 86 this.linkPaths('route.__queryParams', 'tail.__queryParams'); |
| 87 this.linkPaths('tail.__queryParams', 'route.__queryParams'); | 87 this.linkPaths('tail.__queryParams', 'route.__queryParams'); |
| 88 }, | 88 }, |
| 89 | 89 |
| 90 // IE Object.assign polyfill | 90 // IE Object.assign polyfill |
| 91 __assign: function(target, source) { | 91 __assign: function(target, source) { |
| 92 if (Object.assign) { |
| 93 return Object.assign(target, source); |
| 94 } |
| 92 if (source != null) { | 95 if (source != null) { |
| 93 for (var key in source) { | 96 for (var key in source) { |
| 94 target[key] = source[key]; | 97 target[key] = source[key]; |
| 95 } | 98 } |
| 96 } | 99 } |
| 97 | 100 |
| 98 return target; | 101 return target; |
| 99 }, | 102 }, |
| 100 | 103 |
| 101 // Deal with the query params object being assigned to wholesale | 104 /** |
| 105 * Deal with the query params object being assigned to wholesale. |
| 106 * @export |
| 107 */ |
| 102 __routeQueryParamsChanged: function(queryParams) { | 108 __routeQueryParamsChanged: function(queryParams) { |
| 103 if (queryParams && this.tail) { | 109 if (queryParams && this.tail) { |
| 104 this.set('tail.__queryParams', queryParams); | 110 this.set('tail.__queryParams', queryParams); |
| 105 | 111 |
| 106 if (!this.active || this._skipMatch) { | 112 if (!this.active || this._queryParamsUpdating) { |
| 107 return; | 113 return; |
| 108 } | 114 } |
| 109 | 115 |
| 110 this._skipMatch = true; | 116 this._queryParamsUpdating = true; |
| 111 | 117 this.set('queryParams', this.__assign({}, queryParams)); |
| 112 var qp; | 118 this._queryParamsUpdating = false; |
| 113 | |
| 114 if (Object.assign) { | |
| 115 qp = Object.assign({}, queryParams); | |
| 116 } else { | |
| 117 qp = this.__assign({}, queryParams); | |
| 118 } | |
| 119 | |
| 120 this.set('queryParams', qp); | |
| 121 this._skipMatch = false; | |
| 122 } | 119 } |
| 123 }, | 120 }, |
| 124 | 121 |
| 122 /** |
| 123 * @export |
| 124 */ |
| 125 __tailQueryParamsChanged: function(queryParams) { | 125 __tailQueryParamsChanged: function(queryParams) { |
| 126 if (queryParams && this.route) { | 126 if (queryParams && this.route) { |
| 127 this.set('route.__queryParams', queryParams); | 127 this.set('route.__queryParams', queryParams); |
| 128 } | 128 } |
| 129 }, | 129 }, |
| 130 | 130 |
| 131 /** |
| 132 * @export |
| 133 */ |
| 131 __queryParamsChanged: function(changes) { | 134 __queryParamsChanged: function(changes) { |
| 132 if (!this.active || this._skipMatch) { | 135 if (!this.active || this._queryParamsUpdating) { |
| 133 return; | 136 return; |
| 134 } | 137 } |
| 135 | 138 |
| 136 this.set('route.__' + changes.path, changes.value); | 139 this.set('route.__' + changes.path, changes.value); |
| 137 }, | 140 }, |
| 138 | 141 |
| 139 __resetProperties: function() { | 142 __resetProperties: function() { |
| 140 this._setActive(false); | 143 this._setActive(false); |
| 141 this._matched = null; | 144 this._matched = null; |
| 142 //this.tail = { path: null, prefix: null, queryParams: null }; | 145 //this.tail = { path: null, prefix: null, queryParams: null }; |
| 143 //this.data = {}; | 146 //this.data = {}; |
| 144 }, | 147 }, |
| 145 | 148 |
| 146 __tryToMatch: function(path, pattern) { | 149 /** |
| 147 if (this._skipMatch || !pattern) { | 150 * @export |
| 151 */ |
| 152 __tryToMatch: function() { |
| 153 if (!this.route) { |
| 154 return; |
| 155 } |
| 156 var path = this.route.path; |
| 157 var pattern = this.pattern; |
| 158 if (!pattern) { |
| 148 return; | 159 return; |
| 149 } | 160 } |
| 150 | 161 |
| 151 if (!path) { | 162 if (!path) { |
| 152 this.__resetProperties(); | 163 this.__resetProperties(); |
| 153 return; | 164 return; |
| 154 } | 165 } |
| 155 | 166 |
| 156 var remainingPieces = path.split('/'); | 167 var remainingPieces = path.split('/'); |
| 157 var patternPieces = pattern.split('/'); | 168 var patternPieces = pattern.split('/'); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 174 matched.push(pathPiece); | 185 matched.push(pathPiece); |
| 175 | 186 |
| 176 if (patternPiece.charAt(0) == ':') { | 187 if (patternPiece.charAt(0) == ':') { |
| 177 namedMatches[patternPiece.slice(1)] = pathPiece; | 188 namedMatches[patternPiece.slice(1)] = pathPiece; |
| 178 } else if (patternPiece !== pathPiece) { | 189 } else if (patternPiece !== pathPiece) { |
| 179 this.__resetProperties(); | 190 this.__resetProperties(); |
| 180 return; | 191 return; |
| 181 } | 192 } |
| 182 } | 193 } |
| 183 | 194 |
| 184 matched = matched.join('/'); | 195 this._matched = matched.join('/'); |
| 196 |
| 197 // Properties that must be updated atomically. |
| 198 var propertyUpdates = {}; |
| 199 |
| 200 //this.active |
| 201 if (!this.active) { |
| 202 propertyUpdates.active = true; |
| 203 } |
| 204 |
| 205 // this.tail |
| 206 var tailPrefix = this.route.prefix + this._matched; |
| 185 var tailPath = remainingPieces.join('/'); | 207 var tailPath = remainingPieces.join('/'); |
| 186 if (remainingPieces.length > 0) { | 208 if (remainingPieces.length > 0) { |
| 187 tailPath = '/' + tailPath; | 209 tailPath = '/' + tailPath; |
| 188 } | 210 } |
| 189 | 211 if (!this.tail || |
| 190 this._skipMatch = true; | 212 this.tail.prefix !== tailPrefix || |
| 191 this._matched = matched; | 213 this.tail.path !== tailPath) { |
| 192 this.data = namedMatches; | 214 propertyUpdates.tail = { |
| 193 var tailPrefix = this.route.prefix + matched; | |
| 194 | |
| 195 if (!this.tail || this.tail.prefix !== tailPrefix || this.tail.path !== ta
ilPath) { | |
| 196 this.tail = { | |
| 197 prefix: tailPrefix, | 215 prefix: tailPrefix, |
| 198 path: tailPath, | 216 path: tailPath, |
| 199 __queryParams: this.route.__queryParams | 217 __queryParams: this.route.__queryParams |
| 200 }; | 218 }; |
| 201 } | 219 } |
| 202 this._setActive(true); | 220 |
| 203 this._skipMatch = false; | 221 // this.data |
| 222 propertyUpdates.data = namedMatches; |
| 223 this._dataInUrl = {}; |
| 224 for (var key in namedMatches) { |
| 225 this._dataInUrl[key] = namedMatches[key]; |
| 226 } |
| 227 |
| 228 this.__setMulti(propertyUpdates); |
| 204 }, | 229 }, |
| 205 | 230 |
| 206 __tailPathChanged: function(path) { | 231 /** |
| 207 if (!this.active || this._skipMatch) { | 232 * @export |
| 233 */ |
| 234 __tailPathChanged: function() { |
| 235 if (!this.active) { |
| 208 return; | 236 return; |
| 209 } | 237 } |
| 238 var tailPath = this.tail.path; |
| 210 var newPath = this._matched; | 239 var newPath = this._matched; |
| 211 if (path) { | 240 if (tailPath) { |
| 212 if (path.charAt(0) !== '/') { | 241 if (tailPath.charAt(0) !== '/') { |
| 213 path = '/' + path; | 242 tailPath = '/' + tailPath; |
| 214 } | 243 } |
| 215 newPath += path; | 244 newPath += tailPath; |
| 216 } | 245 } |
| 217 this.set('route.path', newPath); | 246 this.set('route.path', newPath); |
| 218 }, | 247 }, |
| 219 | 248 |
| 249 /** |
| 250 * @export |
| 251 */ |
| 220 __updatePathOnDataChange: function() { | 252 __updatePathOnDataChange: function() { |
| 221 if (!this.route || this._skipMatch || !this.active) { | 253 if (!this.route || !this.active) { |
| 222 return; | 254 return; |
| 223 } | 255 } |
| 224 this._skipMatch = true; | 256 var newPath = this.__getLink({}); |
| 225 this.tail = {path: null, prefix: null, queryParams: null}; | 257 var oldPath = this.__getLink(this._dataInUrl); |
| 226 this.set('route.path', this.__getLink({})); | 258 if (newPath === oldPath) { |
| 227 this._skipMatch = false; | 259 return; |
| 260 } |
| 261 this.set('route.path', newPath); |
| 228 }, | 262 }, |
| 229 | 263 |
| 230 __getLink: function(overrideValues) { | 264 __getLink: function(overrideValues) { |
| 231 var values = {tail: this.tail}; | 265 var values = {tail: null}; |
| 232 for (var key in this.data) { | 266 for (var key in this.data) { |
| 233 values[key] = this.data[key]; | 267 values[key] = this.data[key]; |
| 234 } | 268 } |
| 235 for (var key in overrideValues) { | 269 for (var key in overrideValues) { |
| 236 values[key] = overrideValues[key]; | 270 values[key] = overrideValues[key]; |
| 237 } | 271 } |
| 238 var patternPieces = this.pattern.split('/'); | 272 var patternPieces = this.pattern.split('/'); |
| 239 var interp = patternPieces.map(function(value) { | 273 var interp = patternPieces.map(function(value) { |
| 240 if (value[0] == ':') { | 274 if (value[0] == ':') { |
| 241 value = values[value.slice(1)]; | 275 value = values[value.slice(1)]; |
| 242 } | 276 } |
| 243 return value; | 277 return value; |
| 244 }, this); | 278 }, this); |
| 245 if (values.tail && values.tail.path) { | 279 if (values.tail && values.tail.path) { |
| 246 interp.push(values.tail.path); | 280 if (interp.length > 0 && values.tail.path.charAt(0) === '/') { |
| 281 interp.push(values.tail.path.slice(1)); |
| 282 } else { |
| 283 interp.push(values.tail.path); |
| 284 } |
| 247 } | 285 } |
| 248 return interp.join('/'); | 286 return interp.join('/'); |
| 287 }, |
| 288 |
| 289 __setMulti: function(setObj) { |
| 290 // HACK(rictic): skirting around 1.0's lack of a setMulti by poking at |
| 291 // internal data structures. I would not advise that you copy this |
| 292 // example. |
| 293 // |
| 294 // In the future this will be a feature of Polymer itself. |
| 295 // See: https://github.com/Polymer/polymer/issues/3640 |
| 296 // |
| 297 // Hacking around with private methods like this is juggling footguns, |
| 298 // and is likely to have unexpected and unsupported rough edges. |
| 299 // |
| 300 // Be ye so warned. |
| 301 for (var property in setObj) { |
| 302 this._propertySetter(property, setObj[property]); |
| 303 } |
| 304 |
| 305 for (var property in setObj) { |
| 306 this._pathEffector(property, this[property]); |
| 307 this._notifyPathUp(property, this[property]); |
| 308 } |
| 249 } | 309 } |
| 250 }); | 310 }); |
| OLD | NEW |