/hippo/src/main/webapp/ext/src/widgets/menu/Item.js

http://hdbc.googlecode.com/ · JavaScript · 211 lines · 125 code · 14 blank · 72 comment · 26 complexity · d531d1c5c9aec2d004810009e435d84e MD5 · raw file

  1. /*!
  2. * Ext JS Library 3.0.0
  3. * Copyright(c) 2006-2009 Ext JS, LLC
  4. * licensing@extjs.com
  5. * http://www.extjs.com/license
  6. */
  7. /**
  8. * @class Ext.menu.Item
  9. * @extends Ext.menu.BaseItem
  10. * A base class for all menu items that require menu-related functionality (like sub-menus) and are not static
  11. * display items. Item extends the base functionality of {@link Ext.menu.BaseItem} by adding menu-specific
  12. * activation and click handling.
  13. * @constructor
  14. * Creates a new Item
  15. * @param {Object} config Configuration options
  16. * @xtype menuitem
  17. */
  18. Ext.menu.Item = function(config){
  19. Ext.menu.Item.superclass.constructor.call(this, config);
  20. if(this.menu){
  21. this.menu = Ext.menu.MenuMgr.get(this.menu);
  22. }
  23. };
  24. Ext.extend(Ext.menu.Item, Ext.menu.BaseItem, {
  25. /**
  26. * @property menu
  27. * @type Ext.menu.Menu
  28. * The submenu associated with this Item if one was configured.
  29. */
  30. /**
  31. * @cfg {Mixed} menu (optional) Either an instance of {@link Ext.menu.Menu} or the config object for an
  32. * {@link Ext.menu.Menu} which acts as the submenu when this item is activated.
  33. */
  34. /**
  35. * @cfg {String} icon The path to an icon to display in this item (defaults to Ext.BLANK_IMAGE_URL). If
  36. * icon is specified {@link #iconCls} should not be.
  37. */
  38. /**
  39. * @cfg {String} iconCls A CSS class that specifies a background image that will be used as the icon for
  40. * this item (defaults to ''). If iconCls is specified {@link #icon} should not be.
  41. */
  42. /**
  43. * @cfg {String} text The text to display in this item (defaults to '').
  44. */
  45. /**
  46. * @cfg {String} href The href attribute to use for the underlying anchor link (defaults to '#').
  47. */
  48. /**
  49. * @cfg {String} hrefTarget The target attribute to use for the underlying anchor link (defaults to '').
  50. */
  51. /**
  52. * @cfg {String} itemCls The default CSS class to use for menu items (defaults to 'x-menu-item')
  53. */
  54. itemCls : 'x-menu-item',
  55. /**
  56. * @cfg {Boolean} canActivate True if this item can be visually activated (defaults to true)
  57. */
  58. canActivate : true,
  59. /**
  60. * @cfg {Number} showDelay Length of time in milliseconds to wait before showing this item (defaults to 200)
  61. */
  62. showDelay: 200,
  63. // doc'd in BaseItem
  64. hideDelay: 200,
  65. // private
  66. ctype: 'Ext.menu.Item',
  67. // private
  68. onRender : function(container, position){
  69. if (!this.itemTpl) {
  70. this.itemTpl = Ext.menu.Item.prototype.itemTpl = new Ext.XTemplate(
  71. '<a id="{id}" class="{cls}" hidefocus="true" unselectable="on" href="{href}"',
  72. '<tpl if="hrefTarget">',
  73. ' target="{hrefTarget}"',
  74. '</tpl>',
  75. '>',
  76. '<img src="{icon}" class="x-menu-item-icon {iconCls}"/>',
  77. '<span class="x-menu-item-text">{text}</span>',
  78. '</a>'
  79. );
  80. }
  81. var a = this.getTemplateArgs();
  82. this.el = position ? this.itemTpl.insertBefore(position, a, true) : this.itemTpl.append(container, a, true);
  83. this.iconEl = this.el.child('img.x-menu-item-icon');
  84. this.textEl = this.el.child('.x-menu-item-text');
  85. Ext.menu.Item.superclass.onRender.call(this, container, position);
  86. },
  87. getTemplateArgs: function() {
  88. return {
  89. id: this.id,
  90. cls: this.itemCls + (this.menu ? ' x-menu-item-arrow' : '') + (this.cls ? ' ' + this.cls : ''),
  91. href: this.href || '#',
  92. hrefTarget: this.hrefTarget,
  93. icon: this.icon || Ext.BLANK_IMAGE_URL,
  94. iconCls: this.iconCls || '',
  95. text: this.itemText||this.text||'&#160;'
  96. };
  97. },
  98. /**
  99. * Sets the text to display in this menu item
  100. * @param {String} text The text to display
  101. */
  102. setText : function(text){
  103. this.text = text||'&#160;';
  104. if(this.rendered){
  105. this.textEl.update(this.text);
  106. this.parentMenu.layout.doAutoSize();
  107. }
  108. },
  109. /**
  110. * Sets the CSS class to apply to the item's icon element
  111. * @param {String} cls The CSS class to apply
  112. */
  113. setIconClass : function(cls){
  114. var oldCls = this.iconCls;
  115. this.iconCls = cls;
  116. if(this.rendered){
  117. this.iconEl.replaceClass(oldCls, this.iconCls);
  118. }
  119. },
  120. //private
  121. beforeDestroy: function(){
  122. if (this.menu){
  123. this.menu.destroy();
  124. }
  125. Ext.menu.Item.superclass.beforeDestroy.call(this);
  126. },
  127. // private
  128. handleClick : function(e){
  129. if(!this.href){ // if no link defined, stop the event automatically
  130. e.stopEvent();
  131. }
  132. Ext.menu.Item.superclass.handleClick.apply(this, arguments);
  133. },
  134. // private
  135. activate : function(autoExpand){
  136. if(Ext.menu.Item.superclass.activate.apply(this, arguments)){
  137. this.focus();
  138. if(autoExpand){
  139. this.expandMenu();
  140. }
  141. }
  142. return true;
  143. },
  144. // private
  145. shouldDeactivate : function(e){
  146. if(Ext.menu.Item.superclass.shouldDeactivate.call(this, e)){
  147. if(this.menu && this.menu.isVisible()){
  148. return !this.menu.getEl().getRegion().contains(e.getPoint());
  149. }
  150. return true;
  151. }
  152. return false;
  153. },
  154. // private
  155. deactivate : function(){
  156. Ext.menu.Item.superclass.deactivate.apply(this, arguments);
  157. this.hideMenu();
  158. },
  159. // private
  160. expandMenu : function(autoActivate){
  161. if(!this.disabled && this.menu){
  162. clearTimeout(this.hideTimer);
  163. delete this.hideTimer;
  164. if(!this.menu.isVisible() && !this.showTimer){
  165. this.showTimer = this.deferExpand.defer(this.showDelay, this, [autoActivate]);
  166. }else if (this.menu.isVisible() && autoActivate){
  167. this.menu.tryActivate(0, 1);
  168. }
  169. }
  170. },
  171. // private
  172. deferExpand : function(autoActivate){
  173. delete this.showTimer;
  174. this.menu.show(this.container, this.parentMenu.subMenuAlign || 'tl-tr?', this.parentMenu);
  175. if(autoActivate){
  176. this.menu.tryActivate(0, 1);
  177. }
  178. },
  179. // private
  180. hideMenu : function(){
  181. clearTimeout(this.showTimer);
  182. delete this.showTimer;
  183. if(!this.hideTimer && this.menu && this.menu.isVisible()){
  184. this.hideTimer = this.deferHide.defer(this.hideDelay, this);
  185. }
  186. },
  187. // private
  188. deferHide : function(){
  189. delete this.hideTimer;
  190. if(this.menu.over){
  191. this.parentMenu.setActiveItem(this, false);
  192. }else{
  193. this.menu.hide();
  194. }
  195. }
  196. });
  197. Ext.reg('menuitem', Ext.menu.Item);