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 'use strict'; | 5 'use strict'; |
6 | 6 |
7 // Allow a function to be provided by tests, which will be called when | 7 // Allow a function to be provided by tests, which will be called when |
8 // the page has been populated with site engagement details. | 8 // the page has been populated with site engagement details. |
9 var resolvePageIsPopulated = null; | 9 var resolvePageIsPopulated = null; |
10 var pageIsPopulatedPromise = new Promise((resolve, reject) => { | 10 var pageIsPopulatedPromise = new Promise((resolve, reject) => { |
11 resolvePageIsPopulated = resolve; | 11 resolvePageIsPopulated = resolve; |
12 }); | 12 }); |
13 | 13 |
14 function whenPageIsPopulatedForTest() { | 14 function whenPageIsPopulatedForTest() { |
15 return pageIsPopulatedPromise; | 15 return pageIsPopulatedPromise; |
16 } | 16 } |
17 | 17 |
18 define('main', [ | 18 define( |
19 'chrome/browser/engagement/site_engagement_details.mojom', | 19 'main', |
20 'content/public/renderer/frame_interfaces', | 20 [ |
21 ], (siteEngagementMojom, frameInterfaces) => { | 21 'chrome/browser/engagement/site_engagement_details.mojom', |
22 return () => { | 22 'content/public/renderer/frame_interfaces', |
23 var uiHandler = new siteEngagementMojom.SiteEngagementDetailsProviderPtr( | 23 ], |
24 frameInterfaces.getInterface( | 24 (siteEngagementMojom, frameInterfaces) => { |
25 siteEngagementMojom.SiteEngagementDetailsProvider.name)); | 25 return () => { |
26 | 26 var uiHandler = |
27 var engagementTableBody = $('engagement-table-body'); | 27 new siteEngagementMojom.SiteEngagementDetailsProviderPtr( |
28 var updateInterval = null; | 28 frameInterfaces.getInterface( |
29 var info = null; | 29 siteEngagementMojom.SiteEngagementDetailsProvider.name)); |
30 var sortKey = 'total_score'; | 30 |
31 var sortReverse = true; | 31 var engagementTableBody = $('engagement-table-body'); |
32 | 32 var updateInterval = null; |
33 // Set table header sort handlers. | 33 var info = null; |
34 var engagementTableHeader = $('engagement-table-header'); | 34 var sortKey = 'total_score'; |
35 var headers = engagementTableHeader.children; | 35 var sortReverse = true; |
36 for (var i = 0; i < headers.length; i++) { | 36 |
37 headers[i].addEventListener('click', (e) => { | 37 // Set table header sort handlers. |
38 var newSortKey = e.target.getAttribute('sort-key'); | 38 var engagementTableHeader = $('engagement-table-header'); |
39 if (sortKey == newSortKey) { | 39 var headers = engagementTableHeader.children; |
40 sortReverse = !sortReverse; | 40 for (var i = 0; i < headers.length; i++) { |
41 } else { | 41 headers[i].addEventListener('click', (e) => { |
42 sortKey = newSortKey; | 42 var newSortKey = e.target.getAttribute('sort-key'); |
43 sortReverse = false; | 43 if (sortKey == newSortKey) { |
44 } | 44 sortReverse = !sortReverse; |
45 var oldSortColumn = document.querySelector('.sort-column'); | 45 } else { |
46 oldSortColumn.classList.remove('sort-column'); | 46 sortKey = newSortKey; |
47 e.target.classList.add('sort-column'); | 47 sortReverse = false; |
48 if (sortReverse) | 48 } |
49 e.target.setAttribute('sort-reverse', ''); | 49 var oldSortColumn = document.querySelector('.sort-column'); |
50 else | 50 oldSortColumn.classList.remove('sort-column'); |
51 e.target.removeAttribute('sort-reverse'); | 51 e.target.classList.add('sort-column'); |
52 renderTable(); | 52 if (sortReverse) |
53 }); | 53 e.target.setAttribute('sort-reverse', ''); |
54 } | 54 else |
55 | 55 e.target.removeAttribute('sort-reverse'); |
56 /** | 56 renderTable(); |
57 * Creates a single row in the engagement table. | 57 }); |
58 * @param {SiteEngagementDetails} info The info to create the row from. | 58 } |
59 * @return {HTMLElement} | 59 |
60 */ | 60 /** |
61 function createRow(info) { | 61 * Creates a single row in the engagement table. |
62 var originCell = createElementWithClassName('td', 'origin-cell'); | 62 * @param {SiteEngagementDetails} info The info to create the row from. |
63 originCell.textContent = info.origin.url; | 63 * @return {HTMLElement} |
64 | 64 */ |
65 var baseScoreInput = createElementWithClassName('input', | 65 function createRow(info) { |
66 'base-score-input'); | 66 var originCell = createElementWithClassName('td', 'origin-cell'); |
67 baseScoreInput.addEventListener( | 67 originCell.textContent = info.origin.url; |
68 'change', handleBaseScoreChange.bind(undefined, info.origin)); | 68 |
69 baseScoreInput.addEventListener('focus', disableAutoupdate); | 69 var baseScoreInput = |
70 baseScoreInput.addEventListener('blur', enableAutoupdate); | 70 createElementWithClassName('input', 'base-score-input'); |
71 baseScoreInput.value = info.base_score; | 71 baseScoreInput.addEventListener( |
72 | 72 'change', handleBaseScoreChange.bind(undefined, info.origin)); |
73 var baseScoreCell = createElementWithClassName('td', 'base-score-cell'); | 73 baseScoreInput.addEventListener('focus', disableAutoupdate); |
74 baseScoreCell.appendChild(baseScoreInput); | 74 baseScoreInput.addEventListener('blur', enableAutoupdate); |
75 | 75 baseScoreInput.value = info.base_score; |
76 var bonusScoreCell = createElementWithClassName('td', 'bonus-score-cell'); | 76 |
77 bonusScoreCell.textContent = info.bonus_score; | 77 var baseScoreCell = |
78 | 78 createElementWithClassName('td', 'base-score-cell'); |
79 var totalScoreCell = createElementWithClassName('td', 'total-score-cell'); | 79 baseScoreCell.appendChild(baseScoreInput); |
80 totalScoreCell.textContent = info.total_score; | 80 |
81 | 81 var bonusScoreCell = |
82 var engagementBar = createElementWithClassName('div', 'engagement-bar'); | 82 createElementWithClassName('td', 'bonus-score-cell'); |
83 engagementBar.style.width = (info.total_score * 4) + 'px'; | 83 bonusScoreCell.textContent = info.bonus_score; |
84 | 84 |
85 var engagementBarCell = | 85 var totalScoreCell = |
86 createElementWithClassName('td', 'engagement-bar-cell'); | 86 createElementWithClassName('td', 'total-score-cell'); |
87 engagementBarCell.appendChild(engagementBar); | 87 totalScoreCell.textContent = info.total_score; |
88 | 88 |
89 var row = document.createElement('tr'); | 89 var engagementBar = |
90 row.appendChild(originCell); | 90 createElementWithClassName('div', 'engagement-bar'); |
91 row.appendChild(baseScoreCell); | 91 engagementBar.style.width = (info.total_score * 4) + 'px'; |
92 row.appendChild(bonusScoreCell); | 92 |
93 row.appendChild(totalScoreCell); | 93 var engagementBarCell = |
94 row.appendChild(engagementBarCell); | 94 createElementWithClassName('td', 'engagement-bar-cell'); |
95 | 95 engagementBarCell.appendChild(engagementBar); |
96 // Stores correspondent engagementBarCell to change it's length on | 96 |
97 // scoreChange event. | 97 var row = document.createElement('tr'); |
98 baseScoreInput.barCellRef = engagementBar; | 98 row.appendChild(originCell); |
99 return row; | 99 row.appendChild(baseScoreCell); |
100 } | 100 row.appendChild(bonusScoreCell); |
101 | 101 row.appendChild(totalScoreCell); |
102 function disableAutoupdate() { | 102 row.appendChild(engagementBarCell); |
103 if (updateInterval) | 103 |
104 clearInterval(updateInterval); | 104 // Stores correspondent engagementBarCell to change it's length on |
105 updateInterval = null; | 105 // scoreChange event. |
106 } | 106 baseScoreInput.barCellRef = engagementBar; |
107 | 107 return row; |
108 function enableAutoupdate() { | 108 } |
109 if (updateInterval) | 109 |
110 clearInterval(updateInterval); | 110 function disableAutoupdate() { |
111 updateInterval = setInterval(updateEngagementTable, 5000); | 111 if (updateInterval) |
112 } | 112 clearInterval(updateInterval); |
113 | 113 updateInterval = null; |
114 /** | 114 } |
115 * Sets the base engagement score when a score input is changed. | 115 |
116 * Resets the length of engagement-bar-cell to match the new score. | 116 function enableAutoupdate() { |
117 * Also resets the update interval. | 117 if (updateInterval) |
118 * @param {string} origin The origin of the engagement score to set. | 118 clearInterval(updateInterval); |
119 * @param {Event} e | 119 updateInterval = setInterval(updateEngagementTable, 5000); |
120 */ | 120 } |
121 function handleBaseScoreChange(origin, e) { | 121 |
122 var baseScoreInput = e.target; | 122 /** |
123 uiHandler.setSiteEngagementBaseScoreForUrl(origin, baseScoreInput.value); | 123 * Sets the base engagement score when a score input is changed. |
124 baseScoreInput.barCellRef.style.width = (baseScoreInput.value * 4) + 'px'; | 124 * Resets the length of engagement-bar-cell to match the new score. |
125 baseScoreInput.blur(); | 125 * Also resets the update interval. |
126 enableAutoupdate(); | 126 * @param {string} origin The origin of the engagement score to set. |
127 } | 127 * @param {Event} e |
128 | 128 */ |
129 /** | 129 function handleBaseScoreChange(origin, e) { |
130 * Remove all rows from the engagement table. | 130 var baseScoreInput = e.target; |
131 */ | 131 uiHandler.setSiteEngagementBaseScoreForUrl( |
132 function clearTable() { | 132 origin, baseScoreInput.value); |
133 engagementTableBody.innerHTML = ''; | 133 baseScoreInput.barCellRef.style.width = |
134 } | 134 (baseScoreInput.value * 4) + 'px'; |
135 | 135 baseScoreInput.blur(); |
136 /** | 136 enableAutoupdate(); |
137 * Sort the engagement info based on |sortKey| and |sortReverse|. | 137 } |
138 */ | 138 |
139 function sortInfo() { | 139 /** |
140 info.sort((a, b) => { | 140 * Remove all rows from the engagement table. |
141 return (sortReverse ? -1 : 1) * | 141 */ |
142 compareTableItem(sortKey, a, b); | 142 function clearTable() { |
143 }); | 143 engagementTableBody.innerHTML = ''; |
144 } | 144 } |
145 | 145 |
146 /** | 146 /** |
147 * Compares two SiteEngagementDetails objects based on |sortKey|. | 147 * Sort the engagement info based on |sortKey| and |sortReverse|. |
148 * @param {string} sortKey The name of the property to sort by. | 148 */ |
149 * @return {number} A negative number if |a| should be ordered before |b|, a | 149 function sortInfo() { |
150 * positive number otherwise. | 150 info.sort((a, b) => { |
151 */ | 151 return (sortReverse ? -1 : 1) * compareTableItem(sortKey, a, b); |
152 function compareTableItem(sortKey, a, b) { | 152 }); |
153 var val1 = a[sortKey]; | 153 } |
154 var val2 = b[sortKey]; | 154 |
155 | 155 /** |
156 // Compare the hosts of the origin ignoring schemes. | 156 * Compares two SiteEngagementDetails objects based on |sortKey|. |
157 if (sortKey == 'origin') | 157 * @param {string} sortKey The name of the property to sort by. |
158 return new URL(val1.url).host > new URL(val2.url).host ? 1 : -1; | 158 * @return {number} A negative number if |a| should be ordered before |b
|, a |
159 | 159 * positive number otherwise. |
160 if (sortKey == 'base_score' || | 160 */ |
161 sortKey == 'bonus_score' || | 161 function compareTableItem(sortKey, a, b) { |
162 sortKey == 'total_score') { | 162 var val1 = a[sortKey]; |
163 return val1 - val2; | 163 var val2 = b[sortKey]; |
164 } | 164 |
165 | 165 // Compare the hosts of the origin ignoring schemes. |
166 assertNotReached('Unsupported sort key: ' + sortKey); | 166 if (sortKey == 'origin') |
167 return 0; | 167 return new URL(val1.url).host > new URL(val2.url).host ? 1 : -1; |
168 } | 168 |
169 | 169 if (sortKey == 'base_score' || sortKey == 'bonus_score' || |
170 /** | 170 sortKey == 'total_score') { |
171 * Rounds the supplied value to two decimal places of accuracy. | 171 return val1 - val2; |
172 * @param {number} score | 172 } |
173 * @return {number} | 173 |
174 */ | 174 assertNotReached('Unsupported sort key: ' + sortKey); |
175 function roundScore(score) { | 175 return 0; |
176 return Number(Math.round(score * 100) / 100); | 176 } |
177 } | 177 |
178 | 178 /** |
179 /** | 179 * Rounds the supplied value to two decimal places of accuracy. |
180 * Regenerates the engagement table from |info|. | 180 * @param {number} score |
181 */ | 181 * @return {number} |
182 function renderTable() { | 182 */ |
183 clearTable(); | 183 function roundScore(score) { |
184 sortInfo(); | 184 return Number(Math.round(score * 100) / 100); |
185 | 185 } |
186 info.forEach((info) => { | 186 |
187 // Round all scores to 2 decimal places. | 187 /** |
188 info.base_score = roundScore(info.base_score); | 188 * Regenerates the engagement table from |info|. |
189 info.installed_bonus = roundScore(info.installed_bonus); | 189 */ |
190 info.notifications_bonus = roundScore(info.notifications_bonus); | 190 function renderTable() { |
191 info.total_score = roundScore(info.total_score); | 191 clearTable(); |
192 | 192 sortInfo(); |
193 // Collate the bonuses into a value for the bonus_score column. | 193 |
194 info.bonus_score = info.installed_bonus + info.notifications_bonus; | 194 info.forEach((info) => { |
195 | 195 // Round all scores to 2 decimal places. |
196 engagementTableBody.appendChild(createRow(info)); | 196 info.base_score = roundScore(info.base_score); |
197 }); | 197 info.installed_bonus = roundScore(info.installed_bonus); |
198 } | 198 info.notifications_bonus = roundScore(info.notifications_bonus); |
199 | 199 info.total_score = roundScore(info.total_score); |
200 /** | 200 |
201 * Retrieve site engagement info and render the engagement table. | 201 // Collate the bonuses into a value for the bonus_score column. |
202 */ | 202 info.bonus_score = info.installed_bonus + info.notifications_bonus; |
203 function updateEngagementTable() { | 203 |
204 // Populate engagement table. | 204 engagementTableBody.appendChild(createRow(info)); |
205 uiHandler.getSiteEngagementDetails().then((response) => { | 205 }); |
206 info = response.info; | 206 } |
207 renderTable(info); | 207 |
208 resolvePageIsPopulated(); | 208 /** |
209 }); | 209 * Retrieve site engagement info and render the engagement table. |
210 } | 210 */ |
211 | 211 function updateEngagementTable() { |
212 updateEngagementTable(); | 212 // Populate engagement table. |
213 enableAutoupdate(); | 213 uiHandler.getSiteEngagementDetails().then((response) => { |
214 }; | 214 info = response.info; |
215 }); | 215 renderTable(info); |
| 216 resolvePageIsPopulated(); |
| 217 }); |
| 218 } |
| 219 |
| 220 updateEngagementTable(); |
| 221 enableAutoupdate(); |
| 222 }; |
| 223 }); |
OLD | NEW |