3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
8 * @class Ext.menu.Item
\r
9 * @extends Ext.menu.BaseItem
\r
10 * A base class for all menu items that require menu-related functionality (like sub-menus) and are not static
\r
11 * display items. Item extends the base functionality of {@link Ext.menu.BaseItem} by adding menu-specific
\r
12 * activation and click handling.
\r
14 * Creates a new Item
\r
15 * @param {Object} config Configuration options
\r
18 Ext.menu.Item = Ext.extend(Ext.menu.BaseItem, {
\r
21 * @type Ext.menu.Menu
\r
22 * The submenu associated with this Item if one was configured.
\r
25 * @cfg {Mixed} menu (optional) Either an instance of {@link Ext.menu.Menu} or the config object for an
\r
26 * {@link Ext.menu.Menu} which acts as the submenu when this item is activated.
\r
29 * @cfg {String} icon The path to an icon to display in this item (defaults to Ext.BLANK_IMAGE_URL). If
\r
30 * icon is specified {@link #iconCls} should not be.
\r
33 * @cfg {String} iconCls A CSS class that specifies a background image that will be used as the icon for
\r
34 * this item (defaults to ''). If iconCls is specified {@link #icon} should not be.
\r
37 * @cfg {String} text The text to display in this item (defaults to '').
\r
40 * @cfg {String} href The href attribute to use for the underlying anchor link (defaults to '#').
\r
43 * @cfg {String} hrefTarget The target attribute to use for the underlying anchor link (defaults to '').
\r
46 * @cfg {String} itemCls The default CSS class to use for menu items (defaults to 'x-menu-item')
\r
48 itemCls : 'x-menu-item',
\r
50 * @cfg {Boolean} canActivate True if this item can be visually activated (defaults to true)
\r
54 * @cfg {Number} showDelay Length of time in milliseconds to wait before showing this item (defaults to 200)
\r
57 // doc'd in BaseItem
\r
61 ctype: 'Ext.menu.Item',
\r
63 initComponent : function(){
\r
64 Ext.menu.Item.superclass.initComponent.call(this);
\r
66 this.menu = Ext.menu.MenuMgr.get(this.menu);
\r
67 this.menu.ownerCt = this;
\r
72 onRender : function(container, position){
\r
73 if (!this.itemTpl) {
\r
74 this.itemTpl = Ext.menu.Item.prototype.itemTpl = new Ext.XTemplate(
\r
75 '<a id="{id}" class="{cls}" hidefocus="true" unselectable="on" href="{href}"',
\r
76 '<tpl if="hrefTarget">',
\r
77 ' target="{hrefTarget}"',
\r
80 '<img src="{icon}" class="x-menu-item-icon {iconCls}"/>',
\r
81 '<span class="x-menu-item-text">{text}</span>',
\r
85 var a = this.getTemplateArgs();
\r
86 this.el = position ? this.itemTpl.insertBefore(position, a, true) : this.itemTpl.append(container, a, true);
\r
87 this.iconEl = this.el.child('img.x-menu-item-icon');
\r
88 this.textEl = this.el.child('.x-menu-item-text');
\r
89 if(!this.href) { // if no link defined, prevent the default anchor event
\r
90 this.mon(this.el, 'click', Ext.emptyFn, null, { preventDefault: true });
\r
92 Ext.menu.Item.superclass.onRender.call(this, container, position);
\r
95 getTemplateArgs: function() {
\r
98 cls: this.itemCls + (this.menu ? ' x-menu-item-arrow' : '') + (this.cls ? ' ' + this.cls : ''),
\r
99 href: this.href || '#',
\r
100 hrefTarget: this.hrefTarget,
\r
101 icon: this.icon || Ext.BLANK_IMAGE_URL,
\r
102 iconCls: this.iconCls || '',
\r
103 text: this.itemText||this.text||' '
\r
108 * Sets the text to display in this menu item
\r
109 * @param {String} text The text to display
\r
111 setText : function(text){
\r
112 this.text = text||' ';
\r
114 this.textEl.update(this.text);
\r
115 this.parentMenu.layout.doAutoSize();
\r
120 * Sets the CSS class to apply to the item's icon element
\r
121 * @param {String} cls The CSS class to apply
\r
123 setIconClass : function(cls){
\r
124 var oldCls = this.iconCls;
\r
125 this.iconCls = cls;
\r
127 this.iconEl.replaceClass(oldCls, this.iconCls);
\r
132 beforeDestroy: function(){
\r
134 delete this.menu.ownerCt;
\r
135 this.menu.destroy();
\r
137 Ext.menu.Item.superclass.beforeDestroy.call(this);
\r
141 handleClick : function(e){
\r
142 if(!this.href){ // if no link defined, stop the event automatically
\r
145 Ext.menu.Item.superclass.handleClick.apply(this, arguments);
\r
149 activate : function(autoExpand){
\r
150 if(Ext.menu.Item.superclass.activate.apply(this, arguments)){
\r
160 shouldDeactivate : function(e){
\r
161 if(Ext.menu.Item.superclass.shouldDeactivate.call(this, e)){
\r
162 if(this.menu && this.menu.isVisible()){
\r
163 return !this.menu.getEl().getRegion().contains(e.getPoint());
\r
171 deactivate : function(){
\r
172 Ext.menu.Item.superclass.deactivate.apply(this, arguments);
\r
177 expandMenu : function(autoActivate){
\r
178 if(!this.disabled && this.menu){
\r
179 clearTimeout(this.hideTimer);
\r
180 delete this.hideTimer;
\r
181 if(!this.menu.isVisible() && !this.showTimer){
\r
182 this.showTimer = this.deferExpand.defer(this.showDelay, this, [autoActivate]);
\r
183 }else if (this.menu.isVisible() && autoActivate){
\r
184 this.menu.tryActivate(0, 1);
\r
190 deferExpand : function(autoActivate){
\r
191 delete this.showTimer;
\r
192 this.menu.show(this.container, this.parentMenu.subMenuAlign || 'tl-tr?', this.parentMenu);
\r
194 this.menu.tryActivate(0, 1);
\r
199 hideMenu : function(){
\r
200 clearTimeout(this.showTimer);
\r
201 delete this.showTimer;
\r
202 if(!this.hideTimer && this.menu && this.menu.isVisible()){
\r
203 this.hideTimer = this.deferHide.defer(this.hideDelay, this);
\r
208 deferHide : function(){
\r
209 delete this.hideTimer;
\r
210 if(this.menu.over){
\r
211 this.parentMenu.setActiveItem(this, false);
\r
217 Ext.reg('menuitem', Ext.menu.Item);