viewportposition.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. */
  19. goog.provide('goog.positioning.ViewportPosition');
  20. goog.require('goog.math.Coordinate');
  21. goog.require('goog.positioning');
  22. goog.require('goog.positioning.AbstractPosition');
  23. goog.require('goog.positioning.Corner');
  24. goog.require('goog.style');
  25. /**
  26. * Encapsulates a popup position where the popup is positioned according to
  27. * coordinates relative to the element's viewport (page). This calculates the
  28. * correct position to use even if the element is relatively positioned to some
  29. * other element.
  30. *
  31. * @param {number|goog.math.Coordinate} arg1 Left position or coordinate.
  32. * @param {number=} opt_arg2 Top position.
  33. * @constructor
  34. * @extends {goog.positioning.AbstractPosition}
  35. */
  36. goog.positioning.ViewportPosition = function(arg1, opt_arg2) {
  37. this.coordinate = arg1 instanceof goog.math.Coordinate ?
  38. arg1 :
  39. new goog.math.Coordinate(/** @type {number} */ (arg1), opt_arg2);
  40. };
  41. goog.inherits(
  42. goog.positioning.ViewportPosition, goog.positioning.AbstractPosition);
  43. /**
  44. * Repositions the popup according to the current state
  45. *
  46. * @param {Element} element The DOM element of the popup.
  47. * @param {goog.positioning.Corner} popupCorner The corner of the popup
  48. * element that that should be positioned adjacent to the anchorElement.
  49. * @param {goog.math.Box=} opt_margin A margin specified in pixels.
  50. * @param {goog.math.Size=} opt_preferredSize Preferred size of the element.
  51. * @override
  52. */
  53. goog.positioning.ViewportPosition.prototype.reposition = function(
  54. element, popupCorner, opt_margin, opt_preferredSize) {
  55. goog.positioning.positionAtAnchor(
  56. goog.style.getClientViewportElement(element),
  57. goog.positioning.Corner.TOP_LEFT, element, popupCorner, this.coordinate,
  58. opt_margin, null, opt_preferredSize);
  59. };