| 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 pagePopulatedCallback = null; | 9 var pagePopulatedCallback = null; |
| 10 var isPagePopulated = false; | 10 var isPagePopulated = false; |
| 11 | 11 |
| 12 function setPagePopulatedCallbackForTest(callback) { | 12 function setPagePopulatedCallbackForTest(callback) { |
| 13 if (isPagePopulated) { | 13 if (isPagePopulated) { |
| 14 callback(); | 14 callback(); |
| 15 } else { | 15 } else { |
| 16 pagePopulatedCallback = callback; | 16 pagePopulatedCallback = callback; |
| 17 } | 17 } |
| 18 } | 18 } |
| 19 | 19 |
| 20 function notifyPagePopulated() { | 20 function notifyPagePopulated() { |
| 21 isPagePopulated = true; | 21 isPagePopulated = true; |
| 22 if (pagePopulatedCallback) { | 22 if (pagePopulatedCallback) { |
| 23 pagePopulatedCallback(); | 23 pagePopulatedCallback(); |
| 24 pagePopulatedCallback = null; | 24 pagePopulatedCallback = null; |
| 25 } | 25 } |
| 26 } | 26 } |
| 27 | 27 |
| 28 define('main', [ | 28 define('main', [ |
| 29 'chrome/browser/engagement/site_engagement.mojom', | 29 'chrome/browser/engagement/site_engagement_details.mojom', |
| 30 'content/public/renderer/frame_interfaces', | 30 'content/public/renderer/frame_interfaces', |
| 31 ], function(siteEngagementMojom, frameInterfaces) { | 31 ], function(siteEngagementMojom, frameInterfaces) { |
| 32 return function() { | 32 return function() { |
| 33 var uiHandler = new siteEngagementMojom.SiteEngagementUIHandlerPtr( | 33 var uiHandler = new siteEngagementMojom.SiteEngagementDetailsProviderPtr( |
| 34 frameInterfaces.getInterface( | 34 frameInterfaces.getInterface( |
| 35 siteEngagementMojom.SiteEngagementUIHandler.name)); | 35 siteEngagementMojom.SiteEngagementDetailsProvider.name)); |
| 36 | 36 |
| 37 var engagementTableBody = $('engagement-table-body'); | 37 var engagementTableBody = $('engagement-table-body'); |
| 38 var updateInterval = null; | 38 var updateInterval = null; |
| 39 var info = null; | 39 var info = null; |
| 40 var sortKey = 'score'; | 40 var sortKey = 'base_score'; |
| 41 var sortReverse = true; | 41 var sortReverse = true; |
| 42 | 42 |
| 43 // Set table header sort handlers. | 43 // Set table header sort handlers. |
| 44 var engagementTableHeader = $('engagement-table-header'); | 44 var engagementTableHeader = $('engagement-table-header'); |
| 45 var headers = engagementTableHeader.children; | 45 var headers = engagementTableHeader.children; |
| 46 for (var i = 0; i < headers.length; i++) { | 46 for (var i = 0; i < headers.length; i++) { |
| 47 headers[i].addEventListener('click', function(e) { | 47 headers[i].addEventListener('click', function(e) { |
| 48 var newSortKey = e.target.getAttribute('sort-key'); | 48 var newSortKey = e.target.getAttribute('sort-key'); |
| 49 if (sortKey == newSortKey) { | 49 if (sortKey == newSortKey) { |
| 50 sortReverse = !sortReverse; | 50 sortReverse = !sortReverse; |
| 51 } else { | 51 } else { |
| 52 sortKey = newSortKey; | 52 sortKey = newSortKey; |
| 53 sortReverse = false; | 53 sortReverse = false; |
| 54 } | 54 } |
| 55 var oldSortColumn = document.querySelector('.sort-column'); | 55 var oldSortColumn = document.querySelector('.sort-column'); |
| 56 oldSortColumn.classList.remove('sort-column'); | 56 oldSortColumn.classList.remove('sort-column'); |
| 57 e.target.classList.add('sort-column'); | 57 e.target.classList.add('sort-column'); |
| 58 if (sortReverse) | 58 if (sortReverse) |
| 59 e.target.setAttribute('sort-reverse', ''); | 59 e.target.setAttribute('sort-reverse', ''); |
| 60 else | 60 else |
| 61 e.target.removeAttribute('sort-reverse'); | 61 e.target.removeAttribute('sort-reverse'); |
| 62 renderTable(); | 62 renderTable(); |
| 63 }); | 63 }); |
| 64 } | 64 } |
| 65 | 65 |
| 66 /** | 66 /** |
| 67 * Creates a single row in the engagement table. | 67 * Creates a single row in the engagement table. |
| 68 * @param {SiteEngagementInfo} info The info to create the row from. | 68 * @param {SiteEngagementDetails} info The info to create the row from. |
| 69 * @return {HTMLElement} | 69 * @return {HTMLElement} |
| 70 */ | 70 */ |
| 71 function createRow(info) { | 71 function createRow(info) { |
| 72 var originCell = createElementWithClassName('td', 'origin-cell'); | 72 var originCell = createElementWithClassName('td', 'origin-cell'); |
| 73 originCell.textContent = info.origin.url; | 73 originCell.textContent = info.origin.url; |
| 74 | 74 |
| 75 var scoreInput = createElementWithClassName('input', 'score-input'); | 75 var scoreInput = createElementWithClassName('input', 'score-input'); |
| 76 scoreInput.addEventListener( | 76 scoreInput.addEventListener( |
| 77 'change', handleScoreChange.bind(undefined, info.origin)); | 77 'change', handleScoreChange.bind(undefined, info.origin)); |
| 78 scoreInput.addEventListener('focus', disableAutoupdate); | 78 scoreInput.addEventListener('focus', disableAutoupdate); |
| 79 scoreInput.addEventListener('blur', enableAutoupdate); | 79 scoreInput.addEventListener('blur', enableAutoupdate); |
| 80 scoreInput.value = info.score; | 80 scoreInput.value = info.base_score; |
| 81 | 81 |
| 82 var scoreCell = createElementWithClassName('td', 'score-cell'); | 82 var scoreCell = createElementWithClassName('td', 'score-cell'); |
| 83 scoreCell.appendChild(scoreInput); | 83 scoreCell.appendChild(scoreInput); |
| 84 | 84 |
| 85 var engagementBar = createElementWithClassName('div', 'engagement-bar'); | 85 var engagementBar = createElementWithClassName('div', 'engagement-bar'); |
| 86 engagementBar.style.width = (info.score * 4) + 'px'; | 86 engagementBar.style.width = (info.base_score * 4) + 'px'; |
| 87 | 87 |
| 88 var engagementBarCell = | 88 var engagementBarCell = |
| 89 createElementWithClassName('td', 'engagement-bar-cell'); | 89 createElementWithClassName('td', 'engagement-bar-cell'); |
| 90 engagementBarCell.appendChild(engagementBar); | 90 engagementBarCell.appendChild(engagementBar); |
| 91 | 91 |
| 92 var row = document.createElement('tr'); | 92 var row = document.createElement('tr'); |
| 93 row.appendChild(originCell); | 93 row.appendChild(originCell); |
| 94 row.appendChild(scoreCell); | 94 row.appendChild(scoreCell); |
| 95 row.appendChild(engagementBarCell); | 95 row.appendChild(engagementBarCell); |
| 96 | 96 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 * Sort the engagement info based on |sortKey| and |sortReverse|. | 138 * Sort the engagement info based on |sortKey| and |sortReverse|. |
| 139 */ | 139 */ |
| 140 function sortInfo() { | 140 function sortInfo() { |
| 141 info.sort(function(a, b) { | 141 info.sort(function(a, b) { |
| 142 return (sortReverse ? -1 : 1) * | 142 return (sortReverse ? -1 : 1) * |
| 143 compareTableItem(sortKey, a, b); | 143 compareTableItem(sortKey, a, b); |
| 144 }); | 144 }); |
| 145 } | 145 } |
| 146 | 146 |
| 147 /** | 147 /** |
| 148 * Compares two SiteEngagementInfo objects based on |sortKey|. | 148 * Compares two SiteEngagementDetails objects based on |sortKey|. |
| 149 * @param {string} sortKey The name of the property to sort by. | 149 * @param {string} sortKey The name of the property to sort by. |
| 150 * @return {number} A negative number if |a| should be ordered before |b|, a | 150 * @return {number} A negative number if |a| should be ordered before |b|, a |
| 151 * positive number otherwise. | 151 * positive number otherwise. |
| 152 */ | 152 */ |
| 153 function compareTableItem(sortKey, a, b) { | 153 function compareTableItem(sortKey, a, b) { |
| 154 var val1 = a[sortKey]; | 154 var val1 = a[sortKey]; |
| 155 var val2 = b[sortKey]; | 155 var val2 = b[sortKey]; |
| 156 | 156 |
| 157 // Compare the hosts of the origin ignoring schemes. | 157 // Compare the hosts of the origin ignoring schemes. |
| 158 if (sortKey == 'origin') | 158 if (sortKey == 'origin') |
| 159 return new URL(val1.url).host > new URL(val2.url).host ? 1 : -1; | 159 return new URL(val1.url).host > new URL(val2.url).host ? 1 : -1; |
| 160 | 160 |
| 161 if (sortKey == 'score') | 161 if (sortKey == 'base_score') |
| 162 return val1 - val2; | 162 return val1 - val2; |
| 163 | 163 |
| 164 assertNotReached('Unsupported sort key: ' + sortKey); | 164 assertNotReached('Unsupported sort key: ' + sortKey); |
| 165 return 0; | 165 return 0; |
| 166 } | 166 } |
| 167 | 167 |
| 168 /** | 168 /** |
| 169 * Regenerates the engagement table from |info|. | 169 * Regenerates the engagement table from |info|. |
| 170 */ | 170 */ |
| 171 function renderTable() { | 171 function renderTable() { |
| 172 clearTable(); | 172 clearTable(); |
| 173 sortInfo(); | 173 sortInfo(); |
| 174 // Round each score to 2 decimal places. | 174 // Round each score to 2 decimal places. |
| 175 info.forEach(function(info) { | 175 info.forEach(function(info) { |
| 176 info.score = Number(Math.round(info.score * 100) / 100); | 176 info.base_score = Number(Math.round(info.base_score * 100) / 100); |
| 177 engagementTableBody.appendChild(createRow(info)); | 177 engagementTableBody.appendChild(createRow(info)); |
| 178 }); | 178 }); |
| 179 | 179 |
| 180 notifyPagePopulated(); | 180 notifyPagePopulated(); |
| 181 } | 181 } |
| 182 | 182 |
| 183 /** | 183 /** |
| 184 * Retrieve site engagement info and render the engagement table. | 184 * Retrieve site engagement info and render the engagement table. |
| 185 */ | 185 */ |
| 186 function updateEngagementTable() { | 186 function updateEngagementTable() { |
| 187 // Populate engagement table. | 187 // Populate engagement table. |
| 188 uiHandler.getSiteEngagementInfo().then(function(response) { | 188 uiHandler.getSiteEngagementDetails().then(function(response) { |
| 189 info = response.info; | 189 info = response.info; |
| 190 renderTable(info); | 190 renderTable(info); |
| 191 }); | 191 }); |
| 192 }; | 192 }; |
| 193 | 193 |
| 194 updateEngagementTable(); | 194 updateEngagementTable(); |
| 195 enableAutoupdate(); | 195 enableAutoupdate(); |
| 196 }; | 196 }; |
| 197 }); | 197 }); |
| OLD | NEW |