Upgrade to ExtJS 3.2.2 - Released 06/02/2010
[extjs.git] / src / widgets / menu / BaseItem.js
1 /*!
2  * Ext JS Library 3.2.2
3  * Copyright(c) 2006-2010 Ext JS, Inc.
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**
8  * @class Ext.menu.BaseItem
9  * @extends Ext.Component
10  * The base class for all items that render into menus.  BaseItem provides default rendering, activated state
11  * management and base configuration options shared by all menu components.
12  * @constructor
13  * Creates a new BaseItem
14  * @param {Object} config Configuration options
15  * @xtype menubaseitem
16  */
17 Ext.menu.BaseItem = Ext.extend(Ext.Component, {
18     /**
19      * @property parentMenu
20      * @type Ext.menu.Menu
21      * The parent Menu of this Item.
22      */
23     /**
24      * @cfg {Function} handler
25      * A function that will handle the click event of this menu item (optional).
26      * The handler is passed the following parameters:<div class="mdetail-params"><ul>
27      * <li><code>b</code> : Item<div class="sub-desc">This menu Item.</div></li>
28      * <li><code>e</code> : EventObject<div class="sub-desc">The click event.</div></li>
29      * </ul></div>
30      */
31     /**
32      * @cfg {Object} scope
33      * The scope (<tt><b>this</b></tt> reference) in which the handler function will be called.
34      */
35     /**
36      * @cfg {Boolean} canActivate True if this item can be visually activated (defaults to false)
37      */
38     canActivate : false,
39     /**
40      * @cfg {String} activeClass The CSS class to use when the item becomes activated (defaults to "x-menu-item-active")
41      */
42     activeClass : "x-menu-item-active",
43     /**
44      * @cfg {Boolean} hideOnClick True to hide the containing menu after this item is clicked (defaults to true)
45      */
46     hideOnClick : true,
47     /**
48      * @cfg {Number} clickHideDelay Length of time in milliseconds to wait before hiding after a click (defaults to 1)
49      */
50     clickHideDelay : 1,
51
52     // private
53     ctype : "Ext.menu.BaseItem",
54
55     // private
56     actionMode : "container",
57
58     initComponent : function(){
59         Ext.menu.BaseItem.superclass.initComponent.call(this);
60         this.addEvents(
61             /**
62              * @event click
63              * Fires when this item is clicked
64              * @param {Ext.menu.BaseItem} this
65              * @param {Ext.EventObject} e
66              */
67             'click',
68             /**
69              * @event activate
70              * Fires when this item is activated
71              * @param {Ext.menu.BaseItem} this
72              */
73             'activate',
74             /**
75              * @event deactivate
76              * Fires when this item is deactivated
77              * @param {Ext.menu.BaseItem} this
78              */
79             'deactivate'
80         );
81         if(this.handler){
82             this.on("click", this.handler, this.scope);
83         }
84     },
85
86     // private
87     onRender : function(container, position){
88         Ext.menu.BaseItem.superclass.onRender.apply(this, arguments);
89         if(this.ownerCt && this.ownerCt instanceof Ext.menu.Menu){
90             this.parentMenu = this.ownerCt;
91         }else{
92             this.container.addClass('x-menu-list-item');
93             this.mon(this.el, {
94                 scope: this,
95                 click: this.onClick,
96                 mouseenter: this.activate,
97                 mouseleave: this.deactivate
98             });
99         }
100     },
101
102     /**
103      * Sets the function that will handle click events for this item (equivalent to passing in the {@link #handler}
104      * config property).  If an existing handler is already registered, it will be unregistered for you.
105      * @param {Function} handler The function that should be called on click
106      * @param {Object} scope The scope (<code>this</code> reference) in which the handler function is executed. Defaults to this menu item.
107      */
108     setHandler : function(handler, scope){
109         if(this.handler){
110             this.un("click", this.handler, this.scope);
111         }
112         this.on("click", this.handler = handler, this.scope = scope);
113     },
114
115     // private
116     onClick : function(e){
117         if(!this.disabled && this.fireEvent("click", this, e) !== false
118                 && (this.parentMenu && this.parentMenu.fireEvent("itemclick", this, e) !== false)){
119             this.handleClick(e);
120         }else{
121             e.stopEvent();
122         }
123     },
124
125     // private
126     activate : function(){
127         if(this.disabled){
128             return false;
129         }
130         var li = this.container;
131         li.addClass(this.activeClass);
132         this.region = li.getRegion().adjust(2, 2, -2, -2);
133         this.fireEvent("activate", this);
134         return true;
135     },
136
137     // private
138     deactivate : function(){
139         this.container.removeClass(this.activeClass);
140         this.fireEvent("deactivate", this);
141     },
142
143     // private
144     shouldDeactivate : function(e){
145         return !this.region || !this.region.contains(e.getPoint());
146     },
147
148     // private
149     handleClick : function(e){
150         var pm = this.parentMenu;
151         if(this.hideOnClick){
152             if(pm.floating){
153                 pm.hide.defer(this.clickHideDelay, pm, [true]);
154             }else{
155                 pm.deactivateActive();
156             }
157         }
158     },
159
160     // private. Do nothing
161     expandMenu : Ext.emptyFn,
162
163     // private. Do nothing
164     hideMenu : Ext.emptyFn
165 });
166 Ext.reg('menubaseitem', Ext.menu.BaseItem);