Desktop.js 12 KB

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