Upgrade to ExtJS 4.0.1 - Released 05/18/2011
[extjs.git] / src / util / Floating.js
1 /**
2  * @class Ext.util.Floating
3  * A mixin to add floating capability to a Component
4  */
5 Ext.define('Ext.util.Floating', {
6
7     uses: ['Ext.Layer', 'Ext.window.Window'],
8
9     /**
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.
13      */
14     focusOnToFront: true,
15
16     /**
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>.)
21      */
22     shadow: 'sides',
23
24     constructor: function(config) {
25         this.floating = true;
26         this.el = Ext.create('Ext.Layer', Ext.apply({}, config, {
27             hideMode: this.hideMode,
28             hidden: this.hidden,
29             shadow: Ext.isDefined(this.shadow) ? this.shadow : 'sides',
30             shadowOffset: this.shadowOffset,
31             constrain: false,
32             shim: this.shim === false ? false : undefined
33         }), this.el);
34     },
35
36     onFloatRender: function() {
37         var me = this;
38         me.zIndexParent = me.getZIndexParent();
39         me.setFloatParent(me.ownerCt);
40         delete me.ownerCt;
41
42         if (me.zIndexParent) {
43             me.zIndexParent.registerFloatingItem(me);
44         } else {
45             Ext.WindowManager.register(me);
46         }
47     },
48
49     setFloatParent: function(floatParent) {
50         var me = this;
51
52         // Remove listeners from previous floatParent
53         if (me.floatParent) {
54             me.mun(me.floatParent, {
55                 hide: me.onFloatParentHide,
56                 show: me.onFloatParentShow,
57                 scope: me
58             });
59         }
60
61         me.floatParent = floatParent;
62
63         // Floating Components as children of Containers must hide when their parent hides.
64         if (floatParent) {
65             me.mon(me.floatParent, {
66                 hide: me.onFloatParentHide,
67                 show: me.onFloatParentShow,
68                 scope: me
69             });
70         }
71
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;
76         }
77     },
78
79     onFloatParentHide: function() {
80         this.showOnParentShow = this.isVisible();
81         this.hide();
82     },
83
84     onFloatParentShow: function() {
85         if (this.showOnParentShow) {
86             delete this.showOnParentShow;
87             this.show();
88         }
89     },
90
91     /**
92      * @private
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>
97      */
98     getZIndexParent: function() {
99         var p = this.ownerCt,
100             c;
101
102         if (p) {
103             while (p) {
104                 c = p;
105                 p = p.ownerCt;
106             }
107             if (c.floating) {
108                 return c;
109             }
110         }
111     },
112
113     // private
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) {
119         var me = this;
120         this.el.setZIndex(index);
121
122         // Next item goes 10 above;
123         index += 10;
124
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;
129         }
130         return index;
131     },
132
133     /**
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.
139      */
140     doConstrain: function(constrainTo) {
141         var me = this,
142             vector = me.getConstrainVector(constrainTo),
143             xy;
144
145         if (vector) {
146             xy = me.getPosition();
147             xy[0] += vector[0];
148             xy[1] += vector[1];
149             me.setPosition(xy);
150         }
151     },
152     
153     
154     /**
155      * Gets the x/y offsets to constrain this float
156      * @private
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
159      */
160     getConstrainVector: function(constrainTo){
161         var me = this,
162             el;
163             
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);
168         }
169     },
170
171     /**
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
178      */
179     alignTo: function(element, position, offsets) {
180         if (element.isComponent) {
181             element = element.getEl();
182         }
183         var xy = this.el.getAlignToXY(element, position, offsets);
184         this.setPagePosition(xy);
185         return this;
186     },
187
188     /**
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
193      */
194     toFront: function(preventFocus) {
195         var me = this;
196
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);
201         }
202         if (me.zIndexManager.bringToFront(me)) {
203             if (!Ext.isDefined(preventFocus)) {
204                 preventFocus = !me.focusOnToFront;
205             }
206             if (!preventFocus) {
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);
211             }
212         }
213         return me;
214     },
215
216     /**
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.
223      */
224     setActive: function(active, newActive) {
225         if (active) {
226             if ((this instanceof Ext.window.Window) && !this.maximized) {
227                 this.el.enableShadow(true);
228             }
229             this.fireEvent('activate', this);
230         } else {
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();
235             }
236             this.fireEvent('deactivate', this);
237         }
238     },
239
240     /**
241      * Sends this Component to the back of (lower z-index than) any other visible windows
242      * @return {Component} this
243      */
244     toBack: function() {
245         this.zIndexManager.sendToBack(this);
246         return this;
247     },
248
249     /**
250      * Center this Component in its container.
251      * @return {Component} this
252      */
253     center: function() {
254         var xy = this.el.getAlignToXY(this.container, 'c-c');
255         this.setPagePosition(xy);
256         return this;
257     },
258
259     // private
260     syncShadow : function(){
261         if (this.floating) {
262             this.el.sync(true);
263         }
264     },
265
266     // private
267     fitContainer: function() {
268         var parent = this.floatParent,
269             container = parent ? parent.getTargetEl() : this.container,
270             size = container.getViewSize(false);
271
272         this.setSize(size);
273     }
274 });