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