2 * @class Ext.util.Floating
3 * A mixin to add floating capability to a Component
5 Ext.define('Ext.util.Floating', {
7 uses: ['Ext.Layer', 'Ext.window.Window'],
10 * @cfg {Boolean} focusOnToFront
11 * Specifies whether the floated component should be automatically {@link #focus focused} when it is
12 * {@link #toFront brought to the front}. Defaults to true.
17 * @cfg {String/Boolean} shadow Specifies whether the floating component should be given a shadow. Set to
18 * <tt>true</tt> to automatically create an {@link Ext.Shadow}, or a string indicating the
19 * shadow's display {@link Ext.Shadow#mode}. Set to <tt>false</tt> to disable the shadow.
20 * (Defaults to <tt>'sides'</tt>.)
24 constructor: function(config) {
26 this.el = Ext.create('Ext.Layer', Ext.apply({}, config, {
27 hideMode: this.hideMode,
29 shadow: Ext.isDefined(this.shadow) ? this.shadow : 'sides',
30 shadowOffset: this.shadowOffset,
32 shim: this.shim === false ? false : undefined
36 onFloatRender: function() {
38 me.zIndexParent = me.getZIndexParent();
39 me.setFloatParent(me.ownerCt);
42 if (me.zIndexParent) {
43 me.zIndexParent.registerFloatingItem(me);
45 Ext.WindowManager.register(me);
49 setFloatParent: function(floatParent) {
52 // Remove listeners from previous floatParent
54 me.mun(me.floatParent, {
55 hide: me.onFloatParentHide,
56 show: me.onFloatParentShow,
61 me.floatParent = floatParent;
63 // Floating Components as children of Containers must hide when their parent hides.
65 me.mon(me.floatParent, {
66 hide: me.onFloatParentHide,
67 show: me.onFloatParentShow,
72 // If a floating Component is configured to be constrained, but has no configured
73 // constrainTo setting, set its constrainTo to be it's ownerCt before rendering.
74 if ((me.constrain || me.constrainHeader) && !me.constrainTo) {
75 me.constrainTo = floatParent ? floatParent.getTargetEl() : me.container;
79 onFloatParentHide: function() {
80 this.showOnParentShow = this.isVisible();
84 onFloatParentShow: function() {
85 if (this.showOnParentShow) {
86 delete this.showOnParentShow;
93 * <p>Finds the ancestor Container responsible for allocating zIndexes for the passed Component.</p>
94 * <p>That will be the outermost floating Container (a Container which has no ownerCt and has floating:true).</p>
95 * <p>If we have no ancestors, or we walk all the way up to the document body, there's no zIndexParent,
96 * and the global Ext.WindowManager will be used.</p>
98 getZIndexParent: function() {
114 // z-index is managed by the zIndexManager and may be overwritten at any time.
115 // Returns the next z-index to be used.
116 // If this is a Container, then it will have rebased any managed floating Components,
117 // and so the next available z-index will be approximately 10000 above that.
118 setZIndex: function(index) {
120 this.el.setZIndex(index);
122 // Next item goes 10 above;
125 // When a Container with floating items has its z-index set, it rebases any floating items it is managing.
126 // The returned value is a round number approximately 10000 above the last z-index used.
127 if (me.floatingItems) {
128 index = Math.floor(me.floatingItems.setBase(index) / 100) * 100 + 10000;
134 * <p>Moves this floating Component into a constrain region.</p>
135 * <p>By default, this Component is constrained to be within the container it was added to, or the element
136 * it was rendered to.</p>
137 * <p>An alternative constraint may be passed.</p>
138 * @param {Mixed} constrainTo Optional. The Element or {@link Ext.util.Region Region} into which this Component is to be constrained.
140 doConstrain: function(constrainTo) {
142 vector = me.getConstrainVector(constrainTo),
146 xy = me.getPosition();
155 * Gets the x/y offsets to constrain this float
157 * @param {Mixed} constrainTo Optional. The Element or {@link Ext.util.Region Region} into which this Component is to be constrained.
158 * @return {Array} The x/y constraints
160 getConstrainVector: function(constrainTo){
164 if (me.constrain || me.constrainHeader) {
165 el = me.constrainHeader ? me.header.el : me.el;
166 constrainTo = constrainTo || (me.floatParent && me.floatParent.getTargetEl()) || me.container;
167 return el.getConstrainVector(constrainTo);
172 * Aligns this floating Component to the specified element
173 * @param {Mixed} element The element or {@link Ext.Component} to align to. If passing a component, it must
174 * be a omponent instance. If a string id is passed, it will be used as an element id.
175 * @param {String} position (optional, defaults to "tl-bl?") The position to align to (see {@link Ext.core.Element#alignTo} for more details).
176 * @param {Array} offsets (optional) Offset the positioning by [x, y]
177 * @return {Component} this
179 alignTo: function(element, position, offsets) {
180 if (element.isComponent) {
181 element = element.getEl();
183 var xy = this.el.getAlignToXY(element, position, offsets);
184 this.setPagePosition(xy);
189 * <p>Brings this floating Component to the front of any other visible, floating Components managed by the same {@link Ext.ZIndexManager ZIndexManager}</p>
190 * <p>If this Component is modal, inserts the modal mask just below this Component in the z-index stack.</p>
191 * @param {Boolean} preventFocus (optional) Specify <code>true</code> to prevent the Component from being focused.
192 * @return {Component} this
194 toFront: function(preventFocus) {
197 // Find the floating Component which provides the base for this Component's zIndexing.
198 // That must move to front to then be able to rebase its zIndex stack and move this to the front
199 if (me.zIndexParent) {
200 me.zIndexParent.toFront(true);
202 if (me.zIndexManager.bringToFront(me)) {
203 if (!Ext.isDefined(preventFocus)) {
204 preventFocus = !me.focusOnToFront;
207 // Kick off a delayed focus request.
208 // If another floating Component is toFronted before the delay expires
209 // this will not receive focus.
210 me.focus(false, true);
217 * <p>This method is called internally by {@link Ext.ZIndexManager} to signal that a floating
218 * Component has either been moved to the top of its zIndex stack, or pushed from the top of its zIndex stack.</p>
219 * <p>If a <i>Window</i> is superceded by another Window, deactivating it hides its shadow.</p>
220 * <p>This method also fires the {@link #activate} or {@link #deactivate} event depending on which action occurred.</p>
221 * @param {Boolean} active True to activate the Component, false to deactivate it (defaults to false)
222 * @param {Component} newActive The newly active Component which is taking over topmost zIndex position.
224 setActive: function(active, newActive) {
226 if ((this instanceof Ext.window.Window) && !this.maximized) {
227 this.el.enableShadow(true);
229 this.fireEvent('activate', this);
231 // Only the *Windows* in a zIndex stack share a shadow. All other types of floaters
232 // can keep their shadows all the time
233 if ((this instanceof Ext.window.Window) && (newActive instanceof Ext.window.Window)) {
234 this.el.disableShadow();
236 this.fireEvent('deactivate', this);
241 * Sends this Component to the back of (lower z-index than) any other visible windows
242 * @return {Component} this
245 this.zIndexManager.sendToBack(this);
250 * Center this Component in its container.
251 * @return {Component} this
254 var xy = this.el.getAlignToXY(this.container, 'c-c');
255 this.setPagePosition(xy);
260 syncShadow : function(){
267 fitContainer: function() {
268 var parent = this.floatParent,
269 container = parent ? parent.getTargetEl() : this.container,
270 size = container.getViewSize(false);