3 * Copyright(c) 2006-2010 Ext JS, Inc.
5 * http://www.extjs.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)
61 ctype: 'Ext.menu.Item',
63 initComponent : function(){
64 Ext.menu.Item.superclass.initComponent.call(this);
66 this.menu = Ext.menu.MenuMgr.get(this.menu);
67 this.menu.ownerCt = this;
72 onRender : function(container, position){
74 this.itemTpl = Ext.menu.Item.prototype.itemTpl = new Ext.XTemplate(
75 '<a id="{id}" class="{cls}" hidefocus="true" unselectable="on" href="{href}"',
76 '<tpl if="hrefTarget">',
77 ' target="{hrefTarget}"',
80 '<img src="{icon}" class="x-menu-item-icon {iconCls}"/>',
81 '<span class="x-menu-item-text">{text}</span>',
85 var a = this.getTemplateArgs();
86 this.el = position ? this.itemTpl.insertBefore(position, a, true) : this.itemTpl.append(container, a, true);
87 this.iconEl = this.el.child('img.x-menu-item-icon');
88 this.textEl = this.el.child('.x-menu-item-text');
89 if(!this.href) { // if no link defined, prevent the default anchor event
90 this.mon(this.el, 'click', Ext.emptyFn, null, { preventDefault: true });
92 Ext.menu.Item.superclass.onRender.call(this, container, position);
95 getTemplateArgs: function() {
98 cls: this.itemCls + (this.menu ? ' x-menu-item-arrow' : '') + (this.cls ? ' ' + this.cls : ''),
99 href: this.href || '#',
100 hrefTarget: this.hrefTarget,
101 icon: this.icon || Ext.BLANK_IMAGE_URL,
102 iconCls: this.iconCls || '',
103 text: this.itemText||this.text||' '
108 * Sets the text to display in this menu item
109 * @param {String} text The text to display
111 setText : function(text){
112 this.text = text||' ';
114 this.textEl.update(this.text);
115 this.parentMenu.layout.doAutoSize();
120 * Sets the CSS class to apply to the item's icon element
121 * @param {String} cls The CSS class to apply
123 setIconClass : function(cls){
124 var oldCls = this.iconCls;
127 this.iconEl.replaceClass(oldCls, this.iconCls);
132 beforeDestroy: function(){
134 delete this.menu.ownerCt;
137 Ext.menu.Item.superclass.beforeDestroy.call(this);
141 handleClick : function(e){
142 if(!this.href){ // if no link defined, stop the event automatically
145 Ext.menu.Item.superclass.handleClick.apply(this, arguments);
149 activate : function(autoExpand){
150 if(Ext.menu.Item.superclass.activate.apply(this, arguments)){
160 shouldDeactivate : function(e){
161 if(Ext.menu.Item.superclass.shouldDeactivate.call(this, e)){
162 if(this.menu && this.menu.isVisible()){
163 return !this.menu.getEl().getRegion().contains(e.getPoint());
171 deactivate : function(){
172 Ext.menu.Item.superclass.deactivate.apply(this, arguments);
177 expandMenu : function(autoActivate){
178 if(!this.disabled && this.menu){
179 clearTimeout(this.hideTimer);
180 delete this.hideTimer;
181 if(!this.menu.isVisible() && !this.showTimer){
182 this.showTimer = this.deferExpand.defer(this.showDelay, this, [autoActivate]);
183 }else if (this.menu.isVisible() && autoActivate){
184 this.menu.tryActivate(0, 1);
190 deferExpand : function(autoActivate){
191 delete this.showTimer;
192 this.menu.show(this.container, this.parentMenu.subMenuAlign || 'tl-tr?', this.parentMenu);
194 this.menu.tryActivate(0, 1);
199 hideMenu : function(){
200 clearTimeout(this.showTimer);
201 delete this.showTimer;
202 if(!this.hideTimer && this.menu && this.menu.isVisible()){
203 this.hideTimer = this.deferHide.defer(this.hideDelay, this);
208 deferHide : function(){
209 delete this.hideTimer;
211 this.parentMenu.setActiveItem(this, false);
217 Ext.reg('menuitem', Ext.menu.Item);