| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 * A behavior to help controls that handle a boolean preference, such as | 7 * A behavior to help controls that handle a boolean preference, such as |
| 8 * checkbox and toggle button. | 8 * checkbox and toggle button. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 /** @polymerBehavior SettingsBooleanControlBehavior */ | 11 /** @polymerBehavior SettingsBooleanControlBehavior */ |
| 12 var SettingsBooleanControlBehaviorImpl = { | 12 var SettingsBooleanControlBehaviorImpl = { |
| 13 properties: { | 13 properties: { |
| 14 /** Whether the control should represent the inverted value. */ | 14 /** Whether the control should represent the inverted value. */ |
| 15 inverted: { | 15 inverted: { |
| 16 type: Boolean, | 16 type: Boolean, |
| 17 value: false, | 17 value: false, |
| 18 }, | 18 }, |
| 19 | 19 |
| 20 /** Whether the control is checked. */ | 20 /** Whether the control is checked. */ |
| 21 checked: { | 21 checked: { |
| 22 type: Boolean, | 22 type: Boolean, |
| 23 value: false, | 23 value: false, |
| 24 notify: true, | 24 notify: true, |
| 25 observer: 'checkedChanged_', | 25 observer: 'checkedChanged_', |
| 26 reflectToAttribute: true | 26 reflectToAttribute: true, |
| 27 }, | 27 }, |
| 28 | 28 |
| 29 /** Disabled property for the element. */ | 29 /** Disabled property for the element. */ |
| 30 disabled: { | 30 disabled: { |
| 31 type: Boolean, | 31 type: Boolean, |
| 32 value: false, | 32 value: false, |
| 33 notify: true, | 33 notify: true, |
| 34 reflectToAttribute: true | 34 reflectToAttribute: true, |
| 35 }, |
| 36 |
| 37 /** |
| 38 * If true, do not automatically set the preference value. This allows the |
| 39 * container to confirm the change first then call either sendPrefChange |
| 40 * or resetToPrefValue accordingly. |
| 41 */ |
| 42 noSetPref: { |
| 43 type: Boolean, |
| 44 value: false, |
| 35 }, | 45 }, |
| 36 | 46 |
| 37 /** The main label. */ | 47 /** The main label. */ |
| 38 label: { | 48 label: { |
| 39 type: String, | 49 type: String, |
| 40 value: '', | 50 value: '', |
| 41 }, | 51 }, |
| 42 | 52 |
| 43 /** Additional (optional) sub-label. */ | 53 /** Additional (optional) sub-label. */ |
| 44 subLabel: { | 54 subLabel: { |
| 45 type: String, | 55 type: String, |
| 46 value: '', | 56 value: '', |
| 47 }, | 57 }, |
| 48 }, | 58 }, |
| 49 | 59 |
| 50 observers: [ | 60 observers: [ |
| 51 'prefValueChanged_(pref.value)' | 61 'prefValueChanged_(pref.value)', |
| 52 ], | 62 ], |
| 53 | 63 |
| 64 /** Reset the checked state to match the current pref value. */ |
| 65 resetToPrefValue: function() { |
| 66 this.checked = this.getNewValue_(this.pref.value); |
| 67 }, |
| 68 |
| 69 /** Update the pref to the current |checked| value. */ |
| 70 sendPrefChange: function() { |
| 71 /** @type {boolean} */ var newValue = this.getNewValue_(this.checked); |
| 72 // Ensure that newValue is the correct type for the pref type, either |
| 73 // a boolean or a number. |
| 74 if (this.pref.type == chrome.settingsPrivate.PrefType.NUMBER) { |
| 75 this.set('pref.value', newValue ? 1 : 0); |
| 76 return; |
| 77 } |
| 78 this.set('pref.value', newValue); |
| 79 }, |
| 80 |
| 54 /** | 81 /** |
| 55 * Polymer observer for pref.value. | 82 * Polymer observer for pref.value. |
| 56 * @param {*} prefValue | 83 * @param {*} prefValue |
| 57 * @private | 84 * @private |
| 58 */ | 85 */ |
| 59 prefValueChanged_: function(prefValue) { | 86 prefValueChanged_: function(prefValue) { |
| 60 this.checked = this.getNewValue_(prefValue); | 87 this.checked = this.getNewValue_(prefValue); |
| 61 }, | 88 }, |
| 62 | 89 |
| 63 /** | 90 /** |
| 64 * Polymer observer for checked. | 91 * Polymer observer for checked. |
| 65 * @private | 92 * @private |
| 66 */ | 93 */ |
| 67 checkedChanged_: function() { | 94 checkedChanged_: function() { |
| 68 if (!this.pref) | 95 if (!this.pref || this.noSetPref) |
| 69 return; | 96 return; |
| 70 /** @type {boolean} */ var newValue = this.getNewValue_(this.checked); | 97 this.sendPrefChange(); |
| 71 // Ensure that newValue is the correct type for the pref type, either | |
| 72 // a boolean or a number. | |
| 73 if (this.pref.type == chrome.settingsPrivate.PrefType.NUMBER) { | |
| 74 this.set('pref.value', newValue ? 1 : 0); | |
| 75 return; | |
| 76 } | |
| 77 this.set('pref.value', newValue); | |
| 78 }, | 98 }, |
| 79 | 99 |
| 80 /** | 100 /** |
| 81 * @param {*} value | 101 * @param {*} value |
| 82 * @return {boolean} The value as a boolean, inverted if |inverted| is true. | 102 * @return {boolean} The value as a boolean, inverted if |inverted| is true. |
| 83 * @private | 103 * @private |
| 84 */ | 104 */ |
| 85 getNewValue_: function(value) { | 105 getNewValue_: function(value) { |
| 86 return this.inverted ? !value : !!value; | 106 return this.inverted ? !value : !!value; |
| 87 }, | 107 }, |
| 88 | 108 |
| 89 /** | 109 /** |
| 90 * @param {boolean} disabled | 110 * @param {boolean} disabled |
| 91 * @param {!chrome.settingsPrivate.PrefObject} pref | 111 * @param {!chrome.settingsPrivate.PrefObject} pref |
| 92 * @return {boolean} Whether the checkbox should be disabled. | 112 * @return {boolean} Whether the checkbox should be disabled. |
| 93 * @private | 113 * @private |
| 94 */ | 114 */ |
| 95 controlDisabled_: function(disabled, pref) { | 115 controlDisabled_: function(disabled, pref) { |
| 96 return disabled || this.isPrefPolicyControlled(pref); | 116 return disabled || this.isPrefPolicyControlled(pref); |
| 97 }, | 117 }, |
| 98 }; | 118 }; |
| 99 | 119 |
| 100 /** @polymerBehavior */ | 120 /** @polymerBehavior */ |
| 101 var SettingsBooleanControlBehavior = [ | 121 var SettingsBooleanControlBehavior = [ |
| 102 CrPolicyPrefBehavior, | 122 CrPolicyPrefBehavior, |
| 103 PrefControlBehavior, | 123 PrefControlBehavior, |
| 104 SettingsBooleanControlBehaviorImpl, | 124 SettingsBooleanControlBehaviorImpl, |
| 105 ]; | 125 ]; |
| OLD | NEW |