Chromium Code Reviews| Index: chrome/browser/resources/chromeos/login/bubble.js |
| diff --git a/chrome/browser/resources/chromeos/login/bubble.js b/chrome/browser/resources/chromeos/login/bubble.js |
| index 8e5d1541904cff48828bebf947a093c3ad25c622..5a9aab9f0f85db4de1cb837fe311cfdab95983b6 100644 |
| --- a/chrome/browser/resources/chromeos/login/bubble.js |
| +++ b/chrome/browser/resources/chromeos/login/bubble.js |
| @@ -15,6 +15,17 @@ cr.define('cr.ui', function() { |
| */ |
| var Bubble = cr.ui.define('div'); |
| + /** |
| + * Bubble attachment side. |
| + * @enum {string} |
| + */ |
| + Bubble.Attachment = { |
| + RIGHT: 'bubble-right', |
| + LEFT: 'bubble-left', |
| + TOP: 'bubble-top', |
| + BOTTOM: 'bubble-bottom' |
| + }; |
| + |
| Bubble.prototype = { |
| __proto__: HTMLDivElement.prototype, |
| @@ -32,23 +43,45 @@ cr.define('cr.ui', function() { |
| }, |
| /** |
| + * Sets the attachment of the bubble. |
| + * @param {!Attachment} attachment Bubble attachment. |
| + */ |
| + setAttachment_: function (attachment) { |
| + for (var k in Bubble.Attachment) { |
| + var v = Bubble.Attachment[k]; |
| + this.classList[v == attachment ? 'add' : 'remove'](v); |
| + } |
| + }, |
| + |
| + /** |
| * 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.
|
| - * @param {number} x X position of bubble's reference point. |
| - * @param {number} y Y position of bubble's reference point. |
| + * @param {!Object} pos Bubble position (left, top, right, bottom). |
| * @param {HTMLElement} content Content to show in bubble. |
| + * @param {!Attachment} attachment Bubble attachment (on which side of the |
| + * specified position it should be displayed). |
| * @public |
| */ |
| - showContentAt: function(x, y, content) { |
| + showContentAt: function(pos, content, attachment) { |
| const ARROW_OFFSET = 14; |
| - var anchorX = x - ARROW_OFFSET; |
| - var anchorY = y; |
| - |
| - this.style.left = anchorX + 'px'; |
| - this.style.top = anchorY + 'px'; |
| + // 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.
|
| + if (attachment == Bubble.Attachment.TOP || |
| + attachment == Bubble.Attachment.BOTTOM) { |
| + 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.
|
| + pos.left -= ARROW_OFFSET; |
| + } else { |
| + if (pos.top) |
| + pos.top -= ARROW_OFFSET; |
| + } |
| + |
| + for (var k in pos) { |
| + if (typeof pos[k] == 'number') |
| + this.style[k] = pos[k] + 'px'; |
| + } |
| this.innerHTML = ''; |
| this.appendChild(content); |
| + this.setAttachment_(attachment); |
| this.hidden = false; |
| this.classList.remove('faded'); |
| }, |
| @@ -57,30 +90,61 @@ cr.define('cr.ui', function() { |
| * Shows the bubble for given anchor element. |
| * @param {!HTMLElement} el Anchor element of the bubble. |
| * @param {HTMLElement} content Content to show in bubble. |
| + * @param {!Attachment} attachment Bubble attachment (on which side of the |
| + * element it should be displayed). |
| * @public |
| */ |
| - showContentForElement: function(el, content) { |
| - const HORIZONTAL_PADDING = 10; |
| - const VERTICAL_PADDING = 5; |
| - |
| - var elementOrigin = cr.ui.login.DisplayManager.getOffset(el); |
| - var anchorX = elementOrigin.left + HORIZONTAL_PADDING; |
| - var anchorY = elementOrigin.top + el.offsetHeight + VERTICAL_PADDING; |
| + showContentForElement: function(el, content, attachment) { |
| + const OFFSET = 10; |
| + const PADDING = 5; |
| + |
| + var origin = cr.ui.login.DisplayManager.getPosition(el); |
| + |
| + // Adjust the side w.r.to document direction. |
| + var side = attachment; |
| + if (isRTL()) { |
| + if (side == Bubble.Attachment.RIGHT) |
| + side = Bubble.Attachment.LEFT; |
| + else if (side == Bubble.Attachment.LEFT) |
| + side = Bubble.Attachment.RIGHT; |
| + } |
| + |
| + var pos = {}; |
| + switch (side) { |
| + case Bubble.Attachment.TOP: |
| + pos.left = origin.left + OFFSET; |
| + pos.bottom = origin.bottom + el.offsetHeight + PADDING; |
| + break; |
| + case Bubble.Attachment.RIGHT: |
| + pos.top = origin.top + OFFSET; |
| + pos.left = origin.left + el.offsetWidth + PADDING; |
| + break; |
| + case Bubble.Attachment.BOTTOM: |
| + pos.left = origin.left + OFFSET; |
| + pos.top = origin.top + el.offsetHeight + PADDING; |
| + break; |
| + case Bubble.Attachment.LEFT: |
| + pos.top = origin.top + OFFSET; |
| + pos.right = origin.right + el.offsetWidth + PADDING; |
| + break; |
| + } |
| this.anchor_ = el; |
| - this.showContentAt(anchorX, anchorY, content); |
| + this.showContentAt(pos, content, attachment); |
| }, |
| /** |
| * Shows the bubble for given anchor element. |
| * @param {!HTMLElement} el Anchor element of the bubble. |
| * @param {string} text Text content to show in bubble. |
| + * @param {!Attachment} attachment Bubble attachment (on which side of the |
| + * element it should be displayed). |
| * @public |
| */ |
| - showTextForElement: function(el, text) { |
| + showTextForElement: function(el, text, attachment) { |
| var span = this.ownerDocument.createElement('span'); |
| span.textContent = text; |
| - this.showContentForElement(el, span); |
| + this.showContentForElement(el, span, attachment); |
| }, |
| /** |