| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 Polymer({ |
| 6 is: 'history-query-manager', |
| 7 |
| 8 properties: { |
| 9 /** @type {QueryState} */ |
| 10 queryState: { |
| 11 type: Object, |
| 12 notify: true, |
| 13 }, |
| 14 |
| 15 groupedRange_: { |
| 16 type: Number, |
| 17 // Use a computed property to be able to compare old and new values. |
| 18 computed: 'computeGroupedRange_(queryState.range)', |
| 19 }, |
| 20 |
| 21 /** @type {QueryResult} */ |
| 22 queryResult: Object, |
| 23 }, |
| 24 |
| 25 observers: [ |
| 26 'searchTermChanged_(queryState.searchTerm)', |
| 27 'groupedOffsetChanged_(queryState.groupedOffset)', |
| 28 ], |
| 29 |
| 30 /** @private {?function(!Event)} */ |
| 31 boundOnQueryHistory_: null, |
| 32 |
| 33 /** @override */ |
| 34 attached: function() { |
| 35 this.boundOnQueryHistory_ = this.onQueryHistory_.bind(this); |
| 36 document.addEventListener('query-history', this.boundOnQueryHistory_); |
| 37 }, |
| 38 |
| 39 /** @override */ |
| 40 detached: function() { |
| 41 document.removeEventListener('query-history', this.boundOnQueryHistory_); |
| 42 }, |
| 43 |
| 44 /** |
| 45 * @param {boolean} incremental |
| 46 * @private |
| 47 */ |
| 48 queryHistory_: function(incremental) { |
| 49 var queryState = this.queryState; |
| 50 // Disable querying until the first set of results have been returned. If |
| 51 // there is a search, query immediately to support search query params from |
| 52 // the URL. |
| 53 var noResults = !this.queryResult || this.queryResult.results == null; |
| 54 if (queryState.queryingDisabled || |
| 55 (!this.queryState.searchTerm && noResults)) { |
| 56 return; |
| 57 } |
| 58 |
| 59 this.set('queryState.querying', true); |
| 60 this.set('queryState.incremental', incremental); |
| 61 |
| 62 var lastVisitTime = 0; |
| 63 if (incremental) { |
| 64 var lastVisit = this.queryResult.results.slice(-1)[0]; |
| 65 lastVisitTime = lastVisit ? Math.floor(lastVisit.time) : 0; |
| 66 } |
| 67 |
| 68 var maxResults = |
| 69 this.queryState.range == HistoryRange.ALL_TIME ? RESULTS_PER_PAGE : 0; |
| 70 |
| 71 chrome.send('queryHistory', [ |
| 72 queryState.searchTerm, |
| 73 queryState.groupedOffset, |
| 74 queryState.range, |
| 75 lastVisitTime, |
| 76 maxResults, |
| 77 ]); |
| 78 }, |
| 79 |
| 80 /** |
| 81 * @param {!Event} e |
| 82 * @private |
| 83 */ |
| 84 onQueryHistory_: function(e) { |
| 85 this.queryHistory_(/** @type {boolean} */ e.detail); |
| 86 return false; |
| 87 }, |
| 88 |
| 89 /** @private */ |
| 90 groupedOffsetChanged_: function() { |
| 91 this.queryHistory_(false); |
| 92 }, |
| 93 |
| 94 /** |
| 95 * @param {HistoryRange} range |
| 96 * @return {HistoryRange} |
| 97 * @private |
| 98 */ |
| 99 computeGroupedRange_: function(range) { |
| 100 if (this.groupedRange_ != undefined) { |
| 101 this.set('queryState.groupedOffset', 0); |
| 102 |
| 103 this.queryHistory_(false); |
| 104 this.fire('history-view-changed'); |
| 105 } |
| 106 |
| 107 return range; |
| 108 }, |
| 109 |
| 110 /** @private */ |
| 111 searchTermChanged_: function() { |
| 112 this.queryHistory_(false); |
| 113 // TODO(tsergeant): Ignore incremental searches in this metric. |
| 114 if (this.queryState.searchTerm) |
| 115 md_history.BrowserService.getInstance().recordAction('Search'); |
| 116 }, |
| 117 }); |
| OLD | NEW |