OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 cr.define('options', function() { | |
6 | |
James Hawkins
2011/11/28 21:52:19
Remove blank line.
Mattias Nissler (ping if slow)
2011/11/28 22:26:19
Done.
| |
7 var Preferences = options.Preferences; | |
8 | |
9 /** | |
10 * A controlled setting indicator that can be placed on a setting as an | |
11 * indicator that the value is controlled by some external entity such as | |
12 * policy or an extension. | |
13 * @constructor | |
14 * @extends {HTMLSpanElement} | |
15 */ | |
16 var ControlledSettingIndicator = cr.ui.define('span'); | |
17 | |
18 ControlledSettingIndicator.prototype = { | |
19 __proto__: HTMLSpanElement.prototype, | |
20 | |
21 /** | |
22 * Decorates the base element to show the proper icon. | |
23 */ | |
24 decorate: function() { | |
25 var self = this; | |
26 var doc = self.ownerDocument; | |
27 | |
28 // Create the details and summary elements. | |
29 var detailsContainer = doc.createElement('details'); | |
30 detailsContainer.appendChild(doc.createElement('summary')); | |
31 | |
32 // This should really create a div element, but that breaks :hover. See | |
33 // https://bugs.webkit.org/show_bug.cgi?id=72957 | |
34 var bubbleContainer = doc.createElement('p'); | |
35 bubbleContainer.className = 'controlled-setting-bubble'; | |
36 detailsContainer.appendChild(bubbleContainer); | |
37 | |
38 self.appendChild(detailsContainer); | |
39 | |
40 // If there is a pref, track its controlledBy property in order to be able | |
41 // to bring up the correct bubble. | |
42 if (this.hasAttribute('pref')) { | |
43 Preferences.getInstance().addEventListener( | |
44 this.getAttribute('pref'), | |
45 function(event) { | |
46 if (event.value) { | |
47 var controlledBy = event.value['controlledBy']; | |
48 self.controlledBy = controlledBy ? controlledBy : null; | |
49 } | |
50 }); | |
51 } | |
52 | |
53 self.addEventListener('click', self.show_); | |
54 }, | |
55 | |
56 | |
57 /** | |
58 * Closes the bubble. | |
59 */ | |
60 close: function() { | |
61 this.querySelector('details').removeAttribute('open'); | |
62 this.ownerDocument.removeEventListener('click', this.closeHandler_, true); | |
63 }, | |
64 | |
65 /** | |
66 * Constructs the bubble DOM tree and shows it. | |
67 * @private | |
68 */ | |
69 show_: function() { | |
70 var self = this; | |
71 var doc = self.ownerDocument; | |
72 | |
73 // Clear out the old bubble contents. | |
74 var bubbleContainer = this.querySelector('.controlled-setting-bubble'); | |
75 if (bubbleContainer) { | |
76 while (bubbleContainer.hasChildNodes()) | |
77 bubbleContainer.removeChild(bubbleContainer.lastChild); | |
78 } | |
79 | |
80 // Work out the bubble text. | |
81 defaultStrings = { | |
82 'policy' : localStrings.getString('controlledSettingPolicy'), | |
83 'extension' : localStrings.getString('controlledSettingExtension'), | |
84 'recommended' : localStrings.getString('controlledSettingRecommended'), | |
85 }; | |
86 | |
87 // No controller, no bubble. | |
88 if (!self.controlledBy || !self.controlledBy in defaultStrings) | |
89 return; | |
90 | |
91 var text = defaultStrings[self.controlledBy]; | |
92 | |
93 // Apply text overrides. | |
94 if (self.hasAttribute('text' + self.controlledBy)) | |
95 text = self.getAttribute('text' + self.controlledBy); | |
96 | |
97 // Create the DOM tree. | |
98 var bubbleText = doc.createElement('p'); | |
99 bubbleText.className = 'controlled-setting-bubble-text'; | |
100 bubbleText.textContent = text; | |
101 | |
102 var pref = self.getAttribute('pref'); | |
103 if (self.controlledBy == 'recommended' && pref) { | |
104 var container = doc.createElement('div'); | |
105 var action = doc.createElement('button'); | |
106 action.classList.add('link-button'); | |
107 action.classList.add('controlled-setting-bubble-action'); | |
108 action.textContent = | |
109 localStrings.getString('controlledSettingApplyRecommendation'); | |
110 action.addEventListener( | |
111 'click', | |
112 function(e) { | |
113 Preferences.clearPref(pref); | |
114 }); | |
115 container.appendChild(action); | |
116 bubbleText.appendChild(container); | |
117 } | |
118 | |
119 bubbleContainer.appendChild(bubbleText); | |
120 | |
121 // One-time bubble-closing event handler. | |
122 self.closeHandler_ = this.close.bind(this); | |
123 doc.addEventListener('click', self.closeHandler_, true); | |
124 } | |
125 }; | |
126 | |
127 /** | |
128 * The controlling entity of the setting. Can take the values "policy", | |
129 * "extension", "recommended" or be unset. | |
130 */ | |
131 cr.defineProperty(ControlledSettingIndicator, 'controlledBy', | |
132 cr.PropertyKind.ATTR, | |
133 ControlledSettingIndicator.prototype.close); | |
134 | |
135 // Export. | |
136 return { | |
137 ControlledSettingIndicator : ControlledSettingIndicator | |
138 }; | |
139 }); | |
OLD | NEW |