Chromium Code Reviews| Index: chrome/browser/resources/md_history/query_manager.js |
| diff --git a/chrome/browser/resources/md_history/query_manager.js b/chrome/browser/resources/md_history/query_manager.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4d2ab89b426525e2ca71b88395ed382e3dac2739 |
| --- /dev/null |
| +++ b/chrome/browser/resources/md_history/query_manager.js |
| @@ -0,0 +1,114 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +Polymer({ |
| + is: 'history-query-manager', |
| + |
| + properties: { |
| + /** @type {QueryState} */ |
| + queryState: { |
| + type: Object, |
| + notify: true, |
| + }, |
| + |
| + groupedRange: { |
| + type: Number, |
| + // This needs to be a top-level property so the observer fires with the |
| + // old and new values. |
| + observer: 'groupedRangeChanged_', |
| + }, |
|
calamity
2017/01/17 06:22:10
A private computed property, perhaps?
tsergeant
2017/01/18 03:36:00
Done.
|
| + |
| + /** @type {QueryResult} */ |
| + queryResult: Object, |
| + }, |
| + |
| + observers: [ |
| + 'searchTermChanged_(queryState.searchTerm)', |
| + 'groupedOffsetChanged_(queryState.groupedOffset)', |
| + ], |
| + |
| + /** @private {?function(!Event)} */ |
| + boundOnQueryHistory_: null, |
| + |
| + /** @override */ |
| + attached: function() { |
| + this.boundOnQueryHistory_ = this.onQueryHistory_.bind(this); |
| + document.addEventListener('query-history', this.boundOnQueryHistory_); |
| + }, |
| + |
| + /** @override */ |
| + detached: function() { |
| + document.removeEventListener('query-history', this.boundOnQueryHistory_); |
| + }, |
| + |
| + /** |
| + * @param {boolean} incremental |
| + * @private |
| + */ |
| + queryHistory_: function(incremental) { |
| + var queryState = this.queryState; |
| + // Disable querying until the first set of results have been returned. If |
| + // there is a search, query immediately to support search query params from |
| + // the URL. |
| + var noResults = !this.queryResult || this.queryResult.results == null; |
| + if (queryState.queryingDisabled || |
| + (!this.queryState.searchTerm && noResults)) { |
| + return; |
| + } |
| + |
| + this.set('queryState.querying', true); |
| + this.set('queryState.incremental', incremental); |
| + |
| + var lastVisitTime = 0; |
| + if (incremental) { |
| + var lastVisit = this.queryResult.results.slice(-1)[0]; |
| + lastVisitTime = lastVisit ? Math.floor(lastVisit.time) : 0; |
| + } |
| + |
| + var maxResults = |
| + this.queryState.range == HistoryRange.ALL_TIME ? RESULTS_PER_PAGE : 0; |
| + |
| + chrome.send('queryHistory', [ |
| + queryState.searchTerm, queryState.groupedOffset, queryState.range, |
| + lastVisitTime, maxResults |
|
calamity
2017/01/17 06:22:10
nit: This is pretty hard to read. What happens if
tsergeant
2017/01/18 03:36:00
~~magic~~
|
| + ]); |
| + }, |
| + |
| + /** |
| + * @param {!Event} e |
| + * @private |
| + */ |
| + onQueryHistory_: function(e) { |
| + this.queryHistory_(/** @type {boolean} */ e.detail); |
| + return false; |
| + }, |
| + |
| + /** @private */ |
| + groupedOffsetChanged_: function() { |
| + this.queryHistory_(false); |
| + }, |
| + |
| + /** |
| + * @param {HistoryRange} range |
| + * @param {HistoryRange} oldRange |
| + * @private |
| + */ |
| + groupedRangeChanged_: function(range, oldRange) { |
| + if (oldRange == undefined) |
| + return; |
| + |
| + this.set('queryState.groupedOffset', 0); |
| + |
| + this.queryHistory_(false); |
| + this.fire('history-view-changed'); |
| + }, |
| + |
| + /** @private */ |
| + searchTermChanged_: function() { |
| + this.queryHistory_(false); |
| + // TODO(tsergeant): Ignore incremental searches in this metric. |
| + if (this.queryState.searchTerm) |
| + md_history.BrowserService.getInstance().recordAction('Search'); |
| + }, |
| +}); |