StartMenu.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. /**
  8. * @class Ext.ux.StartMenu
  9. * @extends Ext.menu.Menu
  10. * A start menu object.
  11. * @constructor
  12. * Creates a new StartMenu
  13. * @param {Object} config Configuration options
  14. *
  15. * SAMPLE USAGE:
  16. *
  17. * this.startMenu = new Ext.ux.StartMenu({
  18. * iconCls: 'user',
  19. * height: 300,
  20. * shadow: true,
  21. * title: get_cookie('memberName'),
  22. * width: 300
  23. * });
  24. *
  25. * this.startMenu.add({
  26. * text: 'Grid Window',
  27. * iconCls:'icon-grid',
  28. * handler : this.createWindow,
  29. * scope: this
  30. * });
  31. *
  32. * this.startMenu.addTool({
  33. * text:'Logout',
  34. * iconCls:'logout',
  35. * handler:function(){ window.location = "logout.php"; },
  36. * scope:this
  37. * });
  38. */
  39. Ext.namespace("Ext.ux");
  40. Ext.ux.StartMenu = Ext.extend(Ext.menu.Menu, {
  41. initComponent: function(config) {
  42. Ext.ux.StartMenu.superclass.initComponent.call(this, config);
  43. var tools = this.toolItems;
  44. this.toolItems = new Ext.util.MixedCollection();
  45. if(tools){
  46. this.addTool.apply(this, tools);
  47. }
  48. },
  49. // private
  50. onRender : function(ct, position){
  51. Ext.ux.StartMenu.superclass.onRender.call(this, ct, position);
  52. var el = this.el.addClass('ux-start-menu');
  53. var header = el.createChild({
  54. tag: "div",
  55. cls: "x-window-header x-unselectable x-panel-icon "+this.iconCls
  56. });
  57. this.header = header;
  58. var headerText = header.createChild({
  59. tag: "span",
  60. cls: "x-window-header-text"
  61. });
  62. var tl = header.wrap({
  63. cls: "ux-start-menu-tl"
  64. });
  65. var tr = header.wrap({
  66. cls: "ux-start-menu-tr"
  67. });
  68. var tc = header.wrap({
  69. cls: "ux-start-menu-tc"
  70. });
  71. this.menuBWrap = el.createChild({
  72. tag: "div",
  73. cls: "x-window-body x-border-layout-ct ux-start-menu-body"
  74. });
  75. var ml = this.menuBWrap.wrap({
  76. cls: "ux-start-menu-ml"
  77. });
  78. var mc = this.menuBWrap.wrap({
  79. cls: "x-window-mc ux-start-menu-bwrap"
  80. });
  81. this.menuPanel = this.menuBWrap.createChild({
  82. tag: "div",
  83. cls: "x-panel x-border-panel ux-start-menu-apps-panel"
  84. });
  85. this.toolsPanel = this.menuBWrap.createChild({
  86. tag: "div",
  87. cls: "x-panel x-border-panel ux-start-menu-tools-panel"
  88. });
  89. var bwrap = ml.wrap({cls: "x-window-bwrap"});
  90. var bc = bwrap.createChild({
  91. tag: "div",
  92. cls: "ux-start-menu-bc"
  93. });
  94. var bl = bc.wrap({
  95. cls: "ux-start-menu-bl x-panel-nofooter"
  96. });
  97. var br = bc.wrap({
  98. cls: "ux-start-menu-br"
  99. });
  100. this.ul.appendTo(this.menuPanel);
  101. var toolsUl = this.toolsPanel.createChild({
  102. tag: "ul",
  103. cls: "x-menu-list"
  104. });
  105. this.mon(toolsUl, 'click', this.onClick, this);
  106. this.mon(toolsUl, 'mouseover', this.onMouseOver, this);
  107. this.mon(toolsUl, 'mouseout', this.onMouseOut, this);
  108. this.items.each(function(item){
  109. item.parentMenu = this;
  110. }, this);
  111. this.toolItems.each(
  112. function(item){
  113. var li = document.createElement("li");
  114. li.className = "x-menu-list-item";
  115. toolsUl.dom.appendChild(li);
  116. item.render(li);
  117. item.parentMenu = this;
  118. }, this);
  119. this.toolsUl = toolsUl;
  120. this.menuBWrap.setStyle('position', 'relative');
  121. this.menuBWrap.setHeight(this.height - 28);
  122. this.menuPanel.setStyle({
  123. padding: '2px',
  124. position: 'absolute',
  125. overflow: 'auto'
  126. });
  127. this.toolsPanel.setStyle({
  128. padding: '2px 4px 2px 2px',
  129. position: 'absolute',
  130. overflow: 'auto'
  131. });
  132. this.setTitle(this.title);
  133. },
  134. // private
  135. findTargetItem : function(e){
  136. var t = e.getTarget(".x-menu-list-item", this.ul, true);
  137. if(t && t.menuItemId){
  138. if(this.items.get(t.menuItemId)){
  139. return this.items.get(t.menuItemId);
  140. }else{
  141. return this.toolItems.get(t.menuItemId);
  142. }
  143. }
  144. },
  145. /**
  146. * Displays this menu relative to another element
  147. * @param {Mixed} element The element to align to
  148. * @param {String} position (optional) The {@link Ext.Element#alignTo} anchor position to use in aligning to
  149. * the element (defaults to this.defaultAlign)
  150. * @param {Ext.ux.StartMenu} parentMenu (optional) This menu's parent menu, if applicable (defaults to undefined)
  151. */
  152. show : function(el, pos, parentMenu){
  153. this.parentMenu = parentMenu;
  154. if(!this.el){
  155. this.render();
  156. }
  157. this.fireEvent("beforeshow", this);
  158. this.showAt(this.el.getAlignToXY(el, pos || this.defaultAlign), parentMenu, false);
  159. var tPanelWidth = 100;
  160. var box = this.menuBWrap.getBox();
  161. this.menuPanel.setWidth(box.width-tPanelWidth);
  162. this.menuPanel.setHeight(box.height);
  163. this.toolsPanel.setWidth(tPanelWidth);
  164. this.toolsPanel.setX(box.x+box.width-tPanelWidth);
  165. this.toolsPanel.setHeight(box.height);
  166. },
  167. addTool : function(){
  168. var a = arguments, l = a.length, item;
  169. for(var i = 0; i < l; i++){
  170. var el = a[i];
  171. if(el.render){ // some kind of Item
  172. item = this.addToolItem(el);
  173. }else if(typeof el == "string"){ // string
  174. if(el == "separator" || el == "-"){
  175. item = this.addToolSeparator();
  176. }else{
  177. item = this.addText(el);
  178. }
  179. }else if(el.tagName || el.el){ // element
  180. item = this.addElement(el);
  181. }else if(typeof el == "object"){ // must be menu item config?
  182. item = this.addToolMenuItem(el);
  183. }
  184. }
  185. return item;
  186. },
  187. /**
  188. * Adds a separator bar to the Tools
  189. * @return {Ext.menu.Item} The menu item that was added
  190. */
  191. addToolSeparator : function(){
  192. return this.addToolItem(new Ext.menu.Separator({itemCls: 'ux-toolmenu-sep'}));
  193. },
  194. addToolItem : function(item){
  195. this.toolItems.add(item);
  196. if(this.ul){
  197. var li = document.createElement("li");
  198. li.className = "x-menu-list-item";
  199. this.ul.dom.appendChild(li);
  200. item.render(li, this);
  201. this.delayAutoWidth();
  202. }
  203. return item;
  204. },
  205. addToolMenuItem : function(config){
  206. if(!(config instanceof Ext.menu.Item)){
  207. if(typeof config.checked == "boolean"){ // must be check menu item config?
  208. config = new Ext.menu.CheckItem(config);
  209. }else{
  210. config = new Ext.menu.Item(config);
  211. }
  212. }
  213. return this.addToolItem(config);
  214. },
  215. setTitle : function(title, iconCls){
  216. this.title = title;
  217. this.header.child('span').update(title);
  218. return this;
  219. }
  220. });