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