Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / examples / sandbox / js / Desktop.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /*!
16  * Ext JS Library 3.3.1
17  * Copyright(c) 2006-2010 Sencha Inc.
18  * licensing@sencha.com
19  * http://www.sencha.com/license
20  */
21 Ext.Desktop = function(app) {
22     this.taskbar = new Ext.ux.TaskBar(app);
23     this.xTickSize = this.yTickSize = 1;
24     var taskbar = this.taskbar;
25
26     var desktopEl = Ext.get('x-desktop');
27     var taskbarEl = Ext.get('ux-taskbar');
28     var shortcuts = Ext.get('x-shortcuts');
29
30     var windows = new Ext.WindowGroup();
31     var activeWindow;
32
33     function minimizeWin(win) {
34         win.minimized = true;
35         win.hide();
36     }
37
38     function markActive(win) {
39         if (activeWindow && activeWindow != win) {
40             markInactive(activeWindow);
41         }
42         taskbar.setActiveButton(win.taskButton);
43         activeWindow = win;
44         Ext.fly(win.taskButton.el).addClass('active-win');
45         win.minimized = false;
46     }
47
48     function markInactive(win) {
49         if (win == activeWindow) {
50             activeWindow = null;
51             Ext.fly(win.taskButton.el).removeClass('active-win');
52         }
53     }
54
55     function removeWin(win) {
56         taskbar.removeTaskButton(win.taskButton);
57         layout();
58     }
59
60     function layout() {
61         desktopEl.setHeight(Ext.lib.Dom.getViewHeight() - taskbarEl.getHeight());
62     }
63     Ext.EventManager.onWindowResize(layout);
64
65     this.layout = layout;
66
67     this.createWindow = function(config, cls) {
68         var win = new(cls || Ext.Window)(
69         Ext.applyIf(config || {},
70         {
71             renderTo: desktopEl,
72             manager: windows,
73             minimizable: true,
74             maximizable: true
75         })
76         );
77         win.dd.xTickSize = this.xTickSize;
78         win.dd.yTickSize = this.yTickSize;
79         if (win.resizer) {
80             win.resizer.widthIncrement = this.xTickSize;
81             win.resizer.heightIncrement = this.yTickSize;
82         }
83         win.render(desktopEl);
84         win.taskButton = taskbar.addTaskButton(win);
85
86         win.cmenu = new Ext.menu.Menu({
87             items: [
88
89             ]
90         });
91
92         win.animateTarget = win.taskButton.el;
93
94         win.on({
95             'activate': {
96                 fn: markActive
97             },
98             'beforeshow': {
99                 fn: markActive
100             },
101             'deactivate': {
102                 fn: markInactive
103             },
104             'minimize': {
105                 fn: minimizeWin
106             },
107             'close': {
108                 fn: removeWin
109             }
110         });
111
112         layout();
113         return win;
114     };
115
116     this.getManager = function() {
117         return windows;
118     };
119
120     this.getWindow = function(id) {
121         return windows.get(id);
122     };
123
124     this.getWinWidth = function() {
125         var width = Ext.lib.Dom.getViewWidth();
126         return width < 200 ? 200: width;
127     };
128
129     this.getWinHeight = function() {
130         var height = (Ext.lib.Dom.getViewHeight() - taskbarEl.getHeight());
131         return height < 100 ? 100: height;
132     };
133
134     this.getWinX = function(width) {
135         return (Ext.lib.Dom.getViewWidth() - width) / 2;
136     };
137
138     this.getWinY = function(height) {
139         return (Ext.lib.Dom.getViewHeight() - taskbarEl.getHeight() - height) / 2;
140     };
141
142     this.setTickSize = function(xTickSize, yTickSize) {
143         this.xTickSize = xTickSize;
144         if (arguments.length == 1) {
145             this.yTickSize = xTickSize;
146         } else {
147             this.yTickSize = yTickSize;
148         }
149         windows.each(function(win) {
150             win.dd.xTickSize = this.xTickSize;
151             win.dd.yTickSize = this.yTickSize;
152             win.resizer.widthIncrement = this.xTickSize;
153             win.resizer.heightIncrement = this.yTickSize;
154         },
155         this);
156     };
157
158     this.cascade = function() {
159         var x = 0,
160         y = 0;
161         windows.each(function(win) {
162             if (win.isVisible() && !win.maximized) {
163                 win.setPosition(x, y);
164                 x += 20;
165                 y += 20;
166             }
167         },
168         this);
169     };
170
171     this.tile = function() {
172         var availWidth = desktopEl.getWidth(true);
173         var x = this.xTickSize;
174         var y = this.yTickSize;
175         var nextY = y;
176         windows.each(function(win) {
177             if (win.isVisible() && !win.maximized) {
178                 var w = win.el.getWidth();
179
180                 //              Wrap to next row if we are not at the line start and this Window will go off the end
181                 if ((x > this.xTickSize) && (x + w > availWidth)) {
182                     x = this.xTickSize;
183                     y = nextY;
184                 }
185
186                 win.setPosition(x, y);
187                 x += w + this.xTickSize;
188                 nextY = Math.max(nextY, y + win.el.getHeight() + this.yTickSize);
189             }
190         },
191         this);
192     };
193
194     this.contextMenu = new Ext.menu.Menu({
195         items: [{
196             text: 'Tile',
197             handler: this.tile,
198             scope: this
199         },
200         {
201             text: 'Cascade',
202             handler: this.cascade,
203             scope: this
204         }]
205     });
206     desktopEl.on('contextmenu',
207         function(e) {
208             e.stopEvent();
209             this.contextMenu.showAt(e.getXY());
210         },
211         this);
212
213     layout();
214
215     if (shortcuts) {
216         shortcuts.on('click',
217         function(e, t) {
218             t = e.getTarget('dt', shortcuts);
219             if (t) {
220                 e.stopEvent();
221                 var module = app.getModule(t.id.replace('-shortcut', ''));
222                 if (module) {
223                     module.createWindow();
224                 }
225             }
226         });
227     }
228 };
229