Desktop.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*!
  2. * Ext JS Library 3.3.1
  3. * Copyright(c) 2006-2010 Sencha Inc.
  4. * licensing@sencha.com
  5. * http://www.sencha.com/license
  6. */
  7. Ext.Desktop = function(app) {
  8. this.taskbar = new Ext.ux.TaskBar(app);
  9. this.xTickSize = this.yTickSize = 1;
  10. var taskbar = this.taskbar;
  11. var desktopEl = Ext.get('x-desktop');
  12. var taskbarEl = Ext.get('ux-taskbar');
  13. var shortcuts = Ext.get('x-shortcuts');
  14. var windows = new Ext.WindowGroup();
  15. var activeWindow;
  16. function minimizeWin(win) {
  17. win.minimized = true;
  18. win.hide();
  19. }
  20. function markActive(win) {
  21. if (activeWindow && activeWindow != win) {
  22. markInactive(activeWindow);
  23. }
  24. taskbar.setActiveButton(win.taskButton);
  25. activeWindow = win;
  26. Ext.fly(win.taskButton.el).addClass('active-win');
  27. win.minimized = false;
  28. }
  29. function markInactive(win) {
  30. if (win == activeWindow) {
  31. activeWindow = null;
  32. Ext.fly(win.taskButton.el).removeClass('active-win');
  33. }
  34. }
  35. function removeWin(win) {
  36. taskbar.removeTaskButton(win.taskButton);
  37. layout();
  38. }
  39. function layout() {
  40. desktopEl.setHeight(Ext.lib.Dom.getViewHeight() - taskbarEl.getHeight());
  41. }
  42. Ext.EventManager.onWindowResize(layout);
  43. this.layout = layout;
  44. this.createWindow = function(config, cls) {
  45. var win = new(cls || Ext.Window)(
  46. Ext.applyIf(config || {},
  47. {
  48. renderTo: desktopEl,
  49. manager: windows,
  50. minimizable: true,
  51. maximizable: true
  52. })
  53. );
  54. win.dd.xTickSize = this.xTickSize;
  55. win.dd.yTickSize = this.yTickSize;
  56. if (win.resizer) {
  57. win.resizer.widthIncrement = this.xTickSize;
  58. win.resizer.heightIncrement = this.yTickSize;
  59. }
  60. win.render(desktopEl);
  61. win.taskButton = taskbar.addTaskButton(win);
  62. win.cmenu = new Ext.menu.Menu({
  63. items: [
  64. ]
  65. });
  66. win.animateTarget = win.taskButton.el;
  67. win.on({
  68. 'activate': {
  69. fn: markActive
  70. },
  71. 'beforeshow': {
  72. fn: markActive
  73. },
  74. 'deactivate': {
  75. fn: markInactive
  76. },
  77. 'minimize': {
  78. fn: minimizeWin
  79. },
  80. 'close': {
  81. fn: removeWin
  82. }
  83. });
  84. layout();
  85. return win;
  86. };
  87. this.getManager = function() {
  88. return windows;
  89. };
  90. this.getWindow = function(id) {
  91. return windows.get(id);
  92. };
  93. this.getWinWidth = function() {
  94. var width = Ext.lib.Dom.getViewWidth();
  95. return width < 200 ? 200: width;
  96. };
  97. this.getWinHeight = function() {
  98. var height = (Ext.lib.Dom.getViewHeight() - taskbarEl.getHeight());
  99. return height < 100 ? 100: height;
  100. };
  101. this.getWinX = function(width) {
  102. return (Ext.lib.Dom.getViewWidth() - width) / 2;
  103. };
  104. this.getWinY = function(height) {
  105. return (Ext.lib.Dom.getViewHeight() - taskbarEl.getHeight() - height) / 2;
  106. };
  107. this.setTickSize = function(xTickSize, yTickSize) {
  108. this.xTickSize = xTickSize;
  109. if (arguments.length == 1) {
  110. this.yTickSize = xTickSize;
  111. } else {
  112. this.yTickSize = yTickSize;
  113. }
  114. windows.each(function(win) {
  115. win.dd.xTickSize = this.xTickSize;
  116. win.dd.yTickSize = this.yTickSize;
  117. win.resizer.widthIncrement = this.xTickSize;
  118. win.resizer.heightIncrement = this.yTickSize;
  119. },
  120. this);
  121. };
  122. this.cascade = function() {
  123. var x = 0,
  124. y = 0;
  125. windows.each(function(win) {
  126. if (win.isVisible() && !win.maximized) {
  127. win.setPosition(x, y);
  128. x += 20;
  129. y += 20;
  130. }
  131. },
  132. this);
  133. };
  134. this.tile = function() {
  135. var availWidth = desktopEl.getWidth(true);
  136. var x = this.xTickSize;
  137. var y = this.yTickSize;
  138. var nextY = y;
  139. windows.each(function(win) {
  140. if (win.isVisible() && !win.maximized) {
  141. var w = win.el.getWidth();
  142. // Wrap to next row if we are not at the line start and this Window will go off the end
  143. if ((x > this.xTickSize) && (x + w > availWidth)) {
  144. x = this.xTickSize;
  145. y = nextY;
  146. }
  147. win.setPosition(x, y);
  148. x += w + this.xTickSize;
  149. nextY = Math.max(nextY, y + win.el.getHeight() + this.yTickSize);
  150. }
  151. },
  152. this);
  153. };
  154. this.contextMenu = new Ext.menu.Menu({
  155. items: [{
  156. text: 'Tile',
  157. handler: this.tile,
  158. scope: this
  159. },
  160. {
  161. text: 'Cascade',
  162. handler: this.cascade,
  163. scope: this
  164. }]
  165. });
  166. desktopEl.on('contextmenu',
  167. function(e) {
  168. e.stopEvent();
  169. this.contextMenu.showAt(e.getXY());
  170. },
  171. this);
  172. layout();
  173. if (shortcuts) {
  174. shortcuts.on('click',
  175. function(e, t) {
  176. t = e.getTarget('dt', shortcuts);
  177. if (t) {
  178. e.stopEvent();
  179. var module = app.getModule(t.id.replace('-shortcut', ''));
  180. if (module) {
  181. module.createWindow();
  182. }
  183. }
  184. });
  185. }
  186. };