OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 cr.define('options', function() { | 5 cr.define('options', function() { |
6 var Preferences = options.Preferences; | 6 var Preferences = options.Preferences; |
7 | 7 |
8 /** | 8 /** |
9 * A controlled setting indicator that can be placed on a setting as an | 9 * A controlled setting indicator that can be placed on a setting as an |
10 * indicator that the value is controlled by some external entity such as | 10 * indicator that the value is controlled by some external entity such as |
11 * policy or an extension. | 11 * policy or an extension. |
12 * @constructor | 12 * @constructor |
13 * @extends {HTMLSpanElement} | 13 * @extends {HTMLSpanElement} |
14 */ | 14 */ |
15 var ControlledSettingIndicator = cr.ui.define('span'); | 15 var ControlledSettingIndicator = cr.ui.define('span'); |
16 | 16 |
17 ControlledSettingIndicator.prototype = { | 17 ControlledSettingIndicator.prototype = { |
18 __proto__: HTMLSpanElement.prototype, | 18 __proto__: HTMLSpanElement.prototype, |
19 | 19 |
20 /** | 20 /** |
21 * Decorates the base element to show the proper icon. | 21 * Decorates the base element to show the proper icon. |
22 */ | 22 */ |
23 decorate: function() { | 23 decorate: function() { |
24 var self = this; | 24 var self = this; |
25 | 25 |
26 // If there is a pref, track its controlledBy property in order to be able | 26 // If there is a pref, track its controlledBy and recommendedValue |
27 // to bring up the correct bubble. | 27 // properties in order to be able to bring up the correct bubble. |
28 if (this.pref) { | 28 if (this.pref) { |
29 Preferences.getInstance().addEventListener(this.pref, | 29 Preferences.getInstance().addEventListener( |
30 function(event) { | 30 this.pref, this.handlePrefChange.bind(this)); |
31 var controlledBy = event.value.controlledBy; | |
32 self.controlledBy = controlledBy ? controlledBy : null; | |
33 OptionsPage.hideBubble(); | |
34 }); | |
35 this.resetHandler = this.clearAssociatedPref_; | 31 this.resetHandler = this.clearAssociatedPref_; |
36 } | 32 } |
37 | 33 |
38 this.tabIndex = 0; | 34 this.tabIndex = 0; |
39 this.setAttribute('role', 'button'); | 35 this.setAttribute('role', 'button'); |
40 this.addEventListener('click', this); | 36 this.addEventListener('click', this); |
41 this.addEventListener('keydown', this); | 37 this.addEventListener('keydown', this); |
42 this.addEventListener('mousedown', this); | 38 this.addEventListener('mousedown', this); |
43 }, | 39 }, |
44 | 40 |
45 /** | 41 /** |
46 * The given handler will be called when the user clicks on the 'reset to | 42 * The given handler will be called when the user clicks on the 'reset to |
47 * recommended value' link shown in the indicator bubble. | 43 * recommended value' link shown in the indicator bubble. The |this| object |
44 * will be the indicator itself. | |
48 * @param {function()} handler The handler to be called. | 45 * @param {function()} handler The handler to be called. |
49 */ | 46 */ |
50 set resetHandler(handler) { | 47 set resetHandler(handler) { |
51 this.resetHandler_ = handler; | 48 this.resetHandler_ = handler; |
52 }, | 49 }, |
53 | 50 |
54 /** | 51 /** |
55 * Whether the indicator is currently showing a bubble. | 52 * Whether the indicator is currently showing a bubble. |
56 * @type {boolean} | 53 * @type {boolean} |
57 */ | 54 */ |
58 get showingBubble() { | 55 get showingBubble() { |
59 return !!this.showingBubble_; | 56 return !!this.showingBubble_; |
60 }, | 57 }, |
61 set showingBubble(showing) { | 58 set showingBubble(showing) { |
62 if (showing) | 59 if (showing) |
63 this.classList.add('showing-bubble'); | 60 this.classList.add('showing-bubble'); |
64 else | 61 else |
65 this.classList.remove('showing-bubble'); | 62 this.classList.remove('showing-bubble'); |
66 this.showingBubble_ = showing; | 63 this.showingBubble_ = showing; |
67 }, | 64 }, |
68 | 65 |
69 /** | 66 /** |
70 * Clears the preference associated with this indicator. | 67 * Clears the preference associated with this indicator. |
71 * @private | 68 * @private |
72 */ | 69 */ |
73 clearAssociatedPref_: function() { | 70 clearAssociatedPref_: function() { |
74 Preferences.clearPref(this.pref, this.dialogPref); | 71 Preferences.clearPref(this.pref, !this.dialogPref); |
72 }, | |
73 | |
74 /* Handle changes to the associated pref by hiding any currently visible | |
75 * bubble and updating the controlledBy property. | |
76 * @param {Event} event Pref change event. | |
77 */ | |
78 handlePrefChange: function(event) { | |
79 OptionsPage.hideBubble(); | |
80 if (event.value.controlledBy) { | |
81 this.controlledBy = | |
82 !this.value || String(event.value.value) == this.value ? | |
83 event.value.controlledBy : null; | |
84 } else if (event.value.recommendedValue != undefined) { | |
85 this.controlledBy = | |
86 !this.value || String(event.value.recommendedValue) == this.value ? | |
87 'hasRecommendation' : null; | |
88 } else { | |
89 this.controlledBy = null; | |
Vitaly Pavlenko
2014/09/15 22:35:51
Hi,
I'm compiling Chrome JS with Closure Compiler
bartfab (slow)
2014/09/16 14:26:08
|controlledBy| controls two things:
1) If the pro
bartfab (slow)
2014/09/17 08:18:36
https://code.google.com/p/chromium/codesearch#chro
| |
90 } | |
75 }, | 91 }, |
76 | 92 |
77 /** | 93 /** |
78 * Handle mouse and keyboard events, allowing the user to open and close a | 94 * Handle mouse and keyboard events, allowing the user to open and close a |
79 * bubble with further information. | 95 * bubble with further information. |
80 * @param {Event} event Mouse or keyboard event. | 96 * @param {Event} event Mouse or keyboard event. |
81 */ | 97 */ |
82 handleEvent: function(event) { | 98 handleEvent: function(event) { |
83 switch (event.type) { | 99 switch (event.type) { |
84 // Toggle the bubble on left click. Let any other clicks propagate. | 100 // Toggle the bubble on left click. Let any other clicks propagate. |
(...skipping 28 matching lines...) Expand all Loading... | |
113 /** | 129 /** |
114 * Open or close a bubble with further information about the pref. | 130 * Open or close a bubble with further information about the pref. |
115 * @private | 131 * @private |
116 */ | 132 */ |
117 toggleBubble_: function() { | 133 toggleBubble_: function() { |
118 if (this.showingBubble) { | 134 if (this.showingBubble) { |
119 OptionsPage.hideBubble(); | 135 OptionsPage.hideBubble(); |
120 } else { | 136 } else { |
121 var self = this; | 137 var self = this; |
122 | 138 |
123 // Work out the popup text. | 139 // Construct the bubble text. |
124 defaultStrings = { | 140 defaultStrings = { |
125 'policy': loadTimeData.getString('controlledSettingPolicy'), | 141 'policy': loadTimeData.getString('controlledSettingPolicy'), |
126 'extension': loadTimeData.getString('controlledSettingExtension'), | 142 'extension': loadTimeData.getString('controlledSettingExtension'), |
127 'recommended': loadTimeData.getString('controlledSettingRecommended'), | 143 'recommended': loadTimeData.getString('controlledSettingRecommended'), |
144 'hasRecommendation': | |
145 loadTimeData.getString('controlledSettingHasRecommendation'), | |
128 }; | 146 }; |
129 | 147 |
130 // No controller, no popup. | 148 // No controller, no bubble. |
131 if (!this.controlledBy || !(this.controlledBy in defaultStrings)) | 149 if (!this.controlledBy || !(this.controlledBy in defaultStrings)) |
132 return; | 150 return; |
133 | 151 |
134 var text = defaultStrings[this.controlledBy]; | 152 var text = defaultStrings[this.controlledBy]; |
135 | 153 |
136 // Apply text overrides. | 154 // Apply text overrides. |
137 if (this.hasAttribute('text' + this.controlledBy)) | 155 if (this.hasAttribute('text' + this.controlledBy)) |
138 text = this.getAttribute('text' + this.controlledBy); | 156 text = this.getAttribute('text' + this.controlledBy); |
139 | 157 |
140 // Create the DOM tree. | 158 // Create the DOM tree. |
141 var content = document.createElement('div'); | 159 var content = document.createElement('div'); |
142 content.className = 'controlled-setting-bubble-content'; | 160 content.className = 'controlled-setting-bubble-content'; |
143 content.setAttribute('controlled-by', this.controlledBy); | 161 content.setAttribute('controlled-by', this.controlledBy); |
144 content.textContent = text; | 162 content.textContent = text; |
145 | 163 |
146 if (this.controlledBy == 'recommended' && this.resetHandler_) { | 164 if (this.controlledBy == 'hasRecommendation' && this.resetHandler_ && |
165 !this.readOnly) { | |
147 var container = document.createElement('div'); | 166 var container = document.createElement('div'); |
148 var action = document.createElement('button'); | 167 var action = document.createElement('button'); |
149 action.classList.add('link-button'); | 168 action.classList.add('link-button'); |
150 action.classList.add('controlled-setting-bubble-action'); | 169 action.classList.add('controlled-setting-bubble-action'); |
151 action.textContent = | 170 action.textContent = |
152 loadTimeData.getString('controlledSettingApplyRecommendation'); | 171 loadTimeData.getString('controlledSettingFollowRecommendation'); |
153 action.addEventListener('click', function(event) { | 172 action.addEventListener('click', function(event) { |
154 self.resetHandler_(); | 173 self.resetHandler_(); |
155 }); | 174 }); |
156 container.appendChild(action); | 175 container.appendChild(action); |
157 content.appendChild(container); | 176 content.appendChild(container); |
158 } | 177 } |
159 | 178 |
160 OptionsPage.showBubble(content, this); | 179 OptionsPage.showBubble(content, this); |
161 } | 180 } |
162 }, | 181 }, |
(...skipping 10 matching lines...) Expand all Loading... | |
173 * associated preference take effect in the settings UI immediately but are | 192 * associated preference take effect in the settings UI immediately but are |
174 * only actually committed when the user confirms the dialog. If the user | 193 * only actually committed when the user confirms the dialog. If the user |
175 * cancels the dialog instead, the changes are rolled back in the settings UI | 194 * cancels the dialog instead, the changes are rolled back in the settings UI |
176 * and never committed. | 195 * and never committed. |
177 * @type {boolean} | 196 * @type {boolean} |
178 */ | 197 */ |
179 cr.defineProperty(ControlledSettingIndicator, 'dialogPref', | 198 cr.defineProperty(ControlledSettingIndicator, 'dialogPref', |
180 cr.PropertyKind.BOOL_ATTR); | 199 cr.PropertyKind.BOOL_ATTR); |
181 | 200 |
182 /** | 201 /** |
183 * Whether the associated preference is controlled by a source other than the | 202 * The value of the associated preference that the indicator represents. If |
184 * user's setting (can be 'policy', 'extension', 'recommended' or unset). | 203 * this is not set, the indicator will be visible whenever any value is |
204 * enforced or recommended. If it is set, the indicator will be visible only | |
205 * when the enforced or recommended value matches the value it represents. | |
206 * This allows multiple indicators to be created for a set of radio buttons, | |
207 * ensuring that only one of them is visible at a time. | |
208 */ | |
209 cr.defineProperty(ControlledSettingIndicator, 'value', | |
210 cr.PropertyKind.ATTR); | |
211 | |
212 /** | |
213 * The status of the associated preference: | |
214 * - 'policy': A specific value is enfoced by policy. | |
215 * - 'extension': A specific value is enforced by an extension. | |
216 * - 'recommended': A value is recommended by policy. The user could | |
217 * override this recommendation but has not done so. | |
218 * - 'hasRecommendation': A value is recommended by policy. The user has | |
219 * overridden this recommendation. | |
220 * - unset: The value is controlled by the user alone. | |
185 * @type {string} | 221 * @type {string} |
186 */ | 222 */ |
187 cr.defineProperty(ControlledSettingIndicator, 'controlledBy', | 223 cr.defineProperty(ControlledSettingIndicator, 'controlledBy', |
188 cr.PropertyKind.ATTR); | 224 cr.PropertyKind.ATTR); |
189 | 225 |
190 // Export. | 226 // Export. |
191 return { | 227 return { |
192 ControlledSettingIndicator: ControlledSettingIndicator | 228 ControlledSettingIndicator: ControlledSettingIndicator |
193 }; | 229 }; |
194 }); | 230 }); |
OLD | NEW |