OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 /** | 5 /** |
6 * Javascript for suggestions_internals.html, served from | 6 * Javascript for suggestions_internals.html, served from |
7 * chrome://suggestions-internals/. This is used to debug suggestions ranking. | 7 * chrome://suggestions-internals/. This is used to debug suggestions ranking. |
8 * When loaded, the page will show the current set of suggestions, along with a | 8 * When loaded, the page will show the current set of suggestions, along with a |
9 * large set of information (e.g. all the signals that were taken into | 9 * large set of information (e.g. all the signals that were taken into |
10 * consideration for deciding which pages were selected to be shown to the user) | 10 * consideration for deciding which pages were selected to be shown to the user) |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 /** | 74 /** |
75 * Called by Chrome code, with a ranked list of suggestions. The columns | 75 * Called by Chrome code, with a ranked list of suggestions. The columns |
76 * to be displayed are calculated automatically from the properties of the | 76 * to be displayed are calculated automatically from the properties of the |
77 * elements in the list, such that all properties have a column. | 77 * elements in the list, such that all properties have a column. |
78 */ | 78 */ |
79 function setSuggestions(list) { | 79 function setSuggestions(list) { |
80 // Build a list of all the columns that will be displayed. | 80 // Build a list of all the columns that will be displayed. |
81 var columns = []; | 81 var columns = []; |
82 list.forEach(function(entry) { | 82 list.forEach(function(entry) { |
83 for (var column in entry) { | 83 for (var column in entry) { |
84 if (typeof(entry[column]) == 'object') { | 84 if (typeof entry[column] == 'object') { |
85 // Expand one level deep | 85 // Expand one level deep |
86 for (var sub_column in entry[column]) { | 86 for (var subColumn in entry[column]) { |
87 var path = column + '.' + sub_column; | 87 var path = column + '.' + subColumn; |
88 if (columns.indexOf(path) < 0) | 88 if (columns.indexOf(path) < 0) |
89 columns.push(path); | 89 columns.push(path); |
90 } | 90 } |
91 } else if (columns.indexOf(column) < 0) { | 91 } else if (columns.indexOf(column) < 0) { |
92 columns.push(column); | 92 columns.push(column); |
93 } | 93 } |
94 } | 94 } |
95 }); | 95 }); |
96 | 96 |
97 // Remove columns that we don't want to display. | 97 // Remove columns that we don't want to display. |
(...skipping 25 matching lines...) Expand all Loading... |
123 var column = document.createElement('th'); | 123 var column = document.createElement('th'); |
124 column.innerText = entry; | 124 column.innerText = entry; |
125 header.appendChild(column); | 125 header.appendChild(column); |
126 }); | 126 }); |
127 table.appendChild(header); | 127 table.appendChild(header); |
128 | 128 |
129 // Add all the suggestions to the table. | 129 // Add all the suggestions to the table. |
130 var rank = 1; | 130 var rank = 1; |
131 list.forEach(function(entry) { | 131 list.forEach(function(entry) { |
132 var row = document.createElement('tr'); | 132 var row = document.createElement('tr'); |
133 columns.forEach(function(column_name) { | 133 columns.forEach(function(columnName) { |
134 var column = document.createElement('td'); | 134 var column = document.createElement('td'); |
135 // Expand the path and find the data if it's there. | 135 // Expand the path and find the data if it's there. |
136 var path = column_name.split('.'); | 136 var path = columnName.split('.'); |
137 var data = entry; | 137 var data = entry; |
138 for (var i = 0; i < path.length; ++i) { | 138 for (var i = 0; i < path.length; ++i) { |
139 if (data && data.hasOwnProperty(path[i])) | 139 if (data && data.hasOwnProperty(path[i])) |
140 data = data[path[i]]; | 140 data = data[path[i]]; |
141 else | 141 else |
142 data = undefined; | 142 data = undefined; |
143 } | 143 } |
144 // Only add the column if the current suggestion has this property | 144 // Only add the column if the current suggestion has this property |
145 // (otherwise, leave the cell empty). | 145 // (otherwise, leave the cell empty). |
146 if (typeof(data) != 'undefined') { | 146 if (typeof(data) != 'undefined') { |
147 if (typeof(data) == 'boolean') { | 147 if (typeof(data) == 'boolean') { |
148 setBooleanColumn(column, data); | 148 setBooleanColumn(column, data); |
149 } else if (/^https?:\/\/.+$/.test(data)) { | 149 } else if (/^https?:\/\/.+$/.test(data)) { |
150 // If the text is a URL, make it an anchor element. | 150 // If the text is a URL, make it an anchor element. |
151 var anchor = document.createElement('a'); | 151 var anchor = document.createElement('a'); |
152 anchor.href = data; | 152 anchor.href = data; |
153 anchor.innerText = data; | 153 anchor.innerText = data; |
154 column.appendChild(anchor); | 154 column.appendChild(anchor); |
155 } else { | 155 } else { |
156 column.innerText = data; | 156 column.innerText = data; |
157 } | 157 } |
158 } else if (column_name == 'rank') { | 158 } else if (columnName == 'rank') { |
159 column.innerText = rank++; | 159 column.innerText = rank++; |
160 } else if (column_name == 'screenshot') { | 160 } else if (columnName == 'screenshot') { |
161 var thumbnailUrl = 'chrome://thumb/' + entry.url; | 161 var thumbnailUrl = 'chrome://thumb/' + entry.url; |
162 var img = document.createElement('img'); | 162 var img = document.createElement('img'); |
163 img.onload = function() { setBooleanColumn(column, true); } | 163 img.onload = function() { setBooleanColumn(column, true); } |
164 img.onerror = function() { setBooleanColumn(column, false); } | 164 img.onerror = function() { setBooleanColumn(column, false); } |
165 img.src = thumbnailUrl; | 165 img.src = thumbnailUrl; |
166 } else if (column_name == 'favicon') { | 166 } else if (columnName == 'favicon') { |
167 var faviconUrl = 'chrome://favicon/size/16@1x/' + entry.url; | 167 var faviconUrl = 'chrome://favicon/size/16@1x/' + entry.url; |
168 column.style.backgroundImage = url(faviconUrl); | 168 column.style.backgroundImage = url(faviconUrl); |
169 column.style.backgroundRepeat = 'no-repeat'; | 169 column.style.backgroundRepeat = 'no-repeat'; |
170 column.style.backgroundPosition = 'center center'; | 170 column.style.backgroundPosition = 'center center'; |
171 } | 171 } |
172 row.appendChild(column); | 172 row.appendChild(column); |
173 }); | 173 }); |
174 table.appendChild(row); | 174 table.appendChild(row); |
175 }); | 175 }); |
176 | 176 |
177 output.appendChild(table); | 177 output.appendChild(table); |
178 } | 178 } |
179 | 179 |
180 return { | 180 return { |
181 initialize: initialize, | 181 initialize: initialize, |
182 setSuggestions: setSuggestions | 182 setSuggestions: setSuggestions |
183 }; | 183 }; |
184 }); | 184 }); |
185 | 185 |
186 document.addEventListener('DOMContentLoaded', suggestionsInternals.initialize); | 186 document.addEventListener('DOMContentLoaded', suggestionsInternals.initialize); |
OLD | NEW |