OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 /** |
| 6 * @typedef {{domain: string, |
| 7 * visits: !Array<HistoryEntry>, |
| 8 * rendered: boolean, |
| 9 * expanded: boolean}} |
| 10 */ |
| 11 var HistoryDomain; |
| 12 |
| 13 /** |
| 14 * @typedef {{title: string, |
| 15 * domains: !Array<HistoryDomain>}} |
| 16 */ |
| 17 var HistoryGroup; |
| 18 |
| 19 // TODO(calamity): Support selection by refactoring selection out of |
| 20 // history-list and into history-app. |
| 21 Polymer({ |
| 22 is: 'history-grouped-list', |
| 23 |
| 24 properties: { |
| 25 // An array of history entries in reverse chronological order. |
| 26 historyData: { |
| 27 type: Array, |
| 28 }, |
| 29 |
| 30 /** |
| 31 * @type {Array<HistoryGroup>} |
| 32 */ |
| 33 groupedHistoryData_: { |
| 34 type: Array, |
| 35 }, |
| 36 |
| 37 searchedTerm: { |
| 38 type: String, |
| 39 value: '' |
| 40 }, |
| 41 |
| 42 range: { |
| 43 type: Number, |
| 44 }, |
| 45 |
| 46 queryStartTime: String, |
| 47 queryEndTime: String, |
| 48 }, |
| 49 |
| 50 observers: [ |
| 51 'updateGroupedHistoryData_(range, historyData)' |
| 52 ], |
| 53 |
| 54 /** |
| 55 * Make a list of domains from visits. |
| 56 * @param {!Array<!HistoryEntry>} visits |
| 57 * @return {!Array<!HistoryDomain>} |
| 58 */ |
| 59 createHistoryDomains_: function(visits) { |
| 60 var domainIndexes = {}; |
| 61 var domains = []; |
| 62 |
| 63 // Group the visits into a dictionary and generate a list of domains. |
| 64 for (var i = 0, visit; visit = visits[i]; i++) { |
| 65 var domain = visit.domain; |
| 66 if (domainIndexes[domain] == undefined) { |
| 67 domainIndexes[domain] = domains.length; |
| 68 domains.push({ |
| 69 domain: domain, |
| 70 visits: [], |
| 71 expanded: false, |
| 72 rendered: false, |
| 73 }); |
| 74 } |
| 75 domains[domainIndexes[domain]].visits.push(visit); |
| 76 } |
| 77 var sortByVisits = function(a, b) { |
| 78 return b.visits.length - a.visits.length; |
| 79 }; |
| 80 domains.sort(sortByVisits); |
| 81 |
| 82 return domains; |
| 83 }, |
| 84 |
| 85 updateGroupedHistoryData_: function() { |
| 86 if (this.historyData.length == 0) { |
| 87 this.groupedHistoryData_ = []; |
| 88 return; |
| 89 } |
| 90 |
| 91 if (this.range == HistoryRange.WEEK) { |
| 92 // Group each day into a list of results. |
| 93 var days = []; |
| 94 var currentDayVisits = [this.historyData[0]]; |
| 95 |
| 96 var pushCurrentDay = function() { |
| 97 days.push({ |
| 98 title: this.searchedTerm ? currentDayVisits[0].dateShort : |
| 99 currentDayVisits[0].dateRelativeDay, |
| 100 domains: this.createHistoryDomains_(currentDayVisits), |
| 101 }); |
| 102 }.bind(this); |
| 103 |
| 104 var visitsSameDay = function(a, b) { |
| 105 if (this.searchedTerm) |
| 106 return a.dateShort == b.dateShort; |
| 107 |
| 108 return a.dateRelativeDay == b.dateRelativeDay; |
| 109 }.bind(this); |
| 110 |
| 111 for (var i = 1; i < this.historyData.length; i++) { |
| 112 var visit = this.historyData[i]; |
| 113 if (!visitsSameDay(visit, currentDayVisits[0])) { |
| 114 pushCurrentDay(); |
| 115 currentDayVisits = []; |
| 116 } |
| 117 currentDayVisits.push(visit); |
| 118 } |
| 119 pushCurrentDay(); |
| 120 |
| 121 this.groupedHistoryData_ = days; |
| 122 } else if (this.range == HistoryRange.MONTH) { |
| 123 // Group each all visits into a single list. |
| 124 this.groupedHistoryData_ = [{ |
| 125 title: this.queryStartTime + ' – ' + this.queryEndTime, |
| 126 domains: this.createHistoryDomains_(this.historyData) |
| 127 }]; |
| 128 } |
| 129 }, |
| 130 |
| 131 /** |
| 132 * @param {{model:Object, currentTarget:IronCollapseElement}} e |
| 133 */ |
| 134 toggleDomainExpanded_: function(e) { |
| 135 var collapse = e.currentTarget.parentNode.querySelector('iron-collapse'); |
| 136 e.model.set('domain.rendered', true); |
| 137 |
| 138 // Give the history-items time to render. |
| 139 setTimeout(function() { collapse.toggle() }, 0); |
| 140 }, |
| 141 |
| 142 /** |
| 143 * Check whether the time difference between the given history item and the |
| 144 * next one is large enough for a spacer to be required. |
| 145 * @param {number} groupIndex |
| 146 * @param {number} domainIndex |
| 147 * @param {number} itemIndex |
| 148 * @return {boolean} Whether or not time gap separator is required. |
| 149 * @private |
| 150 */ |
| 151 needsTimeGap_: function(groupIndex, domainIndex, itemIndex) { |
| 152 var visits = |
| 153 this.groupedHistoryData_[groupIndex].domains[domainIndex].visits; |
| 154 |
| 155 return md_history.HistoryItem.needsTimeGap( |
| 156 visits, itemIndex, this.searchedTerm); |
| 157 }, |
| 158 |
| 159 hasResults_: function(historyDataLength) { |
| 160 return historyDataLength > 0; |
| 161 }, |
| 162 |
| 163 getWebsiteIconStyle_: function(domain) { |
| 164 return 'background-image: ' + |
| 165 cr.icon.getFaviconImageSet(domain.visits[0].url); |
| 166 }, |
| 167 |
| 168 getDropdownIcon_: function(expanded) { |
| 169 return expanded ? 'cr:expand-less' : 'cr:expand-more'; |
| 170 }, |
| 171 |
| 172 noResultsMessage_: function(searchedTerm) { |
| 173 var messageId = searchedTerm !== '' ? 'noSearchResults' : 'noResults'; |
| 174 return loadTimeData.getString(messageId); |
| 175 }, |
| 176 }); |
OLD | NEW |