Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 * `cr-settings-checkbox` is a checkbox that controls a supplied preference. | 7 * `cr-settings-checkbox` is a checkbox that controls a supplied preference. |
| 8 * | 8 * |
| 9 * Example: | 9 * Example: |
| 10 * <cr-settings-checkbox pref="{{prefs.settings.enableFoo}}" | 10 * <cr-settings-checkbox pref="{{prefs.settings.enableFoo}}" |
| 11 * label="Enable foo setting." subLabel="(bar also)"> | 11 * label="Enable foo setting." subLabel="(bar also)"> |
| 12 * </cr-settings-checkbox> | 12 * </cr-settings-checkbox> |
| 13 * | 13 * |
| 14 * @element cr-settings-checkbox | 14 * @element cr-settings-checkbox |
| 15 */ | 15 */ |
| 16 Polymer({ | 16 Polymer({ |
| 17 is: 'cr-settings-checkbox', | 17 is: 'cr-settings-checkbox', |
| 18 | 18 |
| 19 properties: { | 19 properties: { |
| 20 /** | 20 /** |
| 21 * The boolean preference object to control. | 21 * The boolean preference object to control. |
| 22 * @type {?PrefObject} | 22 * @type {?chrome.settingsPrivate.PrefObject} |
| 23 */ | 23 */ |
| 24 pref: { | 24 pref: { |
| 25 type: Object, | 25 type: Object, |
| 26 notify: true | 26 notify: true, |
| 27 value: null | |
|
Dan Beam
2015/08/28 00:20:22
nit: we are not IE, feel free to add trailing , so
stevenjb
2015/08/28 23:18:07
I forget where it's OK and where it's not; I think
michaelpg
2015/09/14 05:14:51
also not OK in JSON
| |
| 27 }, | 28 }, |
| 28 | 29 |
| 30 /** Whether the checkbox should represent the inverted value. */ | |
| 29 inverted: { | 31 inverted: { |
| 30 type: Boolean, | 32 type: Boolean, |
| 31 value: false | 33 value: false |
| 32 }, | 34 }, |
| 33 | 35 |
| 36 /** Whether the checkbox is checked. */ | |
| 34 checked: { | 37 checked: { |
| 35 type: Boolean, | 38 type: Boolean, |
| 36 value: false, | 39 value: false, |
| 37 observer: 'checkedChanged_' | 40 observer: 'checkedChanged_' |
| 38 }, | 41 }, |
| 39 | 42 |
| 43 /** Set to disable the element. */ | |
| 44 disabled: { | |
| 45 type: Boolean, | |
| 46 value: false, | |
| 47 observer: 'disabledChanged_' | |
| 48 }, | |
| 49 | |
| 50 /** The disabled state of the checkbox (may be set by policy). */ | |
| 51 checkboxDisabled: { | |
| 52 type: Boolean, | |
| 53 value: false | |
| 54 }, | |
| 55 | |
| 56 /** Checkbox label. */ | |
| 40 label: { | 57 label: { |
| 41 type: String, | 58 type: String, |
| 42 value: '', | 59 value: '', |
| 43 }, | 60 }, |
| 44 | 61 |
| 62 /** Additional sub-label for the checkbox. */ | |
| 45 subLabel: { | 63 subLabel: { |
| 46 type: String, | 64 type: String, |
| 47 value: '', | 65 value: '', |
| 48 }, | 66 }, |
| 49 }, | 67 }, |
| 50 | 68 |
| 51 observers: [ | 69 observers: [ |
| 52 'prefValueChanged_(pref.value)' | 70 'prefPropertyChanged_(pref.*)' |
| 53 ], | 71 ], |
| 54 | 72 |
| 55 /** @override */ | 73 /** @override */ |
| 56 ready: function() { | 74 ready: function() { |
| 57 this.$.events.forward(this.$.checkbox, ['change']); | 75 this.$.events.forward(this.$.checkbox, ['change']); |
| 58 }, | 76 }, |
| 59 | 77 |
| 60 /** @private */ | 78 /** |
| 61 prefValueChanged_: function(prefValue) { | 79 * Polymer observer for pref. |
| 62 // prefValue is initially undefined when Polymer initializes pref. | 80 * @param {?{path: string, value: *}} change |
| 63 if (prefValue !== undefined) { | 81 * @private |
| 64 this.checked = this.getNewValue_(prefValue); | 82 */ |
| 83 prefPropertyChanged_: function(change) { | |
| 84 if (change.path == 'pref.value') | |
| 85 this.checked = this.getNewValue_(change.value); | |
| 86 if (change.path == 'pref.policySource' || | |
|
michaelpg
2015/08/27 23:40:01
else if
stevenjb
2015/08/28 23:18:07
Done.
| |
| 87 change.path == 'pref.policyEnforcement') { | |
| 88 this.checkboxDisabled = | |
| 89 this.disabled || this.isSettingDisabled_(this.pref); | |
| 65 } | 90 } |
| 66 }, | 91 }, |
| 67 | 92 |
| 68 /** @private */ | 93 /** |
| 94 * Polymer observer for checked. | |
| 95 * @private | |
| 96 */ | |
| 69 checkedChanged_: function() { | 97 checkedChanged_: function() { |
| 70 if (this.pref) { | 98 if (!this.pref) |
| 71 this.pref.value = this.getNewValue_(this.checked); | 99 return; |
| 72 } | 100 this.pref.value = this.getNewValue_(this.checked); |
| 73 }, | 101 }, |
| 74 | 102 |
| 75 /** @private */ | 103 /** |
| 76 getNewValue_: function(val) { | 104 * Polymer observer for disabled. |
| 77 return this.inverted ? !val : val; | 105 * @private |
| 78 } | 106 */ |
| 107 disabledChanged_: function() { | |
| 108 this.checkboxDisabled = this.disabled || this.isSettingDisabled_(this.pref); | |
|
michaelpg
2015/08/27 23:40:01
nit: instead of adding listeners like disabledChan
stevenjb
2015/08/28 23:18:07
Probably... I'll look into that after I merge with
michaelpg
2015/09/14 05:14:51
could you revisit this? I think having a computed
stevenjb
2015/09/14 23:05:30
Done.
| |
| 109 }, | |
| 110 | |
| 111 /** | |
| 112 * @param {*} value | |
| 113 * @return {boolean} The value as a boolean, inverted if |inverted| is true. | |
| 114 * @private | |
| 115 */ | |
| 116 getNewValue_: function(value) { | |
| 117 return this.inverted ? !value : !!value; | |
| 118 }, | |
| 119 | |
| 120 /** | |
| 121 * @param {?chrome.settingsPrivate.PrefObject} pref | |
| 122 * @return {boolean} True if the setting is either explicitly disabled, or | |
| 123 * disabled by the policyEnforcement property. | |
| 124 */ | |
| 125 isSettingDisabled_: function(pref) { | |
| 126 return !!pref && pref.policyEnforcement == | |
| 127 chrome.settingsPrivate.PolicyEnforcement.ENFORCED; | |
| 128 }, | |
| 79 }); | 129 }); |
| OLD | NEW |