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