Path: blob/trunk/third_party/closure/goog/positioning/positioning.js
4514 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56/**7* @fileoverview Common positioning code.8*/910goog.provide('goog.positioning');11goog.provide('goog.positioning.Corner');12goog.provide('goog.positioning.CornerBit');13goog.provide('goog.positioning.Overflow');14goog.provide('goog.positioning.OverflowStatus');1516goog.require('goog.asserts');17goog.require('goog.dom');18goog.require('goog.dom.TagName');19goog.require('goog.math.Coordinate');20goog.require('goog.math.Rect');21goog.require('goog.math.Size');22goog.require('goog.style');23goog.require('goog.style.bidi');24goog.requireType('goog.math.Box');252627/**28* Enum for bits in the {@see goog.positioning.Corner) bitmap.29*30* @enum {number}31*/32goog.positioning.CornerBit = {33BOTTOM: 1,34CENTER: 2,35RIGHT: 4,36FLIP_RTL: 837};383940/**41* Enum for representing an element corner for positioning the popup.42*43* The START constants map to LEFT if element directionality is left44* to right and RIGHT if the directionality is right to left.45* Likewise END maps to RIGHT or LEFT depending on the directionality.46*47* @enum {number}48*/49goog.positioning.Corner = {50TOP_LEFT: 0,51TOP_RIGHT: goog.positioning.CornerBit.RIGHT,52BOTTOM_LEFT: goog.positioning.CornerBit.BOTTOM,53BOTTOM_RIGHT:54goog.positioning.CornerBit.BOTTOM | goog.positioning.CornerBit.RIGHT,55TOP_START: goog.positioning.CornerBit.FLIP_RTL,56TOP_END:57goog.positioning.CornerBit.FLIP_RTL | goog.positioning.CornerBit.RIGHT,58BOTTOM_START:59goog.positioning.CornerBit.BOTTOM | goog.positioning.CornerBit.FLIP_RTL,60BOTTOM_END: goog.positioning.CornerBit.BOTTOM |61goog.positioning.CornerBit.RIGHT | goog.positioning.CornerBit.FLIP_RTL,62TOP_CENTER: goog.positioning.CornerBit.CENTER,63BOTTOM_CENTER:64goog.positioning.CornerBit.BOTTOM | goog.positioning.CornerBit.CENTER65};666768/**69* Enum for representing position handling in cases where the element would be70* positioned outside the viewport.71*72* @enum {number}73*/74goog.positioning.Overflow = {75/** Ignore overflow */76IGNORE: 0,7778/** Try to fit horizontally in the viewport at all costs. */79ADJUST_X: 1,8081/** If the element can't fit horizontally, report positioning failure. */82FAIL_X: 2,8384/** Try to fit vertically in the viewport at all costs. */85ADJUST_Y: 4,8687/** If the element can't fit vertically, report positioning failure. */88FAIL_Y: 8,8990/** Resize the element's width to fit in the viewport. */91RESIZE_WIDTH: 16,9293/** Resize the element's height to fit in the viewport. */94RESIZE_HEIGHT: 32,9596/**97* If the anchor goes off-screen in the x-direction, position the movable98* element off-screen. Otherwise, try to fit horizontally in the viewport.99*/100ADJUST_X_EXCEPT_OFFSCREEN: 64 | 1,101102/**103* If the anchor goes off-screen in the y-direction, position the movable104* element off-screen. Otherwise, try to fit vertically in the viewport.105*/106ADJUST_Y_EXCEPT_OFFSCREEN: 128 | 4107};108109110/**111* Enum for representing the outcome of a positioning call.112*113* @enum {number}114*/115goog.positioning.OverflowStatus = {116NONE: 0,117ADJUSTED_X: 1,118ADJUSTED_Y: 2,119WIDTH_ADJUSTED: 4,120HEIGHT_ADJUSTED: 8,121FAILED_LEFT: 16,122FAILED_RIGHT: 32,123FAILED_TOP: 64,124FAILED_BOTTOM: 128,125FAILED_OUTSIDE_VIEWPORT: 256,126/** Shorthand to check if a status code contains any fail code. */127FAILED: 16 | 32 | 64 | 128 | 256,128/** Shorthand to check if horizontal positioning failed. */129FAILED_HORIZONTAL: 16 | 32,130/** Shorthand to check if vertical positioning failed. */131FAILED_VERTICAL: 64 | 128,132};133134135/**136* Positions a movable element relative to an anchor element. The caller137* specifies the corners that should touch. This functions then moves the138* movable element accordingly.139*140* @param {Element} anchorElement The element that is the anchor for where141* the movable element should position itself.142* @param {goog.positioning.Corner} anchorElementCorner The corner of the143* anchorElement for positioning the movable element.144* @param {Element} movableElement The element to move.145* @param {goog.positioning.Corner} movableElementCorner The corner of the146* movableElement that that should be positioned adjacent to the anchor147* element.148* @param {goog.math.Coordinate=} opt_offset An offset specified in pixels.149* After the normal positioning algorithm is applied, the offset is then150* applied. Positive coordinates move the popup closer to the center of the151* anchor element. Negative coordinates move the popup away from the center152* of the anchor element.153* @param {goog.math.Box=} opt_margin A margin specified in pixels.154* After the normal positioning algorithm is applied and any offset, the155* margin is then applied. Positive coordinates move the popup away from the156* spot it was positioned towards its center. Negative coordinates move it157* towards the spot it was positioned away from its center.158* @param {?number=} opt_overflow Overflow handling mode. Defaults to IGNORE if159* not specified. Bitmap, {@see goog.positioning.Overflow}.160* @param {goog.math.Size=} opt_preferredSize The preferred size of the161* movableElement.162* @param {goog.math.Box=} opt_viewport Box object describing the dimensions of163* the viewport. The viewport is specified relative to offsetParent of164* `movableElement`. In other words, the viewport can be thought of as165* describing a "position: absolute" element contained in the offsetParent.166* It defaults to visible area of nearest scrollable ancestor of167* `movableElement` (see `goog.style.getVisibleRectForElement`).168* @return {goog.positioning.OverflowStatus} Status bitmap,169* {@see goog.positioning.OverflowStatus}.170*/171goog.positioning.positionAtAnchor = function(172anchorElement, anchorElementCorner, movableElement, movableElementCorner,173opt_offset, opt_margin, opt_overflow, opt_preferredSize, opt_viewport) {174'use strict';175goog.asserts.assert(movableElement);176var movableParentTopLeft =177goog.positioning.getOffsetParentPageOffset(movableElement);178179// Get the visible part of the anchor element. anchorRect is180// relative to anchorElement's page.181var anchorRect = goog.positioning.getVisiblePart_(anchorElement);182183// Translate anchorRect to be relative to movableElement's page.184goog.style.translateRectForAnotherFrame(185anchorRect, goog.dom.getDomHelper(anchorElement),186goog.dom.getDomHelper(movableElement));187188// Offset based on which corner of the element we want to position against.189var corner =190goog.positioning.getEffectiveCorner(anchorElement, anchorElementCorner);191var offsetLeft = anchorRect.left;192if (corner & goog.positioning.CornerBit.RIGHT) {193offsetLeft += anchorRect.width;194} else if (corner & goog.positioning.CornerBit.CENTER) {195offsetLeft += anchorRect.width / 2;196}197198// absolutePos is a candidate position relative to the199// movableElement's window.200var absolutePos = new goog.math.Coordinate(201offsetLeft, anchorRect.top +202(corner & goog.positioning.CornerBit.BOTTOM ? anchorRect.height : 0));203204// Translate absolutePos to be relative to the offsetParent.205absolutePos =206goog.math.Coordinate.difference(absolutePos, movableParentTopLeft);207208// Apply offset, if specified209if (opt_offset) {210absolutePos.x +=211(corner & goog.positioning.CornerBit.RIGHT ? -1 : 1) * opt_offset.x;212absolutePos.y +=213(corner & goog.positioning.CornerBit.BOTTOM ? -1 : 1) * opt_offset.y;214}215216// Determine dimension of viewport.217var viewport;218if (opt_overflow) {219if (opt_viewport) {220viewport = opt_viewport;221} else {222viewport = goog.style.getVisibleRectForElement(movableElement);223if (viewport) {224viewport.top -= movableParentTopLeft.y;225viewport.right -= movableParentTopLeft.x;226viewport.bottom -= movableParentTopLeft.y;227viewport.left -= movableParentTopLeft.x;228}229}230}231232return goog.positioning.positionAtCoordinate(233absolutePos, movableElement, movableElementCorner, opt_margin, viewport,234opt_overflow, opt_preferredSize);235};236237238/**239* Calculates the page offset of the given element's240* offsetParent. This value can be used to translate any x- and241* y-offset relative to the page to an offset relative to the242* offsetParent, which can then be used directly with as position243* coordinate for `positionWithCoordinate`.244* @param {!Element} movableElement The element to calculate.245* @return {!goog.math.Coordinate} The page offset, may be (0, 0).246*/247goog.positioning.getOffsetParentPageOffset = function(movableElement) {248'use strict';249// Ignore offset for the BODY element unless its position is non-static.250// For cases where the offset parent is HTML rather than the BODY (such as in251// IE strict mode) there's no need to get the position of the BODY as it252// doesn't affect the page offset.253var movableParentTopLeft;254var parent = /** @type {?} */ (movableElement).offsetParent;255if (parent) {256var isBody = parent.tagName == goog.dom.TagName.HTML ||257parent.tagName == goog.dom.TagName.BODY;258if (!isBody || goog.style.getComputedPosition(parent) != 'static') {259// Get the top-left corner of the parent, in page coordinates.260movableParentTopLeft = goog.style.getPageOffset(parent);261262if (!isBody) {263movableParentTopLeft = goog.math.Coordinate.difference(264movableParentTopLeft,265new goog.math.Coordinate(266goog.style.bidi.getScrollLeft(parent), parent.scrollTop));267}268}269}270271return movableParentTopLeft || new goog.math.Coordinate();272};273274275/**276* Returns intersection of the specified element and277* goog.style.getVisibleRectForElement for it.278*279* @param {Element} el The target element.280* @return {!goog.math.Rect} Intersection of getVisibleRectForElement281* and the current bounding rectangle of the element. If the282* intersection is empty, returns the bounding rectangle.283* @private284*/285goog.positioning.getVisiblePart_ = function(el) {286'use strict';287var rect = goog.style.getBounds(el);288var visibleBox = goog.style.getVisibleRectForElement(el);289if (visibleBox) {290rect.intersection(goog.math.Rect.createFromBox(visibleBox));291}292return rect;293};294295296/**297* Positions the specified corner of the movable element at the298* specified coordinate.299*300* @param {goog.math.Coordinate} absolutePos The coordinate to position the301* element at.302* @param {Element} movableElement The element to be positioned.303* @param {goog.positioning.Corner} movableElementCorner The corner of the304* movableElement that that should be positioned.305* @param {goog.math.Box=} opt_margin A margin specified in pixels.306* After the normal positioning algorithm is applied and any offset, the307* margin is then applied. Positive coordinates move the popup away from the308* spot it was positioned towards its center. Negative coordinates move it309* towards the spot it was positioned away from its center.310* @param {goog.math.Box=} opt_viewport Box object describing the dimensions of311* the viewport. Required if opt_overflow is specified.312* @param {?number=} opt_overflow Overflow handling mode. Defaults to IGNORE if313* not specified, {@see goog.positioning.Overflow}.314* @param {goog.math.Size=} opt_preferredSize The preferred size of the315* movableElement. Defaults to the current size.316* @return {goog.positioning.OverflowStatus} Status bitmap.317*/318goog.positioning.positionAtCoordinate = function(319absolutePos, movableElement, movableElementCorner, opt_margin, opt_viewport,320opt_overflow, opt_preferredSize) {321'use strict';322absolutePos = absolutePos.clone();323324// Offset based on attached corner and desired margin.325var corner =326goog.positioning.getEffectiveCorner(movableElement, movableElementCorner);327var elementSize = goog.style.getSize(movableElement);328var size =329opt_preferredSize ? opt_preferredSize.clone() : elementSize.clone();330331var positionResult = goog.positioning.getPositionAtCoordinate(332absolutePos, size, corner, opt_margin, opt_viewport, opt_overflow);333334if (positionResult.status & goog.positioning.OverflowStatus.FAILED) {335return positionResult.status;336}337338goog.style.setPosition(movableElement, positionResult.rect.getTopLeft());339size = positionResult.rect.getSize();340if (!goog.math.Size.equals(elementSize, size)) {341goog.style.setBorderBoxSize(movableElement, size);342}343344return positionResult.status;345};346347348/**349* Computes the position for an element to be placed on-screen at the350* specified coordinates. Returns an object containing both the resulting351* rectangle, and the overflow status bitmap.352*353* @param {!goog.math.Coordinate} absolutePos The coordinate to position the354* element at.355* @param {!goog.math.Size} elementSize The size of the element to be356* positioned.357* @param {goog.positioning.Corner} elementCorner The corner of the358* movableElement that that should be positioned.359* @param {goog.math.Box=} opt_margin A margin specified in pixels.360* After the normal positioning algorithm is applied and any offset, the361* margin is then applied. Positive coordinates move the popup away from the362* spot it was positioned towards its center. Negative coordinates move it363* towards the spot it was positioned away from its center.364* @param {goog.math.Box=} opt_viewport Box object describing the dimensions of365* the viewport. Required if opt_overflow is specified.366* @param {?number=} opt_overflow Overflow handling mode. Defaults to IGNORE367* if not specified, {@see goog.positioning.Overflow}.368* @return {{rect:!goog.math.Rect, status:goog.positioning.OverflowStatus}}369* Object containing the computed position and status bitmap.370*/371goog.positioning.getPositionAtCoordinate = function(372absolutePos, elementSize, elementCorner, opt_margin, opt_viewport,373opt_overflow) {374'use strict';375absolutePos = absolutePos.clone();376elementSize = elementSize.clone();377var status = goog.positioning.OverflowStatus.NONE;378379if (opt_margin || elementCorner != goog.positioning.Corner.TOP_LEFT) {380if (elementCorner & goog.positioning.CornerBit.RIGHT) {381absolutePos.x -= elementSize.width + (opt_margin ? opt_margin.right : 0);382} else if (elementCorner & goog.positioning.CornerBit.CENTER) {383absolutePos.x -= elementSize.width / 2;384} else if (opt_margin) {385absolutePos.x += opt_margin.left;386}387if (elementCorner & goog.positioning.CornerBit.BOTTOM) {388absolutePos.y -=389elementSize.height + (opt_margin ? opt_margin.bottom : 0);390} else if (opt_margin) {391absolutePos.y += opt_margin.top;392}393}394395// Adjust position to fit inside viewport.396if (opt_overflow) {397status = opt_viewport ?398goog.positioning.adjustForViewport_(399absolutePos, elementSize, opt_viewport, opt_overflow) :400goog.positioning.OverflowStatus.FAILED_OUTSIDE_VIEWPORT;401}402403var rect = new goog.math.Rect(0, 0, 0, 0);404rect.left = absolutePos.x;405rect.top = absolutePos.y;406rect.width = elementSize.width;407rect.height = elementSize.height;408return {rect: rect, status: status};409};410411412/**413* Adjusts the position and/or size of an element, identified by its position414* and size, to fit inside the viewport. If the position or size of the element415* is adjusted the pos or size objects, respectively, are modified.416*417* @param {goog.math.Coordinate} pos Position of element, updated if the418* position is adjusted.419* @param {goog.math.Size} size Size of element, updated if the size is420* adjusted.421* @param {goog.math.Box} viewport Bounding box describing the viewport.422* @param {number} overflow Overflow handling mode,423* {@see goog.positioning.Overflow}.424* @return {goog.positioning.OverflowStatus} Status bitmap,425* {@see goog.positioning.OverflowStatus}.426* @private427*/428goog.positioning.adjustForViewport_ = function(pos, size, viewport, overflow) {429'use strict';430var status = goog.positioning.OverflowStatus.NONE;431432var ADJUST_X_EXCEPT_OFFSCREEN =433goog.positioning.Overflow.ADJUST_X_EXCEPT_OFFSCREEN;434var ADJUST_Y_EXCEPT_OFFSCREEN =435goog.positioning.Overflow.ADJUST_Y_EXCEPT_OFFSCREEN;436if ((overflow & ADJUST_X_EXCEPT_OFFSCREEN) == ADJUST_X_EXCEPT_OFFSCREEN &&437(pos.x < viewport.left || pos.x >= viewport.right)) {438overflow &= ~goog.positioning.Overflow.ADJUST_X;439}440if ((overflow & ADJUST_Y_EXCEPT_OFFSCREEN) == ADJUST_Y_EXCEPT_OFFSCREEN &&441(pos.y < viewport.top || pos.y >= viewport.bottom)) {442overflow &= ~goog.positioning.Overflow.ADJUST_Y;443}444445// Left edge outside viewport, try to move it.446if (pos.x < viewport.left && overflow & goog.positioning.Overflow.ADJUST_X) {447pos.x = viewport.left;448status |= goog.positioning.OverflowStatus.ADJUSTED_X;449}450451// Ensure object is inside the viewport width if required.452if (overflow & goog.positioning.Overflow.RESIZE_WIDTH) {453// Move left edge inside viewport.454var originalX = pos.x;455if (pos.x < viewport.left) {456pos.x = viewport.left;457status |= goog.positioning.OverflowStatus.WIDTH_ADJUSTED;458}459460// Shrink width to inside right of viewport.461if (pos.x + size.width > viewport.right) {462// Set the width to be either the new maximum width within the viewport463// or the width originally within the viewport, whichever is less.464size.width = Math.min(465viewport.right - pos.x, originalX + size.width - viewport.left);466size.width = Math.max(size.width, 0);467status |= goog.positioning.OverflowStatus.WIDTH_ADJUSTED;468}469}470471// Right edge outside viewport, try to move it.472if (pos.x + size.width > viewport.right &&473overflow & goog.positioning.Overflow.ADJUST_X) {474pos.x = Math.max(viewport.right - size.width, viewport.left);475status |= goog.positioning.OverflowStatus.ADJUSTED_X;476}477478// Left or right edge still outside viewport, fail if the FAIL_X option was479// specified, ignore it otherwise.480if (overflow & goog.positioning.Overflow.FAIL_X) {481status |=482(pos.x < viewport.left ? goog.positioning.OverflowStatus.FAILED_LEFT :4830) |484(pos.x + size.width > viewport.right ?485goog.positioning.OverflowStatus.FAILED_RIGHT :4860);487}488489// Top edge outside viewport, try to move it.490if (pos.y < viewport.top && overflow & goog.positioning.Overflow.ADJUST_Y) {491pos.y = viewport.top;492status |= goog.positioning.OverflowStatus.ADJUSTED_Y;493}494495// Ensure object is inside the viewport height if required.496if (overflow & goog.positioning.Overflow.RESIZE_HEIGHT) {497// Move top edge inside viewport.498var originalY = pos.y;499if (pos.y < viewport.top) {500pos.y = viewport.top;501status |= goog.positioning.OverflowStatus.HEIGHT_ADJUSTED;502}503504// Shrink height to inside bottom of viewport.505if (pos.y + size.height > viewport.bottom) {506// Set the height to be either the new maximum height within the viewport507// or the height originally within the viewport, whichever is less.508size.height = Math.min(509viewport.bottom - pos.y, originalY + size.height - viewport.top);510size.height = Math.max(size.height, 0);511status |= goog.positioning.OverflowStatus.HEIGHT_ADJUSTED;512}513}514515// Bottom edge outside viewport, try to move it.516if (pos.y + size.height > viewport.bottom &&517overflow & goog.positioning.Overflow.ADJUST_Y) {518pos.y = Math.max(viewport.bottom - size.height, viewport.top);519status |= goog.positioning.OverflowStatus.ADJUSTED_Y;520}521522// Top or bottom edge still outside viewport, fail if the FAIL_Y option was523// specified, ignore it otherwise.524if (overflow & goog.positioning.Overflow.FAIL_Y) {525status |=526(pos.y < viewport.top ? goog.positioning.OverflowStatus.FAILED_TOP :5270) |528(pos.y + size.height > viewport.bottom ?529goog.positioning.OverflowStatus.FAILED_BOTTOM :5300);531}532533return /** @type {!goog.positioning.OverflowStatus} */ (status);534};535536537/**538* Returns an absolute corner (top/bottom left/right) given an absolute539* or relative (top/bottom start/end) corner and the direction of an element.540* Absolute corners remain unchanged.541* @param {Element} element DOM element to test for RTL direction.542* @param {goog.positioning.Corner} corner The popup corner used for543* positioning.544* @return {goog.positioning.Corner} Effective corner.545*/546goog.positioning.getEffectiveCorner = function(element, corner) {547'use strict';548return /** @type {goog.positioning.Corner} */ (549(corner & goog.positioning.CornerBit.FLIP_RTL &&550goog.style.isRightToLeft(element) ?551corner ^ goog.positioning.CornerBit.RIGHT :552corner) &553~goog.positioning.CornerBit.FLIP_RTL);554};555556557/**558* Returns the corner opposite the given one horizontally.559* @param {goog.positioning.Corner} corner The popup corner used to flip.560* @return {goog.positioning.Corner} The opposite corner horizontally.561*/562goog.positioning.flipCornerHorizontal = function(corner) {563'use strict';564return /** @type {goog.positioning.Corner} */ (565corner ^ goog.positioning.CornerBit.RIGHT);566};567568569/**570* Returns the corner opposite the given one vertically.571* @param {goog.positioning.Corner} corner The popup corner used to flip.572* @return {goog.positioning.Corner} The opposite corner vertically.573*/574goog.positioning.flipCornerVertical = function(corner) {575'use strict';576return /** @type {goog.positioning.Corner} */ (577corner ^ goog.positioning.CornerBit.BOTTOM);578};579580581/**582* Returns the corner opposite the given one horizontally and vertically.583* @param {goog.positioning.Corner} corner The popup corner used to flip.584* @return {goog.positioning.Corner} The opposite corner horizontally and585* vertically.586*/587goog.positioning.flipCorner = function(corner) {588'use strict';589return /** @type {goog.positioning.Corner} */ (590corner ^ goog.positioning.CornerBit.BOTTOM ^591goog.positioning.CornerBit.RIGHT);592};593594595