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