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