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