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

Side by Side Diff: chrome/browser/resources/md_history/query_manager.js

Issue 2643533003: MD History: Use one-way binding for history query state. (Closed)
Patch Set: Rebase 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 unified diff | Download patch
OLDNEW
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 19 matching lines...) Expand all
71 chrome.send('queryHistory', [ 87 chrome.send('queryHistory', [
72 queryState.searchTerm, 88 queryState.searchTerm,
73 queryState.groupedOffset, 89 queryState.groupedOffset,
74 queryState.range, 90 queryState.range,
75 lastVisitTime, 91 lastVisitTime,
76 maxResults, 92 maxResults,
77 ]); 93 ]);
78 }, 94 },
79 95
80 /** 96 /**
97 * @param {{range: ?HistoryRange,
98 * offset: ?number,
99 * search: ?string}} changes
100 * @private
101 */
102 changeQuery_: function(changes) {
calamity 2017/01/23 03:03:56 Why not just put this all in onChangeQuery?
tsergeant 2017/01/23 04:18:26 This is a relic from when all this code was in rou
103 var needsUpdate = false;
104
105 if (changes.range != null && changes.range != this.queryState.range) {
106 this.set('queryState.range', changes.range);
107 needsUpdate = true;
108
109 // Reset back to page 0 of the results, unless changing to a specific
110 // page.
111 if (!changes.offset)
112 this.set('queryState.groupedOffset', 0);
113
114 this.fire('history-view-changed');
115 }
116
117 if (changes.offset != null &&
118 changes.offset != this.queryState.groupedOffset) {
119 this.set('queryState.groupedOffset', changes.offset);
120 needsUpdate = true;
121 }
122
123 if (changes.search != null &&
124 changes.search != this.queryState.searchTerm) {
125 this.set('queryState.searchTerm', changes.search);
126 needsUpdate = true;
127 }
128
129 if (needsUpdate)
130 this.queryHistory_(false);
131 },
132
133 /**
134 * @param {Event} e
135 * @private
136 */
137 onChangeQuery_: function(e) {
138 this.changeQuery_(e.detail);
139 },
140
141 /**
81 * @param {!Event} e 142 * @param {!Event} e
82 * @private 143 * @private
83 */ 144 */
84 onQueryHistory_: function(e) { 145 onQueryHistory_: function(e) {
85 this.queryHistory_(/** @type {boolean} */ e.detail); 146 this.queryHistory_(/** @type {boolean} */ e.detail);
86 return false; 147 return false;
87 }, 148 },
88 149
89 /** @private */ 150 /** @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() { 151 searchTermChanged_: function() {
112 this.queryHistory_(false);
113 // TODO(tsergeant): Ignore incremental searches in this metric. 152 // TODO(tsergeant): Ignore incremental searches in this metric.
114 if (this.queryState.searchTerm) 153 if (this.queryState.searchTerm)
115 md_history.BrowserService.getInstance().recordAction('Search'); 154 md_history.BrowserService.getInstance().recordAction('Search');
116 }, 155 },
117 }); 156 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698