viewportclientposition.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 viewport positioning class.
  16. *
  17. * @author robbyw@google.com (Robert Walker)
  18. * @author eae@google.com (Emil A Eklund)
  19. */
  20. goog.provide('goog.positioning.ViewportClientPosition');
  21. goog.require('goog.dom');
  22. goog.require('goog.math.Coordinate');
  23. goog.require('goog.positioning');
  24. goog.require('goog.positioning.ClientPosition');
  25. goog.require('goog.positioning.Overflow');
  26. goog.require('goog.positioning.OverflowStatus');
  27. goog.require('goog.style');
  28. /**
  29. * Encapsulates a popup position where the popup is positioned relative to the
  30. * window (client) coordinates, and made to stay within the viewport.
  31. *
  32. * @param {number|goog.math.Coordinate} arg1 Left position or coordinate.
  33. * @param {number=} opt_arg2 Top position if arg1 is a number representing the
  34. * left position, ignored otherwise.
  35. * @constructor
  36. * @extends {goog.positioning.ClientPosition}
  37. */
  38. goog.positioning.ViewportClientPosition = function(arg1, opt_arg2) {
  39. goog.positioning.ClientPosition.call(this, arg1, opt_arg2);
  40. };
  41. goog.inherits(
  42. goog.positioning.ViewportClientPosition, goog.positioning.ClientPosition);
  43. /**
  44. * The last-resort overflow strategy, if the popup fails to fit.
  45. * @type {number}
  46. * @private
  47. */
  48. goog.positioning.ViewportClientPosition.prototype.lastResortOverflow_ = 0;
  49. /**
  50. * Set the last-resort overflow strategy, if the popup fails to fit.
  51. * @param {number} overflow A bitmask of goog.positioning.Overflow strategies.
  52. */
  53. goog.positioning.ViewportClientPosition.prototype.setLastResortOverflow =
  54. function(overflow) {
  55. this.lastResortOverflow_ = overflow;
  56. };
  57. /**
  58. * Repositions the popup according to the current state.
  59. *
  60. * @param {Element} element The DOM element of the popup.
  61. * @param {goog.positioning.Corner} popupCorner The corner of the popup
  62. * element that that should be positioned adjacent to the anchorElement.
  63. * One of the goog.positioning.Corner constants.
  64. * @param {goog.math.Box=} opt_margin A margin specified in pixels.
  65. * @param {goog.math.Size=} opt_preferredSize Preferred size fo the element.
  66. * @override
  67. */
  68. goog.positioning.ViewportClientPosition.prototype.reposition = function(
  69. element, popupCorner, opt_margin, opt_preferredSize) {
  70. var viewportElt = goog.style.getClientViewportElement(element);
  71. var viewport = goog.style.getVisibleRectForElement(viewportElt);
  72. var scrollEl = goog.dom.getDomHelper(element).getDocumentScrollElement();
  73. var clientPos = new goog.math.Coordinate(
  74. this.coordinate.x + scrollEl.scrollLeft,
  75. this.coordinate.y + scrollEl.scrollTop);
  76. var failXY =
  77. goog.positioning.Overflow.FAIL_X | goog.positioning.Overflow.FAIL_Y;
  78. var corner = popupCorner;
  79. // Try the requested position.
  80. var status = goog.positioning.positionAtCoordinate(
  81. clientPos, element, corner, opt_margin, viewport, failXY,
  82. opt_preferredSize);
  83. if ((status & goog.positioning.OverflowStatus.FAILED) == 0) {
  84. return;
  85. }
  86. // Outside left or right edge of viewport, try try to flip it horizontally.
  87. if (status & goog.positioning.OverflowStatus.FAILED_LEFT ||
  88. status & goog.positioning.OverflowStatus.FAILED_RIGHT) {
  89. corner = goog.positioning.flipCornerHorizontal(corner);
  90. }
  91. // Outside top or bottom edge of viewport, try try to flip it vertically.
  92. if (status & goog.positioning.OverflowStatus.FAILED_TOP ||
  93. status & goog.positioning.OverflowStatus.FAILED_BOTTOM) {
  94. corner = goog.positioning.flipCornerVertical(corner);
  95. }
  96. // Try flipped position.
  97. status = goog.positioning.positionAtCoordinate(
  98. clientPos, element, corner, opt_margin, viewport, failXY,
  99. opt_preferredSize);
  100. if ((status & goog.positioning.OverflowStatus.FAILED) == 0) {
  101. return;
  102. }
  103. // If that failed, the viewport is simply too small to contain the popup.
  104. // Revert to the original position.
  105. goog.positioning.positionAtCoordinate(
  106. clientPos, element, popupCorner, opt_margin, viewport,
  107. this.lastResortOverflow_, opt_preferredSize);
  108. };