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 {?chrome.settingsPrivate.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 | 27 value: null, |
28 }, | 28 }, |
29 | 29 |
30 /** Whether the checkbox should represent the inverted value. */ | |
30 inverted: { | 31 inverted: { |
31 type: Boolean, | 32 type: Boolean, |
32 value: false | 33 value: false, |
33 }, | 34 }, |
34 | 35 |
36 /** Whether the checkbox is checked. */ | |
35 checked: { | 37 checked: { |
36 type: Boolean, | 38 type: Boolean, |
37 value: false, | 39 value: false, |
38 observer: 'checkedChanged_' | 40 observer: 'checkedChanged_', |
39 }, | 41 }, |
40 | 42 |
43 /** Set to disable the element programatically. */ | |
michaelpg
2015/09/14 05:14:51
programatically... or via HTML, like any other pro
stevenjb
2015/09/14 23:05:31
Simplified not that checkboxDisabled_ is a method.
| |
44 disabled: { | |
45 type: Boolean, | |
46 value: false, | |
47 observer: 'disabledChanged_', | |
48 }, | |
49 | |
50 /** | |
51 * Internal disabled state of the checkbox. Always false if |disabled| is | |
52 * false, otherwise may be set to false by a pref policy. | |
michaelpg
2015/09/14 05:14:51
if you're going to keep this, make it @private
stevenjb
2015/09/14 23:05:31
Acknowledged.
| |
53 */ | |
54 checkboxDisabled: { | |
55 type: Boolean, | |
56 value: false, | |
57 }, | |
58 | |
59 /** Checkbox label. */ | |
41 label: { | 60 label: { |
42 type: String, | 61 type: String, |
43 value: '', | 62 value: '', |
44 }, | 63 }, |
45 | 64 |
65 /** Additional sub-label for the checkbox. */ | |
46 subLabel: { | 66 subLabel: { |
47 type: String, | 67 type: String, |
48 value: '', | 68 value: '', |
49 }, | 69 }, |
50 }, | 70 }, |
51 | 71 |
52 observers: [ | 72 observers: [ |
53 'prefValueChanged_(pref.value)' | 73 'prefPropertyChanged_(pref.*)' |
54 ], | 74 ], |
55 | 75 |
56 /** @override */ | 76 /** @override */ |
57 ready: function() { | 77 ready: function() { |
58 this.$.events.forward(this.$.checkbox, ['change']); | 78 this.$.events.forward(this.$.checkbox, ['change']); |
59 }, | 79 }, |
60 | 80 |
61 /** @private */ | 81 /** |
62 prefValueChanged_: function(prefValue) { | 82 * Polymer observer for pref. |
63 // prefValue is initially undefined when Polymer initializes pref. | 83 * @param {?{path: string, value: *}} change |
64 if (prefValue !== undefined) { | 84 * @private |
65 this.checked = this.getNewValue_(prefValue); | 85 */ |
86 prefPropertyChanged_: function(change) { | |
87 if (!this.pref) | |
88 return; | |
89 var path = change.path; | |
90 if (path == 'pref' || path == 'pref.value') { | |
91 this.checked = this.getNewValue_(this.pref.value); | |
92 } | |
93 if (path == 'pref' || path == 'pref.policySource' || | |
94 path == 'pref.policyEnforcement') { | |
95 this.checkboxDisabled = | |
96 this.disabled || this.isSettingDisabled_(this.pref); | |
66 } | 97 } |
67 }, | 98 }, |
68 | 99 |
69 /** @private */ | 100 /** |
101 * Polymer observer for checked. | |
102 * @private | |
103 */ | |
70 checkedChanged_: function() { | 104 checkedChanged_: function() { |
71 if (this.pref) { | 105 if (!this.pref) |
72 this.set('pref.value', this.getNewValue_(this.checked)); | 106 return; |
73 } | 107 this.set('pref.value', this.getNewValue_(this.checked)); |
michaelpg
2015/09/14 05:14:51
this.set('pref.value', foo) does nothing if this.p
stevenjb
2015/09/14 23:05:31
I only changed this to match disabledChanged_, whi
| |
74 }, | 108 }, |
75 | 109 |
76 /** @private */ | 110 /** |
77 getNewValue_: function(val) { | 111 * Polymer observer for disabled. |
78 return this.inverted ? !val : val; | 112 * @private |
79 } | 113 */ |
114 disabledChanged_: function() { | |
115 if (!this.pref) | |
116 return; | |
117 this.checkboxDisabled = this.disabled || this.isSettingDisabled_(this.pref); | |
118 }, | |
119 | |
120 /** | |
121 * @param {*} value | |
122 * @return {boolean} The value as a boolean, inverted if |inverted| is true. | |
123 * @private | |
124 */ | |
125 getNewValue_: function(value) { | |
126 return this.inverted ? !value : !!value; | |
127 }, | |
128 | |
129 /** | |
130 * @param {?chrome.settingsPrivate.PrefObject} pref | |
131 * @return {boolean} True if the setting is either explicitly disabled, or | |
132 * disabled by the policyEnforcement property. | |
133 */ | |
134 isSettingDisabled_: function(pref) { | |
135 return !!pref && pref.policyEnforcement == | |
136 chrome.settingsPrivate.PolicyEnforcement.ENFORCED; | |
137 }, | |
80 }); | 138 }); |
OLD | NEW |