Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2014 Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above |
| 11 | * copyright notice, this list of conditions and the following disclaimer |
| 12 | * in the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * * Neither the name of Google Inc. nor the names of its |
| 15 | * contributors may be used to endorse or promote products derived from |
| 16 | * this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | UI.SettingsUI = {}; |
| 31 | |
| 32 | /** |
| 33 | * @param {string} name |
| 34 | * @param {!Common.Setting} setting |
| 35 | * @param {boolean=} omitParagraphElement |
| 36 | * @param {string=} tooltip |
| 37 | * @return {!Element} |
| 38 | */ |
| 39 | UI.SettingsUI.createSettingCheckbox = function(name, setting, omitParagraphElement, tooltip) { |
| 40 | const label = UI.CheckboxLabel.create(name); |
| 41 | if (tooltip) |
| 42 | label.title = tooltip; |
| 43 | |
| 44 | const input = label.checkboxElement; |
| 45 | input.name = name; |
| 46 | UI.SettingsUI.bindCheckbox(input, setting); |
| 47 | |
| 48 | if (omitParagraphElement) |
| 49 | return label; |
| 50 | |
| 51 | const p = createElement('p'); |
| 52 | p.appendChild(label); |
| 53 | return p; |
| 54 | }; |
| 55 | |
| 56 | /** |
| 57 | * @param {string} name |
| 58 | * @param {!Array<!{text: string, value: *, raw: (boolean|undefined)}>} options |
| 59 | * @param {!Common.Setting} setting |
| 60 | * @return {!Element} |
| 61 | */ |
| 62 | UI.SettingsUI.createSettingSelect = function(name, options, setting) { |
| 63 | const p = createElement('p'); |
| 64 | p.createChild('label').textContent = name; |
| 65 | const select = p.createChild('select', 'chrome-select'); |
| 66 | |
| 67 | for (let i = 0; i < options.length; ++i) { |
| 68 | // The "raw" flag indicates text is non-i18n-izable. |
| 69 | const option = options[i]; |
| 70 | const optionName = option.raw ? option.text : Common.UIString(option.text); |
| 71 | select.add(new Option(optionName, option.value)); |
| 72 | } |
| 73 | |
| 74 | setting.addChangeListener(settingChanged); |
| 75 | settingChanged(); |
| 76 | select.addEventListener('change', selectChanged, false); |
| 77 | return p; |
| 78 | |
| 79 | function settingChanged() { |
| 80 | const newValue = setting.get(); |
| 81 | for (let i = 0; i < options.length; i++) { |
| 82 | if (options[i].value === newValue) |
| 83 | select.selectedIndex = i; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | function selectChanged() { |
| 88 | // Don't use event.target.value to avoid conversion of the value to string. |
| 89 | setting.set(options[select.selectedIndex].value); |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | /** |
| 94 | * @param {!Element} input |
| 95 | * @param {!Common.Setting} setting |
| 96 | */ |
| 97 | UI.SettingsUI.bindCheckbox = function(input, setting) { |
| 98 | function settingChanged() { |
| 99 | if (input.checked !== setting.get()) |
| 100 | input.checked = setting.get(); |
| 101 | } |
| 102 | setting.addChangeListener(settingChanged); |
| 103 | settingChanged(); |
| 104 | |
| 105 | function inputChanged() { |
| 106 | if (setting.get() !== input.checked) |
| 107 | setting.set(input.checked); |
| 108 | } |
| 109 | input.addEventListener('change', inputChanged, false); |
| 110 | }; |
| 111 | |
| 112 | /** |
| 113 | * @param {string} name |
| 114 | * @param {!Element} element |
| 115 | * @return {!Element} |
| 116 | */ |
| 117 | UI.SettingsUI.createCustomSetting = function(name, element) { |
| 118 | const p = createElement('p'); |
| 119 | const fieldsetElement = p.createChild('fieldset'); |
| 120 | fieldsetElement.createChild('label').textContent = name; |
| 121 | fieldsetElement.appendChild(element); |
| 122 | return p; |
| 123 | }; |
| 124 | |
| 125 | /** |
| 126 | * @param {!Common.Setting} setting |
| 127 | * @return {?Element} |
| 128 | */ |
| 129 | UI.SettingsUI.createControlForSetting = function(setting) { |
| 130 | if (!setting.extension()) |
| 131 | return null; |
| 132 | const descriptor = setting.extension().descriptor(); |
| 133 | const uiTitle = Common.UIString(setting.title() || ''); |
| 134 | switch (descriptor['settingType']) { |
| 135 | case 'boolean': |
| 136 | return UI.SettingsUI.createSettingCheckbox(uiTitle, setting); |
| 137 | case 'enum': |
| 138 | if (Array.isArray(descriptor['options'])) |
| 139 | return UI.SettingsUI.createSettingSelect(uiTitle, descriptor['options'], setting); |
| 140 | console.error('Enum setting defined without options'); |
| 141 | return null; |
| 142 | default: |
| 143 | console.error('Invalid setting type: ' + descriptor['settingType']); |
| 144 | return null; |
| 145 | } |
| 146 | }; |
| 147 | |
| 148 | /** |
| 149 | * @interface |
| 150 | */ |
| 151 | UI.SettingUI = function() {}; |
| 152 | |
| 153 | UI.SettingUI.prototype = { |
| 154 | /** |
| 155 | * @return {?Element} |
| 156 | */ |
| 157 | settingElement() {} |
| 158 | }; |