| 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 omnibox.html, served from chrome://omnibox/ | 6 * Javascript for omnibox.html, served from chrome://omnibox/ |
| 7 * This is used to debug omnibox ranking. The user enters some text | 7 * This is used to debug omnibox ranking. The user enters some text |
| 8 * into a box, submits it, and then sees lots of debug information | 8 * into a box, submits it, and then sees lots of debug information |
| 9 * from the autocompleter that shows what omnibox would do with that | 9 * from the autocompleter that shows what omnibox would do with that |
| 10 * input. | 10 * input. |
| 11 * | 11 * |
| 12 * The simple object defined in this javascript file listens for | 12 * The simple object defined in this javascript file listens for |
| 13 * certain events on omnibox.html, sends (when appropriate) the | 13 * certain events on omnibox.html, sends (when appropriate) the |
| 14 * input text to C++ code to start the omnibox autcomplete controller | 14 * input text to C++ code to start the omnibox autcomplete controller |
| 15 * working, and listens from callbacks from the C++ code saying that | 15 * working, and listens from callbacks from the C++ code saying that |
| 16 * results are available. When results (possibly intermediate ones) | 16 * results are available. When results (possibly intermediate ones) |
| 17 * are available, the Javascript formats them and displays them. | 17 * are available, the Javascript formats them and displays them. |
| 18 */ | 18 */ |
| 19 cr.define('omniboxDebug', function() { | 19 cr.define('omniboxDebug', function() { |
| 20 'use strict'; | 20 'use strict'; |
| 21 | 21 |
| 22 /** | 22 /** |
| 23 * Register our event handlers. | 23 * Register our event handlers. |
| 24 */ | 24 */ |
| 25 function initialize() { | 25 function initialize() { |
| 26 document.getElementById('omnibox-input-form').addEventListener( | 26 $('omnibox-input-form').addEventListener( |
| 27 'submit', startOmniboxQuery, false); | 27 'submit', startOmniboxQuery, false); |
| 28 document.getElementById('show-details').addEventListener( | 28 $('show-details').addEventListener('change', refresh); |
| 29 'change', refresh); | 29 $('show-incomplete-results').addEventListener('change', refresh); |
| 30 document.getElementById('show-incomplete-results').addEventListener( | 30 $('show-all-providers').addEventListener('change', refresh); |
| 31 'change', refresh); | |
| 32 document.getElementById('show-all-providers').addEventListener( | |
| 33 'change', refresh); | |
| 34 } | 31 } |
| 35 | 32 |
| 36 /** | 33 /** |
| 37 * @type {Array.<Object>} an array of all autocomplete results we've seen | 34 * @type {Array.<Object>} an array of all autocomplete results we've seen |
| 38 * for this query. We append to this list once for every call to | 35 * for this query. We append to this list once for every call to |
| 39 * handleNewAutocompleteResult. For details on the structure of | 36 * handleNewAutocompleteResult. For details on the structure of |
| 40 * the object inside, see the comments by addResultToOutput. | 37 * the object inside, see the comments by addResultToOutput. |
| 41 */ | 38 */ |
| 42 var progressiveAutocompleteResults = []; | 39 var progressiveAutocompleteResults = []; |
| 43 | 40 |
| 44 /** | 41 /** |
| 45 * Extracts the input text from the text field and sends it to the | 42 * Extracts the input text from the text field and sends it to the |
| 46 * C++ portion of chrome to handle. The C++ code will iteratively | 43 * C++ portion of chrome to handle. The C++ code will iteratively |
| 47 * call handleNewAutocompleteResult as results come in. | 44 * call handleNewAutocompleteResult as results come in. |
| 48 */ | 45 */ |
| 49 function startOmniboxQuery(event) { | 46 function startOmniboxQuery(event) { |
| 50 // First, clear the results of past calls (if any). | 47 // First, clear the results of past calls (if any). |
| 51 progressiveAutocompleteResults = []; | 48 progressiveAutocompleteResults = []; |
| 52 // Then, call chrome with a one-element list: the value in the text box. | 49 // Then, call chrome with a one-element list: the value in the text box. |
| 53 chrome.send('startOmniboxQuery', | 50 chrome.send('startOmniboxQuery', [$('input-text').value]); |
| 54 [document.getElementById('input-text').value]); | |
| 55 // Cancel the submit action. i.e., don't submit the form. (We handle | 51 // Cancel the submit action. i.e., don't submit the form. (We handle |
| 56 // display the results solely with Javascript.) | 52 // display the results solely with Javascript.) |
| 57 event.preventDefault(); | 53 event.preventDefault(); |
| 58 } | 54 } |
| 59 | 55 |
| 60 /** | 56 /** |
| 61 * Returns a simple object with information about how to display an | 57 * Returns a simple object with information about how to display an |
| 62 * autocomplete result data field. | 58 * autocomplete result data field. |
| 63 * @param {string} header the label for the top of the column/table. | 59 * @param {string} header the label for the top of the column/table. |
| 64 * @param {string} urlLabelForHeader the URL that the header should point | 60 * @param {string} urlLabelForHeader the URL that the header should point |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 ]; | 106 ]; |
| 111 | 107 |
| 112 /** | 108 /** |
| 113 * Returns an HTML Element of type table row that contains the | 109 * Returns an HTML Element of type table row that contains the |
| 114 * headers we'll use for labeling the columns. If we're in | 110 * headers we'll use for labeling the columns. If we're in |
| 115 * detailed_mode, we use all the headers. If not, we only use ones | 111 * detailed_mode, we use all the headers. If not, we only use ones |
| 116 * marked displayAlways. | 112 * marked displayAlways. |
| 117 */ | 113 */ |
| 118 function createAutocompleteResultTableHeader() { | 114 function createAutocompleteResultTableHeader() { |
| 119 var row = document.createElement('tr'); | 115 var row = document.createElement('tr'); |
| 120 var inDetailedMode = document.getElementById('show-details').checked; | 116 var inDetailedMode = $('show-details').checked; |
| 121 for (var i = 0; i < PROPERTY_OUTPUT_ORDER.length; i++) { | 117 for (var i = 0; i < PROPERTY_OUTPUT_ORDER.length; i++) { |
| 122 if (inDetailedMode || PROPERTY_OUTPUT_ORDER[i].displayAlways) { | 118 if (inDetailedMode || PROPERTY_OUTPUT_ORDER[i].displayAlways) { |
| 123 var headerCell = document.createElement('th'); | 119 var headerCell = document.createElement('th'); |
| 124 if (PROPERTY_OUTPUT_ORDER[i].urlLabelForHeader != '') { | 120 if (PROPERTY_OUTPUT_ORDER[i].urlLabelForHeader != '') { |
| 125 // Wrap header text in URL. | 121 // Wrap header text in URL. |
| 126 var linkNode = document.createElement('a'); | 122 var linkNode = document.createElement('a'); |
| 127 linkNode.href = PROPERTY_OUTPUT_ORDER[i].urlLabelForHeader; | 123 linkNode.href = PROPERTY_OUTPUT_ORDER[i].urlLabelForHeader; |
| 128 linkNode.textContent = PROPERTY_OUTPUT_ORDER[i].header; | 124 linkNode.textContent = PROPERTY_OUTPUT_ORDER[i].header; |
| 129 headerCell.appendChild(linkNode); | 125 headerCell.appendChild(linkNode); |
| 130 } else { | 126 } else { |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 * } | 211 * } |
| 216 * ... | 212 * ... |
| 217 * } | 213 * } |
| 218 * } | 214 * } |
| 219 * } | 215 * } |
| 220 * </pre> | 216 * </pre> |
| 221 * For more information on how the result is packed, see the | 217 * For more information on how the result is packed, see the |
| 222 * corresponding code in chrome/browser/ui/webui/omnibox_ui.cc | 218 * corresponding code in chrome/browser/ui/webui/omnibox_ui.cc |
| 223 */ | 219 */ |
| 224 function addResultToOutput(result) { | 220 function addResultToOutput(result) { |
| 225 var output = document.getElementById('omnibox-debug-text'); | 221 var output = $('omnibox-debug-text'); |
| 226 var inDetailedMode = document.getElementById('show-details').checked; | 222 var inDetailedMode = $('show-details').checked; |
| 227 var showIncompleteResults = | 223 var showIncompleteResults = $('show-incomplete-results').checked; |
| 228 document.getElementById('show-incomplete-results').checked; | 224 var showPerProviderResults = $('show-all-providers').checked; |
| 229 var showPerProviderResults = | |
| 230 document.getElementById('show-all-providers').checked; | |
| 231 | 225 |
| 232 // Output the result-level features in detailed mode and in | 226 // Output the result-level features in detailed mode and in |
| 233 // show incomplete results mode. We do the latter because without | 227 // show incomplete results mode. We do the latter because without |
| 234 // these result-level features, one can't make sense of each | 228 // these result-level features, one can't make sense of each |
| 235 // batch of results. | 229 // batch of results. |
| 236 if (inDetailedMode || showIncompleteResults) { | 230 if (inDetailedMode || showIncompleteResults) { |
| 237 var p1 = document.createElement('p'); | 231 var p1 = document.createElement('p'); |
| 238 p1.textContent = 'elapsed time = ' + | 232 p1.textContent = 'elapsed time = ' + |
| 239 result.time_since_omnibox_started_ms + 'ms'; | 233 result.time_since_omnibox_started_ms + 'ms'; |
| 240 output.appendChild(p1); | 234 output.appendChild(p1); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 265 | 259 |
| 266 /** | 260 /** |
| 267 * @param {Object} result either the combined_results component of | 261 * @param {Object} result either the combined_results component of |
| 268 * the structure described in the comment by addResultToOutput() | 262 * the structure described in the comment by addResultToOutput() |
| 269 * above or one of the per-provider results in the structure. | 263 * above or one of the per-provider results in the structure. |
| 270 * (Both have the same format.) | 264 * (Both have the same format.) |
| 271 * @return {HTMLTableCellElement} that is a user-readable HTML | 265 * @return {HTMLTableCellElement} that is a user-readable HTML |
| 272 * representation of this object. | 266 * representation of this object. |
| 273 */ | 267 */ |
| 274 function addResultTableToOutput(result) { | 268 function addResultTableToOutput(result) { |
| 275 var inDetailedMode = document.getElementById('show-details').checked; | 269 var inDetailedMode = $('show-details').checked; |
| 276 // Create a table to hold all the autocomplete items. | 270 // Create a table to hold all the autocomplete items. |
| 277 var table = document.createElement('table'); | 271 var table = document.createElement('table'); |
| 278 table.className = 'autocomplete-results-table'; | 272 table.className = 'autocomplete-results-table'; |
| 279 table.appendChild(createAutocompleteResultTableHeader()); | 273 table.appendChild(createAutocompleteResultTableHeader()); |
| 280 // Loop over every autocomplete item and add it as a row in the table. | 274 // Loop over every autocomplete item and add it as a row in the table. |
| 281 for (var i = 0; i < result.num_items; i++) { | 275 for (var i = 0; i < result.num_items; i++) { |
| 282 var autocompleteSuggestion = result['item_' + i]; | 276 var autocompleteSuggestion = result['item_' + i]; |
| 283 var row = document.createElement('tr'); | 277 var row = document.createElement('tr'); |
| 284 // Loop over all the columns/properties and output either them | 278 // Loop over all the columns/properties and output either them |
| 285 // all (if we're in detailed mode) or only the ones marked displayAlways. | 279 // all (if we're in detailed mode) or only the ones marked displayAlways. |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 } | 311 } |
| 318 | 312 |
| 319 /* Repaints the page based on the contents of the array | 313 /* Repaints the page based on the contents of the array |
| 320 * progressiveAutocompleteResults, which represents consecutive | 314 * progressiveAutocompleteResults, which represents consecutive |
| 321 * autocomplete results. We only display the last (most recent) | 315 * autocomplete results. We only display the last (most recent) |
| 322 * entry unless we're asked to display incomplete results. For an | 316 * entry unless we're asked to display incomplete results. For an |
| 323 * example of the output, play with chrome://omnibox/ | 317 * example of the output, play with chrome://omnibox/ |
| 324 */ | 318 */ |
| 325 function refresh() { | 319 function refresh() { |
| 326 // Erase whatever is currently being displayed. | 320 // Erase whatever is currently being displayed. |
| 327 var output = document.getElementById('omnibox-debug-text'); | 321 var output = $('omnibox-debug-text'); |
| 328 output.innerHTML = ''; | 322 output.innerHTML = ''; |
| 329 | 323 |
| 330 if (progressiveAutocompleteResults.length > 0) { // if we have results | 324 if (progressiveAutocompleteResults.length > 0) { // if we have results |
| 331 // Display the results. | 325 // Display the results. |
| 332 var showIncompleteResults = | 326 var showIncompleteResults = $('show-incomplete-results').checked; |
| 333 document.getElementById('show-incomplete-results').checked; | |
| 334 var startIndex = showIncompleteResults ? 0 : | 327 var startIndex = showIncompleteResults ? 0 : |
| 335 progressiveAutocompleteResults.length - 1; | 328 progressiveAutocompleteResults.length - 1; |
| 336 for (var i = startIndex; i < progressiveAutocompleteResults.length; i++) { | 329 for (var i = startIndex; i < progressiveAutocompleteResults.length; i++) { |
| 337 addResultToOutput(progressiveAutocompleteResults[i]); | 330 addResultToOutput(progressiveAutocompleteResults[i]); |
| 338 } | 331 } |
| 339 } | 332 } |
| 340 } | 333 } |
| 341 | 334 |
| 342 return { | 335 return { |
| 343 initialize: initialize, | 336 initialize: initialize, |
| 344 startOmniboxQuery: startOmniboxQuery, | 337 startOmniboxQuery: startOmniboxQuery, |
| 345 handleNewAutocompleteResult: handleNewAutocompleteResult | 338 handleNewAutocompleteResult: handleNewAutocompleteResult |
| 346 }; | 339 }; |
| 347 }); | 340 }); |
| 348 | 341 |
| 349 document.addEventListener('DOMContentLoaded', omniboxDebug.initialize); | 342 document.addEventListener('DOMContentLoaded', omniboxDebug.initialize); |
| OLD | NEW |