Upgrade to ExtJS 3.2.2 - Released 06/02/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.2.2
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     // doc'd in BaseItem
66     hideDelay: 200,
67
68     // private
69     ctype: 'Ext.menu.Item',
70
71     initComponent : function(){
72         Ext.menu.Item.superclass.initComponent.call(this);
73         if(this.menu){
74             this.menu = Ext.menu.MenuMgr.get(this.menu);
75             this.menu.ownerCt = this;
76         }
77     },
78
79     // private
80     onRender : function(container, position){
81         if (!this.itemTpl) {
82             this.itemTpl = Ext.menu.Item.prototype.itemTpl = new Ext.XTemplate(
83                 '<a id="{id}" class="{cls}" hidefocus="true" unselectable="on" href="{href}"',
84                     '<tpl if="hrefTarget">',
85                         ' target="{hrefTarget}"',
86                     '</tpl>',
87                  '>',
88                      '<img src="{icon}" class="x-menu-item-icon {iconCls}"/>',
89                      '<span class="x-menu-item-text">{text}</span>',
90                  '</a>'
91              );
92         }
93         var a = this.getTemplateArgs();
94         this.el = position ? this.itemTpl.insertBefore(position, a, true) : this.itemTpl.append(container, a, true);
95         this.iconEl = this.el.child('img.x-menu-item-icon');
96         this.textEl = this.el.child('.x-menu-item-text');
97         if(!this.href) { // if no link defined, prevent the default anchor event
98             this.mon(this.el, 'click', Ext.emptyFn, null, { preventDefault: true });
99         }
100         Ext.menu.Item.superclass.onRender.call(this, container, position);
101     },
102
103     getTemplateArgs: function() {
104         return {
105             id: this.id,
106             cls: this.itemCls + (this.menu ?  ' x-menu-item-arrow' : '') + (this.cls ?  ' ' + this.cls : ''),
107             href: this.href || '#',
108             hrefTarget: this.hrefTarget,
109             icon: this.icon || Ext.BLANK_IMAGE_URL,
110             iconCls: this.iconCls || '',
111             text: this.itemText||this.text||'&#160;'
112         };
113     },
114
115     <div id="method-Ext.menu.Item-setText"></div>/**
116      * Sets the text to display in this menu item
117      * @param {String} text The text to display
118      */
119     setText : function(text){
120         this.text = text||'&#160;';
121         if(this.rendered){
122             this.textEl.update(this.text);
123             this.parentMenu.layout.doAutoSize();
124         }
125     },
126
127     <div id="method-Ext.menu.Item-setIconClass"></div>/**
128      * Sets the CSS class to apply to the item's icon element
129      * @param {String} cls The CSS class to apply
130      */
131     setIconClass : function(cls){
132         var oldCls = this.iconCls;
133         this.iconCls = cls;
134         if(this.rendered){
135             this.iconEl.replaceClass(oldCls, this.iconCls);
136         }
137     },
138
139     //private
140     beforeDestroy: function(){
141         if (this.menu){
142             delete this.menu.ownerCt;
143             this.menu.destroy();
144         }
145         Ext.menu.Item.superclass.beforeDestroy.call(this);
146     },
147
148     // private
149     handleClick : function(e){
150         if(!this.href){ // if no link defined, stop the event automatically
151             e.stopEvent();
152         }
153         Ext.menu.Item.superclass.handleClick.apply(this, arguments);
154     },
155
156     // private
157     activate : function(autoExpand){
158         if(Ext.menu.Item.superclass.activate.apply(this, arguments)){
159             this.focus();
160             if(autoExpand){
161                 this.expandMenu();
162             }
163         }
164         return true;
165     },
166
167     // private
168     shouldDeactivate : function(e){
169         if(Ext.menu.Item.superclass.shouldDeactivate.call(this, e)){
170             if(this.menu && this.menu.isVisible()){
171                 return !this.menu.getEl().getRegion().contains(e.getPoint());
172             }
173             return true;
174         }
175         return false;
176     },
177
178     // private
179     deactivate : function(){
180         Ext.menu.Item.superclass.deactivate.apply(this, arguments);
181         this.hideMenu();
182     },
183
184     // private
185     expandMenu : function(autoActivate){
186         if(!this.disabled && this.menu){
187             clearTimeout(this.hideTimer);
188             delete this.hideTimer;
189             if(!this.menu.isVisible() && !this.showTimer){
190                 this.showTimer = this.deferExpand.defer(this.showDelay, this, [autoActivate]);
191             }else if (this.menu.isVisible() && autoActivate){
192                 this.menu.tryActivate(0, 1);
193             }
194         }
195     },
196
197     // private
198     deferExpand : function(autoActivate){
199         delete this.showTimer;
200         this.menu.show(this.container, this.parentMenu.subMenuAlign || 'tl-tr?', this.parentMenu);
201         if(autoActivate){
202             this.menu.tryActivate(0, 1);
203         }
204     },
205
206     // private
207     hideMenu : function(){
208         clearTimeout(this.showTimer);
209         delete this.showTimer;
210         if(!this.hideTimer && this.menu && this.menu.isVisible()){
211             this.hideTimer = this.deferHide.defer(this.hideDelay, this);
212         }
213     },
214
215     // private
216     deferHide : function(){
217         delete this.hideTimer;
218         if(this.menu.over){
219             this.parentMenu.setActiveItem(this, false);
220         }else{
221             this.menu.hide();
222         }
223     }
224 });
225 Ext.reg('menuitem', Ext.menu.Item);</pre>    
226 </body>
227 </html>