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__: cr.ui.BubbleButton.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 cr.ui.BubbleButton.prototype.decorate.call(this); | 24 var self = this; |
25 this.classList.add('controlled-setting-indicator'); | |
26 | 25 |
27 // If there is a pref, track its controlledBy and recommendedValue | 26 // If there is a pref, track its controlledBy and recommendedValue |
28 // properties in order to be able to bring up the correct bubble. | 27 // properties in order to be able to bring up the correct bubble. |
29 if (this.pref) { | 28 if (this.pref) { |
30 Preferences.getInstance().addEventListener( | 29 Preferences.getInstance().addEventListener( |
31 this.pref, this.handlePrefChange.bind(this)); | 30 this.pref, this.handlePrefChange.bind(this)); |
32 this.resetHandler = this.clearAssociatedPref_; | 31 this.resetHandler = this.clearAssociatedPref_; |
33 } | 32 } |
| 33 |
| 34 this.className = 'controlled-setting-indicator'; |
| 35 this.location = cr.ui.ArrowLocation.TOP_END; |
| 36 this.image = document.createElement('div'); |
| 37 this.image.tabIndex = 0; |
| 38 this.image.setAttribute('role', 'button'); |
| 39 this.image.addEventListener('click', this); |
| 40 this.image.addEventListener('keydown', this); |
| 41 this.image.addEventListener('mousedown', this); |
| 42 this.appendChild(this.image); |
34 }, | 43 }, |
35 | 44 |
36 /** | 45 /** |
37 * The given handler will be called when the user clicks on the 'reset to | 46 * The given handler will be called when the user clicks on the 'reset to |
38 * recommended value' link shown in the indicator bubble. The |this| object | 47 * recommended value' link shown in the indicator bubble. The |this| object |
39 * will be the indicator itself. | 48 * will be the indicator itself. |
40 * @param {function()} handler The handler to be called. | 49 * @param {function()} handler The handler to be called. |
41 */ | 50 */ |
42 set resetHandler(handler) { | 51 set resetHandler(handler) { |
43 this.resetHandler_ = handler; | 52 this.resetHandler_ = handler; |
44 }, | 53 }, |
45 | 54 |
46 /** | 55 /** |
| 56 * Whether the indicator is currently showing a bubble. |
| 57 * @type {boolean} |
| 58 */ |
| 59 get showingBubble() { |
| 60 return this.image.classList.contains('showing-bubble'); |
| 61 }, |
| 62 set showingBubble(showing) { |
| 63 this.image.classList.toggle('showing-bubble', showing); |
| 64 }, |
| 65 |
| 66 /** |
47 * Clears the preference associated with this indicator. | 67 * Clears the preference associated with this indicator. |
48 * @private | 68 * @private |
49 */ | 69 */ |
50 clearAssociatedPref_: function() { | 70 clearAssociatedPref_: function() { |
51 Preferences.clearPref(this.pref, !this.dialogPref); | 71 Preferences.clearPref(this.pref, !this.dialogPref); |
52 }, | 72 }, |
53 | 73 |
54 /* Handle changes to the associated pref by hiding any currently visible | 74 /* Handle changes to the associated pref by hiding any currently visible |
55 * bubble and updating the controlledBy property. | 75 * bubble and updating the controlledBy property. |
56 * @param {Event} event Pref change event. | 76 * @param {Event} event Pref change event. |
57 */ | 77 */ |
58 handlePrefChange: function(event) { | 78 handlePrefChange: function(event) { |
59 OptionsPage.hideBubble(); | 79 OptionsPage.hideBubble(); |
60 if (event.value.controlledBy) { | 80 if (event.value.controlledBy) { |
61 this.controlledBy = | 81 this.controlledBy = |
62 !this.value || String(event.value.value) == this.value ? | 82 !this.value || String(event.value.value) == this.value ? |
63 event.value.controlledBy : null; | 83 event.value.controlledBy : null; |
64 } else if (event.value.recommendedValue != undefined) { | 84 } else if (event.value.recommendedValue != undefined) { |
65 this.controlledBy = | 85 this.controlledBy = |
66 !this.value || String(event.value.recommendedValue) == this.value ? | 86 !this.value || String(event.value.recommendedValue) == this.value ? |
67 'hasRecommendation' : null; | 87 'hasRecommendation' : null; |
68 } else { | 88 } else { |
69 this.controlledBy = null; | 89 this.controlledBy = null; |
70 } | 90 } |
71 }, | 91 }, |
72 | 92 |
73 /** | 93 /** |
| 94 * Handle mouse and keyboard events, allowing the user to open and close a |
| 95 * bubble with further information. |
| 96 * @param {Event} event Mouse or keyboard event. |
| 97 */ |
| 98 handleEvent: function(event) { |
| 99 switch (event.type) { |
| 100 // Toggle the bubble on left click. Let any other clicks propagate. |
| 101 case 'click': |
| 102 if (event.button != 0) |
| 103 return; |
| 104 break; |
| 105 // Toggle the bubble when <Return> or <Space> is pressed. Let any other |
| 106 // key presses propagate. |
| 107 case 'keydown': |
| 108 switch (event.keyCode) { |
| 109 case 13: // Return. |
| 110 case 32: // Space. |
| 111 break; |
| 112 default: |
| 113 return; |
| 114 } |
| 115 break; |
| 116 // Blur focus when a mouse button is pressed, matching the behavior of |
| 117 // other Web UI elements. |
| 118 case 'mousedown': |
| 119 if (document.activeElement) |
| 120 document.activeElement.blur(); |
| 121 event.preventDefault(); |
| 122 return; |
| 123 } |
| 124 this.toggleBubble_(); |
| 125 event.preventDefault(); |
| 126 event.stopPropagation(); |
| 127 }, |
| 128 |
| 129 /** |
74 * Open or close a bubble with further information about the pref. | 130 * Open or close a bubble with further information about the pref. |
75 * @private | 131 * @private |
76 */ | 132 */ |
77 toggleBubble_: function() { | 133 toggleBubble_: function() { |
78 if (this.showingBubble) { | 134 if (this.showingBubble) { |
79 OptionsPage.hideBubble(); | 135 OptionsPage.hideBubble(); |
80 } else { | 136 } else { |
81 var self = this; | 137 var self = this; |
82 | 138 |
83 // Construct the bubble text. | 139 // Construct the bubble text. |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 * @type {string} | 229 * @type {string} |
174 */ | 230 */ |
175 cr.defineProperty(ControlledSettingIndicator, 'controlledBy', | 231 cr.defineProperty(ControlledSettingIndicator, 'controlledBy', |
176 cr.PropertyKind.ATTR); | 232 cr.PropertyKind.ATTR); |
177 | 233 |
178 // Export. | 234 // Export. |
179 return { | 235 return { |
180 ControlledSettingIndicator: ControlledSettingIndicator | 236 ControlledSettingIndicator: ControlledSettingIndicator |
181 }; | 237 }; |
182 }); | 238 }); |
OLD | NEW |