| 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 // Redefine '$' here rather than including 'cr.js', since this is | 5 // Redefine '$' here rather than including 'cr.js', since this is |
| 6 // the only function needed. This allows this file to be loaded | 6 // the only function needed. This allows this file to be loaded |
| 7 // in a browser directly for layout and some testing purposes. | 7 // in a browser directly for layout and some testing purposes. |
| 8 var $ = function(id) { return document.getElementById(id); }; | 8 var $ = function(id) { return document.getElementById(id); }; |
| 9 | 9 |
| 10 /** | 10 /** |
| 11 * WebUI for configuring instant.* preference values used by | 11 * WebUI for configuring instant.* preference values used by |
| 12 * Chrome's instant search system. | 12 * Chrome's instant search system. |
| 13 */ | 13 */ |
| 14 var instantConfig = (function() { | 14 var instantConfig = (function() { |
| 15 'use strict'; | 15 'use strict'; |
| 16 | 16 |
| 17 /** List of fields used to dynamically build form. **/ | 17 /** List of fields used to dynamically build form. **/ |
| 18 var FIELDS = [ | 18 var FIELDS = [ |
| 19 { | 19 { |
| 20 key: 'instant.animation_scale_factor', | 20 key: 'instant.animation_scale_factor', |
| 21 label: 'Slow down animations by a factor of', | 21 type: 'number', |
| 22 units: 'no units, range 1 to 10', | 22 label: 'Slow down animations by a factor of ', |
| 23 default: 1 | 23 units: 'no units, range 1 to 20', |
| 24 default: 1, |
| 25 min: 1, |
| 26 max: 20 |
| 27 }, |
| 28 { |
| 29 key: 'instant.show_search_provider_logo', |
| 30 type: 'checkbox', |
| 31 label: 'Show search provider logo', |
| 32 default: false |
| 24 } | 33 } |
| 25 ]; | 34 ]; |
| 26 | 35 |
| 27 /** | 36 /** |
| 28 * Returns a DOM element of the given type and class name. | 37 * Returns a DOM element of the given type and class name. |
| 29 */ | 38 */ |
| 30 function createElementWithClass(elementType, className) { | 39 function createElementWithClass(elementType, className) { |
| 31 var element = document.createElement(elementType); | 40 var element = document.createElement(elementType); |
| 32 element.className = className; | 41 element.className = className; |
| 33 return element; | 42 return element; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 45 | 54 |
| 46 var row = createElementWithClass('div', 'row'); | 55 var row = createElementWithClass('div', 'row'); |
| 47 row.id = ''; | 56 row.id = ''; |
| 48 | 57 |
| 49 var label = createElementWithClass('label', 'row-label'); | 58 var label = createElementWithClass('label', 'row-label'); |
| 50 label.setAttribute('for', field.key); | 59 label.setAttribute('for', field.key); |
| 51 label.textContent = field.label; | 60 label.textContent = field.label; |
| 52 row.appendChild(label); | 61 row.appendChild(label); |
| 53 | 62 |
| 54 var input = createElementWithClass('input', 'row-input'); | 63 var input = createElementWithClass('input', 'row-input'); |
| 55 input.type = 'number'; | 64 input.type = field.type; |
| 56 input.size = 3; | 65 input.size = 3; |
| 57 input.id = field.key; | 66 input.id = field.key; |
| 58 input.min = field.min || 0; | 67 input.min = field.min || 0; |
| 59 input.title = "Default Value: " + field.default; | 68 input.title = "Default Value: " + field.default; |
| 60 if (field.max) input.max = field.max; | 69 if (field.max) input.max = field.max; |
| 61 if (field.step) input.step = field.step; | 70 if (field.step) input.step = field.step; |
| 62 row.appendChild(input); | 71 row.appendChild(input); |
| 63 | 72 |
| 64 var units = createElementWithClass('div', 'row-units'); | 73 var units = createElementWithClass('div', 'row-units'); |
| 65 if (field.units) | 74 if (field.units) |
| 66 units.innerHTML = field.units; | 75 units.innerHTML = field.units; |
| 67 row.appendChild(units); | 76 row.appendChild(units); |
| 68 | 77 |
| 69 $('instant-form').appendChild(row); | 78 $('instant-form').appendChild(row); |
| 70 } | 79 } |
| 71 } | 80 } |
| 72 | 81 |
| 73 /** | 82 /** |
| 74 * Initialize the form by adding 'onChange' listeners to all fields. | 83 * Initialize the form by adding 'onChange' listeners to all fields. |
| 75 */ | 84 */ |
| 76 function initForm() { | 85 function initForm() { |
| 77 for (var i = 0; i < FIELDS.length; i++) { | 86 for (var i = 0; i < FIELDS.length; i++) { |
| 78 var field = FIELDS[i]; | 87 var field = FIELDS[i]; |
| 79 $(field.key).onchange = (function(key) { | 88 $(field.key).onchange = (function(key) { |
| 80 setPreferenceValue(key, $(key).value); | 89 setPreferenceValue(key); |
| 81 }).bind(null, field.key); | 90 }).bind(null, field.key); |
| 82 } | 91 } |
| 83 } | 92 } |
| 84 | 93 |
| 85 /** | 94 /** |
| 86 * Request a preference setting's value. | 95 * Request a preference setting's value. |
| 87 * This method is asynchronous; the result is provided by a call to | 96 * This method is asynchronous; the result is provided by a call to |
| 88 * getPreferenceValueResult. | 97 * getPreferenceValueResult. |
| 89 * @param {string} prefName The name of the preference value being requested. | 98 * @param {string} prefName The name of the preference value being requested. |
| 90 */ | 99 */ |
| 91 function getPreferenceValue(prefName) { | 100 function getPreferenceValue(prefName) { |
| 92 chrome.send('getPreferenceValue', [prefName]); | 101 chrome.send('getPreferenceValue', [prefName]); |
| 93 } | 102 } |
| 94 | 103 |
| 95 /** | 104 /** |
| 96 * Handle callback from call to getPreferenceValue. | 105 * Handle callback from call to getPreferenceValue. |
| 97 * @param {string} prefName The name of the requested preference value. | 106 * @param {string} prefName The name of the requested preference value. |
| 98 * @param {value} value The current value associated with prefName. | 107 * @param {value} value The current value associated with prefName. |
| 99 */ | 108 */ |
| 100 function getPreferenceValueResult(prefName, value) { | 109 function getPreferenceValueResult(prefName, value) { |
| 101 $(prefName).value = value; | 110 if ($(prefName).type == 'checkbox') |
| 111 $(prefName).checked = value; |
| 112 else |
| 113 $(prefName).value = value; |
| 102 } | 114 } |
| 103 | 115 |
| 104 /** | 116 /** |
| 105 * Set a preference setting's value. | 117 * Set a preference setting's value stored in the element with prefName. |
| 106 * @param {string} prefName The name of the preference value being set. | 118 * @param {string} prefName The name of the preference value being set. |
| 107 * @param {value} value The value to be associated with prefName. | |
| 108 */ | 119 */ |
| 109 function setPreferenceValue(prefName, value) { | 120 function setPreferenceValue(prefName) { |
| 121 var value; |
| 122 if ($(prefName).type == 'checkbox') |
| 123 value = $(prefName).checked; |
| 124 else if ($(prefName).type == 'number') |
| 125 value = parseFloat($(prefName).value); |
| 126 else |
| 127 value = $(prefName).value; |
| 110 chrome.send( | 128 chrome.send( |
| 111 'setPreferenceValue', | 129 'setPreferenceValue', |
| 112 [prefName, parseFloat(value)]); | 130 [prefName, value]); |
| 113 } | 131 } |
| 114 | 132 |
| 115 /** | 133 /** |
| 116 * Handle processing of "Reset" button. | 134 * Handle processing of "Reset" button. |
| 117 * Causes off form values to be updated based on current preference values. | 135 * Causes off form values to be updated based on current preference values. |
| 118 */ | 136 */ |
| 119 function onReset() { | 137 function onReset() { |
| 120 for (var i = 0; i < FIELDS.length; i++) { | 138 for (var i = 0; i < FIELDS.length; i++) { |
| 121 var field = FIELDS[i]; | 139 var field = FIELDS[i]; |
| 122 $(field.key).value = field.default; | 140 if ($(field.key).type == 'checkbox') |
| 123 setPreferenceValue(field.key, field.default); | 141 $(field.key).checked = field.default; |
| 142 else |
| 143 $(field.key).value = field.default; |
| 144 setPreferenceValue(field.key); |
| 124 } | 145 } |
| 125 return false; | 146 return false; |
| 126 } | 147 } |
| 127 | 148 |
| 128 /** | 149 /** |
| 129 * Saves data back into Chrome preferences. | 150 * Saves data back into Chrome preferences. |
| 130 */ | 151 */ |
| 131 function onSave() { | 152 function onSave() { |
| 132 for (var i = 0; i < FIELDS.length; i++) { | 153 for (var i = 0; i < FIELDS.length; i++) { |
| 133 var field = FIELDS[i]; | 154 var field = FIELDS[i]; |
| 134 setPreferenceValue(field.key, $(field.key).value); | 155 setPreferenceValue(field.key); |
| 135 } | 156 } |
| 136 return false; | 157 return false; |
| 137 } | 158 } |
| 138 | 159 |
| 139 | 160 |
| 140 function loadForm() { | 161 function loadForm() { |
| 141 for (var i = 0; i < FIELDS.length; i++) | 162 for (var i = 0; i < FIELDS.length; i++) |
| 142 getPreferenceValue(FIELDS[i].key); | 163 getPreferenceValue(FIELDS[i].key); |
| 143 } | 164 } |
| 144 | 165 |
| 145 /** | 166 /** |
| 146 * Build and initialize the configuration form. | 167 * Build and initialize the configuration form. |
| 147 */ | 168 */ |
| 148 function initialize() { | 169 function initialize() { |
| 149 buildForm(); | 170 buildForm(); |
| 150 loadForm(); | 171 loadForm(); |
| 151 initForm(); | 172 initForm(); |
| 152 | 173 |
| 153 $('reset-button').onclick = onReset.bind(this); | 174 $('reset-button').onclick = onReset.bind(this); |
| 154 $('save-button').onclick = onSave.bind(this); | 175 $('save-button').onclick = onSave.bind(this); |
| 155 } | 176 } |
| 156 | 177 |
| 157 return { | 178 return { |
| 158 initialize: initialize, | 179 initialize: initialize, |
| 159 getPreferenceValueResult: getPreferenceValueResult | 180 getPreferenceValueResult: getPreferenceValueResult |
| 160 }; | 181 }; |
| 161 })(); | 182 })(); |
| 162 | 183 |
| 163 document.addEventListener('DOMContentLoaded', instantConfig.initialize); | 184 document.addEventListener('DOMContentLoaded', instantConfig.initialize); |
| OLD | NEW |