3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
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>
21 * @param {Object} config The config object
24 Ext.Window = Ext.extend(Ext.Panel, {
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).
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).
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).
41 * @cfg {String/Element} animateTarget
42 * Id or element from which the window should animate while opening (defaults to null with no animation).
45 * @cfg {String} resizeHandles
46 * A valid {@link Ext.Resizable} handles config string (defaults to 'all'). Only applies when resizable = true.
49 * @cfg {Ext.WindowGroup} manager
50 * A reference to the WindowGroup that should manage this window (defaults to {@link Ext.WindowMgr}).
53 * @cfg {String/Number/Button} defaultButton
54 * The id / index of a button or a button instance to focus when this window received the focus.
57 * @cfg {Function} onEsc
58 * Allows override of the built-in processing for the escape key. Default action
59 * is to close the Window (performing whatever action is specified in {@link #closeAction}.
60 * To prevent the Window closing when the escape key is pressed, specify this as
61 * Ext.emptyFn (See {@link Ext#emptyFn}).
64 * @cfg {Boolean} collapsed
65 * True to render the window collapsed, false to render it expanded (defaults to false). Note that if
66 * {@link #expandOnShow} is true (the default) it will override the <tt>collapsed</tt> config and the window
67 * will always be expanded when shown.
70 * @cfg {Boolean} maximized
71 * True to initially display the window in a maximized state. (Defaults to false).
75 * @cfg {String} baseCls
76 * The base CSS class to apply to this panel's element (defaults to 'x-window').
80 * @cfg {Boolean} resizable
81 * True to allow user resizing at each edge and corner of the window, false to disable resizing (defaults to true).
85 * @cfg {Boolean} draggable
86 * True to allow the window to be dragged by the header bar, false to disable dragging (defaults to true). Note
87 * that by default the window will be centered in the viewport, so if dragging is disabled the window may need
88 * to be positioned programmatically after render (e.g., myWindow.setPosition(100, 100);).
92 * @cfg {Boolean} closable
93 * <p>True to display the 'close' tool button and allow the user to close the window, false to
94 * hide the button and disallow closing the window (defaults to true).</p>
95 * <p>By default, when close is requested by either clicking the close button in the header
96 * or pressing ESC when the Window has focus, the {@link #close} method will be called. This
97 * will <i>{@link Ext.Component#destroy destroy}</i> the Window and its content meaning that
98 * it may not be reused.</p>
99 * <p>To make closing a Window <i>hide</i> the Window so that it may be reused, set
100 * {@link #closeAction} to 'hide'.
104 * @cfg {String} closeAction
105 * <p>The action to take when the close header tool is clicked:
106 * <div class="mdetail-params"><ul>
107 * <li><b><code>'{@link #close}'</code></b> : <b>Default</b><div class="sub-desc">
108 * {@link #close remove} the window from the DOM and {@link Ext.Component#destroy destroy}
109 * it and all descendant Components. The window will <b>not</b> be available to be
110 * redisplayed via the {@link #show} method.
112 * <li><b><code>'{@link #hide}'</code></b> : <div class="sub-desc">
113 * {@link #hide} the window by setting visibility to hidden and applying negative offsets.
114 * The window will be available to be redisplayed via the {@link #show} method.
117 * <p><b>Note:</b> This setting does not affect the {@link #close} method
118 * which will always {@link Ext.Component#destroy destroy} the window. To
119 * programatically <i>hide</i> a window, call {@link #hide}.</p>
121 closeAction : 'close',
123 * @cfg {Boolean} constrain
124 * True to constrain the window within its containing element, false to allow it to fall outside of its
125 * containing element. By default the window will be rendered to document.body. To render and constrain the
126 * window within another element specify {@link #renderTo}.
127 * (defaults to false). Optionally the header only can be constrained using {@link #constrainHeader}.
131 * @cfg {Boolean} constrainHeader
132 * True to constrain the window header within its containing element (allowing the window body to fall outside
133 * of its containing element) or false to allow the header to fall outside its containing element (defaults to
134 * false). Optionally the entire window can be constrained using {@link #constrain}.
136 constrainHeader : false,
138 * @cfg {Boolean} plain
139 * True to render the window body with a transparent background so that it will blend into the framing
140 * elements, false to add a lighter background color to visually highlight the body element and separate it
141 * more distinctly from the surrounding frame (defaults to false).
145 * @cfg {Boolean} minimizable
146 * True to display the 'minimize' tool button and allow the user to minimize the window, false to hide the button
147 * and disallow minimizing the window (defaults to false). Note that this button provides no implementation --
148 * the behavior of minimizing a window is implementation-specific, so the minimize event must be handled and a
149 * custom minimize behavior implemented for this option to be useful.
153 * @cfg {Boolean} maximizable
154 * True to display the 'maximize' tool button and allow the user to maximize the window, false to hide the button
155 * and disallow maximizing the window (defaults to false). Note that when a window is maximized, the tool button
156 * will automatically change to a 'restore' button with the appropriate behavior already built-in that will
157 * restore the window to its previous size.
161 * @cfg {Number} minHeight
162 * The minimum height in pixels allowed for this window (defaults to 100). Only applies when resizable = true.
166 * @cfg {Number} minWidth
167 * The minimum width in pixels allowed for this window (defaults to 200). Only applies when resizable = true.
171 * @cfg {Boolean} expandOnShow
172 * True to always expand the window when it is displayed, false to keep it in its current state (which may be
173 * {@link #collapsed}) when displayed (defaults to true).
177 // inherited docs, same default
181 * @cfg {Boolean} initHidden
182 * True to hide the window until show() is explicitly called (defaults to true).
186 * @cfg {Boolean} monitorResize @hide
187 * This is automatically managed based on the value of constrain and constrainToHeader
189 monitorResize : true,
191 // The following configs are set to provide the basic functionality of a window.
192 // Changing them would require additional code to handle correctly and should
193 // usually only be done in subclasses that can provide custom behavior. Changing them
194 // may have unexpected or undesirable results.
195 /** @cfg {String} elements @hide */
196 elements : 'header,body',
197 /** @cfg {Boolean} frame @hide */
199 /** @cfg {Boolean} floating @hide */
203 initComponent : function(){
204 Ext.Window.superclass.initComponent.call(this);
208 * Fires after the window has been visually activated via {@link setActive}.
209 * @param {Ext.Window} this
213 * Fires after the window has been visually deactivated via {@link setActive}.
214 * @param {Ext.Window} this
218 * Fires after the window has been resized.
219 * @param {Ext.Window} this
220 * @param {Number} width The window's new width
221 * @param {Number} height The window's new height
226 * Fires after the window has been maximized.
227 * @param {Ext.Window} this
232 * Fires after the window has been minimized.
233 * @param {Ext.Window} this
238 * Fires after the window has been restored to its original size after being maximized.
239 * @param {Ext.Window} this
243 if(this.initHidden === false){
251 getState : function(){
252 return Ext.apply(Ext.Window.superclass.getState.call(this) || {}, this.getBox(true));
256 onRender : function(ct, position){
257 Ext.Window.superclass.onRender.call(this, ct, position);
260 this.el.addClass('x-window-plain');
263 // this element allows the Window to be focused for keyboard events
264 this.focusEl = this.el.createChild({
265 tag: 'a', href:'#', cls:'x-dlg-focus',
266 tabIndex:'-1', html: ' '});
267 this.focusEl.swallowEvent('click', true);
269 this.proxy = this.el.createProxy('x-window-proxy');
270 this.proxy.enableDisplayMode('block');
273 this.mask = this.container.createChild({cls:'ext-el-mask'}, this.el.dom);
274 this.mask.enableDisplayMode('block');
276 this.mon(this.mask, 'click', this.focus, this);
282 initEvents : function(){
283 Ext.Window.superclass.initEvents.call(this);
284 if(this.animateTarget){
285 this.setAnimateTarget(this.animateTarget);
289 this.resizer = new Ext.Resizable(this.el, {
290 minWidth: this.minWidth,
291 minHeight:this.minHeight,
292 handles: this.resizeHandles || 'all',
294 resizeElement : this.resizerAction
296 this.resizer.window = this;
297 this.mon(this.resizer, 'beforeresize', this.beforeResize, this);
301 this.header.addClass('x-window-draggable');
303 this.mon(this.el, 'mousedown', this.toFront, this);
304 this.manager = this.manager || Ext.WindowMgr;
305 this.manager.register(this);
307 this.maximized = false;
311 var km = this.getKeyMap();
312 km.on(27, this.onEsc, this);
317 initDraggable : function(){
319 * If this Window is configured {@link #draggable}, this property will contain
320 * an instance of {@link Ext.dd.DD} which handles dragging the Window's DOM Element.
324 this.dd = new Ext.Window.DD(this);
329 this[this.closeAction]();
333 beforeDestroy : function(){
337 Ext.EventManager.removeResizeListener(this.doAnchor, this);
338 Ext.EventManager.un(window, 'scroll', this.doAnchor, this);
348 Ext.Window.superclass.beforeDestroy.call(this);
352 onDestroy : function(){
354 this.manager.unregister(this);
356 Ext.Window.superclass.onDestroy.call(this);
360 initTools : function(){
361 if(this.minimizable){
364 handler: this.minimize.createDelegate(this, [])
367 if(this.maximizable){
370 handler: this.maximize.createDelegate(this, [])
374 handler: this.restore.createDelegate(this, []),
377 this.mon(this.header, 'dblclick', this.toggleMaximize, this);
382 handler: this[this.closeAction].createDelegate(this, [])
388 resizerAction : function(){
389 var box = this.proxy.getBox();
391 this.window.handleResize(box);
396 beforeResize : function(){
397 this.resizer.minHeight = Math.max(this.minHeight, this.getFrameHeight() + 40); // 40 is a magic minimum content size?
398 this.resizer.minWidth = Math.max(this.minWidth, this.getFrameWidth() + 40);
399 this.resizeBox = this.el.getBox();
403 updateHandles : function(){
404 if(Ext.isIE && this.resizer){
405 this.resizer.syncHandleHeight();
411 handleResize : function(box){
412 var rz = this.resizeBox;
413 if(rz.x != box.x || rz.y != box.y){
419 this.updateHandles();
422 this.fireEvent('resize', this, box.width, box.height);
426 * Focuses the window. If a defaultButton is set, it will receive focus, otherwise the
427 * window itself will receive focus.
430 var f = this.focusEl, db = this.defaultButton, t = typeof db;
431 if(t != 'undefined'){
432 if(t == 'number' && this.fbar){
433 f = this.fbar.items.get(db);
434 }else if(t == 'string'){
440 f = f || this.focusEl;
441 f.focus.defer(10, f);
445 * Sets the target element from which the window should animate while opening.
446 * @param {String/Element} el The target element or id
448 setAnimateTarget : function(el){
450 this.animateTarget = el;
454 beforeShow : function(){
455 delete this.el.lastXY;
456 delete this.el.lastLT;
457 if(this.x === undefined || this.y === undefined){
458 var xy = this.el.getAlignToXY(this.container, 'c-c');
459 var pos = this.el.translatePoints(xy[0], xy[1]);
460 this.x = this.x === undefined? pos.left : this.x;
461 this.y = this.y === undefined? pos.top : this.y;
463 this.el.setLeftTop(this.x, this.y);
465 if(this.expandOnShow){
470 Ext.getBody().addClass('x-body-masked');
471 this.mask.setSize(Ext.lib.Dom.getViewWidth(true), Ext.lib.Dom.getViewHeight(true));
477 * Shows the window, rendering it first if necessary, or activates it and brings it to front if hidden.
478 * @param {String/Element} animateTarget (optional) The target element or id from which the window should
479 * animate while opening (defaults to null with no animation)
480 * @param {Function} callback (optional) A callback function to call after the window is displayed
481 * @param {Object} scope (optional) The scope in which to execute the callback
482 * @return {Ext.Window} this
484 show : function(animateTarget, cb, scope){
486 this.render(Ext.getBody());
488 if(this.hidden === false){
492 if(this.fireEvent('beforeshow', this) === false){
496 this.on('show', cb, scope, {single:true});
499 if(animateTarget !== undefined){
500 this.setAnimateTarget(animateTarget);
503 if(this.animateTarget){
512 afterShow : function(isAnim){
514 this.el.setStyle('display', 'block');
519 if(Ext.isMac && Ext.isGecko){ // work around stupid FF 2.0/Mac scroll bar bug
520 this.cascade(this.setAutoScroll);
523 if(this.monitorResize || this.modal || this.constrain || this.constrainHeader){
524 Ext.EventManager.onWindowResize(this.onWindowResize, this);
529 this.keyMap.enable();
532 this.updateHandles();
533 if(isAnim && (Ext.isIE || Ext.isWebKit)){
534 var sz = this.getSize();
535 this.onResize(sz.width, sz.height);
537 this.fireEvent('show', this);
541 animShow : function(){
543 this.proxy.setBox(this.animateTarget.getBox());
544 this.proxy.setOpacity(0);
545 var b = this.getBox(false);
546 b.callback = this.afterShow.createDelegate(this, [true], false);
549 b.easing = 'easeNone';
552 this.el.setStyle('display', 'none');
557 * Hides the window, setting it to invisible and applying negative offsets.
558 * @param {String/Element} animateTarget (optional) The target element or id to which the window should
559 * animate while hiding (defaults to null with no animation)
560 * @param {Function} callback (optional) A callback function to call after the window is hidden
561 * @param {Object} scope (optional) The scope in which to execute the callback
562 * @return {Ext.Window} this
564 hide : function(animateTarget, cb, scope){
565 if(this.hidden || this.fireEvent('beforehide', this) === false){
569 this.on('hide', cb, scope, {single:true});
572 if(animateTarget !== undefined){
573 this.setAnimateTarget(animateTarget);
577 Ext.getBody().removeClass('x-body-masked');
579 if(this.animateTarget){
589 afterHide : function(){
591 if(this.monitorResize || this.modal || this.constrain || this.constrainHeader){
592 Ext.EventManager.removeResizeListener(this.onWindowResize, this);
595 this.keyMap.disable();
597 this.fireEvent('hide', this);
601 animHide : function(){
602 this.proxy.setOpacity(0.5);
604 var tb = this.getBox(false);
605 this.proxy.setBox(tb);
607 var b = this.animateTarget.getBox();
608 b.callback = this.afterHide;
611 b.easing = 'easeNone';
618 onWindowResize : function(){
623 this.mask.setSize('100%', '100%');
624 var force = this.mask.dom.offsetHeight;
625 this.mask.setSize(Ext.lib.Dom.getViewWidth(true), Ext.lib.Dom.getViewHeight(true));
631 doConstrain : function(){
632 if(this.constrain || this.constrainHeader){
636 right:this.el.shadowOffset,
637 left:this.el.shadowOffset,
638 bottom:this.el.shadowOffset
641 var s = this.getSize();
643 right:-(s.width - 100),
644 bottom:-(s.height - 25)
648 var xy = this.el.getConstrainToXY(this.container, true, offsets);
650 this.setPosition(xy[0], xy[1]);
655 // private - used for dragging
656 ghost : function(cls){
657 var ghost = this.createGhost(cls);
658 var box = this.getBox(true);
659 ghost.setLeftTop(box.x, box.y);
660 ghost.setWidth(box.width);
662 this.activeGhost = ghost;
667 unghost : function(show, matchPosition){
668 if(!this.activeGhost) {
674 if(Ext.isMac && Ext.isGecko){ // work around stupid FF 2.0/Mac scroll bar bug
675 this.cascade(this.setAutoScroll);
678 if(matchPosition !== false){
679 this.setPosition(this.activeGhost.getLeft(true), this.activeGhost.getTop(true));
681 this.activeGhost.hide();
682 this.activeGhost.remove();
683 delete this.activeGhost;
687 * Placeholder method for minimizing the window. By default, this method simply fires the {@link #minimize} event
688 * since the behavior of minimizing a window is application-specific. To implement custom minimize behavior,
689 * either the minimize event can be handled or this method can be overridden.
690 * @return {Ext.Window} this
692 minimize : function(){
693 this.fireEvent('minimize', this);
698 * <p>Closes the Window, removes it from the DOM, {@link Ext.Component#destroy destroy}s
699 * the Window object and all its descendant Components. The {@link Ext.Panel#beforeclose beforeclose}
700 * event is fired before the close happens and will cancel the close action if it returns false.<p>
701 * <p><b>Note:</b> This method is not affected by the {@link #closeAction} setting which
702 * only affects the action triggered when clicking the {@link #closable 'close' tool in the header}.
703 * To hide the Window without destroying it, call {@link #hide}.</p>
706 if(this.fireEvent('beforeclose', this) !== false){
707 this.hide(null, function(){
708 this.fireEvent('close', this);
715 * Fits the window within its current container and automatically replaces
716 * the {@link #maximizable 'maximize' tool button} with the 'restore' tool button.
717 * Also see {@link #toggleMaximize}.
718 * @return {Ext.Window} this
720 maximize : function(){
723 this.restoreSize = this.getSize();
724 this.restorePos = this.getPosition(true);
725 if (this.maximizable){
726 this.tools.maximize.hide();
727 this.tools.restore.show();
729 this.maximized = true;
730 this.el.disableShadow();
735 if(this.collapsible){
736 this.tools.toggle.hide();
738 this.el.addClass('x-window-maximized');
739 this.container.addClass('x-window-maximized-ct');
741 this.setPosition(0, 0);
743 this.fireEvent('maximize', this);
749 * Restores a {@link #maximizable maximized} window back to its original
750 * size and position prior to being maximized and also replaces
751 * the 'restore' tool button with the 'maximize' tool button.
752 * Also see {@link #toggleMaximize}.
753 * @return {Ext.Window} this
755 restore : function(){
757 this.el.removeClass('x-window-maximized');
758 this.tools.restore.hide();
759 this.tools.maximize.show();
760 this.setPosition(this.restorePos[0], this.restorePos[1]);
761 this.setSize(this.restoreSize.width, this.restoreSize.height);
762 delete this.restorePos;
763 delete this.restoreSize;
764 this.maximized = false;
765 this.el.enableShadow(true);
770 if(this.collapsible){
771 this.tools.toggle.show();
773 this.container.removeClass('x-window-maximized-ct');
776 this.fireEvent('restore', this);
782 * A shortcut method for toggling between {@link #maximize} and {@link #restore} based on the current maximized
783 * state of the window.
784 * @return {Ext.Window} this
786 toggleMaximize : function(){
787 return this[this.maximized ? 'restore' : 'maximize']();
791 fitContainer : function(){
792 var vs = this.container.getViewSize();
793 this.setSize(vs.width, vs.height);
797 // z-index is managed by the WindowManager and may be overwritten at any time
798 setZIndex : function(index){
800 this.mask.setStyle('z-index', index);
802 this.el.setZIndex(++index);
806 this.resizer.proxy.setStyle('z-index', ++index);
809 this.lastZIndex = index;
813 * Aligns the window to the specified element
814 * @param {Mixed} element The element to align to.
815 * @param {String} position The position to align to (see {@link Ext.Element#alignTo} for more details).
816 * @param {Array} offsets (optional) Offset the positioning by [x, y]
817 * @return {Ext.Window} this
819 alignTo : function(element, position, offsets){
820 var xy = this.el.getAlignToXY(element, position, offsets);
821 this.setPagePosition(xy[0], xy[1]);
826 * Anchors this window to another element and realigns it when the window is resized or scrolled.
827 * @param {Mixed} element The element to align to.
828 * @param {String} position The position to align to (see {@link Ext.Element#alignTo} for more details)
829 * @param {Array} offsets (optional) Offset the positioning by [x, y]
830 * @param {Boolean/Number} monitorScroll (optional) true to monitor body scroll and reposition. If this parameter
831 * is a number, it is used as the buffer delay (defaults to 50ms).
832 * @return {Ext.Window} this
834 anchorTo : function(el, alignment, offsets, monitorScroll){
836 Ext.EventManager.removeResizeListener(this.doAnchor, this);
837 Ext.EventManager.un(window, 'scroll', this.doAnchor, this);
839 this.doAnchor = function(){
840 this.alignTo(el, alignment, offsets);
842 Ext.EventManager.onWindowResize(this.doAnchor, this);
844 var tm = typeof monitorScroll;
845 if(tm != 'undefined'){
846 Ext.EventManager.on(window, 'scroll', this.doAnchor, this,
847 {buffer: tm == 'number' ? monitorScroll : 50});
854 * Brings this window to the front of any other visible windows
855 * @param {Boolean} e (optional) Specify <tt>false</tt> to prevent the window from being focused.
856 * @return {Ext.Window} this
858 toFront : function(e){
859 if(this.manager.bringToFront(this)){
860 if(!e || !e.getTarget().focus){
868 * Makes this the active window by showing its shadow, or deactivates it by hiding its shadow. This method also
869 * fires the {@link #activate} or {@link #deactivate} event depending on which action occurred.
870 * @param {Boolean} active True to activate the window, false to deactivate it (defaults to false)
872 setActive : function(active){
875 this.el.enableShadow(true);
877 this.fireEvent('activate', this);
879 this.el.disableShadow();
880 this.fireEvent('deactivate', this);
885 * Sends this window to the back of (lower z-index than) any other visible windows
886 * @return {Ext.Window} this
889 this.manager.sendToBack(this);
894 * Centers this window in the viewport
895 * @return {Ext.Window} this
898 var xy = this.el.getAlignToXY(this.container, 'c-c');
899 this.setPagePosition(xy[0], xy[1]);
904 * @cfg {Boolean} autoWidth @hide
907 Ext.reg('window', Ext.Window);
909 // private - custom Window DD implementation
910 Ext.Window.DD = function(win){
912 Ext.Window.DD.superclass.constructor.call(this, win.el.id, 'WindowDD-'+win.id);
913 this.setHandleElId(win.header.id);
917 Ext.extend(Ext.Window.DD, Ext.dd.DD, {
919 headerOffsets:[100, 25],
920 startDrag : function(){
922 this.proxy = w.ghost();
923 if(w.constrain !== false){
924 var so = w.el.shadowOffset;
925 this.constrainTo(w.container, {right: so, left: so, bottom: so});
926 }else if(w.constrainHeader !== false){
927 var s = this.proxy.getSize();
928 this.constrainTo(w.container, {right: -(s.width-this.headerOffsets[0]), bottom: -(s.height-this.headerOffsets[1])});
931 b4Drag : Ext.emptyFn,
933 onDrag : function(e){
934 this.alignElWithMouse(this.proxy, e.getPageX(), e.getPageY());
937 endDrag : function(e){
939 this.win.saveState();