| OLD | NEW |
| 1 'use strict'; | 1 'use strict'; |
| 2 | 2 |
| 3 Polymer({ | 3 Polymer({ |
| 4 is: 'iron-location', | 4 is: 'iron-location', |
| 5 properties: { | 5 properties: { |
| 6 /** | 6 /** |
| 7 * The pathname component of the URL. | 7 * The pathname component of the URL. |
| 8 */ | 8 */ |
| 9 path: { | 9 path: { |
| 10 type: String, | 10 type: String, |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 }, | 88 }, |
| 89 observers: [ | 89 observers: [ |
| 90 '_updateUrl(path, query, hash)' | 90 '_updateUrl(path, query, hash)' |
| 91 ], | 91 ], |
| 92 attached: function() { | 92 attached: function() { |
| 93 this.listen(window, 'hashchange', '_hashChanged'); | 93 this.listen(window, 'hashchange', '_hashChanged'); |
| 94 this.listen(window, 'location-changed', '_urlChanged'); | 94 this.listen(window, 'location-changed', '_urlChanged'); |
| 95 this.listen(window, 'popstate', '_urlChanged'); | 95 this.listen(window, 'popstate', '_urlChanged'); |
| 96 this.listen(/** @type {!HTMLBodyElement} */(document.body), 'click', '_glo
balOnClick'); | 96 this.listen(/** @type {!HTMLBodyElement} */(document.body), 'click', '_glo
balOnClick'); |
| 97 | 97 |
| 98 this._initialized = true; |
| 98 this._urlChanged(); | 99 this._urlChanged(); |
| 99 this._initialized = true; | |
| 100 }, | 100 }, |
| 101 detached: function() { | 101 detached: function() { |
| 102 this.unlisten(window, 'hashchange', '_hashChanged'); | 102 this.unlisten(window, 'hashchange', '_hashChanged'); |
| 103 this.unlisten(window, 'location-changed', '_urlChanged'); | 103 this.unlisten(window, 'location-changed', '_urlChanged'); |
| 104 this.unlisten(window, 'popstate', '_urlChanged'); | 104 this.unlisten(window, 'popstate', '_urlChanged'); |
| 105 this.unlisten(/** @type {!HTMLBodyElement} */(document.body), 'click', '_g
lobalOnClick'); | 105 this.unlisten(/** @type {!HTMLBodyElement} */(document.body), 'click', '_g
lobalOnClick'); |
| 106 this._initialized = false; | 106 this._initialized = false; |
| 107 }, | 107 }, |
| 108 /** | 108 /** |
| 109 * @return {number} the number of milliseconds since some point in the | 109 * @return {number} the number of milliseconds since some point in the |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 var newUrl = this._getUrl(); | 150 var newUrl = this._getUrl(); |
| 151 var currentUrl = ( | 151 var currentUrl = ( |
| 152 window.location.pathname + | 152 window.location.pathname + |
| 153 window.location.search + | 153 window.location.search + |
| 154 window.location.hash | 154 window.location.hash |
| 155 ); | 155 ); |
| 156 if (newUrl == currentUrl) { | 156 if (newUrl == currentUrl) { |
| 157 // nothing to do, the URL didn't change | 157 // nothing to do, the URL didn't change |
| 158 return; | 158 return; |
| 159 } | 159 } |
| 160 // Need to use a full URL in case the containing page has a base URI. |
| 161 var fullNewUrl = new URL( |
| 162 newUrl, window.location.protocol + '//' + window.location.host).href; |
| 160 var now = this._now(); | 163 var now = this._now(); |
| 161 var shouldReplace = | 164 var shouldReplace = |
| 162 this._lastChangedAt + this.dwellTime > now; | 165 this._lastChangedAt + this.dwellTime > now; |
| 163 this._lastChangedAt = now; | 166 this._lastChangedAt = now; |
| 164 if (shouldReplace) { | 167 if (shouldReplace) { |
| 165 window.history.replaceState({}, '', newUrl); | 168 window.history.replaceState({}, '', fullNewUrl); |
| 166 } else { | 169 } else { |
| 167 window.history.pushState({}, '', newUrl); | 170 window.history.pushState({}, '', fullNewUrl); |
| 168 } | 171 } |
| 169 this.fire('location-changed', {}, {node: window}); | 172 this.fire('location-changed', {}, {node: window}); |
| 170 }, | 173 }, |
| 171 /** | 174 /** |
| 172 * A necessary evil so that links work as expected. Does its best to | 175 * A necessary evil so that links work as expected. Does its best to |
| 173 * bail out early if possible. | 176 * bail out early if possible. |
| 174 * | 177 * |
| 175 * @param {MouseEvent} event . | 178 * @param {MouseEvent} event . |
| 176 */ | 179 */ |
| 177 _globalOnClick: function(event) { | 180 _globalOnClick: function(event) { |
| 178 var href = this._getSameOriginLinkHref(event); | 181 var href = this._getSameOriginLinkHref(event); |
| 179 if (!href) { | 182 if (!href) { |
| 180 return; | 183 return; |
| 181 } | 184 } |
| 182 | |
| 183 window.history.pushState({}, '', href); | 185 window.history.pushState({}, '', href); |
| 184 this.fire('location-changed', {}, {node: window}); | 186 this.fire('location-changed', {}, {node: window}); |
| 185 event.preventDefault(); | 187 event.preventDefault(); |
| 186 }, | 188 }, |
| 187 /** | 189 /** |
| 188 * Returns the absolute URL of the link (if any) that this click event | 190 * Returns the absolute URL of the link (if any) that this click event |
| 189 * is clicking on, if we can and should override the resulting full | 191 * is clicking on, if we can and should override the resulting full |
| 190 * page navigation. Returns null otherwise. | 192 * page navigation. Returns null otherwise. |
| 191 * | 193 * |
| 192 * This method is separated away from _globalOnClick for testability, | 194 * This method is separated away from _globalOnClick for testability, |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 if (url.origin !== origin) { | 247 if (url.origin !== origin) { |
| 246 return null; | 248 return null; |
| 247 } | 249 } |
| 248 var normalizedHref = url.pathname + url.search + url.hash; | 250 var normalizedHref = url.pathname + url.search + url.hash; |
| 249 | 251 |
| 250 // If we've been configured not to handle this url... don't handle it! | 252 // If we've been configured not to handle this url... don't handle it! |
| 251 if (this._urlSpaceRegExp && | 253 if (this._urlSpaceRegExp && |
| 252 !this._urlSpaceRegExp.test(normalizedHref)) { | 254 !this._urlSpaceRegExp.test(normalizedHref)) { |
| 253 return null; | 255 return null; |
| 254 } | 256 } |
| 255 | 257 // Need to use a full URL in case the containing page has a base URI. |
| 256 return normalizedHref; | 258 var fullNormalizedHref = new URL( |
| 259 normalizedHref, window.location.href).href; |
| 260 return fullNormalizedHref; |
| 257 }, | 261 }, |
| 258 _makeRegExp: function(urlSpaceRegex) { | 262 _makeRegExp: function(urlSpaceRegex) { |
| 259 return RegExp(urlSpaceRegex); | 263 return RegExp(urlSpaceRegex); |
| 260 } | 264 } |
| 261 }); | 265 }); |
| OLD | NEW |