OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * This variable structure is here to document the structure that the template |
| 7 * expects to correctly populate the page. |
| 8 */ |
| 9 var flagsExperimentsDataFormat = { |
| 10 'flagsExperiments': [ |
| 11 { |
| 12 'internal_name': 'Experiment ID string', |
| 13 'name': 'Experiment Name', |
| 14 'description': 'description', |
| 15 // enabled is only set if the experiment is single valued. |
| 16 'enabled': true, |
| 17 // choices is only set if the experiment has multiple values. |
| 18 'choices': [ |
| 19 { |
| 20 'internal_name': 'Experiment ID string', |
| 21 'description': 'description', |
| 22 'selected': true |
| 23 } |
| 24 ] |
| 25 } |
| 26 ], |
| 27 'needsRestart': false |
| 28 }; |
| 29 |
| 30 /** |
| 31 * Takes the |flagsExperimentsData| input argument which represents data about |
| 32 * the currently available experiments and populates the html jstemplate |
| 33 * with that data. It expects an object structure like the above. |
| 34 * @param {Object} flagsExperimentsData Information about available experiments |
| 35 */ |
| 36 function renderTemplate(flagsExperimentsData) { |
| 37 // This is the javascript code that processes the template: |
| 38 var input = new JsEvalContext(flagsExperimentsData); |
| 39 var output = document.getElementById('flagsExperimentTemplate'); |
| 40 jstProcess(input, output); |
| 41 |
| 42 // Add handlers to dynamically created HTML elements. |
| 43 var elements = document.getElementsByClassName('experiment-select'); |
| 44 for (var i = 0; i < elements.length; ++i) { |
| 45 elements[i].onclick = function () { |
| 46 handleSelectChoiceExperiment(this, this.selectedIndex); |
| 47 return false; |
| 48 }; |
| 49 } |
| 50 |
| 51 elements = document.getElementsByClassName('experiment-disable-link'); |
| 52 for (var i = 0; i < elements.length; ++i) { |
| 53 elements[i].onclick = function () { |
| 54 handleEnableExperiment(this, false); |
| 55 return false; |
| 56 }; |
| 57 } |
| 58 |
| 59 elements = document.getElementsByClassName('experiment-enable-link'); |
| 60 for (var i = 0; i < elements.length; ++i) { |
| 61 elements[i].onclick = function () { |
| 62 handleEnableExperiment(this, true); |
| 63 return false; |
| 64 }; |
| 65 } |
| 66 |
| 67 elements = document.getElementsByClassName('experiment-restart-button'); |
| 68 for (var i = 0; i < elements.length; ++i) { |
| 69 elements[i].onclick = restartBrowser; |
| 70 } |
| 71 } |
| 72 |
| 73 /** |
| 74 * Asks the C++ FlagsDOMHandler to get details about the available experiments |
| 75 * and return detailed data about the configuration. The FlagsDOMHandler |
| 76 * should reply to returnFlagsExperiments() (below). |
| 77 */ |
| 78 function requestFlagsExperimentsData() { |
| 79 chrome.send('requestFlagsExperiments', []); |
| 80 } |
| 81 |
| 82 /** |
| 83 * Asks the C++ FlagsDOMHandler to restart the browser (restoring tabs). |
| 84 */ |
| 85 function restartBrowser() { |
| 86 chrome.send('restartBrowser', []); |
| 87 } |
| 88 |
| 89 /** |
| 90 * Called by the WebUI to re-populate the page with data representing the |
| 91 * current state of installed experiments. |
| 92 */ |
| 93 function returnFlagsExperiments(flagsExperimentsData){ |
| 94 var bodyContainer = document.getElementById('body-container'); |
| 95 renderTemplate(flagsExperimentsData); |
| 96 bodyContainer.style.visibility = 'visible'; |
| 97 } |
| 98 |
| 99 /** |
| 100 * Handles a 'enable' or 'disable' button getting clicked. |
| 101 */ |
| 102 function handleEnableExperiment(node, enable) { |
| 103 // Tell the C++ FlagsDOMHandler to enable/disable the experiment. |
| 104 chrome.send('enableFlagsExperiment', [String(node.internal_name), |
| 105 String(enable)]); |
| 106 requestFlagsExperimentsData(); |
| 107 } |
| 108 |
| 109 /** |
| 110 * Invoked when the selection of a multi-value choice is changed to the |
| 111 * specified index. |
| 112 */ |
| 113 function handleSelectChoiceExperiment(node, index) { |
| 114 // Tell the C++ FlagsDOMHandler to enable the selected choice. |
| 115 chrome.send('enableFlagsExperiment', |
| 116 [String(node.internal_name) + "@" + index, "true"]); |
| 117 requestFlagsExperimentsData(); |
| 118 } |
| 119 |
| 120 // Get data and have it displayed upon loading. |
| 121 document.addEventListener('DOMContentLoaded', requestFlagsExperimentsData); |
| 122 |
OLD | NEW |