| 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 Definition of the Popup class. |
| 17 |
* |
| 18 |
* @author [email protected] (Emil A Eklund) |
| 19 |
* @see ../demos/popup.html |
| 20 |
*/ |
| 21 |
|
| 22 |
goog.provide('goog.ui.Popup'); |
| 23 |
|
| 24 |
goog.require('goog.math.Box'); |
| 25 |
goog.require('goog.positioning.AbstractPosition'); |
| 26 |
goog.require('goog.positioning.Corner'); |
| 27 |
goog.require('goog.style'); |
| 28 |
goog.require('goog.ui.PopupBase'); |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
/** |
| 33 |
* The Popup class provides functionality for displaying an absolutely |
| 34 |
* positioned element at a particular location in the window. It's designed to |
| 35 |
* be used as the foundation for building controls like a menu or tooltip. The |
| 36 |
* Popup class includes functionality for displaying a Popup near adjacent to |
| 37 |
* an anchor element. |
| 38 |
* |
| 39 |
* This works cross browser and thus does not use IE's createPopup feature |
| 40 |
* which supports extending outside the edge of the brower window. |
| 41 |
* |
| 42 |
* @param {Element=} opt_element A DOM element for the popup. |
| 43 |
* @param {goog.positioning.AbstractPosition=} opt_position A positioning helper |
| 44 |
* object. |
| 45 |
* @constructor |
| 46 |
* @extends {goog.ui.PopupBase} |
| 47 |
*/ |
| 48 |
goog.ui.Popup = function(opt_element, opt_position) { |
| 49 |
/** |
| 50 |
* Corner of the popup to used in the positioning algorithm. |
| 51 |
* |
| 52 |
* @type {goog.positioning.Corner} |
| 53 |
* @private |
| 54 |
*/ |
| 55 |
this.popupCorner_ = goog.positioning.Corner.TOP_START; |
| 56 |
|
| 57 |
/** |
| 58 |
* Positioning helper object. |
| 59 |
* |
| 60 |
* @private {goog.positioning.AbstractPosition|undefined} |
| 61 |
*/ |
| 62 |
this.position_ = opt_position || undefined; |
| 63 |
goog.ui.PopupBase.call(this, opt_element); |
| 64 |
}; |
| 65 |
goog.inherits(goog.ui.Popup, goog.ui.PopupBase); |
| 66 |
goog.tagUnsealableClass(goog.ui.Popup); |
| 67 |
|
| 68 |
|
| 69 |
/** |
| 70 |
* Margin for the popup used in positioning algorithms. |
| 71 |
* |
| 72 |
* @type {goog.math.Box|undefined} |
| 73 |
* @private |
| 74 |
*/ |
| 75 |
goog.ui.Popup.prototype.margin_; |
| 76 |
|
| 77 |
|
| 78 |
/** |
| 79 |
* Returns the corner of the popup to used in the positioning algorithm. |
| 80 |
* |
| 81 |
* @return {goog.positioning.Corner} The popup corner used for positioning. |
| 82 |
*/ |
| 83 |
goog.ui.Popup.prototype.getPinnedCorner = function() { |
| 84 |
return this.popupCorner_; |
| 85 |
}; |
| 86 |
|
| 87 |
|
| 88 |
/** |
| 89 |
* Sets the corner of the popup to used in the positioning algorithm. |
| 90 |
* |
| 91 |
* @param {goog.positioning.Corner} corner The popup corner used for |
| 92 |
* positioning. |
| 93 |
*/ |
| 94 |
goog.ui.Popup.prototype.setPinnedCorner = function(corner) { |
| 95 |
this.popupCorner_ = corner; |
| 96 |
if (this.isVisible()) { |
| 97 |
this.reposition(); |
| 98 |
} |
| 99 |
}; |
| 100 |
|
| 101 |
|
| 102 |
/** |
| 103 |
* @return {goog.positioning.AbstractPosition} The position helper object |
| 104 |
* associated with the popup. |
| 105 |
*/ |
| 106 |
goog.ui.Popup.prototype.getPosition = function() { |
| 107 |
return this.position_ || null; |
| 108 |
}; |
| 109 |
|
| 110 |
|
| 111 |
/** |
| 112 |
* Sets the position helper object associated with the popup. |
| 113 |
* |
| 114 |
* @param {goog.positioning.AbstractPosition} position A position helper object. |
| 115 |
*/ |
| 116 |
goog.ui.Popup.prototype.setPosition = function(position) { |
| 117 |
this.position_ = position || undefined; |
| 118 |
if (this.isVisible()) { |
| 119 |
this.reposition(); |
| 120 |
} |
| 121 |
}; |
| 122 |
|
| 123 |
|
| 124 |
/** |
| 125 |
* Returns the margin to place around the popup. |
| 126 |
* |
| 127 |
* @return {goog.math.Box?} The margin. |
| 128 |
*/ |
| 129 |
goog.ui.Popup.prototype.getMargin = function() { |
| 130 |
return this.margin_ || null; |
| 131 |
}; |
| 132 |
|
| 133 |
|
| 134 |
/** |
| 135 |
* Sets the margin to place around the popup. |
| 136 |
* |
| 137 |
* @param {goog.math.Box|number|null} arg1 Top value or Box. |
| 138 |
* @param {number=} opt_arg2 Right value. |
| 139 |
* @param {number=} opt_arg3 Bottom value. |
| 140 |
* @param {number=} opt_arg4 Left value. |
| 141 |
*/ |
| 142 |
goog.ui.Popup.prototype.setMargin = function( |
| 143 |
arg1, opt_arg2, opt_arg3, opt_arg4) { |
| 144 |
if (arg1 == null || arg1 instanceof goog.math.Box) { |
| 145 |
this.margin_ = arg1; |
| 146 |
} else { |
| 147 |
this.margin_ = new goog.math.Box( |
| 148 |
arg1, |
| 149 |
/** @type {number} */ (opt_arg2), |
| 150 |
/** @type {number} */ (opt_arg3), |
| 151 |
/** @type {number} */ (opt_arg4)); |
| 152 |
} |
| 153 |
if (this.isVisible()) { |
| 154 |
this.reposition(); |
| 155 |
} |
| 156 |
}; |
| 157 |
|
| 158 |
|
| 159 |
/** |
| 160 |
* Repositions the popup according to the current state. |
| 161 |
* @override |
| 162 |
*/ |
| 163 |
goog.ui.Popup.prototype.reposition = function() { |
| 164 |
if (!this.position_) { |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
var hideForPositioning = !this.isVisible() && |
| 169 |
this.getType() != goog.ui.PopupBase.Type.MOVE_OFFSCREEN; |
| 170 |
var el = this.getElement(); |
| 171 |
if (hideForPositioning) { |
| 172 |
el.style.visibility = 'hidden'; |
| 173 |
goog.style.setElementShown(el, true); |
| 174 |
} |
| 175 |
|
| 176 |
this.position_.reposition(el, this.popupCorner_, this.margin_); |
| 177 |
|
| 178 |
if (hideForPositioning) { |
| 179 |
// NOTE(eae): The visibility property is reset to 'visible' by the show_ |
| 180 |
// method in PopupBase. Resetting it here causes flickering in some |
| 181 |
// situations, even if set to visible after the display property has been |
| 182 |
// set to none by the call below. |
| 183 |
goog.style.setElementShown(el, false); |
| 184 |
} |
| 185 |
}; |