| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 Polymer({ | |
| 6 is: 'history-card-manager', | |
| 7 | |
| 8 properties: { | |
| 9 // An array of objects sorted in reverse chronological order. | |
| 10 // Each object has a date and the history items belonging to that date. | |
| 11 historyDataByDay_: { | |
| 12 type: Array, | |
| 13 value: function() { return []; } | |
| 14 }, | |
| 15 | |
| 16 // The time of access of the last element of historyDataByDay_. | |
| 17 lastVisitedTime: { | |
| 18 type: Number, | |
| 19 value: 0 | |
| 20 }, | |
| 21 | |
| 22 menuOpen: { | |
| 23 type: Boolean, | |
| 24 value: false, | |
| 25 reflectToAttribute: true | |
| 26 }, | |
| 27 | |
| 28 menuIdentifier: { | |
| 29 type: Number, | |
| 30 value: 0 | |
| 31 } | |
| 32 }, | |
| 33 | |
| 34 /** @const @private */ | |
| 35 X_OFFSET_: 30, | |
| 36 | |
| 37 listeners: { | |
| 38 'tap': 'closeMenu', | |
| 39 'toggle-menu': 'toggleMenu_' | |
| 40 }, | |
| 41 | |
| 42 /** | |
| 43 * Closes the overflow menu. | |
| 44 */ | |
| 45 closeMenu: function() { | |
| 46 this.menuOpen = false; | |
| 47 }, | |
| 48 | |
| 49 /** | |
| 50 * Opens the overflow menu unless the menu is already open and the same button | |
| 51 * is pressed. | |
| 52 * @param {Event} e The event with details of the menu item that was clicked. | |
| 53 * @private | |
| 54 */ | |
| 55 toggleMenu_: function(e) { | |
| 56 var menu = this.$['overflow-menu']; | |
| 57 | |
| 58 // Menu closes if the same button is clicked. | |
| 59 if (this.menuOpen && this.menuIdentifier == e.detail.accessTime) { | |
| 60 this.closeMenu(); | |
| 61 } else { | |
| 62 this.menuOpen = true; | |
| 63 this.menuIdentifier = e.detail.accessTime; | |
| 64 | |
| 65 cr.ui.positionPopupAtPoint(e.detail.x + this.X_OFFSET_, e.detail.y, menu, | |
| 66 cr.ui.AnchorType.BEFORE); | |
| 67 | |
| 68 menu.focus(); | |
| 69 } | |
| 70 }, | |
| 71 | |
| 72 /** | |
| 73 * Split the newly updated history results into history items sorted via day | |
| 74 * accessed. | |
| 75 * @param {!Array<!HistoryEntry>} results The new history results. | |
| 76 */ | |
| 77 addNewResults: function(results) { | |
| 78 if (results.length == 0) | |
| 79 return; | |
| 80 | |
| 81 var dateSortedData = []; | |
| 82 var historyItems = []; | |
| 83 var currentDate = results[0].dateRelativeDay; | |
| 84 | |
| 85 for (var i = 0; i < results.length; i++) { | |
| 86 if (!currentDate) | |
| 87 continue; | |
| 88 | |
| 89 results[i].selected = false; | |
| 90 if (results[i].dateRelativeDay != currentDate) { | |
| 91 this.appendHistoryData_(currentDate, historyItems); | |
| 92 currentDate = results[i].dateRelativeDay; | |
| 93 historyItems = []; | |
| 94 } | |
| 95 historyItems.push(results[i]); | |
| 96 } | |
| 97 | |
| 98 if (currentDate) | |
| 99 this.appendHistoryData_(currentDate, historyItems); | |
| 100 | |
| 101 this.lastVisitedTime = historyItems[historyItems.length - 1].time; | |
| 102 }, | |
| 103 | |
| 104 /** | |
| 105 * Cycle through each entry in historyDataByDay_ and set all items to be | |
| 106 * unselected. | |
| 107 * @param {number} overallItemCount The number of items selected. | |
| 108 */ | |
| 109 unselectAllItems: function(overallItemCount) { | |
| 110 var historyCardData = this.historyDataByDay_; | |
| 111 | |
| 112 for (var i = 0; i < historyCardData.length; i++) { | |
| 113 var items = historyCardData[i].historyItems; | |
| 114 for (var j = 0; j < items.length; j++) { | |
| 115 if (items[j].selected) { | |
| 116 this.set('historyDataByDay_.' + i + '.historyItems.' + j + | |
| 117 '.selected', false); | |
| 118 overallItemCount--; | |
| 119 if (overallItemCount == 0) | |
| 120 return; | |
| 121 } | |
| 122 } | |
| 123 } | |
| 124 }, | |
| 125 | |
| 126 /** | |
| 127 * Remove all selected items from the overall array so that they are also | |
| 128 * removed from view. Make sure that the card length and positioning is | |
| 129 * updated accordingly. | |
| 130 * @param {number} overallItemCount The number of items selected. | |
| 131 */ | |
| 132 removeDeletedHistory: function(overallItemCount) { | |
| 133 var infiniteList = /** @type {IronListElement} */(this.$['infinite-list']); | |
| 134 for (var i = 0; i < this.historyDataByDay_.length; i++) { | |
| 135 var items = this.historyDataByDay_[i].historyItems; | |
| 136 var itemDeletedFromCard = false; | |
| 137 | |
| 138 for (var j = items.length - 1; j >= 0; j--) { | |
| 139 if (!items[j].selected) | |
| 140 continue; | |
| 141 | |
| 142 this.splice('historyDataByDay_.' + i + '.historyItems', j, 1); | |
| 143 itemDeletedFromCard = true; | |
| 144 overallItemCount--; | |
| 145 if (overallItemCount == 0) { | |
| 146 this.removeEmptyCards_(); | |
| 147 // If the last card has been removed don't try to update its size. | |
| 148 if (i < this.historyDataByDay_.length) | |
| 149 infiniteList.updateSizeForItem(i); | |
| 150 return; | |
| 151 } | |
| 152 } | |
| 153 if (itemDeletedFromCard) | |
| 154 infiniteList.updateSizeForItem(i); | |
| 155 } | |
| 156 }, | |
| 157 | |
| 158 /** | |
| 159 * If a day has had all the history it contains removed, remove this day from | |
| 160 * the array. | |
| 161 * @private | |
| 162 */ | |
| 163 removeEmptyCards_: function() { | |
| 164 var historyCards = this.historyDataByDay_; | |
| 165 for (var i = historyCards.length - 1; i >= 0; i--) { | |
| 166 if (historyCards[i].historyItems.length == 0) { | |
| 167 this.splice('historyDataByDay_', i, 1); | |
| 168 } | |
| 169 } | |
| 170 }, | |
| 171 | |
| 172 /** | |
| 173 * Adds the given items into historyDataByDay_. Adds items to the last | |
| 174 * existing day if the date matches, creates a new element otherwise. | |
| 175 * @param {string} date The date of the history items. | |
| 176 * @param {!Array<!HistoryEntry>} historyItems The list of history items for | |
| 177 * the current date. | |
| 178 * @private | |
| 179 */ | |
| 180 appendHistoryData_: function(date, historyItems) { | |
| 181 var lastDay = this.historyDataByDay_.length - 1; | |
| 182 if (lastDay > 0 && date == this.historyDataByDay_[lastDay].date) { | |
| 183 this.set('historyDataByDay_.' + lastDay + '.historyItems', | |
| 184 this.historyDataByDay_[lastDay].historyItems.concat(historyItems)); | |
| 185 } else { | |
| 186 this.push('historyDataByDay_', { | |
| 187 date: date, | |
| 188 historyItems: historyItems | |
| 189 }); | |
| 190 } | |
| 191 }, | |
| 192 | |
| 193 /** | |
| 194 * Based on which items are selected, collect an array of the info required | |
| 195 * for chrome.send('removeHistory', ...). | |
| 196 * @param {number} count The number of items that are selected. | |
| 197 * @return {Array<HistoryEntry>} toBeRemoved An array of objects which contain | |
| 198 * information on which history-items should be deleted. | |
| 199 */ | |
| 200 getSelectedItems: function(count) { | |
| 201 var toBeRemoved = []; | |
| 202 for (var i = 0; i < this.historyDataByDay_.length; i++) { | |
| 203 var items = this.historyDataByDay_[i].historyItems; | |
| 204 for (var j = 0; j < items.length; j++) { | |
| 205 if (items[j].selected) { | |
| 206 | |
| 207 toBeRemoved.push({ | |
| 208 url: items[j].url, | |
| 209 timestamps: items[j].allTimestamps | |
| 210 }); | |
| 211 | |
| 212 count--; | |
| 213 if (count == 0) | |
| 214 return toBeRemoved; | |
| 215 } | |
| 216 } | |
| 217 } | |
| 218 return toBeRemoved; | |
| 219 }, | |
| 220 | |
| 221 /** | |
| 222 * Called when the card manager is scrolled. | |
| 223 * @private | |
| 224 */ | |
| 225 scrollHandler_: function() { | |
| 226 // Close overflow menu on scroll. | |
| 227 this.closeMenu(); | |
| 228 | |
| 229 // Requests the next list of results when the scrollbar is near the bottom | |
| 230 // of the window. | |
| 231 var scrollOffset = 10; | |
| 232 var scrollElem = this.$['infinite-list']; | |
| 233 | |
| 234 if (scrollElem.scrollHeight <= | |
| 235 scrollElem.scrollTop + scrollElem.clientHeight + scrollOffset) { | |
| 236 chrome.send('queryHistory', | |
| 237 ['', 0, 0, this.lastVisitedTime, RESULTS_PER_PAGE]); | |
| 238 } | |
| 239 } | |
| 240 }); | |
| OLD | NEW |