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', 'score-input'); |
calamity
2017/04/11 01:16:56
Change this class name to base-score-input.
Wez
2017/04/12 23:49:48
Done.
| |
66 scoreInput.addEventListener( | 66 baseScoreInput.addEventListener( |
67 'change', handleScoreChange.bind(undefined, info.origin)); | 67 'change', handleBaseScoreChange.bind(undefined, info.origin)); |
68 scoreInput.addEventListener('focus', disableAutoupdate); | 68 baseScoreInput.addEventListener('focus', disableAutoupdate); |
69 scoreInput.addEventListener('blur', enableAutoupdate); | 69 baseScoreInput.addEventListener('blur', enableAutoupdate); |
70 scoreInput.value = info.total_score; | 70 baseScoreInput.value = info.base_score; |
71 | 71 |
72 var scoreCell = createElementWithClassName('td', 'score-cell'); | 72 var baseScoreCell = createElementWithClassName('td', 'base-score-cell'); |
73 scoreCell.appendChild(scoreInput); | 73 baseScoreCell.appendChild(baseScoreInput); |
74 | |
75 var bonusScoreCell = createElementWithClassName('td', 'bonus-score-cell'); | |
76 bonusScoreCell.textContent = | |
77 info.installed_bonus + info.notifications_bonus; | |
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') || |
calamity
2017/04/11 01:16:56
nit: no parens around each condition.
Wez
2017/04/12 23:49:48
Done.
| |
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 /** |
159 * Regenerates the engagement table from |info|. | 171 * Regenerates the engagement table from |info|. |
160 */ | 172 */ |
161 function renderTable() { | 173 function renderTable() { |
162 clearTable(); | 174 clearTable(); |
163 sortInfo(); | 175 sortInfo(); |
164 // Round each score to 2 decimal places. | 176 |
165 info.forEach((info) => { | 177 info.forEach((info) => { |
178 // Round all scores to 2 decimal places. | |
179 info.base_score = Number(Math.round(info.base_score * 100) / 100); | |
180 info.bonus_score = Number(Math.round(info.bonus_score * 100) / 100); | |
calamity
2017/04/11 01:16:56
This key doesn't exist? Only info.installed_bonus
Wez
2017/04/12 23:49:48
Yup; didn't spot that because of course these neve
| |
166 info.total_score = Number(Math.round(info.total_score * 100) / 100); | 181 info.total_score = Number(Math.round(info.total_score * 100) / 100); |
calamity
2017/04/11 01:16:56
nit: Pull this rounding into a function roundScore
Wez
2017/04/12 23:49:48
Done.
| |
182 | |
167 engagementTableBody.appendChild(createRow(info)); | 183 engagementTableBody.appendChild(createRow(info)); |
168 }); | 184 }); |
169 | 185 |
170 resolvePageIsPopulated(); | 186 resolvePageIsPopulated(); |
171 } | 187 } |
172 | 188 |
173 /** | 189 /** |
174 * Retrieve site engagement info and render the engagement table. | 190 * Retrieve site engagement info and render the engagement table. |
175 */ | 191 */ |
176 function updateEngagementTable() { | 192 function updateEngagementTable() { |
177 // Populate engagement table. | 193 // Populate engagement table. |
178 uiHandler.getSiteEngagementDetails().then((response) => { | 194 uiHandler.getSiteEngagementDetails().then((response) => { |
179 info = response.info; | 195 info = response.info; |
180 renderTable(info); | 196 renderTable(info); |
181 }); | 197 }); |
182 }; | 198 }; |
183 | 199 |
184 updateEngagementTable(); | 200 updateEngagementTable(); |
185 enableAutoupdate(); | 201 enableAutoupdate(); |
186 }; | 202 }; |
187 }); | 203 }); |
OLD | NEW |