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 it's controlledBy property in order to be | |
|
Patrick Dubroy
2011/11/21 12:41:16
its, not it's.
Mattias Nissler (ping if slow)
2011/11/21 13:45:01
Done.
| |
| 28 // able 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 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 bubbleSpan = doc.createElement('div'); | |
|
Patrick Dubroy
2011/11/21 12:41:16
Is bubbleSpan the right name for this? :-)
Mattias Nissler (ping if slow)
2011/11/21 13:45:01
Changed to bubbleContainer.
| |
| 72 bubbleSpan.className = 'controlled-setting-bubble'; | |
| 73 | |
| 74 var bubbleText = doc.createElement('div'); | |
| 75 bubbleText.className = 'controlled-setting-bubble-text'; | |
| 76 bubbleText.appendChild(doc.createTextNode(text)); | |
|
Patrick Dubroy
2011/11/21 12:41:16
Wouldn't it be simpler to use:
bubbleText.tex
Mattias Nissler (ping if slow)
2011/11/21 13:45:01
I was writing this when I had inserted more childr
| |
| 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 e.stopPropagation(); | |
|
Patrick Dubroy
2011/11/21 12:41:16
Why do you need to stop propagation here?
Mattias Nissler (ping if slow)
2011/11/21 13:45:01
Because otherwise the click handler on the icon re
| |
| 92 }); | |
| 93 container.appendChild(action); | |
| 94 bubbleText.appendChild(container); | |
| 95 } | |
| 96 | |
| 97 bubbleSpan.appendChild(bubbleText); | |
| 98 self.appendChild(bubbleSpan); | |
| 99 | |
| 100 // One-time bubble-closing event handler. | |
| 101 doc.addEventListener( | |
| 102 'click', | |
| 103 function() { | |
| 104 self.hide_(); | |
| 105 doc.removeEventListener('click', arguments.callee, true); | |
| 106 }, | |
| 107 true); | |
| 108 }, | |
| 109 | |
| 110 /** | |
| 111 * Hides the bubble. | |
| 112 * @private | |
| 113 */ | |
| 114 hide_: function() { | |
|
Patrick Dubroy
2011/11/21 12:41:16
I think it would be better to just remove the elem
Mattias Nissler (ping if slow)
2011/11/21 13:45:01
Done.
| |
| 115 while (this.firstChild) | |
| 116 this.removeChild(this.firstChild); | |
| 117 } | |
| 118 }; | |
| 119 | |
| 120 // Export. | |
| 121 return { | |
| 122 ControlledSettingIndicator : ControlledSettingIndicator | |
| 123 }; | |
| 124 }); | |
| OLD | NEW |