Upgrade to ExtJS 3.0.3 - Released 10/11/2009
[extjs.git] / src / widgets / WindowManager.js
1 /*!
2  * Ext JS Library 3.0.3
3  * Copyright(c) 2006-2009 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**
8  * @class Ext.WindowGroup
9  * An object that represents a group of {@link Ext.Window} instances and provides z-order management
10  * and window activation behavior.
11  * @constructor
12  */
13 Ext.WindowGroup = function(){
14     var list = {};
15     var accessList = [];
16     var front = null;
17
18     // private
19     var sortWindows = function(d1, d2){
20         return (!d1._lastAccess || d1._lastAccess < d2._lastAccess) ? -1 : 1;
21     };
22
23     // private
24     var orderWindows = function(){
25         var a = accessList, len = a.length;
26         if(len > 0){
27             a.sort(sortWindows);
28             var seed = a[0].manager.zseed;
29             for(var i = 0; i < len; i++){
30                 var win = a[i];
31                 if(win && !win.hidden){
32                     win.setZIndex(seed + (i*10));
33                 }
34             }
35         }
36         activateLast();
37     };
38
39     // private
40     var setActiveWin = function(win){
41         if(win != front){
42             if(front){
43                 front.setActive(false);
44             }
45             front = win;
46             if(win){
47                 win.setActive(true);
48             }
49         }
50     };
51
52     // private
53     var activateLast = function(){
54         for(var i = accessList.length-1; i >=0; --i) {
55             if(!accessList[i].hidden){
56                 setActiveWin(accessList[i]);
57                 return;
58             }
59         }
60         // none to activate
61         setActiveWin(null);
62     };
63
64     return {
65         /**
66          * The starting z-index for windows (defaults to 9000)
67          * @type Number The z-index value
68          */
69         zseed : 9000,
70
71         // private
72         register : function(win){
73             list[win.id] = win;
74             accessList.push(win);
75             win.on('hide', activateLast);
76         },
77
78         // private
79         unregister : function(win){
80             delete list[win.id];
81             win.un('hide', activateLast);
82             accessList.remove(win);
83         },
84
85         /**
86          * Gets a registered window by id.
87          * @param {String/Object} id The id of the window or a {@link Ext.Window} instance
88          * @return {Ext.Window}
89          */
90         get : function(id){
91             return typeof id == "object" ? id : list[id];
92         },
93
94         /**
95          * Brings the specified window to the front of any other active windows.
96          * @param {String/Object} win The id of the window or a {@link Ext.Window} instance
97          * @return {Boolean} True if the dialog was brought to the front, else false
98          * if it was already in front
99          */
100         bringToFront : function(win){
101             win = this.get(win);
102             if(win != front){
103                 win._lastAccess = new Date().getTime();
104                 orderWindows();
105                 return true;
106             }
107             return false;
108         },
109
110         /**
111          * Sends the specified window to the back of other active windows.
112          * @param {String/Object} win The id of the window or a {@link Ext.Window} instance
113          * @return {Ext.Window} The window
114          */
115         sendToBack : function(win){
116             win = this.get(win);
117             win._lastAccess = -(new Date().getTime());
118             orderWindows();
119             return win;
120         },
121
122         /**
123          * Hides all windows in the group.
124          */
125         hideAll : function(){
126             for(var id in list){
127                 if(list[id] && typeof list[id] != "function" && list[id].isVisible()){
128                     list[id].hide();
129                 }
130             }
131         },
132
133         /**
134          * Gets the currently-active window in the group.
135          * @return {Ext.Window} The active window
136          */
137         getActive : function(){
138             return front;
139         },
140
141         /**
142          * Returns zero or more windows in the group using the custom search function passed to this method.
143          * The function should accept a single {@link Ext.Window} reference as its only argument and should
144          * return true if the window matches the search criteria, otherwise it should return false.
145          * @param {Function} fn The search function
146          * @param {Object} scope (optional) The scope in which to execute the function (defaults to the window
147          * that gets passed to the function if not specified)
148          * @return {Array} An array of zero or more matching windows
149          */
150         getBy : function(fn, scope){
151             var r = [];
152             for(var i = accessList.length-1; i >=0; --i) {
153                 var win = accessList[i];
154                 if(fn.call(scope||win, win) !== false){
155                     r.push(win);
156                 }
157             }
158             return r;
159         },
160
161         /**
162          * Executes the specified function once for every window in the group, passing each
163          * window as the only parameter. Returning false from the function will stop the iteration.
164          * @param {Function} fn The function to execute for each item
165          * @param {Object} scope (optional) The scope in which to execute the function
166          */
167         each : function(fn, scope){
168             for(var id in list){
169                 if(list[id] && typeof list[id] != "function"){
170                     if(fn.call(scope || list[id], list[id]) === false){
171                         return;
172                     }
173                 }
174             }
175         }
176     };
177 };
178
179
180 /**
181  * @class Ext.WindowMgr
182  * @extends Ext.WindowGroup
183  * The default global window group that is available automatically.  To have more than one group of windows
184  * with separate z-order stacks, create additional instances of {@link Ext.WindowGroup} as needed.
185  * @singleton
186  */
187 Ext.WindowMgr = new Ext.WindowGroup();