Chromium Code Reviews| 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 | |
| 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 | |
| 27 // If there is a pref, track its controlledBy property in order to be able | |
| 28 // to bring up the correct bubble. | |
| 29 if (this.hasAttribute('pref')) { | |
| 30 Preferences.getInstance().addEventListener( | |
| 31 this.getAttribute('pref'), | |
| 32 function(event) { | |
| 33 if (event.value && event.value['controlledBy']) | |
| 34 self.setAttribute('controlledBy', event.value['controlledBy']); | |
| 35 else | |
| 36 self.removeAttribute('controlledBy'); | |
| 37 }); | |
| 38 } | |
| 39 | |
| 40 self.addEventListener('click', self.show_); | |
| 41 }, | |
| 42 | |
| 43 /** | |
| 44 * Constructs the bubble DOM tree and shows it. | |
| 45 * @private | |
| 46 */ | |
| 47 show_: function() { | |
| 48 var self = this; | |
| 49 var doc = self.ownerDocument; | |
| 50 self.hide_(); | |
| 51 | |
| 52 // Work out the bubble text. | |
| 53 defaultStrings = { | |
| 54 'policy' : localStrings.getString('controlledSettingPolicy'), | |
| 55 'extension' : localStrings.getString('controlledSettingExtension'), | |
| 56 'recommended' : localStrings.getString('controlledSettingRecommended'), | |
| 57 }; | |
| 58 | |
| 59 // No controller, no bubble. | |
| 60 var controller = self.getAttribute('controlledBy'); | |
| 61 if (!controller || !controller in defaultStrings) | |
| 62 return; | |
| 63 | |
| 64 var text = defaultStrings[controller]; | |
| 65 | |
| 66 // Apply text overrides. | |
| 67 if (self.hasAttribute('text' + controller)) | |
| 68 text = self.getAttribute('text' + controller); | |
| 69 | |
| 70 // Create the DOM tree. | |
| 71 var bubbleContainer = doc.createElement('div'); | |
| 72 bubbleContainer.className = 'controlled-setting-bubble'; | |
| 73 | |
| 74 var bubbleText = doc.createElement('div'); | |
| 75 bubbleText.className = 'controlled-setting-bubble-text'; | |
| 76 bubbleText.textContent = text; | |
| 77 | |
| 78 var pref = self.getAttribute('pref'); | |
| 79 if (controller == 'recommended' && pref) { | |
| 80 var container = doc.createElement('div'); | |
| 81 var action = doc.createElement('button'); | |
| 82 action.classList.add('link-button'); | |
| 83 action.classList.add('controlled-setting-bubble-action'); | |
| 84 action.textContent = | |
| 85 localStrings.getString('controlledSettingApplyRecommendation'); | |
| 86 action.addEventListener( | |
| 87 'click', | |
| 88 function(e) { | |
| 89 Preferences.clearPref(pref); | |
| 90 self.hide_(); | |
| 91 // Stop propagation s.t. the click handler on the indicator icon | |
|
Patrick Dubroy
2011/11/21 13:53:19
Nit: I don't think this is worth abbreviating. It
| |
| 92 // doesn't trigger and rebuild the bubble. | |
| 93 e.stopPropagation(); | |
| 94 }); | |
| 95 container.appendChild(action); | |
| 96 bubbleText.appendChild(container); | |
| 97 } | |
| 98 | |
| 99 bubbleContainer.appendChild(bubbleText); | |
| 100 self.appendChild(bubbleContainer); | |
| 101 | |
| 102 // One-time bubble-closing event handler. | |
| 103 doc.addEventListener( | |
| 104 'click', | |
| 105 function() { | |
| 106 self.hide_(); | |
| 107 doc.removeEventListener('click', arguments.callee, true); | |
| 108 }, | |
| 109 true); | |
| 110 }, | |
| 111 | |
| 112 /** | |
| 113 * Hides the bubble. | |
| 114 * @private | |
| 115 */ | |
| 116 hide_: function() { | |
| 117 var bubble = this.querySelector('.controlled-setting-bubble'); | |
| 118 if (bubble) | |
| 119 this.removeChild(bubble); | |
| 120 } | |
| 121 }; | |
| 122 | |
| 123 // Export. | |
| 124 return { | |
| 125 ControlledSettingIndicator : ControlledSettingIndicator | |
| 126 }; | |
| 127 }); | |
| OLD | NEW |