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