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