Chromium Code Reviews| 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, | |
| 45 reflectToAttribute: true, | |
|
michaelpg
2016/11/15 22:22:04
Unnecessary unless you're styling based on this or
stevenjb
2016/11/17 01:43:28
Done.
| |
| 35 }, | 46 }, |
| 36 | 47 |
| 37 /** The main label. */ | 48 /** The main label. */ |
| 38 label: { | 49 label: { |
| 39 type: String, | 50 type: String, |
| 40 value: '', | 51 value: '', |
| 41 }, | 52 }, |
| 42 | 53 |
| 43 /** Additional (optional) sub-label. */ | 54 /** Additional (optional) sub-label. */ |
| 44 subLabel: { | 55 subLabel: { |
| 45 type: String, | 56 type: String, |
| 46 value: '', | 57 value: '', |
| 47 }, | 58 }, |
| 48 }, | 59 }, |
| 49 | 60 |
| 50 observers: [ | 61 observers: [ |
| 51 'prefValueChanged_(pref.value)' | 62 'prefValueChanged_(pref.value)', |
| 52 ], | 63 ], |
| 53 | 64 |
| 65 /** Reset the checked state to match the current pref value. */ | |
| 66 resetToPrefValue: function() { | |
| 67 this.checked = this.getNewValue_(this.pref.value); | |
| 68 }, | |
| 69 | |
| 70 /** Update the pref to the current |checked| value. */ | |
| 71 sendPrefChange: function() { | |
| 72 /** @type {boolean} */ var newValue = this.getNewValue_(this.checked); | |
| 73 // Ensure that newValue is the correct type for the pref type, either | |
| 74 // a boolean or a number. | |
| 75 if (this.pref.type == chrome.settingsPrivate.PrefType.NUMBER) { | |
| 76 this.set('pref.value', newValue ? 1 : 0); | |
| 77 return; | |
| 78 } | |
| 79 this.set('pref.value', newValue); | |
| 80 }, | |
| 81 | |
| 54 /** | 82 /** |
| 55 * Polymer observer for pref.value. | 83 * Polymer observer for pref.value. |
| 56 * @param {*} prefValue | 84 * @param {*} prefValue |
| 57 * @private | 85 * @private |
| 58 */ | 86 */ |
| 59 prefValueChanged_: function(prefValue) { | 87 prefValueChanged_: function(prefValue) { |
| 60 this.checked = this.getNewValue_(prefValue); | 88 this.checked = this.getNewValue_(prefValue); |
| 61 }, | 89 }, |
| 62 | 90 |
| 63 /** | 91 /** |
| 64 * Polymer observer for checked. | 92 * Polymer observer for checked. |
| 65 * @private | 93 * @private |
| 66 */ | 94 */ |
| 67 checkedChanged_: function() { | 95 checkedChanged_: function() { |
| 68 if (!this.pref) | 96 if (!this.pref || this.noSetPref) |
| 69 return; | 97 return; |
| 70 /** @type {boolean} */ var newValue = this.getNewValue_(this.checked); | 98 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 }, | 99 }, |
| 79 | 100 |
| 80 /** | 101 /** |
| 81 * @param {*} value | 102 * @param {*} value |
| 82 * @return {boolean} The value as a boolean, inverted if |inverted| is true. | 103 * @return {boolean} The value as a boolean, inverted if |inverted| is true. |
| 83 * @private | 104 * @private |
| 84 */ | 105 */ |
| 85 getNewValue_: function(value) { | 106 getNewValue_: function(value) { |
| 86 return this.inverted ? !value : !!value; | 107 return this.inverted ? !value : !!value; |
| 87 }, | 108 }, |
| 88 | 109 |
| 89 /** | 110 /** |
| 90 * @param {boolean} disabled | 111 * @param {boolean} disabled |
| 91 * @param {!chrome.settingsPrivate.PrefObject} pref | 112 * @param {!chrome.settingsPrivate.PrefObject} pref |
| 92 * @return {boolean} Whether the checkbox should be disabled. | 113 * @return {boolean} Whether the checkbox should be disabled. |
| 93 * @private | 114 * @private |
| 94 */ | 115 */ |
| 95 controlDisabled_: function(disabled, pref) { | 116 controlDisabled_: function(disabled, pref) { |
| 96 return disabled || this.isPrefPolicyControlled(pref); | 117 return disabled || this.isPrefPolicyControlled(pref); |
| 97 }, | 118 }, |
| 98 }; | 119 }; |
| 99 | 120 |
| 100 /** @polymerBehavior */ | 121 /** @polymerBehavior */ |
| 101 var SettingsBooleanControlBehavior = [ | 122 var SettingsBooleanControlBehavior = [ |
| 102 CrPolicyPrefBehavior, | 123 CrPolicyPrefBehavior, |
| 103 PrefControlBehavior, | 124 PrefControlBehavior, |
| 104 SettingsBooleanControlBehaviorImpl, | 125 SettingsBooleanControlBehaviorImpl, |
| 105 ]; | 126 ]; |
| OLD | NEW |