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