| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 * @fileoverview | 6 * @fileoverview |
| 7 * `settings-checkbox` is a checkbox that controls a supplied preference. | 7 * `settings-checkbox` is a checkbox that controls a supplied preference. |
| 8 * | |
| 9 * Example: | |
| 10 * <settings-checkbox pref="{{prefs.settings.enableFoo}}" | |
| 11 * label="Enable foo setting." subLabel="(bar also)"> | |
| 12 * </settings-checkbox> | |
| 13 */ | 8 */ |
| 14 Polymer({ | 9 Polymer({ |
| 15 is: 'settings-checkbox', | 10 is: 'settings-checkbox', |
| 16 | 11 |
| 17 behaviors: [CrPolicyPrefBehavior, PrefControlBehavior], | 12 behaviors: [SettingsBooleanControlBehavior], |
| 18 | |
| 19 properties: { | |
| 20 /** Whether the checkbox should represent the inverted value. */ | |
| 21 inverted: { | |
| 22 type: Boolean, | |
| 23 value: false, | |
| 24 }, | |
| 25 | |
| 26 /** Whether the checkbox is checked. */ | |
| 27 checked: { | |
| 28 type: Boolean, | |
| 29 value: false, | |
| 30 notify: true, | |
| 31 observer: 'checkedChanged_', | |
| 32 reflectToAttribute: true | |
| 33 }, | |
| 34 | |
| 35 /** Disabled property for the element. */ | |
| 36 disabled: { | |
| 37 type: Boolean, | |
| 38 value: false, | |
| 39 notify: true, | |
| 40 reflectToAttribute: true | |
| 41 }, | |
| 42 | |
| 43 /** Checkbox label. */ | |
| 44 label: { | |
| 45 type: String, | |
| 46 value: '', | |
| 47 }, | |
| 48 | |
| 49 /** Additional sub-label for the checkbox. */ | |
| 50 subLabel: { | |
| 51 type: String, | |
| 52 value: '', | |
| 53 }, | |
| 54 }, | |
| 55 | |
| 56 observers: [ | |
| 57 'prefValueChanged_(pref.value)' | |
| 58 ], | |
| 59 | |
| 60 /** | |
| 61 * Polymer observer for pref.value. | |
| 62 * @param {*} prefValue | |
| 63 * @private | |
| 64 */ | |
| 65 prefValueChanged_: function(prefValue) { | |
| 66 this.checked = this.getNewValue_(prefValue); | |
| 67 }, | |
| 68 | |
| 69 /** | |
| 70 * Polymer observer for checked. | |
| 71 * @private | |
| 72 */ | |
| 73 checkedChanged_: function() { | |
| 74 if (!this.pref) | |
| 75 return; | |
| 76 /** @type {boolean} */ var newValue = this.getNewValue_(this.checked); | |
| 77 // Ensure that newValue is the correct type for the pref type, either | |
| 78 // a boolean or a number. | |
| 79 if (this.pref.type == chrome.settingsPrivate.PrefType.NUMBER) { | |
| 80 this.set('pref.value', newValue ? 1 : 0); | |
| 81 return; | |
| 82 } | |
| 83 this.set('pref.value', newValue); | |
| 84 }, | |
| 85 | |
| 86 /** | |
| 87 * @param {*} value | |
| 88 * @return {boolean} The value as a boolean, inverted if |inverted| is true. | |
| 89 * @private | |
| 90 */ | |
| 91 getNewValue_: function(value) { | |
| 92 return this.inverted ? !value : !!value; | |
| 93 }, | |
| 94 | |
| 95 /** | |
| 96 * @param {boolean} disabled | |
| 97 * @param {!chrome.settingsPrivate.PrefObject} pref | |
| 98 * @return {boolean} Whether the checkbox should be disabled. | |
| 99 * @private | |
| 100 */ | |
| 101 checkboxDisabled_: function(disabled, pref) { | |
| 102 return disabled || this.isPrefPolicyControlled(pref); | |
| 103 }, | |
| 104 }); | 13 }); |
| OLD | NEW |