| OLD | NEW |
| 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 properties: { | 8 properties: { |
| 9 // An array of history entries in reverse chronological order. | 9 // An array of history entries in reverse chronological order. |
| 10 historyData: { | 10 historyData: { |
| 11 type: Array, | 11 type: Array, |
| 12 value: function() { return []; } | 12 value: function() { return []; } |
| 13 }, | 13 }, |
| 14 | 14 |
| 15 // The time in seconds of the last history item in historyData. | 15 // The time in seconds of the last history item in historyData. |
| 16 lastVisitedTime: { | 16 lastVisitedTime: { |
| 17 type: Number, | 17 type: Number, |
| 18 value: 0 | 18 value: 0 |
| 19 }, | 19 }, |
| 20 | 20 |
| 21 menuOpen: { | 21 menuOpen: { |
| 22 type: Boolean, | 22 type: Boolean, |
| 23 value: false, | 23 value: false, |
| 24 reflectToAttribute: true | 24 reflectToAttribute: true |
| 25 }, | 25 }, |
| 26 | 26 |
| 27 searchTerm: { |
| 28 type: String, |
| 29 value: '' |
| 30 }, |
| 31 |
| 27 menuIdentifier: { | 32 menuIdentifier: { |
| 28 type: Number, | 33 type: Number, |
| 29 value: 0 | 34 value: 0 |
| 35 }, |
| 36 |
| 37 // Page waits until results have finished loading before displaying message. |
| 38 loading_: { |
| 39 type: Boolean, |
| 40 value: true |
| 30 } | 41 } |
| 31 }, | 42 }, |
| 32 | 43 |
| 33 /** @const @private */ | 44 /** @const @private */ |
| 34 X_OFFSET_: 30, | 45 X_OFFSET_: 30, |
| 35 | 46 |
| 36 listeners: { | 47 listeners: { |
| 37 'tap': 'closeMenu', | 48 'tap': 'closeMenu', |
| 38 'toggle-menu': 'toggleMenu_' | 49 'toggle-menu': 'toggleMenu_' |
| 39 }, | 50 }, |
| 40 | 51 |
| 41 /** | 52 /** |
| 42 * Closes the overflow menu. | 53 * Closes the overflow menu. |
| 43 */ | 54 */ |
| 44 closeMenu: function() { | 55 closeMenu: function() { |
| 45 this.menuOpen = false; | 56 this.menuOpen = false; |
| 46 }, | 57 }, |
| 47 | 58 |
| 48 /** | 59 /** |
| 60 * Clear the page completely so the page can display new results (search). |
| 61 */ |
| 62 resetHistoryResults: function() { |
| 63 // TODO(hsampson): make it so the old results aren't replaced immediately, |
| 64 // only replaced when new results come in. |
| 65 this.loading_ = true; |
| 66 this.splice('historyData', 0, this.historyData.length); |
| 67 }, |
| 68 |
| 69 /** |
| 49 * Opens the overflow menu unless the menu is already open and the same button | 70 * Opens the overflow menu unless the menu is already open and the same button |
| 50 * is pressed. | 71 * is pressed. |
| 51 * @param {Event} e The event with details of the menu item that was clicked. | 72 * @param {Event} e The event with details of the menu item that was clicked. |
| 52 * @private | 73 * @private |
| 53 */ | 74 */ |
| 54 toggleMenu_: function(e) { | 75 toggleMenu_: function(e) { |
| 55 var menu = this.$['overflow-menu']; | 76 var menu = this.$['overflow-menu']; |
| 56 | 77 |
| 57 // Menu closes if the same button is clicked. | 78 // Menu closes if the same button is clicked. |
| 58 if (this.menuOpen && this.menuIdentifier == e.detail.accessTime) { | 79 if (this.menuOpen && this.menuIdentifier == e.detail.accessTime) { |
| 59 this.closeMenu(); | 80 this.closeMenu(); |
| 60 } else { | 81 } else { |
| 61 this.menuOpen = true; | 82 this.menuOpen = true; |
| 62 this.menuIdentifier = e.detail.accessTime; | 83 this.menuIdentifier = e.detail.accessTime; |
| 63 | 84 |
| 64 cr.ui.positionPopupAtPoint(e.detail.x + this.X_OFFSET_, e.detail.y, menu, | 85 cr.ui.positionPopupAtPoint(e.detail.x + this.X_OFFSET_, e.detail.y, menu, |
| 65 cr.ui.AnchorType.BEFORE); | 86 cr.ui.AnchorType.BEFORE); |
| 66 | 87 |
| 67 menu.focus(); | 88 menu.focus(); |
| 68 } | 89 } |
| 69 }, | 90 }, |
| 70 | 91 |
| 71 /** | 92 /** |
| 72 * Adds the newly updated history results into historyData. Adds new fields | 93 * Adds the newly updated history results into historyData. Adds new fields |
| 73 * for each result. | 94 * for each result. |
| 74 * @param {!Array<!HistoryEntry>} historyResults The new history results. | 95 * @param {!Array<!HistoryEntry>} historyResults The new history results. |
| 75 */ | 96 */ |
| 76 addNewResults: function(historyResults) { | 97 addNewResults: function(historyResults, search) { |
| 77 if (historyResults.length == 0) | 98 this.searchTerm = search; |
| 99 |
| 100 if (historyResults.length == 0) { |
| 101 this.loading_ = false; |
| 78 return; | 102 return; |
| 103 } |
| 79 | 104 |
| 80 // Creates a copy of historyResults to prevent accidentally modifying this | 105 // Creates a copy of historyResults to prevent accidentally modifying this |
| 81 // field. | 106 // field. |
| 82 var results = historyResults.slice(); | 107 var results = historyResults.slice(); |
| 83 | 108 |
| 84 var currentDate = results[0].dateRelativeDay; | 109 var currentDate = results[0].dateRelativeDay; |
| 85 | 110 |
| 86 // Resets the last history item for the currentDate if new history results | 111 // Resets the last history item for the currentDate if new history results |
| 87 // for currentDate is loaded. | 112 // for currentDate is loaded. |
| 88 var lastHistoryItem = this.historyData[this.historyData.length - 1]; | 113 var lastHistoryItem = this.historyData[this.historyData.length - 1]; |
| 89 if (lastHistoryItem && lastHistoryItem.isLastItem && | 114 if (lastHistoryItem && lastHistoryItem.isLastItem && |
| 90 lastHistoryItem.dateRelativeDay == currentDate) { | 115 lastHistoryItem.dateRelativeDay == currentDate) { |
| 91 this.set('historyData.' + parseInt(this.historyData.length - 1) + | 116 this.set('historyData.' + parseInt(this.historyData.length - 1) + |
| 92 '.isLastItem', false); | 117 '.isLastItem', false); |
| 93 } | 118 } |
| 94 | 119 |
| 95 for (var i = 0; i < results.length; i++) { | 120 for (var i = 0; i < results.length; i++) { |
| 96 // Sets the default values for these fields to prevent undefined types. | 121 // Sets the default values for these fields to prevent undefined types. |
| 97 results[i].selected = false; | 122 results[i].selected = false; |
| 98 results[i].isLastItem = false; | 123 results[i].isLastItem = false; |
| 99 results[i].isFirstItem = false; | 124 results[i].isFirstItem = false; |
| 125 results[i].visibleTimestamp = |
| 126 search == '' ? results[i].dateTimeOfDay : results[i].dateShort; |
| 100 | 127 |
| 101 if (results[i].dateRelativeDay != currentDate) { | 128 if (results[i].dateRelativeDay != currentDate) { |
| 102 results[i - 1].isLastItem = true; | 129 results[i - 1].isLastItem = true; |
| 103 results[i].isFirstItem = true; | 130 results[i].isFirstItem = true; |
| 104 currentDate = results[i].dateRelativeDay; | 131 currentDate = results[i].dateRelativeDay; |
| 105 } | 132 } |
| 106 results[i].needsTimeGap = this.needsTimeGap_(results, i); | 133 if (i != 0) { |
| 134 results[i - 1].needsTimeGap = this.needsTimeGap_(results, i - 1); |
| 135 } |
| 107 } | 136 } |
| 108 results[i - 1].isLastItem = true; | 137 results[i - 1].isLastItem = true; |
| 138 results[i - 1].needsTimeGap = false; |
| 109 | 139 |
| 110 // If it's the first time we get data, the first item will always be the | 140 // If it's the first time we get data, the first item will always be the |
| 111 // first card. | 141 // first card. |
| 112 if (this.historyData.length == 0) | 142 if (this.historyData.length == 0) |
| 113 results[0].isFirstItem = true; | 143 results[0].isFirstItem = true; |
| 114 | 144 |
| 115 // Adds results to the beginning of the historyData array. | 145 // Adds results to the beginning of the historyData array. |
| 116 results.unshift('historyData'); | 146 results.unshift('historyData'); |
| 117 this.push.apply(this, results); | 147 this.push.apply(this, results); |
| 118 | 148 |
| 119 this.lastVisitedTime = this.historyData[this.historyData.length - 1].time; | 149 this.lastVisitedTime = this.historyData[this.historyData.length - 1].time; |
| 150 this.loading_ = false; |
| 120 }, | 151 }, |
| 121 | 152 |
| 122 /** | 153 /** |
| 123 * Cycle through each entry in historyData and set all items to be | 154 * Cycle through each entry in historyData and set all items to be |
| 124 * unselected. | 155 * unselected. |
| 125 * @param {number} overallItemCount The number of checkboxes selected. | 156 * @param {number} overallItemCount The number of checkboxes selected. |
| 126 */ | 157 */ |
| 127 unselectAllItems: function(overallItemCount) { | 158 unselectAllItems: function(overallItemCount) { |
| 128 for (var i = 0; i < this.historyData.length; i++) { | 159 for (var i = 0; i < this.historyData.length; i++) { |
| 129 if (this.historyData[i].selected) { | 160 if (this.historyData[i].selected) { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 toBeRemoved.push({ | 222 toBeRemoved.push({ |
| 192 url: this.historyData[i].url, | 223 url: this.historyData[i].url, |
| 193 timestamps: this.historyData[i].allTimestamps | 224 timestamps: this.historyData[i].allTimestamps |
| 194 }); | 225 }); |
| 195 | 226 |
| 196 count--; | 227 count--; |
| 197 if (count == 0) | 228 if (count == 0) |
| 198 return toBeRemoved; | 229 return toBeRemoved; |
| 199 } | 230 } |
| 200 } | 231 } |
| 232 return toBeRemoved; |
| 201 }, | 233 }, |
| 202 | 234 |
| 203 /** | 235 /** |
| 204 * Called when the card manager is scrolled. | 236 * Called when the card manager is scrolled. |
| 205 * @private | 237 * @private |
| 206 */ | 238 */ |
| 207 scrollHandler_: function() { | 239 scrollHandler_: function() { |
| 208 // Close overflow menu on scroll. | 240 // Close overflow menu on scroll. |
| 209 this.closeMenu(); | 241 this.closeMenu(); |
| 210 | 242 |
| 211 // Requests the next list of results when the scrollbar is near the bottom | 243 // Requests the next list of results when the scrollbar is near the bottom |
| 212 // of the window. | 244 // of the window. |
| 213 var scrollOffset = 10; | 245 var scrollOffset = 10; |
| 214 var scrollElem = this.$['infinite-list']; | 246 var scrollElem = this.$['infinite-list']; |
| 215 | 247 |
| 216 if (scrollElem.scrollHeight <= | 248 if (!this.loading_ && scrollElem.scrollHeight <= |
| 217 scrollElem.scrollTop + scrollElem.clientHeight + scrollOffset) { | 249 scrollElem.scrollTop + scrollElem.clientHeight + scrollOffset) { |
| 218 chrome.send('queryHistory', | 250 chrome.send('queryHistory', |
| 219 ['', 0, 0, this.lastVisitedTime, RESULTS_PER_PAGE]); | 251 [this.searchTerm, 0, 0, this.lastVisitedTime, RESULTS_PER_PAGE]); |
| 220 } | 252 } |
| 221 }, | 253 }, |
| 222 | 254 |
| 223 /** | 255 /** |
| 224 * Check whether the time difference between the given history item and the | 256 * Check whether the time difference between the given history item and the |
| 225 * next one is large enough for a spacer to be required. | 257 * next one is large enough for a spacer to be required. |
| 226 * @param {Array<HistoryEntry>} results A list of history results. | 258 * @param {Array<HistoryEntry>} results A list of history results. |
| 227 * @param {number} index The index number of the first item being compared. | 259 * @param {number} index The index number of the first item being compared. |
| 228 * @return {boolean} Whether or not time gap separator is required. | 260 * @return {boolean} Whether or not time gap separator is required. |
| 229 * @private | 261 * @private |
| 230 */ | 262 */ |
| 231 needsTimeGap_: function(results, index) { | 263 needsTimeGap_: function(results, index) { |
| 232 var currentItem = results[index]; | 264 var currentItem = results[index]; |
| 233 var nextItem = results[index + 1]; | 265 var nextItem = results[index + 1]; |
| 234 | 266 |
| 235 if (index + 1 >= results.length) | 267 if (this.searchTerm) |
| 236 return false; | 268 return currentItem.visibleTimestamp != nextItem.visibleTimestamp; |
| 269 else |
| 270 return currentItem.time - nextItem.time > BROWSING_GAP_TIME && |
| 271 currentItem.dateRelativeDay == nextItem.dateRelativeDay; |
| 272 }, |
| 237 | 273 |
| 238 return currentItem.time - nextItem.time > BROWSING_GAP_TIME && | 274 /** |
| 239 currentItem.dateRelativeDay == nextItem.dateRelativeDay; | 275 * True if there are no results available and the page has finished loading. |
| 276 * @param {number} numberOfResults The length of historyDataByDay_. |
| 277 * @param {boolean} loading Whether the page has finished loading results. |
| 278 * @return {boolean} Whether there are results to show. |
| 279 * @private |
| 280 */ |
| 281 noResultsAvailable_: function(numberOfResults, loading) { |
| 282 return numberOfResults == 0 && !loading; |
| 283 }, |
| 284 |
| 285 /** |
| 286 * Return the message to be displayed if there are no results based on whether |
| 287 * the empty results were for a search or normal history results. |
| 288 * @param {string} search The search term for these results. |
| 289 * @return {string} The displayed message. |
| 290 * @private |
| 291 */ |
| 292 noResultsMessage_: function(search) { |
| 293 var messageId = search != '' ? 'noSearchResults' : 'noResults'; |
| 294 return loadTimeData.getString(messageId); |
| 240 } | 295 } |
| 241 }); | 296 }); |
| OLD | NEW |