Upgrade to ExtJS 3.2.2 - Released 06/02/2010
[extjs.git] / src / widgets / Window.js
1 /*!
2  * Ext JS Library 3.2.2
3  * Copyright(c) 2006-2010 Ext JS, Inc.
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**
8  * @class Ext.Window
9  * @extends Ext.Panel
10  * <p>A specialized panel intended for use as an application window.  Windows are floated, {@link #resizable}, and
11  * {@link #draggable} by default.  Windows can be {@link #maximizable maximized} to fill the viewport,
12  * restored to their prior size, and can be {@link #minimize}d.</p>
13  * <p>Windows can also be linked to a {@link Ext.WindowGroup} or managed by the {@link Ext.WindowMgr} to provide
14  * grouping, activation, to front, to back and other application-specific behavior.</p>
15  * <p>By default, Windows will be rendered to document.body. To {@link #constrain} a Window to another element
16  * specify {@link Ext.Component#renderTo renderTo}.</p>
17  * <p><b>Note:</b> By default, the <code>{@link #closable close}</code> header tool <i>destroys</i> the Window resulting in
18  * destruction of any child Components. This makes the Window object, and all its descendants <b>unusable</b>. To enable
19  * re-use of a Window, use <b><code>{@link #closeAction closeAction: 'hide'}</code></b>.</p>
20  * @constructor
21  * @param {Object} config The config object
22  * @xtype window
23  */
24 Ext.Window = Ext.extend(Ext.Panel, {
25     /**
26      * @cfg {Number} x
27      * The X position of the left edge of the window on initial showing. Defaults to centering the Window within
28      * the width of the Window's container {@link Ext.Element Element) (The Element that the Window is rendered to).
29      */
30     /**
31      * @cfg {Number} y
32      * The Y position of the top edge of the window on initial showing. Defaults to centering the Window within
33      * the height of the Window's container {@link Ext.Element Element) (The Element that the Window is rendered to).
34      */
35     /**
36      * @cfg {Boolean} modal
37      * True to make the window modal and mask everything behind it when displayed, false to display it without
38      * restricting access to other UI elements (defaults to false).
39      */
40     /**
41      * @cfg {String/Element} animateTarget
42      * Id or element from which the window should animate while opening (defaults to null with no animation).
43      */
44     /**
45      * @cfg {String} resizeHandles
46      * A valid {@link Ext.Resizable} handles config string (defaults to 'all').  Only applies when resizable = true.
47      */
48     /**
49      * @cfg {Ext.WindowGroup} manager
50      * A reference to the WindowGroup that should manage this window (defaults to {@link Ext.WindowMgr}).
51      */
52     /**
53     * @cfg {String/Number/Component} defaultButton
54     * <p>Specifies a Component to receive focus when this Window is focussed.</p>
55     * <p>This may be one of:</p><div class="mdetail-params"><ul>
56     * <li>The index of a footer Button.</li>
57     * <li>The id of a Component.</li>
58     * <li>A Component.</li>
59     * </ul></div>
60     */
61     /**
62     * @cfg {Function} onEsc
63     * Allows override of the built-in processing for the escape key. Default action
64     * is to close the Window (performing whatever action is specified in {@link #closeAction}.
65     * To prevent the Window closing when the escape key is pressed, specify this as
66     * Ext.emptyFn (See {@link Ext#emptyFn}).
67     */
68     /**
69      * @cfg {Boolean} collapsed
70      * True to render the window collapsed, false to render it expanded (defaults to false). Note that if
71      * {@link #expandOnShow} is true (the default) it will override the <tt>collapsed</tt> config and the window
72      * will always be expanded when shown.
73      */
74     /**
75      * @cfg {Boolean} maximized
76      * True to initially display the window in a maximized state. (Defaults to false).
77      */
78
79     /**
80     * @cfg {String} baseCls
81     * The base CSS class to apply to this panel's element (defaults to 'x-window').
82     */
83     baseCls : 'x-window',
84     /**
85      * @cfg {Boolean} resizable
86      * True to allow user resizing at each edge and corner of the window, false to disable resizing (defaults to true).
87      */
88     resizable : true,
89     /**
90      * @cfg {Boolean} draggable
91      * True to allow the window to be dragged by the header bar, false to disable dragging (defaults to true).  Note
92      * that by default the window will be centered in the viewport, so if dragging is disabled the window may need
93      * to be positioned programmatically after render (e.g., myWindow.setPosition(100, 100);).
94      */
95     draggable : true,
96     /**
97      * @cfg {Boolean} closable
98      * <p>True to display the 'close' tool button and allow the user to close the window, false to
99      * hide the button and disallow closing the window (defaults to true).</p>
100      * <p>By default, when close is requested by either clicking the close button in the header
101      * or pressing ESC when the Window has focus, the {@link #close} method will be called. This
102      * will <i>{@link Ext.Component#destroy destroy}</i> the Window and its content meaning that
103      * it may not be reused.</p>
104      * <p>To make closing a Window <i>hide</i> the Window so that it may be reused, set
105      * {@link #closeAction} to 'hide'.
106      */
107     closable : true,
108     /**
109      * @cfg {String} closeAction
110      * <p>The action to take when the close header tool is clicked:
111      * <div class="mdetail-params"><ul>
112      * <li><b><code>'{@link #close}'</code></b> : <b>Default</b><div class="sub-desc">
113      * {@link #close remove} the window from the DOM and {@link Ext.Component#destroy destroy}
114      * it and all descendant Components. The window will <b>not</b> be available to be
115      * redisplayed via the {@link #show} method.
116      * </div></li>
117      * <li><b><code>'{@link #hide}'</code></b> : <div class="sub-desc">
118      * {@link #hide} the window by setting visibility to hidden and applying negative offsets.
119      * The window will be available to be redisplayed via the {@link #show} method.
120      * </div></li>
121      * </ul></div>
122      * <p><b>Note:</b> This setting does not affect the {@link #close} method
123      * which will always {@link Ext.Component#destroy destroy} the window. To
124      * programatically <i>hide</i> a window, call {@link #hide}.</p>
125      */
126     closeAction : 'close',
127     /**
128      * @cfg {Boolean} constrain
129      * True to constrain the window within its containing element, false to allow it to fall outside of its
130      * containing element. By default the window will be rendered to document.body.  To render and constrain the
131      * window within another element specify {@link #renderTo}.
132      * (defaults to false).  Optionally the header only can be constrained using {@link #constrainHeader}.
133      */
134     constrain : false,
135     /**
136      * @cfg {Boolean} constrainHeader
137      * True to constrain the window header within its containing element (allowing the window body to fall outside
138      * of its containing element) or false to allow the header to fall outside its containing element (defaults to
139      * false). Optionally the entire window can be constrained using {@link #constrain}.
140      */
141     constrainHeader : false,
142     /**
143      * @cfg {Boolean} plain
144      * True to render the window body with a transparent background so that it will blend into the framing
145      * elements, false to add a lighter background color to visually highlight the body element and separate it
146      * more distinctly from the surrounding frame (defaults to false).
147      */
148     plain : false,
149     /**
150      * @cfg {Boolean} minimizable
151      * True to display the 'minimize' tool button and allow the user to minimize the window, false to hide the button
152      * and disallow minimizing the window (defaults to false).  Note that this button provides no implementation --
153      * the behavior of minimizing a window is implementation-specific, so the minimize event must be handled and a
154      * custom minimize behavior implemented for this option to be useful.
155      */
156     minimizable : false,
157     /**
158      * @cfg {Boolean} maximizable
159      * True to display the 'maximize' tool button and allow the user to maximize the window, false to hide the button
160      * and disallow maximizing the window (defaults to false).  Note that when a window is maximized, the tool button
161      * will automatically change to a 'restore' button with the appropriate behavior already built-in that will
162      * restore the window to its previous size.
163      */
164     maximizable : false,
165     /**
166      * @cfg {Number} minHeight
167      * The minimum height in pixels allowed for this window (defaults to 100).  Only applies when resizable = true.
168      */
169     minHeight : 100,
170     /**
171      * @cfg {Number} minWidth
172      * The minimum width in pixels allowed for this window (defaults to 200).  Only applies when resizable = true.
173      */
174     minWidth : 200,
175     /**
176      * @cfg {Boolean} expandOnShow
177      * True to always expand the window when it is displayed, false to keep it in its current state (which may be
178      * {@link #collapsed}) when displayed (defaults to true).
179      */
180     expandOnShow : true,
181
182     // inherited docs, same default
183     collapsible : false,
184
185     /**
186      * @cfg {Boolean} initHidden
187      * True to hide the window until show() is explicitly called (defaults to true).
188      * @deprecated
189      */
190     initHidden : undefined,
191
192     /**
193      * @cfg {Boolean} hidden
194      * Render this component hidden (default is <tt>true</tt>). If <tt>true</tt>, the
195      * {@link #hide} method will be called internally.
196      */
197     hidden : true,
198
199     // The following configs are set to provide the basic functionality of a window.
200     // Changing them would require additional code to handle correctly and should
201     // usually only be done in subclasses that can provide custom behavior.  Changing them
202     // may have unexpected or undesirable results.
203     /** @cfg {String} elements @hide */
204     elements : 'header,body',
205     /** @cfg {Boolean} frame @hide */
206     frame : true,
207     /** @cfg {Boolean} floating @hide */
208     floating : true,
209
210     // private
211     initComponent : function(){
212         this.initTools();
213         Ext.Window.superclass.initComponent.call(this);
214         this.addEvents(
215             /**
216              * @event activate
217              * Fires after the window has been visually activated via {@link #setActive}.
218              * @param {Ext.Window} this
219              */
220             /**
221              * @event deactivate
222              * Fires after the window has been visually deactivated via {@link #setActive}.
223              * @param {Ext.Window} this
224              */
225             /**
226              * @event resize
227              * Fires after the window has been resized.
228              * @param {Ext.Window} this
229              * @param {Number} width The window's new width
230              * @param {Number} height The window's new height
231              */
232             'resize',
233             /**
234              * @event maximize
235              * Fires after the window has been maximized.
236              * @param {Ext.Window} this
237              */
238             'maximize',
239             /**
240              * @event minimize
241              * Fires after the window has been minimized.
242              * @param {Ext.Window} this
243              */
244             'minimize',
245             /**
246              * @event restore
247              * Fires after the window has been restored to its original size after being maximized.
248              * @param {Ext.Window} this
249              */
250             'restore'
251         );
252         // for backwards compat, this should be removed at some point
253         if(Ext.isDefined(this.initHidden)){
254             this.hidden = this.initHidden;
255         }
256         if(this.hidden === false){
257             this.hidden = true;
258             this.show();
259         }
260     },
261
262     // private
263     getState : function(){
264         return Ext.apply(Ext.Window.superclass.getState.call(this) || {}, this.getBox(true));
265     },
266
267     // private
268     onRender : function(ct, position){
269         Ext.Window.superclass.onRender.call(this, ct, position);
270
271         if(this.plain){
272             this.el.addClass('x-window-plain');
273         }
274
275         // this element allows the Window to be focused for keyboard events
276         this.focusEl = this.el.createChild({
277                     tag: 'a', href:'#', cls:'x-dlg-focus',
278                     tabIndex:'-1', html: '&#160;'});
279         this.focusEl.swallowEvent('click', true);
280
281         this.proxy = this.el.createProxy('x-window-proxy');
282         this.proxy.enableDisplayMode('block');
283
284         if(this.modal){
285             this.mask = this.container.createChild({cls:'ext-el-mask'}, this.el.dom);
286             this.mask.enableDisplayMode('block');
287             this.mask.hide();
288             this.mon(this.mask, 'click', this.focus, this);
289         }
290         if(this.maximizable){
291             this.mon(this.header, 'dblclick', this.toggleMaximize, this);
292         }
293     },
294
295     // private
296     initEvents : function(){
297         Ext.Window.superclass.initEvents.call(this);
298         if(this.animateTarget){
299             this.setAnimateTarget(this.animateTarget);
300         }
301
302         if(this.resizable){
303             this.resizer = new Ext.Resizable(this.el, {
304                 minWidth: this.minWidth,
305                 minHeight:this.minHeight,
306                 handles: this.resizeHandles || 'all',
307                 pinned: true,
308                 resizeElement : this.resizerAction,
309                 handleCls: 'x-window-handle'
310             });
311             this.resizer.window = this;
312             this.mon(this.resizer, 'beforeresize', this.beforeResize, this);
313         }
314
315         if(this.draggable){
316             this.header.addClass('x-window-draggable');
317         }
318         this.mon(this.el, 'mousedown', this.toFront, this);
319         this.manager = this.manager || Ext.WindowMgr;
320         this.manager.register(this);
321         if(this.maximized){
322             this.maximized = false;
323             this.maximize();
324         }
325         if(this.closable){
326             var km = this.getKeyMap();
327             km.on(27, this.onEsc, this);
328             km.disable();
329         }
330     },
331
332     initDraggable : function(){
333         /**
334          * <p>If this Window is configured {@link #draggable}, this property will contain
335          * an instance of {@link Ext.dd.DD} which handles dragging the Window's DOM Element.</p>
336          * <p>This has implementations of <code>startDrag</code>, <code>onDrag</code> and <code>endDrag</code>
337          * which perform the dragging action. If extra logic is needed at these points, use
338          * {@link Function#createInterceptor createInterceptor} or {@link Function#createSequence createSequence} to
339          * augment the existing implementations.</p>
340          * @type Ext.dd.DD
341          * @property dd
342          */
343         this.dd = new Ext.Window.DD(this);
344     },
345
346    // private
347     onEsc : function(k, e){
348         e.stopEvent();
349         this[this.closeAction]();
350     },
351
352     // private
353     beforeDestroy : function(){
354         if(this.rendered){
355             this.hide();
356             this.clearAnchor();
357             Ext.destroy(
358                 this.focusEl,
359                 this.resizer,
360                 this.dd,
361                 this.proxy,
362                 this.mask
363             );
364         }
365         Ext.Window.superclass.beforeDestroy.call(this);
366     },
367
368     // private
369     onDestroy : function(){
370         if(this.manager){
371             this.manager.unregister(this);
372         }
373         Ext.Window.superclass.onDestroy.call(this);
374     },
375
376     // private
377     initTools : function(){
378         if(this.minimizable){
379             this.addTool({
380                 id: 'minimize',
381                 handler: this.minimize.createDelegate(this, [])
382             });
383         }
384         if(this.maximizable){
385             this.addTool({
386                 id: 'maximize',
387                 handler: this.maximize.createDelegate(this, [])
388             });
389             this.addTool({
390                 id: 'restore',
391                 handler: this.restore.createDelegate(this, []),
392                 hidden:true
393             });
394         }
395         if(this.closable){
396             this.addTool({
397                 id: 'close',
398                 handler: this[this.closeAction].createDelegate(this, [])
399             });
400         }
401     },
402
403     // private
404     resizerAction : function(){
405         var box = this.proxy.getBox();
406         this.proxy.hide();
407         this.window.handleResize(box);
408         return box;
409     },
410
411     // private
412     beforeResize : function(){
413         this.resizer.minHeight = Math.max(this.minHeight, this.getFrameHeight() + 40); // 40 is a magic minimum content size?
414         this.resizer.minWidth = Math.max(this.minWidth, this.getFrameWidth() + 40);
415         this.resizeBox = this.el.getBox();
416     },
417
418     // private
419     updateHandles : function(){
420         if(Ext.isIE && this.resizer){
421             this.resizer.syncHandleHeight();
422             this.el.repaint();
423         }
424     },
425
426     // private
427     handleResize : function(box){
428         var rz = this.resizeBox;
429         if(rz.x != box.x || rz.y != box.y){
430             this.updateBox(box);
431         }else{
432             this.setSize(box);
433             if (Ext.isIE6 && Ext.isStrict) {
434                 this.doLayout();
435             }
436         }
437         this.focus();
438         this.updateHandles();
439         this.saveState();
440     },
441
442     /**
443      * Focuses the window.  If a defaultButton is set, it will receive focus, otherwise the
444      * window itself will receive focus.
445      */
446     focus : function(){
447         var f = this.focusEl,
448             db = this.defaultButton,
449             t = typeof db,
450             el,
451             ct;
452         if(Ext.isDefined(db)){
453             if(Ext.isNumber(db) && this.fbar){
454                 f = this.fbar.items.get(db);
455             }else if(Ext.isString(db)){
456                 f = Ext.getCmp(db);
457             }else{
458                 f = db;
459             }
460             el = f.getEl();
461             ct = Ext.getDom(this.container);
462             if (el && ct) {
463                 if (!Ext.lib.Region.getRegion(ct).contains(Ext.lib.Region.getRegion(el.dom))){
464                     return;
465                 }
466             }
467         }
468         f = f || this.focusEl;
469         f.focus.defer(10, f);
470     },
471
472     /**
473      * Sets the target element from which the window should animate while opening.
474      * @param {String/Element} el The target element or id
475      */
476     setAnimateTarget : function(el){
477         el = Ext.get(el);
478         this.animateTarget = el;
479     },
480
481     // private
482     beforeShow : function(){
483         delete this.el.lastXY;
484         delete this.el.lastLT;
485         if(this.x === undefined || this.y === undefined){
486             var xy = this.el.getAlignToXY(this.container, 'c-c');
487             var pos = this.el.translatePoints(xy[0], xy[1]);
488             this.x = this.x === undefined? pos.left : this.x;
489             this.y = this.y === undefined? pos.top : this.y;
490         }
491         this.el.setLeftTop(this.x, this.y);
492
493         if(this.expandOnShow){
494             this.expand(false);
495         }
496
497         if(this.modal){
498             Ext.getBody().addClass('x-body-masked');
499             this.mask.setSize(Ext.lib.Dom.getViewWidth(true), Ext.lib.Dom.getViewHeight(true));
500             this.mask.show();
501         }
502     },
503
504     /**
505      * Shows the window, rendering it first if necessary, or activates it and brings it to front if hidden.
506      * @param {String/Element} animateTarget (optional) The target element or id from which the window should
507      * animate while opening (defaults to null with no animation)
508      * @param {Function} callback (optional) A callback function to call after the window is displayed
509      * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the callback is executed. Defaults to this Window.
510      * @return {Ext.Window} this
511      */
512     show : function(animateTarget, cb, scope){
513         if(!this.rendered){
514             this.render(Ext.getBody());
515         }
516         if(this.hidden === false){
517             this.toFront();
518             return this;
519         }
520         if(this.fireEvent('beforeshow', this) === false){
521             return this;
522         }
523         if(cb){
524             this.on('show', cb, scope, {single:true});
525         }
526         this.hidden = false;
527         if(Ext.isDefined(animateTarget)){
528             this.setAnimateTarget(animateTarget);
529         }
530         this.beforeShow();
531         if(this.animateTarget){
532             this.animShow();
533         }else{
534             this.afterShow();
535         }
536         return this;
537     },
538
539     // private
540     afterShow : function(isAnim){
541         if (this.isDestroyed){
542             return false;
543         }
544         this.proxy.hide();
545         this.el.setStyle('display', 'block');
546         this.el.show();
547         if(this.maximized){
548             this.fitContainer();
549         }
550         if(Ext.isMac && Ext.isGecko2){ // work around stupid FF 2.0/Mac scroll bar bug
551             this.cascade(this.setAutoScroll);
552         }
553
554         if(this.monitorResize || this.modal || this.constrain || this.constrainHeader){
555             Ext.EventManager.onWindowResize(this.onWindowResize, this);
556         }
557         this.doConstrain();
558         this.doLayout();
559         if(this.keyMap){
560             this.keyMap.enable();
561         }
562         this.toFront();
563         this.updateHandles();
564         if(isAnim && (Ext.isIE || Ext.isWebKit)){
565             var sz = this.getSize();
566             this.onResize(sz.width, sz.height);
567         }
568         this.onShow();
569         this.fireEvent('show', this);
570     },
571
572     // private
573     animShow : function(){
574         this.proxy.show();
575         this.proxy.setBox(this.animateTarget.getBox());
576         this.proxy.setOpacity(0);
577         var b = this.getBox();
578         this.el.setStyle('display', 'none');
579         this.proxy.shift(Ext.apply(b, {
580             callback: this.afterShow.createDelegate(this, [true], false),
581             scope: this,
582             easing: 'easeNone',
583             duration: 0.25,
584             opacity: 0.5
585         }));
586     },
587
588     /**
589      * Hides the window, setting it to invisible and applying negative offsets.
590      * @param {String/Element} animateTarget (optional) The target element or id to which the window should
591      * animate while hiding (defaults to null with no animation)
592      * @param {Function} callback (optional) A callback function to call after the window is hidden
593      * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the callback is executed. Defaults to this Window.
594      * @return {Ext.Window} this
595      */
596     hide : function(animateTarget, cb, scope){
597         if(this.hidden || this.fireEvent('beforehide', this) === false){
598             return this;
599         }
600         if(cb){
601             this.on('hide', cb, scope, {single:true});
602         }
603         this.hidden = true;
604         if(animateTarget !== undefined){
605             this.setAnimateTarget(animateTarget);
606         }
607         if(this.modal){
608             this.mask.hide();
609             Ext.getBody().removeClass('x-body-masked');
610         }
611         if(this.animateTarget){
612             this.animHide();
613         }else{
614             this.el.hide();
615             this.afterHide();
616         }
617         return this;
618     },
619
620     // private
621     afterHide : function(){
622         this.proxy.hide();
623         if(this.monitorResize || this.modal || this.constrain || this.constrainHeader){
624             Ext.EventManager.removeResizeListener(this.onWindowResize, this);
625         }
626         if(this.keyMap){
627             this.keyMap.disable();
628         }
629         this.onHide();
630         this.fireEvent('hide', this);
631     },
632
633     // private
634     animHide : function(){
635         this.proxy.setOpacity(0.5);
636         this.proxy.show();
637         var tb = this.getBox(false);
638         this.proxy.setBox(tb);
639         this.el.hide();
640         this.proxy.shift(Ext.apply(this.animateTarget.getBox(), {
641             callback: this.afterHide,
642             scope: this,
643             duration: 0.25,
644             easing: 'easeNone',
645             opacity: 0
646         }));
647     },
648
649     /**
650      * Method that is called immediately before the <code>show</code> event is fired.
651      * Defaults to <code>Ext.emptyFn</code>.
652      */
653     onShow : Ext.emptyFn,
654
655     /**
656      * Method that is called immediately before the <code>hide</code> event is fired.
657      * Defaults to <code>Ext.emptyFn</code>.
658      */
659     onHide : Ext.emptyFn,
660
661     // private
662     onWindowResize : function(){
663         if(this.maximized){
664             this.fitContainer();
665         }
666         if(this.modal){
667             this.mask.setSize('100%', '100%');
668             var force = this.mask.dom.offsetHeight;
669             this.mask.setSize(Ext.lib.Dom.getViewWidth(true), Ext.lib.Dom.getViewHeight(true));
670         }
671         this.doConstrain();
672     },
673
674     // private
675     doConstrain : function(){
676         if(this.constrain || this.constrainHeader){
677             var offsets;
678             if(this.constrain){
679                 offsets = {
680                     right:this.el.shadowOffset,
681                     left:this.el.shadowOffset,
682                     bottom:this.el.shadowOffset
683                 };
684             }else {
685                 var s = this.getSize();
686                 offsets = {
687                     right:-(s.width - 100),
688                     bottom:-(s.height - 25)
689                 };
690             }
691
692             var xy = this.el.getConstrainToXY(this.container, true, offsets);
693             if(xy){
694                 this.setPosition(xy[0], xy[1]);
695             }
696         }
697     },
698
699     // private - used for dragging
700     ghost : function(cls){
701         var ghost = this.createGhost(cls);
702         var box = this.getBox(true);
703         ghost.setLeftTop(box.x, box.y);
704         ghost.setWidth(box.width);
705         this.el.hide();
706         this.activeGhost = ghost;
707         return ghost;
708     },
709
710     // private
711     unghost : function(show, matchPosition){
712         if(!this.activeGhost) {
713             return;
714         }
715         if(show !== false){
716             this.el.show();
717             this.focus.defer(10, this);
718             if(Ext.isMac && Ext.isGecko2){ // work around stupid FF 2.0/Mac scroll bar bug
719                 this.cascade(this.setAutoScroll);
720             }
721         }
722         if(matchPosition !== false){
723             this.setPosition(this.activeGhost.getLeft(true), this.activeGhost.getTop(true));
724         }
725         this.activeGhost.hide();
726         this.activeGhost.remove();
727         delete this.activeGhost;
728     },
729
730     /**
731      * Placeholder method for minimizing the window.  By default, this method simply fires the {@link #minimize} event
732      * since the behavior of minimizing a window is application-specific.  To implement custom minimize behavior,
733      * either the minimize event can be handled or this method can be overridden.
734      * @return {Ext.Window} this
735      */
736     minimize : function(){
737         this.fireEvent('minimize', this);
738         return this;
739     },
740
741     /**
742      * <p>Closes the Window, removes it from the DOM, {@link Ext.Component#destroy destroy}s
743      * the Window object and all its descendant Components. The {@link Ext.Panel#beforeclose beforeclose}
744      * event is fired before the close happens and will cancel the close action if it returns false.<p>
745      * <p><b>Note:</b> This method is not affected by the {@link #closeAction} setting which
746      * only affects the action triggered when clicking the {@link #closable 'close' tool in the header}.
747      * To hide the Window without destroying it, call {@link #hide}.</p>
748      */
749     close : function(){
750         if(this.fireEvent('beforeclose', this) !== false){
751             if(this.hidden){
752                 this.doClose();
753             }else{
754                 this.hide(null, this.doClose, this);
755             }
756         }
757     },
758
759     // private
760     doClose : function(){
761         this.fireEvent('close', this);
762         this.destroy();
763     },
764
765     /**
766      * Fits the window within its current container and automatically replaces
767      * the {@link #maximizable 'maximize' tool button} with the 'restore' tool button.
768      * Also see {@link #toggleMaximize}.
769      * @return {Ext.Window} this
770      */
771     maximize : function(){
772         if(!this.maximized){
773             this.expand(false);
774             this.restoreSize = this.getSize();
775             this.restorePos = this.getPosition(true);
776             if (this.maximizable){
777                 this.tools.maximize.hide();
778                 this.tools.restore.show();
779             }
780             this.maximized = true;
781             this.el.disableShadow();
782
783             if(this.dd){
784                 this.dd.lock();
785             }
786             if(this.collapsible){
787                 this.tools.toggle.hide();
788             }
789             this.el.addClass('x-window-maximized');
790             this.container.addClass('x-window-maximized-ct');
791
792             this.setPosition(0, 0);
793             this.fitContainer();
794             this.fireEvent('maximize', this);
795         }
796         return this;
797     },
798
799     /**
800      * Restores a {@link #maximizable maximized}  window back to its original
801      * size and position prior to being maximized and also replaces
802      * the 'restore' tool button with the 'maximize' tool button.
803      * Also see {@link #toggleMaximize}.
804      * @return {Ext.Window} this
805      */
806     restore : function(){
807         if(this.maximized){
808             var t = this.tools;
809             this.el.removeClass('x-window-maximized');
810             if(t.restore){
811                 t.restore.hide();
812             }
813             if(t.maximize){
814                 t.maximize.show();
815             }
816             this.setPosition(this.restorePos[0], this.restorePos[1]);
817             this.setSize(this.restoreSize.width, this.restoreSize.height);
818             delete this.restorePos;
819             delete this.restoreSize;
820             this.maximized = false;
821             this.el.enableShadow(true);
822
823             if(this.dd){
824                 this.dd.unlock();
825             }
826             if(this.collapsible && t.toggle){
827                 t.toggle.show();
828             }
829             this.container.removeClass('x-window-maximized-ct');
830
831             this.doConstrain();
832             this.fireEvent('restore', this);
833         }
834         return this;
835     },
836
837     /**
838      * A shortcut method for toggling between {@link #maximize} and {@link #restore} based on the current maximized
839      * state of the window.
840      * @return {Ext.Window} this
841      */
842     toggleMaximize : function(){
843         return this[this.maximized ? 'restore' : 'maximize']();
844     },
845
846     // private
847     fitContainer : function(){
848         var vs = this.container.getViewSize(false);
849         this.setSize(vs.width, vs.height);
850     },
851
852     // private
853     // z-index is managed by the WindowManager and may be overwritten at any time
854     setZIndex : function(index){
855         if(this.modal){
856             this.mask.setStyle('z-index', index);
857         }
858         this.el.setZIndex(++index);
859         index += 5;
860
861         if(this.resizer){
862             this.resizer.proxy.setStyle('z-index', ++index);
863         }
864
865         this.lastZIndex = index;
866     },
867
868     /**
869      * Aligns the window to the specified element
870      * @param {Mixed} element The element to align to.
871      * @param {String} position (optional, defaults to "tl-bl?") The position to align to (see {@link Ext.Element#alignTo} for more details).
872      * @param {Array} offsets (optional) Offset the positioning by [x, y]
873      * @return {Ext.Window} this
874      */
875     alignTo : function(element, position, offsets){
876         var xy = this.el.getAlignToXY(element, position, offsets);
877         this.setPagePosition(xy[0], xy[1]);
878         return this;
879     },
880
881     /**
882      * Anchors this window to another element and realigns it when the window is resized or scrolled.
883      * @param {Mixed} element The element to align to.
884      * @param {String} position The position to align to (see {@link Ext.Element#alignTo} for more details)
885      * @param {Array} offsets (optional) Offset the positioning by [x, y]
886      * @param {Boolean/Number} monitorScroll (optional) true to monitor body scroll and reposition. If this parameter
887      * is a number, it is used as the buffer delay (defaults to 50ms).
888      * @return {Ext.Window} this
889      */
890     anchorTo : function(el, alignment, offsets, monitorScroll){
891         this.clearAnchor();
892         this.anchorTarget = {
893             el: el,
894             alignment: alignment,
895             offsets: offsets
896         };
897
898         Ext.EventManager.onWindowResize(this.doAnchor, this);
899         var tm = typeof monitorScroll;
900         if(tm != 'undefined'){
901             Ext.EventManager.on(window, 'scroll', this.doAnchor, this,
902                 {buffer: tm == 'number' ? monitorScroll : 50});
903         }
904         return this.doAnchor();
905     },
906
907     /**
908      * Performs the anchor, using the saved anchorTarget property.
909      * @return {Ext.Window} this
910      * @private
911      */
912     doAnchor : function(){
913         var o = this.anchorTarget;
914         this.alignTo(o.el, o.alignment, o.offsets);
915         return this;
916     },
917
918     /**
919      * Removes any existing anchor from this window. See {@link #anchorTo}.
920      * @return {Ext.Window} this
921      */
922     clearAnchor : function(){
923         if(this.anchorTarget){
924             Ext.EventManager.removeResizeListener(this.doAnchor, this);
925             Ext.EventManager.un(window, 'scroll', this.doAnchor, this);
926             delete this.anchorTarget;
927         }
928         return this;
929     },
930
931     /**
932      * Brings this window to the front of any other visible windows
933      * @param {Boolean} e (optional) Specify <tt>false</tt> to prevent the window from being focused.
934      * @return {Ext.Window} this
935      */
936     toFront : function(e){
937         if(this.manager.bringToFront(this)){
938             if(!e || !e.getTarget().focus){
939                 this.focus();
940             }
941         }
942         return this;
943     },
944
945     /**
946      * Makes this the active window by showing its shadow, or deactivates it by hiding its shadow.  This method also
947      * fires the {@link #activate} or {@link #deactivate} event depending on which action occurred. This method is
948      * called internally by {@link Ext.WindowMgr}.
949      * @param {Boolean} active True to activate the window, false to deactivate it (defaults to false)
950      */
951     setActive : function(active){
952         if(active){
953             if(!this.maximized){
954                 this.el.enableShadow(true);
955             }
956             this.fireEvent('activate', this);
957         }else{
958             this.el.disableShadow();
959             this.fireEvent('deactivate', this);
960         }
961     },
962
963     /**
964      * Sends this window to the back of (lower z-index than) any other visible windows
965      * @return {Ext.Window} this
966      */
967     toBack : function(){
968         this.manager.sendToBack(this);
969         return this;
970     },
971
972     /**
973      * Centers this window in the viewport
974      * @return {Ext.Window} this
975      */
976     center : function(){
977         var xy = this.el.getAlignToXY(this.container, 'c-c');
978         this.setPagePosition(xy[0], xy[1]);
979         return this;
980     }
981
982     /**
983      * @cfg {Boolean} autoWidth @hide
984      **/
985 });
986 Ext.reg('window', Ext.Window);
987
988 // private - custom Window DD implementation
989 Ext.Window.DD = function(win){
990     this.win = win;
991     Ext.Window.DD.superclass.constructor.call(this, win.el.id, 'WindowDD-'+win.id);
992     this.setHandleElId(win.header.id);
993     this.scroll = false;
994 };
995
996 Ext.extend(Ext.Window.DD, Ext.dd.DD, {
997     moveOnly:true,
998     headerOffsets:[100, 25],
999     startDrag : function(){
1000         var w = this.win;
1001         this.proxy = w.ghost();
1002         if(w.constrain !== false){
1003             var so = w.el.shadowOffset;
1004             this.constrainTo(w.container, {right: so, left: so, bottom: so});
1005         }else if(w.constrainHeader !== false){
1006             var s = this.proxy.getSize();
1007             this.constrainTo(w.container, {right: -(s.width-this.headerOffsets[0]), bottom: -(s.height-this.headerOffsets[1])});
1008         }
1009     },
1010     b4Drag : Ext.emptyFn,
1011
1012     onDrag : function(e){
1013         this.alignElWithMouse(this.proxy, e.getPageX(), e.getPageY());
1014     },
1015
1016     endDrag : function(e){
1017         this.win.unghost();
1018         this.win.saveState();
1019     }
1020 });