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