clientposition.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2006 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Client positioning class.
  16. *
  17. * @author eae@google.com (Emil A Eklund)
  18. * @author chrishenry@google.com (Chris Henry)
  19. */
  20. goog.provide('goog.positioning.ClientPosition');
  21. goog.require('goog.asserts');
  22. goog.require('goog.dom');
  23. goog.require('goog.math.Coordinate');
  24. goog.require('goog.positioning');
  25. goog.require('goog.positioning.AbstractPosition');
  26. goog.require('goog.style');
  27. /**
  28. * Encapsulates a popup position where the popup is positioned relative to the
  29. * window (client) coordinates. This calculates the correct position to
  30. * use even if the element is relatively positioned to some other element. This
  31. * is for trying to position an element at the spot of the mouse cursor in
  32. * a MOUSEMOVE event. Just use the event.clientX and event.clientY as the
  33. * parameters.
  34. *
  35. * @param {number|goog.math.Coordinate} arg1 Left position or coordinate.
  36. * @param {number=} opt_arg2 Top position.
  37. * @constructor
  38. * @extends {goog.positioning.AbstractPosition}
  39. */
  40. goog.positioning.ClientPosition = function(arg1, opt_arg2) {
  41. /**
  42. * Coordinate to position popup at.
  43. * @type {goog.math.Coordinate}
  44. */
  45. this.coordinate = arg1 instanceof goog.math.Coordinate ?
  46. arg1 :
  47. new goog.math.Coordinate(/** @type {number} */ (arg1), opt_arg2);
  48. };
  49. goog.inherits(
  50. goog.positioning.ClientPosition, goog.positioning.AbstractPosition);
  51. /**
  52. * Repositions the popup according to the current state
  53. *
  54. * @param {Element} movableElement The DOM element of the popup.
  55. * @param {goog.positioning.Corner} movableElementCorner The corner of
  56. * the popup element that that should be positioned adjacent to
  57. * the anchorElement. One of the goog.positioning.Corner
  58. * constants.
  59. * @param {goog.math.Box=} opt_margin A margin specified in pixels.
  60. * @param {goog.math.Size=} opt_preferredSize Preferred size of the element.
  61. * @override
  62. */
  63. goog.positioning.ClientPosition.prototype.reposition = function(
  64. movableElement, movableElementCorner, opt_margin, opt_preferredSize) {
  65. goog.asserts.assert(movableElement);
  66. // Translates the coordinate to be relative to the page.
  67. var viewportOffset = goog.style.getViewportPageOffset(
  68. goog.dom.getOwnerDocument(movableElement));
  69. var x = this.coordinate.x + viewportOffset.x;
  70. var y = this.coordinate.y + viewportOffset.y;
  71. // Translates the coordinate to be relative to the offset parent.
  72. var movableParentTopLeft =
  73. goog.positioning.getOffsetParentPageOffset(movableElement);
  74. x -= movableParentTopLeft.x;
  75. y -= movableParentTopLeft.y;
  76. goog.positioning.positionAtCoordinate(
  77. new goog.math.Coordinate(x, y), movableElement, movableElementCorner,
  78. opt_margin, null, null, opt_preferredSize);
  79. };