Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * @fileoverview Bubble implementation. | 6 * @fileoverview Bubble implementation. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 // TODO(xiyuan): Move this into shared. | 9 // TODO(xiyuan): Move this into shared. |
| 10 cr.define('cr.ui', function() { | 10 cr.define('cr.ui', function() { |
| 11 /** | 11 /** |
| 12 * Creates a bubble div. | 12 * Creates a bubble div. |
| 13 * @constructor | 13 * @constructor |
| 14 * @extends {HTMLDivElement} | 14 * @extends {HTMLDivElement} |
| 15 */ | 15 */ |
| 16 var Bubble = cr.ui.define('div'); | 16 var Bubble = cr.ui.define('div'); |
| 17 | 17 |
| 18 /** | |
| 19 * Bubble attachment side. | |
| 20 * @enum {string} | |
| 21 */ | |
| 22 Bubble.Attachment = { | |
| 23 RIGHT: 'bubble-right', | |
| 24 LEFT: 'bubble-left', | |
| 25 TOP: 'bubble-top', | |
| 26 BOTTOM: 'bubble-bottom' | |
| 27 }; | |
| 28 | |
| 18 Bubble.prototype = { | 29 Bubble.prototype = { |
| 19 __proto__: HTMLDivElement.prototype, | 30 __proto__: HTMLDivElement.prototype, |
| 20 | 31 |
| 21 // Anchor element | 32 // Anchor element |
| 22 anchor_: undefined, | 33 anchor_: undefined, |
| 23 | 34 |
| 24 /** @inheritDoc */ | 35 /** @inheritDoc */ |
| 25 decorate: function() { | 36 decorate: function() { |
| 26 this.ownerDocument.addEventListener('click', | 37 this.ownerDocument.addEventListener('click', |
| 27 this.handleDocClick_.bind(this)); | 38 this.handleDocClick_.bind(this)); |
| 28 this.ownerDocument.addEventListener('keydown', | 39 this.ownerDocument.addEventListener('keydown', |
| 29 this.handleDocKeyDown_.bind(this)); | 40 this.handleDocKeyDown_.bind(this)); |
| 30 this.addEventListener('webkitTransitionEnd', | 41 this.addEventListener('webkitTransitionEnd', |
| 31 this.handleTransitionEnd_.bind(this)); | 42 this.handleTransitionEnd_.bind(this)); |
| 32 }, | 43 }, |
| 33 | 44 |
| 34 /** | 45 /** |
| 46 * Sets the attachment of the bubble. | |
| 47 * @param {!Attachment} attachment Bubble attachment. | |
| 48 */ | |
| 49 setAttachment_: function (attachment) { | |
| 50 for (var k in Bubble.Attachment) { | |
| 51 var v = Bubble.Attachment[k]; | |
| 52 this.classList[v == attachment ? 'add' : 'remove'](v); | |
| 53 } | |
| 54 }, | |
| 55 | |
| 56 /** | |
| 35 * Shows the bubble for given anchor element. | 57 * Shows the bubble for given anchor element. |
|
altimofeev
2012/02/03 11:49:30
nit: please update comment
Ivan Korotkov
2012/02/07 17:18:00
Done.
| |
| 36 * @param {number} x X position of bubble's reference point. | 58 * @param {!Object} pos Bubble position (left, top, right, bottom). |
| 37 * @param {number} y Y position of bubble's reference point. | |
| 38 * @param {HTMLElement} content Content to show in bubble. | 59 * @param {HTMLElement} content Content to show in bubble. |
| 60 * @param {!Attachment} attachment Bubble attachment (on which side of the | |
| 61 * specified position it should be displayed). | |
| 39 * @public | 62 * @public |
| 40 */ | 63 */ |
| 41 showContentAt: function(x, y, content) { | 64 showContentAt: function(pos, content, attachment) { |
| 42 const ARROW_OFFSET = 14; | 65 const ARROW_OFFSET = 14; |
| 43 | 66 |
| 44 var anchorX = x - ARROW_OFFSET; | 67 // Adjust position so that arrow points exactly at |pos|. |
|
altimofeev
2012/02/03 11:49:30
nit: why not put ARROW_OFFSET to showContentForEle
Ivan Korotkov
2012/02/07 17:18:00
Makes sense.
| |
| 45 var anchorY = y; | 68 if (attachment == Bubble.Attachment.TOP || |
| 69 attachment == Bubble.Attachment.BOTTOM) { | |
| 70 if (pos.left) | |
|
altimofeev
2012/02/03 11:49:30
please fix as we discussed (caller guarantees corr
Ivan Korotkov
2012/02/07 17:18:00
Done.
| |
| 71 pos.left -= ARROW_OFFSET; | |
| 72 } else { | |
| 73 if (pos.top) | |
| 74 pos.top -= ARROW_OFFSET; | |
| 75 } | |
| 46 | 76 |
| 47 this.style.left = anchorX + 'px'; | 77 for (var k in pos) { |
| 48 this.style.top = anchorY + 'px'; | 78 if (typeof pos[k] == 'number') |
| 79 this.style[k] = pos[k] + 'px'; | |
| 80 } | |
| 49 | 81 |
| 50 this.innerHTML = ''; | 82 this.innerHTML = ''; |
| 51 this.appendChild(content); | 83 this.appendChild(content); |
| 84 this.setAttachment_(attachment); | |
| 52 this.hidden = false; | 85 this.hidden = false; |
| 53 this.classList.remove('faded'); | 86 this.classList.remove('faded'); |
| 54 }, | 87 }, |
| 55 | 88 |
| 56 /** | 89 /** |
| 57 * Shows the bubble for given anchor element. | 90 * Shows the bubble for given anchor element. |
| 58 * @param {!HTMLElement} el Anchor element of the bubble. | 91 * @param {!HTMLElement} el Anchor element of the bubble. |
| 59 * @param {HTMLElement} content Content to show in bubble. | 92 * @param {HTMLElement} content Content to show in bubble. |
| 93 * @param {!Attachment} attachment Bubble attachment (on which side of the | |
| 94 * element it should be displayed). | |
| 60 * @public | 95 * @public |
| 61 */ | 96 */ |
| 62 showContentForElement: function(el, content) { | 97 showContentForElement: function(el, content, attachment) { |
| 63 const HORIZONTAL_PADDING = 10; | 98 const OFFSET = 10; |
| 64 const VERTICAL_PADDING = 5; | 99 const PADDING = 5; |
| 65 | 100 |
| 66 var elementOrigin = cr.ui.login.DisplayManager.getOffset(el); | 101 var origin = cr.ui.login.DisplayManager.getPosition(el); |
| 67 var anchorX = elementOrigin.left + HORIZONTAL_PADDING; | 102 |
| 68 var anchorY = elementOrigin.top + el.offsetHeight + VERTICAL_PADDING; | 103 // Adjust the side w.r.to document direction. |
| 104 var side = attachment; | |
| 105 if (isRTL()) { | |
| 106 if (side == Bubble.Attachment.RIGHT) | |
| 107 side = Bubble.Attachment.LEFT; | |
| 108 else if (side == Bubble.Attachment.LEFT) | |
| 109 side = Bubble.Attachment.RIGHT; | |
| 110 } | |
| 111 | |
| 112 var pos = {}; | |
| 113 switch (side) { | |
| 114 case Bubble.Attachment.TOP: | |
| 115 pos.left = origin.left + OFFSET; | |
| 116 pos.bottom = origin.bottom + el.offsetHeight + PADDING; | |
| 117 break; | |
| 118 case Bubble.Attachment.RIGHT: | |
| 119 pos.top = origin.top + OFFSET; | |
| 120 pos.left = origin.left + el.offsetWidth + PADDING; | |
| 121 break; | |
| 122 case Bubble.Attachment.BOTTOM: | |
| 123 pos.left = origin.left + OFFSET; | |
| 124 pos.top = origin.top + el.offsetHeight + PADDING; | |
| 125 break; | |
| 126 case Bubble.Attachment.LEFT: | |
| 127 pos.top = origin.top + OFFSET; | |
| 128 pos.right = origin.right + el.offsetWidth + PADDING; | |
| 129 break; | |
| 130 } | |
| 69 | 131 |
| 70 this.anchor_ = el; | 132 this.anchor_ = el; |
| 71 this.showContentAt(anchorX, anchorY, content); | 133 this.showContentAt(pos, content, attachment); |
| 72 }, | 134 }, |
| 73 | 135 |
| 74 /** | 136 /** |
| 75 * Shows the bubble for given anchor element. | 137 * Shows the bubble for given anchor element. |
| 76 * @param {!HTMLElement} el Anchor element of the bubble. | 138 * @param {!HTMLElement} el Anchor element of the bubble. |
| 77 * @param {string} text Text content to show in bubble. | 139 * @param {string} text Text content to show in bubble. |
| 140 * @param {!Attachment} attachment Bubble attachment (on which side of the | |
| 141 * element it should be displayed). | |
| 78 * @public | 142 * @public |
| 79 */ | 143 */ |
| 80 showTextForElement: function(el, text) { | 144 showTextForElement: function(el, text, attachment) { |
| 81 var span = this.ownerDocument.createElement('span'); | 145 var span = this.ownerDocument.createElement('span'); |
| 82 span.textContent = text; | 146 span.textContent = text; |
| 83 this.showContentForElement(el, span); | 147 this.showContentForElement(el, span, attachment); |
| 84 }, | 148 }, |
| 85 | 149 |
| 86 /** | 150 /** |
| 87 * Hides the bubble. | 151 * Hides the bubble. |
| 88 */ | 152 */ |
| 89 hide: function() { | 153 hide: function() { |
| 90 if (!this.classList.contains('faded')) | 154 if (!this.classList.contains('faded')) |
| 91 this.classList.add('faded'); | 155 this.classList.add('faded'); |
| 92 }, | 156 }, |
| 93 | 157 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 129 handleDocKeyDown_: function(e) { | 193 handleDocKeyDown_: function(e) { |
| 130 if (!this.hidden) | 194 if (!this.hidden) |
| 131 this.hide(); | 195 this.hide(); |
| 132 } | 196 } |
| 133 }; | 197 }; |
| 134 | 198 |
| 135 return { | 199 return { |
| 136 Bubble: Bubble | 200 Bubble: Bubble |
| 137 }; | 201 }; |
| 138 }); | 202 }); |
| OLD | NEW |