Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4848)

Unified Diff: chrome/browser/resources/md_history/router.js

Issue 2656443004: MD History: Add routing for grouped history mode. (Closed)
Patch Set: Review comments Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..38c5f0514ebf4753273f7914efed353b552ced9e 100644
--- a/chrome/browser/resources/md_history/router.js
+++ b/chrome/browser/resources/md_history/router.js
@@ -8,24 +8,25 @@ Polymer({
properties: {
selectedPage: {
type: String,
- observer: 'serializePath_',
notify: true,
- },
-
- path_: {
- type: String,
- observer: 'pathChanged_',
+ observer: 'selectedPageChanged_'
},
/** @type {QueryState} */
queryState: Object,
+ grouped: Boolean,
+
+ path_: String,
+
queryParams_: Object,
},
+ /** @private {boolean} */
+ parsing_: false,
+
observers: [
- 'queryParamsChanged_(queryParams_.*)',
- 'searchTermChanged_(queryState.searchTerm)',
+ 'onUrlChanged_(path_, queryParams_)',
],
/** @override */
@@ -37,25 +38,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;
+ }
+ }
});
« no previous file with comments | « chrome/browser/resources/md_history/query_manager.js ('k') | chrome/browser/resources/md_history/side_bar.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698