| 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_', | |
| 26 reflectToAttribute: true, | 25 reflectToAttribute: true, |
| 27 }, | 26 }, |
| 28 | 27 |
| 29 /** Disabled property for the element. */ | 28 /** Disabled property for the element. */ |
| 30 disabled: { | 29 disabled: { |
| 31 type: Boolean, | 30 type: Boolean, |
| 32 value: false, | 31 value: false, |
| 33 notify: true, | 32 notify: true, |
| 34 reflectToAttribute: true, | 33 reflectToAttribute: true, |
| 35 }, | 34 }, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 48 label: { | 47 label: { |
| 49 type: String, | 48 type: String, |
| 50 value: '', | 49 value: '', |
| 51 }, | 50 }, |
| 52 | 51 |
| 53 /** Additional (optional) sub-label. */ | 52 /** Additional (optional) sub-label. */ |
| 54 subLabel: { | 53 subLabel: { |
| 55 type: String, | 54 type: String, |
| 56 value: '', | 55 value: '', |
| 57 }, | 56 }, |
| 57 |
| 58 /** |
| 59 * For numeric prefs only, the integer value equivalent to the unchecked |
| 60 * state. This is the value sent to prefs if the user unchecks the control. |
| 61 * During initialization, the control is unchecked if and only if the pref |
| 62 * value is equal to the this value. (Values 2, 3, 4, etc. all are checked.) |
| 63 */ |
| 64 numericUncheckedValue: { |
| 65 type: Number, |
| 66 readOnly: true, |
| 67 value: 0, |
| 68 } |
| 58 }, | 69 }, |
| 59 | 70 |
| 60 observers: [ | 71 observers: [ |
| 61 'prefValueChanged_(pref.value)', | 72 'prefValueChanged_(pref.value)', |
| 62 ], | 73 ], |
| 63 | 74 |
| 64 notifyChangedByUserInteraction: function() { | 75 notifyChangedByUserInteraction: function() { |
| 65 this.fire('settings-boolean-control-change'); | 76 this.fire('settings-boolean-control-change'); |
| 77 |
| 78 if (!this.pref || this.noSetPref) |
| 79 return; |
| 80 this.sendPrefChange(); |
| 66 }, | 81 }, |
| 67 | 82 |
| 68 /** Reset the checked state to match the current pref value. */ | 83 /** Reset the checked state to match the current pref value. */ |
| 69 resetToPrefValue: function() { | 84 resetToPrefValue: function() { |
| 70 this.checked = this.getNewValue_(this.pref.value); | 85 this.checked = this.getNewValue_(this.pref.value); |
| 71 }, | 86 }, |
| 72 | 87 |
| 73 /** Update the pref to the current |checked| value. */ | 88 /** Update the pref to the current |checked| value. */ |
| 74 sendPrefChange: function() { | 89 sendPrefChange: function() { |
| 75 /** @type {boolean} */ var newValue = this.getNewValue_(this.checked); | |
| 76 // Ensure that newValue is the correct type for the pref type, either | 90 // Ensure that newValue is the correct type for the pref type, either |
| 77 // a boolean or a number. | 91 // a boolean or a number. |
| 78 if (this.pref.type == chrome.settingsPrivate.PrefType.NUMBER) { | 92 if (this.pref.type == chrome.settingsPrivate.PrefType.NUMBER) { |
| 79 this.set('pref.value', newValue ? 1 : 0); | 93 this.set('pref.value', this.checked ? 1 : this.numericUncheckedValue); |
| 80 return; | 94 return; |
| 81 } | 95 } |
| 82 this.set('pref.value', newValue); | 96 this.set('pref.value', this.checked); |
| 83 }, | 97 }, |
| 84 | 98 |
| 85 /** | 99 /** |
| 86 * Polymer observer for pref.value. | 100 * Polymer observer for pref.value. |
| 87 * @param {*} prefValue | 101 * @param {*} prefValue |
| 88 * @private | 102 * @private |
| 89 */ | 103 */ |
| 90 prefValueChanged_: function(prefValue) { | 104 prefValueChanged_: function(prefValue) { |
| 91 this.checked = this.getNewValue_(prefValue); | 105 this.checked = this.getNewValue_(prefValue); |
| 92 }, | 106 }, |
| 93 | 107 |
| 94 /** | 108 /** |
| 95 * Polymer observer for checked. | |
| 96 * @private | |
| 97 */ | |
| 98 checkedChanged_: function() { | |
| 99 if (!this.pref || this.noSetPref) | |
| 100 return; | |
| 101 this.sendPrefChange(); | |
| 102 }, | |
| 103 | |
| 104 /** | |
| 105 * @param {*} value | 109 * @param {*} value |
| 106 * @return {boolean} The value as a boolean, inverted if |inverted| is true. | 110 * @return {boolean} The value as a boolean, inverted if |inverted| is true. |
| 107 * @private | 111 * @private |
| 108 */ | 112 */ |
| 109 getNewValue_: function(value) { | 113 getNewValue_: function(value) { |
| 114 // For numeric prefs, the control is only false if the value is exactly |
| 115 // equal to the unchecked-equivalent value. |
| 116 if (this.pref.type == chrome.settingsPrivate.PrefType.NUMBER) |
| 117 value = value != this.numericUncheckedValue; |
| 118 |
| 110 return this.inverted ? !value : !!value; | 119 return this.inverted ? !value : !!value; |
| 111 }, | 120 }, |
| 112 | 121 |
| 113 /** | 122 /** |
| 114 * @return {boolean} Whether the checkbox should be disabled. | 123 * @return {boolean} Whether the checkbox should be disabled. |
| 115 * @private | 124 * @private |
| 116 */ | 125 */ |
| 117 controlDisabled_: function() { | 126 controlDisabled_: function() { |
| 118 return this.disabled || this.isPrefPolicyControlled(assert(this.pref)); | 127 return this.disabled || this.isPrefPolicyControlled(assert(this.pref)); |
| 119 }, | 128 }, |
| 120 }; | 129 }; |
| 121 | 130 |
| 122 /** @polymerBehavior */ | 131 /** @polymerBehavior */ |
| 123 var SettingsBooleanControlBehavior = [ | 132 var SettingsBooleanControlBehavior = [ |
| 124 CrPolicyPrefBehavior, | 133 CrPolicyPrefBehavior, |
| 125 PrefControlBehavior, | 134 PrefControlBehavior, |
| 126 SettingsBooleanControlBehaviorImpl, | 135 SettingsBooleanControlBehaviorImpl, |
| 127 ]; | 136 ]; |
| OLD | NEW |