| OLD | NEW |
| (Empty) |
| 1 'use strict'; | |
| 2 | |
| 3 Polymer({ | |
| 4 is: 'carbon-route', | |
| 5 | |
| 6 properties: { | |
| 7 /** | |
| 8 * The URL component managed by this element. | |
| 9 */ | |
| 10 route: { | |
| 11 type: Object, | |
| 12 notify: true | |
| 13 }, | |
| 14 | |
| 15 /** | |
| 16 * The pattern of slash-separated segments to match `path` against. | |
| 17 * | |
| 18 * For example the pattern "/foo" will match "/foo" or "/foo/bar" | |
| 19 * but not "/foobar". | |
| 20 * | |
| 21 * Path segments like `/:named` are mapped to properties on the `data` obj
ect. | |
| 22 */ | |
| 23 pattern: { | |
| 24 type: String | |
| 25 }, | |
| 26 | |
| 27 /** | |
| 28 * The parameterized values that are extracted from the route as | |
| 29 * described by `pattern`. | |
| 30 */ | |
| 31 data: { | |
| 32 type: Object, | |
| 33 value: function() {return {};}, | |
| 34 notify: true | |
| 35 }, | |
| 36 | |
| 37 /** | |
| 38 * @type {?Object} | |
| 39 */ | |
| 40 queryParams: { | |
| 41 type: Object, | |
| 42 value: function() { | |
| 43 return {}; | |
| 44 }, | |
| 45 notify: true | |
| 46 }, | |
| 47 | |
| 48 /** | |
| 49 * The part of `path` NOT consumed by `pattern`. | |
| 50 */ | |
| 51 tail: { | |
| 52 type: Object, | |
| 53 value: function() {return {path: null, prefix: null, __queryParams: null
};}, | |
| 54 notify: true | |
| 55 }, | |
| 56 | |
| 57 active: { | |
| 58 type: Boolean, | |
| 59 notify: true, | |
| 60 readOnly: true | |
| 61 }, | |
| 62 | |
| 63 _skipMatch: { | |
| 64 type: Boolean, | |
| 65 value: false | |
| 66 }, | |
| 67 /** | |
| 68 * @type {?string} | |
| 69 */ | |
| 70 _matched: { | |
| 71 type: String, | |
| 72 value: '' | |
| 73 } | |
| 74 }, | |
| 75 | |
| 76 observers: [ | |
| 77 '__tryToMatch(route.path, pattern)', | |
| 78 '__updatePathOnDataChange(data.*)', | |
| 79 '__tailPathChanged(tail.path)', | |
| 80 '__routeQueryParamsChanged(route.__queryParams)', | |
| 81 '__tailQueryParamsChanged(tail.__queryParams)', | |
| 82 '__queryParamsChanged(queryParams.*)' | |
| 83 ], | |
| 84 | |
| 85 created: function() { | |
| 86 this.linkPaths('route.__queryParams', 'tail.__queryParams'); | |
| 87 this.linkPaths('tail.__queryParams', 'route.__queryParams'); | |
| 88 }, | |
| 89 | |
| 90 // IE Object.assign polyfill | |
| 91 __assign: function(target, source) { | |
| 92 if (source != null) { | |
| 93 for (var key in source) { | |
| 94 target[key] = source[key]; | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 return target; | |
| 99 }, | |
| 100 | |
| 101 // Deal with the query params object being assigned to wholesale | |
| 102 __routeQueryParamsChanged: function(queryParams) { | |
| 103 if (queryParams && this.tail) { | |
| 104 this.set('tail.__queryParams', queryParams); | |
| 105 | |
| 106 if (!this.active || this._skipMatch) { | |
| 107 return; | |
| 108 } | |
| 109 | |
| 110 this._skipMatch = true; | |
| 111 | |
| 112 var qp; | |
| 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 } | |
| 123 }, | |
| 124 | |
| 125 __tailQueryParamsChanged: function(queryParams) { | |
| 126 if (queryParams && this.route) { | |
| 127 this.set('route.__queryParams', queryParams); | |
| 128 } | |
| 129 }, | |
| 130 | |
| 131 __queryParamsChanged: function(changes) { | |
| 132 if (!this.active || this._skipMatch) { | |
| 133 return; | |
| 134 } | |
| 135 | |
| 136 this.set('route.__' + changes.path, changes.value); | |
| 137 }, | |
| 138 | |
| 139 __resetProperties: function() { | |
| 140 this._setActive(false); | |
| 141 this._matched = null; | |
| 142 //this.tail = { path: null, prefix: null, queryParams: null }; | |
| 143 //this.data = {}; | |
| 144 }, | |
| 145 | |
| 146 __tryToMatch: function(path, pattern) { | |
| 147 if (this._skipMatch || !pattern) { | |
| 148 return; | |
| 149 } | |
| 150 | |
| 151 if (!path) { | |
| 152 this.__resetProperties(); | |
| 153 return; | |
| 154 } | |
| 155 | |
| 156 var remainingPieces = path.split('/'); | |
| 157 var patternPieces = pattern.split('/'); | |
| 158 | |
| 159 var matched = []; | |
| 160 var namedMatches = {}; | |
| 161 | |
| 162 for (var i=0; i < patternPieces.length; i++) { | |
| 163 var patternPiece = patternPieces[i]; | |
| 164 if (!patternPiece && patternPiece !== '') { | |
| 165 break; | |
| 166 } | |
| 167 var pathPiece = remainingPieces.shift(); | |
| 168 | |
| 169 // We don't match this path. | |
| 170 if (!pathPiece && pathPiece !== '') { | |
| 171 this.__resetProperties(); | |
| 172 return; | |
| 173 } | |
| 174 matched.push(pathPiece); | |
| 175 | |
| 176 if (patternPiece.charAt(0) == ':') { | |
| 177 namedMatches[patternPiece.slice(1)] = pathPiece; | |
| 178 } else if (patternPiece !== pathPiece) { | |
| 179 this.__resetProperties(); | |
| 180 return; | |
| 181 } | |
| 182 } | |
| 183 | |
| 184 matched = matched.join('/'); | |
| 185 var tailPath = remainingPieces.join('/'); | |
| 186 if (remainingPieces.length > 0) { | |
| 187 tailPath = '/' + tailPath; | |
| 188 } | |
| 189 | |
| 190 this._skipMatch = true; | |
| 191 this._matched = matched; | |
| 192 this.data = namedMatches; | |
| 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, | |
| 198 path: tailPath, | |
| 199 __queryParams: this.route.__queryParams | |
| 200 }; | |
| 201 } | |
| 202 this._setActive(true); | |
| 203 this._skipMatch = false; | |
| 204 }, | |
| 205 | |
| 206 __tailPathChanged: function(path) { | |
| 207 if (!this.active || this._skipMatch) { | |
| 208 return; | |
| 209 } | |
| 210 var newPath = this._matched; | |
| 211 if (path) { | |
| 212 if (path.charAt(0) !== '/') { | |
| 213 path = '/' + path; | |
| 214 } | |
| 215 newPath += path; | |
| 216 } | |
| 217 this.set('route.path', newPath); | |
| 218 }, | |
| 219 | |
| 220 __updatePathOnDataChange: function() { | |
| 221 if (!this.route || this._skipMatch || !this.active) { | |
| 222 return; | |
| 223 } | |
| 224 this._skipMatch = true; | |
| 225 this.tail = {path: null, prefix: null, queryParams: null}; | |
| 226 this.set('route.path', this.__getLink({})); | |
| 227 this._skipMatch = false; | |
| 228 }, | |
| 229 | |
| 230 __getLink: function(overrideValues) { | |
| 231 var values = {tail: this.tail}; | |
| 232 for (var key in this.data) { | |
| 233 values[key] = this.data[key]; | |
| 234 } | |
| 235 for (var key in overrideValues) { | |
| 236 values[key] = overrideValues[key]; | |
| 237 } | |
| 238 var patternPieces = this.pattern.split('/'); | |
| 239 var interp = patternPieces.map(function(value) { | |
| 240 if (value[0] == ':') { | |
| 241 value = values[value.slice(1)]; | |
| 242 } | |
| 243 return value; | |
| 244 }, this); | |
| 245 if (values.tail && values.tail.path) { | |
| 246 interp.push(values.tail.path); | |
| 247 } | |
| 248 return interp.join('/'); | |
| 249 } | |
| 250 }); | |
| OLD | NEW |