Chromium Code Reviews| Index: chrome/browser/resources/md_history/router.js |
| diff --git a/chrome/browser/resources/md_history/router.js b/chrome/browser/resources/md_history/router.js |
| index 986d5fb1b7b49b9a48105e0c8a2084c02f5dfd16..04276a11c764c3e4691f787ea700d018b91bb30d 100644 |
| --- a/chrome/browser/resources/md_history/router.js |
| +++ b/chrome/browser/resources/md_history/router.js |
| @@ -8,24 +8,28 @@ Polymer({ |
| properties: { |
| selectedPage: { |
| type: String, |
| - observer: 'serializePath_', |
| notify: true, |
| - }, |
| - |
| - path_: { |
| - type: String, |
| - observer: 'pathChanged_', |
| + observer: 'selectedPageChanged_' |
| }, |
| /** @type {QueryState} */ |
| queryState: Object, |
| + /** @type {QueryResult} */ |
| + queryResult: Object, |
|
calamity
2017/01/27 03:47:34
Remove.
tsergeant
2017/01/30 02:13:14
Oops, done.
|
| + |
| + grouped: Boolean, |
| + |
| + path_: String, |
| + |
| queryParams_: Object, |
| }, |
| + /** @private{boolean} */ |
|
calamity
2017/01/27 03:47:34
nit: Missing a space.
tsergeant
2017/01/30 02:13:14
Done.
|
| + parsing_: false, |
| + |
| observers: [ |
| - 'queryParamsChanged_(queryParams_.*)', |
| - 'searchTermChanged_(queryState.searchTerm)', |
| + 'onUrlChanged_(path_, queryParams_)', |
| ], |
| /** @override */ |
| @@ -37,25 +41,97 @@ Polymer({ |
| } |
| }, |
| + /** |
| + * Write all relevant page state to the URL. |
| + */ |
| + serializeUrl: function() { |
| + var path = this.selectedPage; |
| + |
| + if (path == 'history' && this.queryState.range != HistoryRange.ALL_TIME) |
| + path += '/' + this.rangeToString_(this.queryState.range); |
| + |
| + if (path == 'history') |
| + path = ''; |
| + |
| + var offsetParam = null; |
| + if (this.selectedPage == 'history' && this.queryState.groupedOffset) |
| + offsetParam = this.queryState.groupedOffset; |
| + |
| + // Make all modifications at the end of the method so observers can't change |
| + // the outcome. |
| + this.path_ = '/' + path; |
| + this.set('queryParams_.offset', offsetParam); |
| + this.set('queryParams_.q', this.queryState.searchTerm || null); |
| + }, |
| + |
| /** @private */ |
| - serializePath_: function() { |
| - var page = this.selectedPage == 'history' ? '' : this.selectedPage; |
| - this.path_ = '/' + page; |
| + selectedPageChanged_: function() { |
| + // Update the URL if the page was changed externally, but ignore the update |
| + // if it came from parseUrl_(). |
| + if (!this.parsing_) |
| + this.serializeUrl(); |
| }, |
| /** @private */ |
| - pathChanged_: function() { |
| + parseUrl_: function() { |
| + this.parsing_ = true; |
| + var changes = {}; |
| var sections = this.path_.substr(1).split('/'); |
| - this.selectedPage = sections[0] || 'history'; |
| + var page = sections[0] || 'history'; |
| + |
| + if (page == 'history' && this.grouped) { |
| + var range = sections.length > 1 ? this.stringToRange_(sections[1]) : |
| + HistoryRange.ALL_TIME; |
| + changes.range = range; |
| + changes.offset = Number(this.queryParams_.offset) || 0; |
| + } |
| + |
| + changes.search = this.queryParams_.q || ''; |
| + |
| + // Must change selectedPage before `change-query`, otherwise the |
| + // query-manager will call serializeUrl() with the old page. |
| + this.selectedPage = page; |
| + this.fire('change-query', changes); |
| + this.serializeUrl(); |
| + |
| + this.parsing_ = false; |
| }, |
| /** @private */ |
| - queryParamsChanged_: function() { |
| - this.fire('change-query', {search: this.queryParams_.q || ''}); |
| + onUrlChanged_: function() { |
| + // Changing the url and query parameters at the same time will cause two |
| + // calls to onUrlChanged_. Debounce the actual work so that these two |
| + // changes get processed together. |
| + this.debounce('parseUrl', this.parseUrl_.bind(this)); |
| }, |
| - /** @private */ |
| - searchTermChanged_: function() { |
| - this.set('queryParams_.q', this.queryState.searchTerm || null); |
| + /** |
| + * @param {!HistoryRange} range |
| + * @return {string} |
| + */ |
| + rangeToString_: function(range) { |
| + switch (range) { |
| + case HistoryRange.WEEK: |
| + return 'week'; |
| + case HistoryRange.MONTH: |
| + return 'month'; |
| + default: |
| + return ''; |
| + } |
| }, |
| + |
| + /** |
| + * @param {string} str |
| + * @return {HistoryRange} |
| + */ |
| + stringToRange_: function(str) { |
| + switch (str) { |
| + case 'week': |
| + return HistoryRange.WEEK; |
| + case 'month': |
| + return HistoryRange.MONTH; |
| + default: |
| + return HistoryRange.ALL_TIME; |
| + } |
| + } |
| }); |