Path: blob/trunk/third_party/closure/goog/positioning/viewportclientposition.js
2868 views
// Copyright 2006 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Client viewport positioning class.16*17* @author [email protected] (Robert Walker)18* @author [email protected] (Emil A Eklund)19*/2021goog.provide('goog.positioning.ViewportClientPosition');2223goog.require('goog.dom');24goog.require('goog.math.Coordinate');25goog.require('goog.positioning');26goog.require('goog.positioning.ClientPosition');27goog.require('goog.positioning.Overflow');28goog.require('goog.positioning.OverflowStatus');29goog.require('goog.style');30313233/**34* Encapsulates a popup position where the popup is positioned relative to the35* window (client) coordinates, and made to stay within the viewport.36*37* @param {number|goog.math.Coordinate} arg1 Left position or coordinate.38* @param {number=} opt_arg2 Top position if arg1 is a number representing the39* left position, ignored otherwise.40* @constructor41* @extends {goog.positioning.ClientPosition}42*/43goog.positioning.ViewportClientPosition = function(arg1, opt_arg2) {44goog.positioning.ClientPosition.call(this, arg1, opt_arg2);45};46goog.inherits(47goog.positioning.ViewportClientPosition, goog.positioning.ClientPosition);484950/**51* The last-resort overflow strategy, if the popup fails to fit.52* @type {number}53* @private54*/55goog.positioning.ViewportClientPosition.prototype.lastResortOverflow_ = 0;565758/**59* Set the last-resort overflow strategy, if the popup fails to fit.60* @param {number} overflow A bitmask of goog.positioning.Overflow strategies.61*/62goog.positioning.ViewportClientPosition.prototype.setLastResortOverflow =63function(overflow) {64this.lastResortOverflow_ = overflow;65};666768/**69* Repositions the popup according to the current state.70*71* @param {Element} element The DOM element of the popup.72* @param {goog.positioning.Corner} popupCorner The corner of the popup73* element that that should be positioned adjacent to the anchorElement.74* One of the goog.positioning.Corner constants.75* @param {goog.math.Box=} opt_margin A margin specified in pixels.76* @param {goog.math.Size=} opt_preferredSize Preferred size fo the element.77* @override78*/79goog.positioning.ViewportClientPosition.prototype.reposition = function(80element, popupCorner, opt_margin, opt_preferredSize) {81var viewportElt = goog.style.getClientViewportElement(element);82var viewport = goog.style.getVisibleRectForElement(viewportElt);83var scrollEl = goog.dom.getDomHelper(element).getDocumentScrollElement();84var clientPos = new goog.math.Coordinate(85this.coordinate.x + scrollEl.scrollLeft,86this.coordinate.y + scrollEl.scrollTop);8788var failXY =89goog.positioning.Overflow.FAIL_X | goog.positioning.Overflow.FAIL_Y;90var corner = popupCorner;9192// Try the requested position.93var status = goog.positioning.positionAtCoordinate(94clientPos, element, corner, opt_margin, viewport, failXY,95opt_preferredSize);96if ((status & goog.positioning.OverflowStatus.FAILED) == 0) {97return;98}99100// Outside left or right edge of viewport, try try to flip it horizontally.101if (status & goog.positioning.OverflowStatus.FAILED_LEFT ||102status & goog.positioning.OverflowStatus.FAILED_RIGHT) {103corner = goog.positioning.flipCornerHorizontal(corner);104}105106// Outside top or bottom edge of viewport, try try to flip it vertically.107if (status & goog.positioning.OverflowStatus.FAILED_TOP ||108status & goog.positioning.OverflowStatus.FAILED_BOTTOM) {109corner = goog.positioning.flipCornerVertical(corner);110}111112// Try flipped position.113status = goog.positioning.positionAtCoordinate(114clientPos, element, corner, opt_margin, viewport, failXY,115opt_preferredSize);116if ((status & goog.positioning.OverflowStatus.FAILED) == 0) {117return;118}119120// If that failed, the viewport is simply too small to contain the popup.121// Revert to the original position.122goog.positioning.positionAtCoordinate(123clientPos, element, popupCorner, opt_margin, viewport,124this.lastResortOverflow_, opt_preferredSize);125};126127128