Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(210)

Side by Side Diff: chrome/browser/resources/settings/controls/settings_boolean_control_behavior.js

Issue 2692213005: Reland: MD Settings: Fix the Network Prediction toggle box. (Closed)
Patch Set: Fix infinite loop issue Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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: {
Dan Beam 2017/02/15 23:25:53 nit: does readOnly: true, work here?
tommycli 2017/02/15 23:36:52 Done. Did a quick scan of the code. Doesn't look l
65 type: Number,
66 value: 0,
67 }
58 }, 68 },
59 69
60 observers: [ 70 observers: [
61 'prefValueChanged_(pref.value)', 71 'prefValueChanged_(pref.value)',
62 ], 72 ],
63 73
64 notifyChangedByUserInteraction: function() { 74 notifyChangedByUserInteraction: function() {
65 this.fire('settings-boolean-control-change'); 75 this.fire('settings-boolean-control-change');
76
77 if (!this.pref || this.noSetPref)
78 return;
79 this.sendPrefChange();
66 }, 80 },
67 81
68 /** Reset the checked state to match the current pref value. */ 82 /** Reset the checked state to match the current pref value. */
69 resetToPrefValue: function() { 83 resetToPrefValue: function() {
70 this.checked = this.getNewValue_(this.pref.value); 84 this.checked = this.getNewValue_(this.pref.value);
71 }, 85 },
72 86
73 /** Update the pref to the current |checked| value. */ 87 /** Update the pref to the current |checked| value. */
74 sendPrefChange: function() { 88 sendPrefChange: function() {
75 /** @type {boolean} */ var newValue = this.getNewValue_(this.checked);
76 // Ensure that newValue is the correct type for the pref type, either 89 // Ensure that newValue is the correct type for the pref type, either
77 // a boolean or a number. 90 // a boolean or a number.
78 if (this.pref.type == chrome.settingsPrivate.PrefType.NUMBER) { 91 if (this.pref.type == chrome.settingsPrivate.PrefType.NUMBER) {
79 this.set('pref.value', newValue ? 1 : 0); 92 this.set('pref.value', this.checked ? 1 : this.numericUncheckedValue);
80 return; 93 return;
81 } 94 }
82 this.set('pref.value', newValue); 95 this.set('pref.value', this.checked);
83 }, 96 },
84 97
85 /** 98 /**
86 * Polymer observer for pref.value. 99 * Polymer observer for pref.value.
87 * @param {*} prefValue 100 * @param {*} prefValue
88 * @private 101 * @private
89 */ 102 */
90 prefValueChanged_: function(prefValue) { 103 prefValueChanged_: function(prefValue) {
91 this.checked = this.getNewValue_(prefValue); 104 this.checked = this.getNewValue_(prefValue);
92 }, 105 },
93 106
94 /** 107 /**
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 108 * @param {*} value
106 * @return {boolean} The value as a boolean, inverted if |inverted| is true. 109 * @return {boolean} The value as a boolean, inverted if |inverted| is true.
107 * @private 110 * @private
108 */ 111 */
109 getNewValue_: function(value) { 112 getNewValue_: function(value) {
113 // For numeric prefs, the control is only false if the value is exactly
114 // equal to the unchecked-equivalent value.
115 if (this.pref.type == chrome.settingsPrivate.PrefType.NUMBER)
116 value = value != this.numericUncheckedValue;
117
110 return this.inverted ? !value : !!value; 118 return this.inverted ? !value : !!value;
111 }, 119 },
112 120
113 /** 121 /**
114 * @return {boolean} Whether the checkbox should be disabled. 122 * @return {boolean} Whether the checkbox should be disabled.
115 * @private 123 * @private
116 */ 124 */
117 controlDisabled_: function() { 125 controlDisabled_: function() {
118 return this.disabled || this.isPrefPolicyControlled(assert(this.pref)); 126 return this.disabled || this.isPrefPolicyControlled(assert(this.pref));
119 }, 127 },
120 }; 128 };
121 129
122 /** @polymerBehavior */ 130 /** @polymerBehavior */
123 var SettingsBooleanControlBehavior = [ 131 var SettingsBooleanControlBehavior = [
124 CrPolicyPrefBehavior, 132 CrPolicyPrefBehavior,
125 PrefControlBehavior, 133 PrefControlBehavior,
126 SettingsBooleanControlBehaviorImpl, 134 SettingsBooleanControlBehaviorImpl,
127 ]; 135 ];
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698