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