3 * Copyright(c) 2006-2010 Sencha Inc.
5 * http://www.sencha.com/license
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.
15 * @param {Object} config Configuration options
18 Ext.menu.Item = Ext.extend(Ext.menu.BaseItem, {
22 * The submenu associated with this Item if one was configured.
25 * @cfg {Mixed} menu (optional) Either an instance of {@link Ext.menu.Menu} or the config object for an
26 * {@link Ext.menu.Menu} which acts as the submenu when this item is activated.
29 * @cfg {String} icon The path to an icon to display in this item (defaults to Ext.BLANK_IMAGE_URL). If
30 * icon is specified {@link #iconCls} should not be.
33 * @cfg {String} iconCls A CSS class that specifies a background image that will be used as the icon for
34 * this item (defaults to ''). If iconCls is specified {@link #icon} should not be.
37 * @cfg {String} text The text to display in this item (defaults to '').
40 * @cfg {String} href The href attribute to use for the underlying anchor link (defaults to '#').
43 * @cfg {String} hrefTarget The target attribute to use for the underlying anchor link (defaults to '').
46 * @cfg {String} itemCls The default CSS class to use for menu items (defaults to 'x-menu-item')
48 itemCls : 'x-menu-item',
50 * @cfg {Boolean} canActivate True if this item can be visually activated (defaults to true)
54 * @cfg {Number} showDelay Length of time in milliseconds to wait before showing this item (defaults to 200)
59 * @cfg {String} altText The altText to use for the icon, if it exists. Defaults to <tt>''</tt>.
67 ctype: 'Ext.menu.Item',
69 initComponent : function(){
70 Ext.menu.Item.superclass.initComponent.call(this);
72 this.menu = Ext.menu.MenuMgr.get(this.menu);
73 this.menu.ownerCt = this;
78 onRender : function(container, position){
80 this.itemTpl = Ext.menu.Item.prototype.itemTpl = new Ext.XTemplate(
81 '<a id="{id}" class="{cls}" hidefocus="true" unselectable="on" href="{href}"',
82 '<tpl if="hrefTarget">',
83 ' target="{hrefTarget}"',
86 '<img alt="{altText}" src="{icon}" class="x-menu-item-icon {iconCls}"/>',
87 '<span class="x-menu-item-text">{text}</span>',
91 var a = this.getTemplateArgs();
92 this.el = position ? this.itemTpl.insertBefore(position, a, true) : this.itemTpl.append(container, a, true);
93 this.iconEl = this.el.child('img.x-menu-item-icon');
94 this.textEl = this.el.child('.x-menu-item-text');
95 if(!this.href) { // if no link defined, prevent the default anchor event
96 this.mon(this.el, 'click', Ext.emptyFn, null, { preventDefault: true });
98 Ext.menu.Item.superclass.onRender.call(this, container, position);
101 getTemplateArgs: function() {
104 cls: this.itemCls + (this.menu ? ' x-menu-item-arrow' : '') + (this.cls ? ' ' + this.cls : ''),
105 href: this.href || '#',
106 hrefTarget: this.hrefTarget,
107 icon: this.icon || Ext.BLANK_IMAGE_URL,
108 iconCls: this.iconCls || '',
109 text: this.itemText||this.text||' ',
110 altText: this.altText || ''
115 * Sets the text to display in this menu item
116 * @param {String} text The text to display
118 setText : function(text){
119 this.text = text||' ';
121 this.textEl.update(this.text);
122 this.parentMenu.layout.doAutoSize();
127 * Sets the CSS class to apply to the item's icon element
128 * @param {String} cls The CSS class to apply
130 setIconClass : function(cls){
131 var oldCls = this.iconCls;
134 this.iconEl.replaceClass(oldCls, this.iconCls);
139 beforeDestroy: function(){
141 delete this.menu.ownerCt;
144 Ext.menu.Item.superclass.beforeDestroy.call(this);
148 handleClick : function(e){
149 if(!this.href){ // if no link defined, stop the event automatically
152 Ext.menu.Item.superclass.handleClick.apply(this, arguments);
156 activate : function(autoExpand){
157 if(Ext.menu.Item.superclass.activate.apply(this, arguments)){
167 shouldDeactivate : function(e){
168 if(Ext.menu.Item.superclass.shouldDeactivate.call(this, e)){
169 if(this.menu && this.menu.isVisible()){
170 return !this.menu.getEl().getRegion().contains(e.getPoint());
178 deactivate : function(){
179 Ext.menu.Item.superclass.deactivate.apply(this, arguments);
184 expandMenu : function(autoActivate){
185 if(!this.disabled && this.menu){
186 clearTimeout(this.hideTimer);
187 delete this.hideTimer;
188 if(!this.menu.isVisible() && !this.showTimer){
189 this.showTimer = this.deferExpand.defer(this.showDelay, this, [autoActivate]);
190 }else if (this.menu.isVisible() && autoActivate){
191 this.menu.tryActivate(0, 1);
197 deferExpand : function(autoActivate){
198 delete this.showTimer;
199 this.menu.show(this.container, this.parentMenu.subMenuAlign || 'tl-tr?', this.parentMenu);
201 this.menu.tryActivate(0, 1);
206 hideMenu : function(){
207 clearTimeout(this.showTimer);
208 delete this.showTimer;
209 if(!this.hideTimer && this.menu && this.menu.isVisible()){
210 this.hideTimer = this.deferHide.defer(this.hideDelay, this);
215 deferHide : function(){
216 delete this.hideTimer;
218 this.parentMenu.setActiveItem(this, false);
224 Ext.reg('menuitem', Ext.menu.Item);