commit extjs-2.2.1
[extjs.git] / source / widgets / menu / Item.js
1 /*\r
2  * Ext JS Library 2.2.1\r
3  * Copyright(c) 2006-2009, Ext JS, LLC.\r
4  * licensing@extjs.com\r
5  * \r
6  * http://extjs.com/license\r
7  */\r
8 \r
9 /**\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  */\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     /**\r
27      * @cfg {Mixed} menu 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     /**\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     /**\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     /**\r
39      * @cfg {String} text The text to display in this item (defaults to '').\r
40      */\r
41     /**\r
42      * @cfg {String} href The href attribute to use for the underlying anchor link (defaults to '#').\r
43      */\r
44     /**\r
45      * @cfg {String} hrefTarget The target attribute to use for the underlying anchor link (defaults to '').\r
46      */\r
47     /**\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     /**\r
52      * @cfg {Boolean} canActivate True if this item can be visually activated (defaults to true)\r
53      */\r
54     canActivate : true,\r
55     /**\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     // private\r
66     onRender : function(container, position){\r
67         var el = document.createElement("a");\r
68         el.hideFocus = true;\r
69         el.unselectable = "on";\r
70         el.href = this.href || "#";\r
71         if(this.hrefTarget){\r
72             el.target = this.hrefTarget;\r
73         }\r
74         el.className = this.itemCls + (this.menu ?  " x-menu-item-arrow" : "") + (this.cls ?  " " + this.cls : "");\r
75         el.innerHTML = String.format(\r
76                 '<img src="{0}" class="x-menu-item-icon {2}" />{1}',\r
77                 this.icon || Ext.BLANK_IMAGE_URL, this.itemText||this.text, this.iconCls || '');\r
78         this.el = el;\r
79         Ext.menu.Item.superclass.onRender.call(this, container, position);\r
80     },\r
81 \r
82     /**\r
83      * Sets the text to display in this menu item\r
84      * @param {String} text The text to display\r
85      */\r
86     setText : function(text){\r
87         this.text = text;\r
88         if(this.rendered){\r
89             this.el.update(String.format(\r
90                 '<img src="{0}" class="x-menu-item-icon {2}">{1}',\r
91                 this.icon || Ext.BLANK_IMAGE_URL, this.text, this.iconCls || ''));\r
92             this.parentMenu.autoWidth();\r
93         }\r
94     },\r
95 \r
96     /**\r
97      * Sets the CSS class to apply to the item's icon element\r
98      * @param {String} cls The CSS class to apply\r
99      */\r
100     setIconClass : function(cls){\r
101         var oldCls = this.iconCls;\r
102         this.iconCls = cls;\r
103         if(this.rendered){\r
104             this.el.child('img.x-menu-item-icon').replaceClass(oldCls, this.iconCls);\r
105         }\r
106     },\r
107     \r
108     //private\r
109     beforeDestroy: function(){\r
110         if (this.menu){\r
111             this.menu.destroy();\r
112         }\r
113         Ext.menu.Item.superclass.beforeDestroy.call(this);\r
114     },\r
115 \r
116     // private\r
117     handleClick : function(e){\r
118         if(!this.href){ // if no link defined, stop the event automatically\r
119             e.stopEvent();\r
120         }\r
121         Ext.menu.Item.superclass.handleClick.apply(this, arguments);\r
122     },\r
123 \r
124     // private\r
125     activate : function(autoExpand){\r
126         if(Ext.menu.Item.superclass.activate.apply(this, arguments)){\r
127             this.focus();\r
128             if(autoExpand){\r
129                 this.expandMenu();\r
130             }\r
131         }\r
132         return true;\r
133     },\r
134 \r
135     // private\r
136     shouldDeactivate : function(e){\r
137         if(Ext.menu.Item.superclass.shouldDeactivate.call(this, e)){\r
138             if(this.menu && this.menu.isVisible()){\r
139                 return !this.menu.getEl().getRegion().contains(e.getPoint());\r
140             }\r
141             return true;\r
142         }\r
143         return false;\r
144     },\r
145 \r
146     // private\r
147     deactivate : function(){\r
148         Ext.menu.Item.superclass.deactivate.apply(this, arguments);\r
149         this.hideMenu();\r
150     },\r
151 \r
152     // private\r
153     expandMenu : function(autoActivate){\r
154         if(!this.disabled && this.menu){\r
155             clearTimeout(this.hideTimer);\r
156             delete this.hideTimer;\r
157             if(!this.menu.isVisible() && !this.showTimer){\r
158                 this.showTimer = this.deferExpand.defer(this.showDelay, this, [autoActivate]);\r
159             }else if (this.menu.isVisible() && autoActivate){\r
160                 this.menu.tryActivate(0, 1);\r
161             }\r
162         }\r
163     },\r
164 \r
165     // private\r
166     deferExpand : function(autoActivate){\r
167         delete this.showTimer;\r
168         this.menu.show(this.container, this.parentMenu.subMenuAlign || "tl-tr?", this.parentMenu);\r
169         if(autoActivate){\r
170             this.menu.tryActivate(0, 1);\r
171         }\r
172     },\r
173 \r
174     // private\r
175     hideMenu : function(){\r
176         clearTimeout(this.showTimer);\r
177         delete this.showTimer;\r
178         if(!this.hideTimer && this.menu && this.menu.isVisible()){\r
179             this.hideTimer = this.deferHide.defer(this.hideDelay, this);\r
180         }\r
181     },\r
182 \r
183     // private\r
184     deferHide : function(){\r
185         delete this.hideTimer;\r
186         if(this.menu.over){\r
187             this.parentMenu.setActiveItem(this, false);\r
188         }else{\r
189             this.menu.hide();\r
190         }\r
191     }\r
192 });