Upgrade to ExtJS 3.3.0 - Released 10/06/2010
[extjs.git] / src / widgets / menu / Item.js
1 /*!
2  * Ext JS Library 3.3.0
3  * Copyright(c) 2006-2010 Ext JS, Inc.
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 = Ext.extend(Ext.menu.BaseItem, {
19     /**
20      * @property menu
21      * @type Ext.menu.Menu
22      * The submenu associated with this Item if one was configured.
23      */
24     /**
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.
27      */
28     /**
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.
31      */
32     /**
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.
35      */
36     /**
37      * @cfg {String} text The text to display in this item (defaults to '').
38      */
39     /**
40      * @cfg {String} href The href attribute to use for the underlying anchor link (defaults to '#').
41      */
42     /**
43      * @cfg {String} hrefTarget The target attribute to use for the underlying anchor link (defaults to '').
44      */
45     /**
46      * @cfg {String} itemCls The default CSS class to use for menu items (defaults to 'x-menu-item')
47      */
48     itemCls : 'x-menu-item',
49     /**
50      * @cfg {Boolean} canActivate True if this item can be visually activated (defaults to true)
51      */
52     canActivate : true,
53     /**
54      * @cfg {Number} showDelay Length of time in milliseconds to wait before showing this item (defaults to 200)
55      */
56     showDelay: 200,
57     
58     /**
59      * @cfg {String} altText The altText to use for the icon, if it exists. Defaults to <tt>''</tt>.
60      */
61     altText: '',
62     
63     // doc'd in BaseItem
64     hideDelay: 200,
65
66     // private
67     ctype: 'Ext.menu.Item',
68
69     initComponent : function(){
70         Ext.menu.Item.superclass.initComponent.call(this);
71         if(this.menu){
72             this.menu = Ext.menu.MenuMgr.get(this.menu);
73             this.menu.ownerCt = this;
74         }
75     },
76
77     // private
78     onRender : function(container, position){
79         if (!this.itemTpl) {
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}"',
84                     '</tpl>',
85                  '>',
86                      '<img alt="{altText}" src="{icon}" class="x-menu-item-icon {iconCls}"/>',
87                      '<span class="x-menu-item-text">{text}</span>',
88                  '</a>'
89              );
90         }
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 });
97         }
98         Ext.menu.Item.superclass.onRender.call(this, container, position);
99     },
100
101     getTemplateArgs: function() {
102         return {
103             id: this.id,
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||'&#160;',
110             altText: this.altText || ''
111         };
112     },
113
114     /**
115      * Sets the text to display in this menu item
116      * @param {String} text The text to display
117      */
118     setText : function(text){
119         this.text = text||'&#160;';
120         if(this.rendered){
121             this.textEl.update(this.text);
122             this.parentMenu.layout.doAutoSize();
123         }
124     },
125
126     /**
127      * Sets the CSS class to apply to the item's icon element
128      * @param {String} cls The CSS class to apply
129      */
130     setIconClass : function(cls){
131         var oldCls = this.iconCls;
132         this.iconCls = cls;
133         if(this.rendered){
134             this.iconEl.replaceClass(oldCls, this.iconCls);
135         }
136     },
137
138     //private
139     beforeDestroy: function(){
140         if (this.menu){
141             delete this.menu.ownerCt;
142             this.menu.destroy();
143         }
144         Ext.menu.Item.superclass.beforeDestroy.call(this);
145     },
146
147     // private
148     handleClick : function(e){
149         if(!this.href){ // if no link defined, stop the event automatically
150             e.stopEvent();
151         }
152         Ext.menu.Item.superclass.handleClick.apply(this, arguments);
153     },
154
155     // private
156     activate : function(autoExpand){
157         if(Ext.menu.Item.superclass.activate.apply(this, arguments)){
158             this.focus();
159             if(autoExpand){
160                 this.expandMenu();
161             }
162         }
163         return true;
164     },
165
166     // private
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());
171             }
172             return true;
173         }
174         return false;
175     },
176
177     // private
178     deactivate : function(){
179         Ext.menu.Item.superclass.deactivate.apply(this, arguments);
180         this.hideMenu();
181     },
182
183     // private
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);
192             }
193         }
194     },
195
196     // private
197     deferExpand : function(autoActivate){
198         delete this.showTimer;
199         this.menu.show(this.container, this.parentMenu.subMenuAlign || 'tl-tr?', this.parentMenu);
200         if(autoActivate){
201             this.menu.tryActivate(0, 1);
202         }
203     },
204
205     // private
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);
211         }
212     },
213
214     // private
215     deferHide : function(){
216         delete this.hideTimer;
217         if(this.menu.over){
218             this.parentMenu.setActiveItem(this, false);
219         }else{
220             this.menu.hide();
221         }
222     }
223 });
224 Ext.reg('menuitem', Ext.menu.Item);