/javascripts/lib/src/widgets/WindowManager.js

https://bitbucket.org/ksokmesa/sina-asian · JavaScript · 209 lines · 108 code · 17 blank · 84 comment · 28 complexity · eaac50d5b9fd62399af826641bcfc75c MD5 · raw file

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