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) => { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 | 55 |
56 /** | 56 /** |
57 * Creates a single row in the engagement table. | 57 * Creates a single row in the engagement table. |
58 * @param {SiteEngagementDetails} info The info to create the row from. | 58 * @param {SiteEngagementDetails} info The info to create the row from. |
59 * @return {HTMLElement} | 59 * @return {HTMLElement} |
60 */ | 60 */ |
61 function createRow(info) { | 61 function createRow(info) { |
62 var originCell = createElementWithClassName('td', 'origin-cell'); | 62 var originCell = createElementWithClassName('td', 'origin-cell'); |
63 originCell.textContent = info.origin.url; | 63 originCell.textContent = info.origin.url; |
64 | 64 |
65 var scoreInput = createElementWithClassName('input', 'score-input'); | 65 var baseScoreInput = createElementWithClassName('input', |
66 scoreInput.addEventListener( | 66 'base-score-input'); |
67 'change', handleScoreChange.bind(undefined, info.origin)); | 67 baseScoreInput.addEventListener( |
68 scoreInput.addEventListener('focus', disableAutoupdate); | 68 'change', handleBaseScoreChange.bind(undefined, info.origin)); |
69 scoreInput.addEventListener('blur', enableAutoupdate); | 69 baseScoreInput.addEventListener('focus', disableAutoupdate); |
70 scoreInput.value = info.total_score; | 70 baseScoreInput.addEventListener('blur', enableAutoupdate); |
| 71 baseScoreInput.value = info.base_score; |
71 | 72 |
72 var scoreCell = createElementWithClassName('td', 'score-cell'); | 73 var baseScoreCell = createElementWithClassName('td', 'base-score-cell'); |
73 scoreCell.appendChild(scoreInput); | 74 baseScoreCell.appendChild(baseScoreInput); |
| 75 |
| 76 var bonusScoreCell = createElementWithClassName('td', 'bonus-score-cell'); |
| 77 bonusScoreCell.textContent = info.bonus_score; |
| 78 |
| 79 var totalScoreCell = createElementWithClassName('td', 'total-score-cell'); |
| 80 totalScoreCell.textContent = info.total_score; |
74 | 81 |
75 var engagementBar = createElementWithClassName('div', 'engagement-bar'); | 82 var engagementBar = createElementWithClassName('div', 'engagement-bar'); |
76 engagementBar.style.width = (info.total_score * 4) + 'px'; | 83 engagementBar.style.width = (info.total_score * 4) + 'px'; |
77 | 84 |
78 var engagementBarCell = | 85 var engagementBarCell = |
79 createElementWithClassName('td', 'engagement-bar-cell'); | 86 createElementWithClassName('td', 'engagement-bar-cell'); |
80 engagementBarCell.appendChild(engagementBar); | 87 engagementBarCell.appendChild(engagementBar); |
81 | 88 |
82 var row = document.createElement('tr'); | 89 var row = document.createElement('tr'); |
83 row.appendChild(originCell); | 90 row.appendChild(originCell); |
84 row.appendChild(scoreCell); | 91 row.appendChild(baseScoreCell); |
| 92 row.appendChild(bonusScoreCell); |
| 93 row.appendChild(totalScoreCell); |
85 row.appendChild(engagementBarCell); | 94 row.appendChild(engagementBarCell); |
86 | 95 |
87 // Stores correspondent engagementBarCell to change it's length on | 96 // Stores correspondent engagementBarCell to change it's length on |
88 // scoreChange event. | 97 // scoreChange event. |
89 scoreInput.barCellRef = engagementBar; | 98 baseScoreInput.barCellRef = engagementBar; |
90 return row; | 99 return row; |
91 } | 100 } |
92 | 101 |
93 function disableAutoupdate() { | 102 function disableAutoupdate() { |
94 if (updateInterval) | 103 if (updateInterval) |
95 clearInterval(updateInterval); | 104 clearInterval(updateInterval); |
96 updateInterval = null; | 105 updateInterval = null; |
97 } | 106 } |
98 | 107 |
99 function enableAutoupdate() { | 108 function enableAutoupdate() { |
100 if (updateInterval) | 109 if (updateInterval) |
101 clearInterval(updateInterval); | 110 clearInterval(updateInterval); |
102 updateInterval = setInterval(updateEngagementTable, 5000); | 111 updateInterval = setInterval(updateEngagementTable, 5000); |
103 } | 112 } |
104 | 113 |
105 /** | 114 /** |
106 * Sets the engagement score when a score input is changed. | 115 * Sets the base engagement score when a score input is changed. |
107 * Resets the length of engagement-bar-cell to match the new score. | 116 * Resets the length of engagement-bar-cell to match the new score. |
108 * Also resets the update interval. | 117 * Also resets the update interval. |
109 * @param {string} origin The origin of the engagement score to set. | 118 * @param {string} origin The origin of the engagement score to set. |
110 * @param {Event} e | 119 * @param {Event} e |
111 */ | 120 */ |
112 function handleScoreChange(origin, e) { | 121 function handleBaseScoreChange(origin, e) { |
113 var scoreInput = e.target; | 122 var baseScoreInput = e.target; |
114 uiHandler.setSiteEngagementScoreForUrl(origin, scoreInput.value); | 123 uiHandler.setSiteEngagementScoreForUrl(origin, baseScoreInput.value); |
115 scoreInput.barCellRef.style.width = (scoreInput.value * 4) + 'px'; | 124 baseScoreInput.barCellRef.style.width = (baseScoreInput.value * 4) + 'px'; |
116 scoreInput.blur(); | 125 baseScoreInput.blur(); |
117 enableAutoupdate(); | 126 enableAutoupdate(); |
118 } | 127 } |
119 | 128 |
120 /** | 129 /** |
121 * Remove all rows from the engagement table. | 130 * Remove all rows from the engagement table. |
122 */ | 131 */ |
123 function clearTable() { | 132 function clearTable() { |
124 engagementTableBody.innerHTML = ''; | 133 engagementTableBody.innerHTML = ''; |
125 } | 134 } |
126 | 135 |
(...skipping 14 matching lines...) Expand all Loading... |
141 * positive number otherwise. | 150 * positive number otherwise. |
142 */ | 151 */ |
143 function compareTableItem(sortKey, a, b) { | 152 function compareTableItem(sortKey, a, b) { |
144 var val1 = a[sortKey]; | 153 var val1 = a[sortKey]; |
145 var val2 = b[sortKey]; | 154 var val2 = b[sortKey]; |
146 | 155 |
147 // Compare the hosts of the origin ignoring schemes. | 156 // Compare the hosts of the origin ignoring schemes. |
148 if (sortKey == 'origin') | 157 if (sortKey == 'origin') |
149 return new URL(val1.url).host > new URL(val2.url).host ? 1 : -1; | 158 return new URL(val1.url).host > new URL(val2.url).host ? 1 : -1; |
150 | 159 |
151 if (sortKey == 'total_score') | 160 if (sortKey == 'base_score' || |
| 161 sortKey == 'bonus_score' || |
| 162 sortKey == 'total_score') { |
152 return val1 - val2; | 163 return val1 - val2; |
| 164 } |
153 | 165 |
154 assertNotReached('Unsupported sort key: ' + sortKey); | 166 assertNotReached('Unsupported sort key: ' + sortKey); |
155 return 0; | 167 return 0; |
156 } | 168 } |
157 | 169 |
158 /** | 170 /** |
| 171 * Rounds the supplied value to two decimal places of accuracy. |
| 172 * @param {number} score |
| 173 * @return {number} |
| 174 */ |
| 175 function roundScore(score) { |
| 176 return Number(Math.round(score * 100) / 100); |
| 177 } |
| 178 |
| 179 /** |
159 * Regenerates the engagement table from |info|. | 180 * Regenerates the engagement table from |info|. |
160 */ | 181 */ |
161 function renderTable() { | 182 function renderTable() { |
162 clearTable(); | 183 clearTable(); |
163 sortInfo(); | 184 sortInfo(); |
164 // Round each score to 2 decimal places. | 185 |
165 info.forEach((info) => { | 186 info.forEach((info) => { |
166 info.total_score = Number(Math.round(info.total_score * 100) / 100); | 187 // Round all scores to 2 decimal places. |
| 188 info.base_score = roundScore(info.base_score); |
| 189 info.installed_bonus = roundScore(info.installed_bonus); |
| 190 info.notifications_bonus = roundScore(info.notifications_bonus); |
| 191 info.total_score = roundScore(info.total_score); |
| 192 |
| 193 // Collate the bonuses into a value for the bonus_score column. |
| 194 info.bonus_score = info.installed_bonus + info.notifications_bonus; |
| 195 |
167 engagementTableBody.appendChild(createRow(info)); | 196 engagementTableBody.appendChild(createRow(info)); |
168 }); | 197 }); |
169 | 198 |
170 resolvePageIsPopulated(); | 199 resolvePageIsPopulated(); |
171 } | 200 } |
172 | 201 |
173 /** | 202 /** |
174 * Retrieve site engagement info and render the engagement table. | 203 * Retrieve site engagement info and render the engagement table. |
175 */ | 204 */ |
176 function updateEngagementTable() { | 205 function updateEngagementTable() { |
177 // Populate engagement table. | 206 // Populate engagement table. |
178 uiHandler.getSiteEngagementDetails().then((response) => { | 207 uiHandler.getSiteEngagementDetails().then((response) => { |
179 info = response.info; | 208 info = response.info; |
180 renderTable(info); | 209 renderTable(info); |
181 }); | 210 }); |
182 }; | 211 }; |
183 | 212 |
184 updateEngagementTable(); | 213 updateEngagementTable(); |
185 enableAutoupdate(); | 214 enableAutoupdate(); |
186 }; | 215 }; |
187 }); | 216 }); |
OLD | NEW |