Path: blob/trunk/third_party/closure/goog/positioning/clientposition.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 positioning class.16*17* @author [email protected] (Emil A Eklund)18* @author [email protected] (Chris Henry)19*/2021goog.provide('goog.positioning.ClientPosition');2223goog.require('goog.asserts');24goog.require('goog.dom');25goog.require('goog.math.Coordinate');26goog.require('goog.positioning');27goog.require('goog.positioning.AbstractPosition');28goog.require('goog.style');29303132/**33* Encapsulates a popup position where the popup is positioned relative to the34* window (client) coordinates. This calculates the correct position to35* use even if the element is relatively positioned to some other element. This36* is for trying to position an element at the spot of the mouse cursor in37* a MOUSEMOVE event. Just use the event.clientX and event.clientY as the38* parameters.39*40* @param {number|goog.math.Coordinate} arg1 Left position or coordinate.41* @param {number=} opt_arg2 Top position.42* @constructor43* @extends {goog.positioning.AbstractPosition}44*/45goog.positioning.ClientPosition = function(arg1, opt_arg2) {46/**47* Coordinate to position popup at.48* @type {goog.math.Coordinate}49*/50this.coordinate = arg1 instanceof goog.math.Coordinate ?51arg1 :52new goog.math.Coordinate(/** @type {number} */ (arg1), opt_arg2);53};54goog.inherits(55goog.positioning.ClientPosition, goog.positioning.AbstractPosition);565758/**59* Repositions the popup according to the current state60*61* @param {Element} movableElement The DOM element of the popup.62* @param {goog.positioning.Corner} movableElementCorner The corner of63* the popup element that that should be positioned adjacent to64* the anchorElement. One of the goog.positioning.Corner65* constants.66* @param {goog.math.Box=} opt_margin A margin specified in pixels.67* @param {goog.math.Size=} opt_preferredSize Preferred size of the element.68* @override69*/70goog.positioning.ClientPosition.prototype.reposition = function(71movableElement, movableElementCorner, opt_margin, opt_preferredSize) {72goog.asserts.assert(movableElement);7374// Translates the coordinate to be relative to the page.75var viewportOffset = goog.style.getViewportPageOffset(76goog.dom.getOwnerDocument(movableElement));77var x = this.coordinate.x + viewportOffset.x;78var y = this.coordinate.y + viewportOffset.y;7980// Translates the coordinate to be relative to the offset parent.81var movableParentTopLeft =82goog.positioning.getOffsetParentPageOffset(movableElement);83x -= movableParentTopLeft.x;84y -= movableParentTopLeft.y;8586goog.positioning.positionAtCoordinate(87new goog.math.Coordinate(x, y), movableElement, movableElementCorner,88opt_margin, null, null, opt_preferredSize);89};909192