Desktop.js
13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*!
* Ext JS Library 4.0
* Copyright(c) 2006-2011 Sencha Inc.
* licensing@sencha.com
* http://www.sencha.com/license
*/
/**
* @class Ext.ux.desktop.Desktop
* @extends Ext.panel.Panel
* <p>This class manages the wallpaper, shortcuts and taskbar.</p>
*/
Ext.define('Ext.ux.desktop.Desktop', {
extend: 'Ext.panel.Panel',
alias: 'widget.desktop',
uses: [
'Ext.util.MixedCollection',
'Ext.menu.Menu',
'Ext.view.View', // dataview
'Ext.window.Window',
'Ext.ux.desktop.TaskBar',
'Ext.ux.desktop.Wallpaper'
],
activeWindowCls: 'ux-desktop-active-win',
inactiveWindowCls: 'ux-desktop-inactive-win',
lastActiveWindow: null,
border: false,
html: ' ',
layout: 'fit',
xTickSize: 1,
yTickSize: 1,
app: null,
/**
* @cfg {Array|Store} shortcuts
* The items to add to the DataView. This can be a {@link Ext.data.Store Store} or a
* simple array. Items should minimally provide the fields in the
* {@link Ext.ux.desktop.ShorcutModel ShortcutModel}.
*/
shortcuts: null,
/**
* @cfg {String} shortcutItemSelector
* This property is passed to the DataView for the desktop to select shortcut items.
* If the {@link #shortcutTpl} is modified, this will probably need to be modified as
* well.
*/
shortcutItemSelector: 'td.ux-desktop-shortcut-item',
/**
* @cfg {String} shortcutTpl
* This XTemplate is used to render items in the DataView. If this is changed, the
* {@link shortcutItemSelect} will probably also need to changed.
*/
/* shortcutTpl: [
'<tpl for=".">',
'<div class="ux-desktop-shortcut" id="{name}-shortcut">',
'<div class="ux-desktop-shortcut-icon {iconCls}">',
'<img src="',Ext.BLANK_IMAGE_URL,'" title="{name}">',
'</div>',
'<span class="ux-desktop-shortcut-text">{name}</span>',
'</div>',
'</tpl>',
'<div class="x-clear"></div>'
],
*/
/**
* @cfg {Object} taskbarConfig
* The config object for the TaskBar.
*/
taskbarConfig: null,
windowMenu: null,
initComponent: function () {
var me = this;
me.windowMenu = new Ext.menu.Menu(me.createWindowMenu());
me.bbar = me.taskbar = new Ext.ux.desktop.TaskBar(me.taskbarConfig);
me.taskbar.windowMenu = me.windowMenu;
me.windows = new Ext.util.MixedCollection();
me.contextMenu = new Ext.menu.Menu(me.createDesktopMenu());
me.items = [
{ xtype: 'wallpaper', id: me.id+'_wallpaper' },
me.createDataView()
];
me.callParent();
me.shortcutsView = me.items.getAt(1);
me.shortcutsView.on('itemclick', me.onShortcutItemClick, me);
var wallpaper = me.wallpaper;
me.wallpaper = me.items.getAt(0);
if (wallpaper) {
me.setWallpaper(wallpaper, me.wallpaperStretch);
}
},
afterRender: function () {
var me = this;
me.callParent();
me.el.on('contextmenu', me.onDesktopMenu, me);
},
//------------------------------------------------------
// Overrideable configuration creation methods
/*
createDataView: function () {
var me = this;
return {
xtype: 'dataview',
overItemCls: 'x-view-over',
trackOver: true,
itemSelector: me.shortcutItemSelector,
store: me.shortcuts,
style: {
position: 'absolute'
},
x: 0, y: 0,
tpl: new Ext.XTemplate(me.shortcutTpl)
};
},
*/
createDataView: function () {
var me = this;
tpl = new Ext.XTemplate (
'<table class="ux-desktop-shortcut">',
'<tr width="100%">',
'<td width="100%"/>',
'<tpl for=".">',
'<td class="ux-desktop-shortcut-item" " id="{name}-shortcut" >',
'<span class="ux-desktop-shortcut-text">{name}</span>',
'<br/>',
'<img class="ux-desktop-shortcut-icon" src="js/resources/images/64x64/{iconCls}.png" />',
'</td>',
'</tpl>',
'</tr>',
'</table>'
);
return {
xtype: 'dataview',
overItemCls: 'x-view-over',
trackOver: true,
itemSelector: me.shortcutItemSelector ,
store: me.shortcuts,
style: {
position: 'absolute'
},
tpl: tpl
};
},
createDesktopMenu: function () {
var me = this, ret = {
items: me.contextMenuItems || []
};
if (ret.items.length) {
ret.items.push('-');
}
ret.items.push(
{ text: 'Tile', handler: me.tileWindows, scope: me, minWindows: 1 },
{ text: 'Cascade', handler: me.cascadeWindows, scope: me, minWindows: 1 })
return ret;
},
createWindowMenu: function () {
var me = this;
return {
defaultAlign: 'br-tr',
items: [
{ text: 'Restore', handler: me.onWindowMenuRestore, scope: me },
{ text: 'Minimize', handler: me.onWindowMenuMinimize, scope: me },
{ text: 'Maximize', handler: me.onWindowMenuMaximize, scope: me },
'-',
{ text: 'Close', handler: me.onWindowMenuClose, scope: me }
],
listeners: {
beforeshow: me.onWindowMenuBeforeShow,
hide: me.onWindowMenuHide,
scope: me
}
};
},
//------------------------------------------------------
// Event handler methods
onDesktopMenu: function (e) {
var me = this, menu = me.contextMenu;
e.stopEvent();
if (!menu.rendered) {
menu.on('beforeshow', me.onDesktopMenuBeforeShow, me);
}
menu.showAt(e.getXY());
menu.doConstrain();
},
onDesktopMenuBeforeShow: function (menu) {
var me = this, count = me.windows.getCount();
menu.items.each(function (item) {
var min = item.minWindows || 0;
item.setDisabled(count < min);
});
},
onShortcutItemClick: function (dataView, record) {
var me = this, module = me.app.getModule(record.data.module),
win = module && module.createWindow();
if (win) {
me.restoreWindow(win);
}
},
onWindowClose: function(win) {
var me = this;
me.windows.remove(win);
me.taskbar.removeTaskButton(win.taskButton);
me.updateActiveWindow();
},
//------------------------------------------------------
// Window context menu handlers
onWindowMenuBeforeShow: function (menu) {
var items = menu.items.items, win = menu.theWin;
items[0].setDisabled(win.maximized !== true && win.hidden !== true); // Restore
items[1].setDisabled(win.minimized === true); // Minimize
items[2].setDisabled(win.maximized === true || win.hidden === true); // Maximize
items[4].setDisabled(win.closable !== true); // Close //BRE
},
onWindowMenuClose: function () {
var me = this, win = me.windowMenu.theWin;
win.close();
},
onWindowMenuHide: function (menu) {
menu.theWin = null;
},
onWindowMenuMaximize: function () {
var me = this, win = me.windowMenu.theWin;
win.maximize();
win.toFront();
},
onWindowMenuMinimize: function () {
var me = this, win = me.windowMenu.theWin;
win.minimize();
},
onWindowMenuRestore: function () {
var me = this, win = me.windowMenu.theWin;
me.restoreWindow(win);
},
//------------------------------------------------------
// Dynamic (re)configuration methods
getWallpaper: function () {
return this.wallpaper.wallpaper;
},
setTickSize: function(xTickSize, yTickSize) {
var me = this,
xt = me.xTickSize = xTickSize,
yt = me.yTickSize = (arguments.length > 1) ? yTickSize : xt;
me.windows.each(function(win) {
var dd = win.dd, resizer = win.resizer;
dd.xTickSize = xt;
dd.yTickSize = yt;
resizer.widthIncrement = xt;
resizer.heightIncrement = yt;
});
},
setWallpaper: function (wallpaper, stretch) {
this.wallpaper.setWallpaper(wallpaper, stretch);
return this;
},
//------------------------------------------------------
// Window management methods
cascadeWindows: function() {
var x = 0, y = 0,
zmgr = this.getDesktopZIndexManager();
zmgr.eachBottomUp(function(win) {
if (win.isWindow && win.isVisible() && !win.maximized) {
win.setPosition(x, y);
x += 20;
y += 20;
}
});
},
createWindow: function(config, cls) {
var me = this, win, cfg = Ext.applyIf(config || {}, {
stateful: false,
isWindow: true,
constrainHeader: true,
minimizable: true,
maximizable: true
});
cls = cls || Ext.window.Window;
win = me.add(new cls(cfg));
me.windows.add(win);
win.taskButton = me.taskbar.addTaskButton(win);
win.animateTarget = win.taskButton.el;
win.on({
activate: me.updateActiveWindow,
beforeshow: me.updateActiveWindow,
deactivate: me.updateActiveWindow,
minimize: me.minimizeWindow,
destroy: me.onWindowClose,
scope: me
});
win.on({
boxready: function () {
win.dd.xTickSize = me.xTickSize;
win.dd.yTickSize = me.yTickSize;
if (win.resizer) {
win.resizer.widthIncrement = me.xTickSize;
win.resizer.heightIncrement = me.yTickSize;
}
},
single: true
});
// replace normal window close w/fadeOut animation:
win.doClose = function () {
win.doClose = Ext.emptyFn; // dblclick can call again...
win.el.disableShadow();
win.el.fadeOut({
listeners: {
afteranimate: function () {
win.destroy();
}
}
});
};
return win;
},
getActiveWindow: function () {
var win = null,
zmgr = this.getDesktopZIndexManager();
if (zmgr) {
// We cannot rely on activate/deactive because that fires against non-Window
// components in the stack.
zmgr.eachTopDown(function (comp) {
if (comp.isWindow && !comp.hidden) {
win = comp;
return false;
}
return true;
});
}
return win;
},
getDesktopZIndexManager: function () {
var windows = this.windows;
// TODO - there has to be a better way to get this...
return (windows.getCount() && windows.getAt(0).zIndexManager) || null;
},
getWindow: function(id) {
return this.windows.get(id);
},
minimizeWindow: function(win) {
win.minimized = true;
win.hide();
},
restoreWindow: function (win) {
if (win.isVisible()) {
win.restore();
win.toFront();
} else {
win.show();
}
return win;
},
tileWindows: function() {
var me = this, availWidth = me.body.getWidth(true);
var x = me.xTickSize, y = me.yTickSize, nextY = y;
me.windows.each(function(win) {
if (win.isVisible() && !win.maximized) {
var w = win.el.getWidth();
// Wrap to next row if we are not at the line start and this Window will
// go off the end
if (x > me.xTickSize && x + w > availWidth) {
x = me.xTickSize;
y = nextY;
}
win.setPosition(x, y);
x += w + me.xTickSize;
nextY = Math.max(nextY, y + win.el.getHeight() + me.yTickSize);
}
});
},
updateActiveWindow: function () {
var me = this, activeWindow = me.getActiveWindow(), last = me.lastActiveWindow;
if (activeWindow === last) {
return;
}
if (last) {
if (last.el.dom) {
last.addCls(me.inactiveWindowCls);
last.removeCls(me.activeWindowCls);
}
last.active = false;
}
me.lastActiveWindow = activeWindow;
if (activeWindow) {
activeWindow.addCls(me.activeWindowCls);
activeWindow.removeCls(me.inactiveWindowCls);
activeWindow.minimized = false;
activeWindow.active = true;
}
me.taskbar.setActiveButton(activeWindow && activeWindow.taskButton);
}
});