Debian Codegraph @

closure-library/closure/goog/positioning/clientposition.js

raw
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 positioning class.
17 *
18 * @author [email protected] (Emil A Eklund)
19 * @author [email protected] (Chris Henry)
20 */
21
22 goog.provide('goog.positioning.ClientPosition');
23
24 goog.require('goog.asserts');
25 goog.require('goog.dom');
26 goog.require('goog.math.Coordinate');
27 goog.require('goog.positioning');
28 goog.require('goog.positioning.AbstractPosition');
29 goog.require('goog.style');
30
31
32
33 /**
34 * Encapsulates a popup position where the popup is positioned relative to the
35 * window (client) coordinates. This calculates the correct position to
36 * use even if the element is relatively positioned to some other element. This
37 * is for trying to position an element at the spot of the mouse cursor in
38 * a MOUSEMOVE event. Just use the event.clientX and event.clientY as the
39 * parameters.
40 *
41 * @param {number|goog.math.Coordinate} arg1 Left position or coordinate.
42 * @param {number=} opt_arg2 Top position.
43 * @constructor
44 * @extends {goog.positioning.AbstractPosition}
45 */
46 goog.positioning.ClientPosition = function(arg1, opt_arg2) {
47 /**
48 * Coordinate to position popup at.
49 * @type {goog.math.Coordinate}
50 */
51 this.coordinate = arg1 instanceof goog.math.Coordinate ?
52 arg1 :
53 new goog.math.Coordinate(/** @type {number} */ (arg1), opt_arg2);
54 };
55 goog.inherits(
56 goog.positioning.ClientPosition, goog.positioning.AbstractPosition);
57
58
59 /**
60 * Repositions the popup according to the current state
61 *
62 * @param {Element} movableElement The DOM element of the popup.
63 * @param {goog.positioning.Corner} movableElementCorner The corner of
64 * the popup element that that should be positioned adjacent to
65 * the anchorElement. One of the goog.positioning.Corner
66 * constants.
67 * @param {goog.math.Box=} opt_margin A margin specified in pixels.
68 * @param {goog.math.Size=} opt_preferredSize Preferred size of the element.
69 * @override
70 */
71 goog.positioning.ClientPosition.prototype.reposition = function(
72 movableElement, movableElementCorner, opt_margin, opt_preferredSize) {
73 goog.asserts.assert(movableElement);
74
75 // Translates the coordinate to be relative to the page.
76 var viewportOffset = goog.style.getViewportPageOffset(
77 goog.dom.getOwnerDocument(movableElement));
78 var x = this.coordinate.x + viewportOffset.x;
79 var y = this.coordinate.y + viewportOffset.y;
80
81 // Translates the coordinate to be relative to the offset parent.
82 var movableParentTopLeft =
83 goog.positioning.getOffsetParentPageOffset(movableElement);
84 x -= movableParentTopLeft.x;
85 y -= movableParentTopLeft.y;
86
87 goog.positioning.positionAtCoordinate(
88 new goog.math.Coordinate(x, y), movableElement, movableElementCorner,
89 opt_margin, null, null, opt_preferredSize);
90 };