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 // require: event_tracker.js |
| 6 |
| 7 cr.define('cr.ui', function() { |
| 8 |
| 9 /** |
| 10 * Bubble is a free-floating informational bubble with a triangular arrow |
| 11 * that points at a place of interest on the page. Currently the arrow is |
| 12 * always positioned at the bottom left and points down. |
| 13 */ |
| 14 var Bubble = cr.ui.define('div'); |
| 15 |
| 16 Bubble.prototype = { |
| 17 __proto__: HTMLDivElement.prototype, |
| 18 |
| 19 decorate: function() { |
| 20 this.className = 'bubble'; |
| 21 this.innerHTML = |
| 22 '<div class=\"bubble-contents\"></div>' + |
| 23 '<div class=\"bubble-shadow\"></div>' + |
| 24 '<div class=\"bubble-arrow\"></div>'; |
| 25 |
| 26 this.hidden = true; |
| 27 }, |
| 28 |
| 29 /** |
| 30 * Sets the text message within the bubble. |
| 31 * @param {String} text The message string. |
| 32 */ |
| 33 set text(text) { |
| 34 this.querySelector('.bubble-contents').textContent = text; |
| 35 }, |
| 36 |
| 37 /** |
| 38 * Sets the anchor node, i.e. the node that this bubble points at. |
| 39 * @param {HTMLElement} node The new anchor node. |
| 40 */ |
| 41 set anchorNode(node) { |
| 42 this.anchorNode_ = node; |
| 43 |
| 44 if (!this.hidden) |
| 45 reposition(); |
| 46 }, |
| 47 |
| 48 /** |
| 49 * Updates the position of the bubble. This is automatically called when |
| 50 * the window is resized, but should also be called any time the layout |
| 51 * may have changed. |
| 52 */ |
| 53 reposition: function() { |
| 54 var node = this.anchorNode_; |
| 55 var clientRect = node.getBoundingClientRect(); |
| 56 |
| 57 this.style.left = (clientRect.left + clientRect.right) / 2 + 'px'; |
| 58 this.style.top = (clientRect.top - this.clientHeight) + 'px'; |
| 59 }, |
| 60 |
| 61 /** |
| 62 * Starts showing the bubble. The bubble will grab input and show until the |
| 63 * user clicks away. |
| 64 */ |
| 65 show: function() { |
| 66 if (!this.hidden) |
| 67 return; |
| 68 |
| 69 document.body.appendChild(this); |
| 70 this.hidden = false; |
| 71 this.reposition(); |
| 72 |
| 73 this.eventTracker_ = new EventTracker; |
| 74 this.eventTracker_.add(window, 'resize', this.reposition.bind(this)); |
| 75 |
| 76 var doc = this.ownerDocument; |
| 77 this.eventTracker_.add(doc, 'keydown', this, true); |
| 78 this.eventTracker_.add(doc, 'mousedown', this, true); |
| 79 }, |
| 80 |
| 81 /** |
| 82 * Hides the bubble from view. |
| 83 */ |
| 84 hide: function() { |
| 85 this.hidden = true; |
| 86 this.eventTracker_.removeAll(); |
| 87 this.parentNode.removeChild(this); |
| 88 }, |
| 89 |
| 90 /** |
| 91 * Handles keydown and mousedown events, dismissing the bubble if |
| 92 * necessary. |
| 93 * @param {Event} e The event. |
| 94 */ |
| 95 handleEvent: function(e) { |
| 96 switch (e.type) { |
| 97 case 'keydown': |
| 98 if (e.keyCode == 27) // Esc |
| 99 this.hide(); |
| 100 break; |
| 101 |
| 102 case 'mousedown': |
| 103 if (!this.contains(e.target)) |
| 104 this.hide(); |
| 105 break; |
| 106 } |
| 107 |
| 108 e.stopPropagation(); |
| 109 e.preventDefault(); |
| 110 return; |
| 111 }, |
| 112 }; |
| 113 |
| 114 return { |
| 115 Bubble: Bubble |
| 116 }; |
| 117 }); |
OLD | NEW |