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

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

Issue 2207323002: [MD History] Factor out a common HistoryListBehavior. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix nits Created 4 years, 4 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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-list', 6 is: 'history-list',
7 7
8 behaviors: [HistoryListBehavior],
9
8 properties: { 10 properties: {
9 // The search term for the current query. Set when the query returns. 11 // The search term for the current query. Set when the query returns.
10 searchedTerm: { 12 searchedTerm: {
11 type: String, 13 type: String,
12 value: '', 14 value: '',
13 }, 15 },
14 16
15 lastSearchedTerm_: String, 17 lastSearchedTerm_: String,
16 18
17 querying: Boolean, 19 querying: Boolean,
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 results.unshift('historyData_'); 88 results.unshift('historyData_');
87 this.push.apply(this, results); 89 this.push.apply(this, results);
88 } else { 90 } else {
89 // The first time we receive data, use set() to ensure the iron-list is 91 // The first time we receive data, use set() to ensure the iron-list is
90 // initialized correctly. 92 // initialized correctly.
91 this.set('historyData_', results); 93 this.set('historyData_', results);
92 } 94 }
93 }, 95 },
94 96
95 /** 97 /**
96 * Cycle through each entry in historyData_ and set all items to be
97 * unselected.
98 * @param {number} overallItemCount The number of checkboxes selected.
99 */
100 unselectAllItems: function(overallItemCount) {
101 if (this.historyData_ === undefined)
102 return;
103
104 for (var i = 0; i < this.historyData_.length; i++) {
105 if (this.historyData_[i].selected) {
106 this.set('historyData_.' + i + '.selected', false);
107 overallItemCount--;
108 if (overallItemCount == 0)
109 break;
110 }
111 }
112 },
113
114 /**
115 * Remove the given |items| from the list. Expected to be called after the
116 * items are removed from the backend.
117 * @param {!Array<!HistoryEntry>} removalList
118 * @private
119 */
120 removeDeletedHistory_: function(removalList) {
121 // This set is only for speed. Note that set inclusion for objects is by
122 // reference, so this relies on the HistoryEntry objects never being copied.
123 var deletedItems = new Set(removalList);
124 var splices = [];
125
126 for (var i = this.historyData_.length - 1; i >= 0; i--) {
127 var item = this.historyData_[i];
128 if (deletedItems.has(item)) {
129 // Removes the selected item from historyData_. Use unshift so
130 // |splices| ends up in index order.
131 splices.unshift({
132 index: i,
133 removed: [item],
134 addedCount: 0,
135 object: this.historyData_,
136 type: 'splice'
137 });
138 this.historyData_.splice(i, 1);
139 }
140 }
141 // notifySplices gives better performance than individually splicing as it
142 // batches all of the updates together.
143 this.notifySplices('historyData_', splices);
144 },
145
146 /**
147 * Performs a request to the backend to delete all selected items. If
148 * successful, removes them from the view. Does not prompt the user before
149 * deleting -- see <history-list-container> for a version of this method which
150 * does prompt.
151 */
152 deleteSelected: function() {
153 var toBeRemoved = this.historyData_.filter(function(item) {
154 return item.selected;
155 });
156 md_history.BrowserService.getInstance()
157 .deleteItems(toBeRemoved)
158 .then(function(items) {
159 this.removeDeletedHistory_(items);
160 this.fire('unselect-all');
161 }.bind(this));
162 },
163
164 /**
165 * Called when the page is scrolled to near the bottom of the list. 98 * Called when the page is scrolled to near the bottom of the list.
166 * @private 99 * @private
167 */ 100 */
168 loadMoreData_: function() { 101 loadMoreData_: function() {
169 if (this.resultLoadingDisabled_ || this.querying) 102 if (this.resultLoadingDisabled_ || this.querying)
170 return; 103 return;
171 104
172 this.fire('load-more-history'); 105 this.fire('load-more-history');
173 }, 106 },
174 107
175 /** 108 /**
176 * Check whether the time difference between the given history item and the 109 * Check whether the time difference between the given history item and the
177 * next one is large enough for a spacer to be required. 110 * next one is large enough for a spacer to be required.
178 * @param {HistoryEntry} item 111 * @param {HistoryEntry} item
179 * @param {number} index The index of |item| in |historyData_|. 112 * @param {number} index The index of |item| in |historyData_|.
180 * @param {number} length The length of |historyData_|. 113 * @param {number} length The length of |historyData_|.
181 * @return {boolean} Whether or not time gap separator is required. 114 * @return {boolean} Whether or not time gap separator is required.
182 * @private 115 * @private
183 */ 116 */
184 needsTimeGap_: function(item, index, length) { 117 needsTimeGap_: function(item, index, length) {
185 return md_history.HistoryItem.needsTimeGap( 118 return md_history.HistoryItem.needsTimeGap(
186 this.historyData_, index, this.searchedTerm); 119 this.historyData_, index, this.searchedTerm);
187 }, 120 },
188 121
189 hasResults: function(historyDataLength) {
190 return historyDataLength > 0;
191 },
192
193 noResultsMessage_: function(searchedTerm, isLoading) {
194 if (isLoading)
195 return '';
196 var messageId = searchedTerm !== '' ? 'noSearchResults' : 'noResults';
197 return loadTimeData.getString(messageId);
198 },
199
200 /** 122 /**
201 * True if the given item is the beginning of a new card. 123 * True if the given item is the beginning of a new card.
202 * @param {HistoryEntry} item 124 * @param {HistoryEntry} item
203 * @param {number} i Index of |item| within |historyData_|. 125 * @param {number} i Index of |item| within |historyData_|.
204 * @param {number} length 126 * @param {number} length
205 * @return {boolean} 127 * @return {boolean}
206 * @private 128 * @private
207 */ 129 */
208 isCardStart_: function(item, i, length) { 130 isCardStart_: function(item, i, length) {
209 if (length == 0 || i > length - 1) 131 if (length == 0 || i > length - 1)
(...skipping 27 matching lines...) Expand all
237 isFirstItem_: function(index) { 159 isFirstItem_: function(index) {
238 return index == 0; 160 return index == 0;
239 }, 161 },
240 162
241 /** 163 /**
242 * @private 164 * @private
243 */ 165 */
244 notifyListScroll_: function() { 166 notifyListScroll_: function() {
245 this.fire('history-list-scrolled'); 167 this.fire('history-list-scrolled');
246 }, 168 },
169
170 /**
171 * @param {number} index
172 * @return {string}
173 * @private
174 */
175 pathForItem_: function(index) {
176 return 'historyData_.' + index;
177 },
247 }); 178 });
OLDNEW
« no previous file with comments | « chrome/browser/resources/md_history/history_list.html ('k') | chrome/browser/resources/md_history/history_list_behavior.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698