| 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-card-manager', | 6 is: 'history-card-manager', |
| 7 | 7 |
| 8 properties: { | 8 properties: { |
| 9 // An array of objects sorted in reverse chronological order. | 9 // An array of objects sorted in reverse chronological order. |
| 10 // Each object has a date and the history items belonging to that date. | 10 // Each object has a date and the history items belonging to that date. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 | 21 |
| 22 menuOpen: { | 22 menuOpen: { |
| 23 type: Boolean, | 23 type: Boolean, |
| 24 value: false, | 24 value: false, |
| 25 reflectToAttribute: true | 25 reflectToAttribute: true |
| 26 }, | 26 }, |
| 27 | 27 |
| 28 menuIdentifier: { | 28 menuIdentifier: { |
| 29 type: Number, | 29 type: Number, |
| 30 value: 0 | 30 value: 0 |
| 31 }, |
| 32 |
| 33 searchTerm: { |
| 34 type: String, |
| 35 value: '' |
| 36 }, |
| 37 |
| 38 // Page waits until results have finished loading before displaying message. |
| 39 loading_: { |
| 40 type: Boolean, |
| 41 value: false |
| 31 } | 42 } |
| 32 }, | 43 }, |
| 33 | 44 |
| 34 /** @const @private */ | 45 /** @const @private */ |
| 35 X_OFFSET_: 30, | 46 X_OFFSET_: 30, |
| 36 | 47 |
| 37 listeners: { | 48 listeners: { |
| 38 'tap': 'closeMenu', | 49 'tap': 'closeMenu', |
| 39 'toggle-menu': 'toggleMenu_' | 50 'toggle-menu': 'toggleMenu_' |
| 40 }, | 51 }, |
| 41 | 52 |
| 42 /** | 53 /** |
| 43 * Closes the overflow menu. | 54 * Closes the overflow menu. |
| 44 */ | 55 */ |
| 45 closeMenu: function() { | 56 closeMenu: function() { |
| 46 this.menuOpen = false; | 57 this.menuOpen = false; |
| 47 }, | 58 }, |
| 48 | 59 |
| 49 /** | 60 /** |
| 61 * Clear the page completely so the page can display new results (search). |
| 62 */ |
| 63 resetHistoryResults: function() { |
| 64 // TODO(hsampson): make it so the old results aren't replaced immediately, |
| 65 // only replaced when new results come in. |
| 66 this.loading_ = true; |
| 67 this.splice('historyDataByDay_', 0, this.historyDataByDay_.length); |
| 68 }, |
| 69 |
| 70 /** |
| 50 * Opens the overflow menu unless the menu is already open and the same button | 71 * Opens the overflow menu unless the menu is already open and the same button |
| 51 * is pressed. | 72 * is pressed. |
| 52 * @param {Event} e The event with details of the menu item that was clicked. | 73 * @param {Event} e The event with details of the menu item that was clicked. |
| 53 * @private | 74 * @private |
| 54 */ | 75 */ |
| 55 toggleMenu_: function(e) { | 76 toggleMenu_: function(e) { |
| 56 var menu = this.$['overflow-menu']; | 77 var menu = this.$['overflow-menu']; |
| 57 | 78 |
| 58 // Menu closes if the same button is clicked. | 79 // Menu closes if the same button is clicked. |
| 59 if (this.menuOpen && this.menuIdentifier == e.detail.accessTime) { | 80 if (this.menuOpen && this.menuIdentifier == e.detail.accessTime) { |
| 60 this.closeMenu(); | 81 this.closeMenu(); |
| 61 } else { | 82 } else { |
| 62 this.menuOpen = true; | 83 this.menuOpen = true; |
| 63 this.menuIdentifier = e.detail.accessTime; | 84 this.menuIdentifier = e.detail.accessTime; |
| 64 | 85 |
| 65 cr.ui.positionPopupAtPoint(e.detail.x + this.X_OFFSET_, e.detail.y, menu, | 86 cr.ui.positionPopupAtPoint(e.detail.x + this.X_OFFSET_, e.detail.y, menu, |
| 66 cr.ui.AnchorType.BEFORE); | 87 cr.ui.AnchorType.BEFORE); |
| 67 | 88 |
| 68 menu.focus(); | 89 menu.focus(); |
| 69 } | 90 } |
| 70 }, | 91 }, |
| 71 | 92 |
| 72 /** | 93 /** |
| 73 * Split the newly updated history results into history items sorted via day | 94 * Split the newly updated history results into history items sorted via day |
| 74 * accessed. | 95 * accessed. |
| 75 * @param {!Array<!HistoryEntry>} results The new history results. | 96 * @param {!Array<!HistoryEntry>} results The new history results. |
| 97 * @param {string} search The search term used for chrome.send. |
| 76 */ | 98 */ |
| 77 addNewResults: function(results) { | 99 addNewResults: function(results, search) { |
| 78 if (results.length == 0) | 100 this.searchTerm = search; |
| 101 this.noResults_(); |
| 102 |
| 103 if (results.length == 0) { |
| 104 this.loading_ = false; |
| 79 return; | 105 return; |
| 106 } |
| 80 | 107 |
| 81 var dateSortedData = []; | 108 var dateSortedData = []; |
| 82 var historyItems = []; | 109 var historyItems = []; |
| 83 var currentDate = results[0].dateRelativeDay; | 110 var currentCardTitle = search == '' ? results[0].dateRelativeDay : search; |
| 84 | 111 |
| 85 for (var i = 0; i < results.length; i++) { | 112 for (var i = 0; i < results.length; i++) { |
| 86 if (!currentDate) | 113 if (!currentCardTitle) |
| 87 continue; | 114 continue; |
| 88 | 115 |
| 116 results[i].cardTitle = search == '' ? results[i].dateRelativeDay : search; |
| 117 results[i].visibleTimestamp = |
| 118 search == '' ? results[i].dateTimeOfDay : results[i].dateShort; |
| 89 results[i].selected = false; | 119 results[i].selected = false; |
| 90 if (results[i].dateRelativeDay != currentDate) { | 120 |
| 91 this.appendHistoryData_(currentDate, historyItems); | 121 if (results[i].cardTitle != currentCardTitle) { |
| 92 currentDate = results[i].dateRelativeDay; | 122 this.appendHistoryData_(currentCardTitle, historyItems); |
| 123 currentCardTitle = results[i].cardTitle; |
| 93 historyItems = []; | 124 historyItems = []; |
| 94 } | 125 } |
| 95 historyItems.push(results[i]); | 126 historyItems.push(results[i]); |
| 96 } | 127 } |
| 97 | 128 |
| 98 if (currentDate) | 129 if (currentCardTitle) |
| 99 this.appendHistoryData_(currentDate, historyItems); | 130 this.appendHistoryData_(currentCardTitle, historyItems); |
| 100 | 131 |
| 101 this.lastVisitedTime = historyItems[historyItems.length - 1].time; | 132 this.lastVisitedTime = historyItems[historyItems.length - 1].time; |
| 133 this.loading_ = false; |
| 134 Polymer.dom.flush(); |
| 102 }, | 135 }, |
| 103 | 136 |
| 104 /** | 137 /** |
| 105 * Cycle through each entry in historyDataByDay_ and set all items to be | 138 * Cycle through each entry in historyDataByDay_ and set all items to be |
| 106 * unselected. | 139 * unselected. |
| 140 * @param {number} overallItemCount The number of selected items. |
| 107 */ | 141 */ |
| 108 unselectAllItems: function(overallItemCount) { | 142 unselectAllItems: function(overallItemCount) { |
| 109 var historyCardData = this.historyDataByDay_; | 143 var historyCardData = this.historyDataByDay_; |
| 110 | 144 |
| 111 for (var i = 0; i < historyCardData.length; i++) { | 145 for (var i = 0; i < historyCardData.length; i++) { |
| 112 var items = historyCardData[i].historyItems; | 146 var items = historyCardData[i].historyItems; |
| 113 for (var j = 0; j < items.length; j++) { | 147 for (var j = 0; j < items.length; j++) { |
| 114 if (items[j].selected) { | 148 if (items[j].selected) { |
| 115 this.set('historyDataByDay_.' + i + '.historyItems.' + j + | 149 this.set('historyDataByDay_.' + i + '.historyItems.' + j + |
| 116 '.selected', false); | 150 '.selected', false); |
| 117 overallItemCount--; | 151 overallItemCount--; |
| 118 if (overallItemCount == 0) | 152 if (overallItemCount == 0) |
| 119 break; | 153 break; |
| 120 } | 154 } |
| 121 } | 155 } |
| 122 } | 156 } |
| 123 }, | 157 }, |
| 124 | 158 |
| 125 /** | 159 /** |
| 126 * Adds the given items into historyDataByDay_. Adds items to the last | 160 * Adds the given items into historyDataByDay_. Adds items to the last |
| 127 * existing day if the date matches, creates a new element otherwise. | 161 * existing day if the cardTitle matches, creates a new element otherwise. |
| 128 * @param {string} date The date of the history items. | 162 * @param {string} cardTitle The cardTitle for this list of history items. |
| 129 * @param {!Array<!HistoryEntry>} historyItems The list of history items for | 163 * @param {!Array<!HistoryEntry>} historyItems The list of history items for |
| 130 * the current date. | 164 * the current card. |
| 131 * @private | 165 * @private |
| 132 */ | 166 */ |
| 133 appendHistoryData_: function(date, historyItems) { | 167 appendHistoryData_: function(cardTitle, historyItems) { |
| 134 var lastDay = this.historyDataByDay_.length - 1; | 168 var lastDay = this.historyDataByDay_.length - 1; |
| 135 if (lastDay > 0 && date == this.historyDataByDay_[lastDay].date) { | 169 if (lastDay >= 0 && |
| 170 cardTitle == this.historyDataByDay_[lastDay].cardTitle) { |
| 136 this.set('historyDataByDay_.' + lastDay + '.historyItems', | 171 this.set('historyDataByDay_.' + lastDay + '.historyItems', |
| 137 this.historyDataByDay_[lastDay].historyItems.concat(historyItems)); | 172 this.historyDataByDay_[lastDay].historyItems.concat(historyItems)); |
| 138 } else { | 173 } else { |
| 139 this.push('historyDataByDay_', { | 174 this.push('historyDataByDay_', { |
| 140 date: date, | 175 cardTitle: cardTitle, |
| 141 historyItems: historyItems | 176 historyItems: historyItems |
| 142 }); | 177 }); |
| 143 } | 178 } |
| 144 }, | 179 }, |
| 145 | 180 |
| 146 /** | 181 /** |
| 147 * Called when the card manager is scrolled. | 182 * Called when the card manager is scrolled. |
| 148 * @private | 183 * @private |
| 149 */ | 184 */ |
| 150 scrollHandler_: function() { | 185 scrollHandler_: function() { |
| 151 // Close overflow menu on scroll. | 186 // Close overflow menu on scroll. |
| 152 this.closeMenu(); | 187 this.closeMenu(); |
| 153 | 188 |
| 154 // Requests the next list of results when the scrollbar is near the bottom | 189 // Requests the next list of results when the scrollbar is near the bottom |
| 155 // of the window. | 190 // of the window. |
| 156 var scrollOffset = 10; | 191 var scrollOffset = 10; |
| 157 var scrollElem = this.$['infinite-list']; | 192 var scrollElem = this.$['infinite-list']; |
| 193 if (!this.loading_ && scrollElem.scrollHeight <= |
| 194 scrollElem.scrollTop + scrollElem.clientHeight + scrollOffset) { |
| 195 chrome.send('queryHistory', |
| 196 [this.searchTerm, 0, 0, this.lastVisitedTime, RESULTS_PER_PAGE]); |
| 197 } |
| 198 }, |
| 158 | 199 |
| 159 if (scrollElem.scrollHeight <= | 200 /** |
| 160 scrollElem.scrollTop + scrollElem.clientHeight + scrollOffset) { | 201 * True if there are results available or the page hasn't finished loading. |
| 161 chrome.send('queryHistory', | 202 * @param {number} numberOfResults The length of historyDataByDay_. |
| 162 ['', 0, 0, this.lastVisitedTime, RESULTS_PER_PAGE]); | 203 * @return {boolean} Whether there are results to show. |
| 163 } | 204 * @private |
| 205 */ |
| 206 resultsAvailable_: function(numberOfResults) { |
| 207 return numberOfResults != 0 || this.loading_; |
| 208 }, |
| 209 |
| 210 /** |
| 211 * Sets the message displayed when there are no results. Displayed if there is |
| 212 * no history at all or if the search returned no results. |
| 213 * @private |
| 214 */ |
| 215 noResults_: function() { |
| 216 var messageId = this.searchTerm == '' ? 'noResults' : 'noSearchResults'; |
| 217 this.$['no-results'].textContent = loadTimeData.getString(messageId); |
| 164 } | 218 } |
| 165 }); | 219 }); |
| OLD | NEW |