| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 Polymer({ | 5 Polymer({ |
| 6 is: 'history-query-manager', | 6 is: 'history-query-manager', |
| 7 | 7 |
| 8 properties: { | 8 properties: { |
| 9 /** @type {QueryState} */ | 9 /** @type {QueryState} */ |
| 10 queryState: { | 10 queryState: { |
| 11 type: Object, | 11 type: Object, |
| 12 notify: true, | 12 notify: true, |
| 13 }, | 13 value: function() { |
| 14 return { |
| 15 // Whether the most recent query was incremental. |
| 16 incremental: false, |
| 17 // A query is initiated by page load. |
| 18 querying: true, |
| 19 queryingDisabled: false, |
| 20 _range: HistoryRange.ALL_TIME, |
| 21 searchTerm: '', |
| 22 groupedOffset: 0, |
| 14 | 23 |
| 15 groupedRange_: { | 24 set range(val) { |
| 16 type: Number, | 25 this._range = Number(val); |
| 17 // Use a computed property to be able to compare old and new values. | 26 }, |
| 18 computed: 'computeGroupedRange_(queryState.range)', | 27 get range() { |
| 28 return this._range; |
| 29 }, |
| 30 }; |
| 31 }, |
| 19 }, | 32 }, |
| 20 | 33 |
| 21 /** @type {QueryResult} */ | 34 /** @type {QueryResult} */ |
| 22 queryResult: Object, | 35 queryResult: Object, |
| 23 }, | 36 }, |
| 24 | 37 |
| 25 observers: [ | 38 observers: [ |
| 26 'searchTermChanged_(queryState.searchTerm)', | 39 'searchTermChanged_(queryState.searchTerm)', |
| 27 'groupedOffsetChanged_(queryState.groupedOffset)', | |
| 28 ], | 40 ], |
| 29 | 41 |
| 30 /** @private {?function(!Event)} */ | 42 /** @private {!Object<string, !function(!Event)>} */ |
| 31 boundOnQueryHistory_: null, | 43 documentListeners_: {}, |
| 32 | 44 |
| 33 /** @override */ | 45 /** @override */ |
| 34 attached: function() { | 46 attached: function() { |
| 35 this.boundOnQueryHistory_ = this.onQueryHistory_.bind(this); | 47 this.documentListeners_['change-query'] = this.onChangeQuery_.bind(this); |
| 36 document.addEventListener('query-history', this.boundOnQueryHistory_); | 48 this.documentListeners_['query-history'] = this.onQueryHistory_.bind(this); |
| 49 |
| 50 for (var e in this.documentListeners_) |
| 51 document.addEventListener(e, this.documentListeners_[e]); |
| 37 }, | 52 }, |
| 38 | 53 |
| 39 /** @override */ | 54 /** @override */ |
| 40 detached: function() { | 55 detached: function() { |
| 41 document.removeEventListener('query-history', this.boundOnQueryHistory_); | 56 for (var e in this.documentListeners_) |
| 57 document.removeEventListener(e, this.documentListeners_[e]); |
| 42 }, | 58 }, |
| 43 | 59 |
| 44 /** | 60 /** |
| 45 * @param {boolean} incremental | 61 * @param {boolean} incremental |
| 46 * @private | 62 * @private |
| 47 */ | 63 */ |
| 48 queryHistory_: function(incremental) { | 64 queryHistory_: function(incremental) { |
| 49 var queryState = this.queryState; | 65 var queryState = this.queryState; |
| 50 // Disable querying until the first set of results have been returned. If | 66 // 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 | 67 // there is a search, query immediately to support search query params from |
| (...skipping 22 matching lines...) Expand all Loading... |
| 74 queryState.range, | 90 queryState.range, |
| 75 lastVisitTime, | 91 lastVisitTime, |
| 76 maxResults, | 92 maxResults, |
| 77 ]); | 93 ]); |
| 78 }, | 94 }, |
| 79 | 95 |
| 80 /** | 96 /** |
| 81 * @param {!Event} e | 97 * @param {!Event} e |
| 82 * @private | 98 * @private |
| 83 */ | 99 */ |
| 100 onChangeQuery_: function(e) { |
| 101 var changes = |
| 102 /** @type {{range: ?HistoryRange, offset: ?number, search: ?string}} */ |
| 103 (e.detail); |
| 104 var needsUpdate = false; |
| 105 |
| 106 if (changes.range != null && changes.range != this.queryState.range) { |
| 107 this.set('queryState.range', changes.range); |
| 108 needsUpdate = true; |
| 109 |
| 110 // Reset back to page 0 of the results, unless changing to a specific |
| 111 // page. |
| 112 if (!changes.offset) |
| 113 this.set('queryState.groupedOffset', 0); |
| 114 |
| 115 this.fire('history-view-changed'); |
| 116 } |
| 117 |
| 118 if (changes.offset != null && |
| 119 changes.offset != this.queryState.groupedOffset) { |
| 120 this.set('queryState.groupedOffset', changes.offset); |
| 121 needsUpdate = true; |
| 122 } |
| 123 |
| 124 if (changes.search != null && |
| 125 changes.search != this.queryState.searchTerm) { |
| 126 this.set('queryState.searchTerm', changes.search); |
| 127 needsUpdate = true; |
| 128 } |
| 129 |
| 130 if (needsUpdate) |
| 131 this.queryHistory_(false); |
| 132 }, |
| 133 |
| 134 /** |
| 135 * @param {!Event} e |
| 136 * @private |
| 137 */ |
| 84 onQueryHistory_: function(e) { | 138 onQueryHistory_: function(e) { |
| 85 this.queryHistory_(/** @type {boolean} */ e.detail); | 139 this.queryHistory_(/** @type {boolean} */ e.detail); |
| 86 return false; | 140 return false; |
| 87 }, | 141 }, |
| 88 | 142 |
| 89 /** @private */ | 143 /** @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() { | 144 searchTermChanged_: function() { |
| 112 this.queryHistory_(false); | |
| 113 // TODO(tsergeant): Ignore incremental searches in this metric. | 145 // TODO(tsergeant): Ignore incremental searches in this metric. |
| 114 if (this.queryState.searchTerm) | 146 if (this.queryState.searchTerm) |
| 115 md_history.BrowserService.getInstance().recordAction('Search'); | 147 md_history.BrowserService.getInstance().recordAction('Search'); |
| 116 }, | 148 }, |
| 117 }); | 149 }); |
| OLD | NEW |