initial import
[roojs1] / Roo / menu / BaseItem.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11  
12
13 /**
14  * @class Roo.menu.BaseItem
15  * @extends Roo.Component
16  * The base class for all items that render into menus.  BaseItem provides default rendering, activated state
17  * management and base configuration options shared by all menu components.
18  * @constructor
19  * Creates a new BaseItem
20  * @param {Object} config Configuration options
21  */
22 Roo.menu.BaseItem = function(config){
23     Roo.menu.BaseItem.superclass.constructor.call(this, config);
24
25     this.addEvents({
26         /**
27          * @event click
28          * Fires when this item is clicked
29          * @param {Roo.menu.BaseItem} this
30          * @param {Roo.EventObject} e
31          */
32         click: true,
33         /**
34          * @event activate
35          * Fires when this item is activated
36          * @param {Roo.menu.BaseItem} this
37          */
38         activate : true,
39         /**
40          * @event deactivate
41          * Fires when this item is deactivated
42          * @param {Roo.menu.BaseItem} this
43          */
44         deactivate : true
45     });
46
47     if(this.handler){
48         this.on("click", this.handler, this.scope, true);
49     }
50 };
51
52 Roo.extend(Roo.menu.BaseItem, Roo.Component, {
53     /**
54      * @cfg {Function} handler
55      * A function that will handle the click event of this menu item (defaults to undefined)
56      */
57     /**
58      * @cfg {Boolean} canActivate True if this item can be visually activated (defaults to false)
59      */
60     canActivate : false,
61     /**
62      * @cfg {String} activeClass The CSS class to use when the item becomes activated (defaults to "x-menu-item-active")
63      */
64     activeClass : "x-menu-item-active",
65     /**
66      * @cfg {Boolean} hideOnClick True to hide the containing menu after this item is clicked (defaults to true)
67      */
68     hideOnClick : true,
69     /**
70      * @cfg {Number} hideDelay Length of time in milliseconds to wait before hiding after a click (defaults to 100)
71      */
72     hideDelay : 100,
73
74     // private
75     ctype: "Roo.menu.BaseItem",
76
77     // private
78     actionMode : "container",
79
80     // private
81     render : function(container, parentMenu){
82         this.parentMenu = parentMenu;
83         Roo.menu.BaseItem.superclass.render.call(this, container);
84         this.container.menuItemId = this.id;
85     },
86
87     // private
88     onRender : function(container, position){
89         this.el = Roo.get(this.el);
90         container.dom.appendChild(this.el.dom);
91     },
92
93     // private
94     onClick : function(e){
95         if(!this.disabled && this.fireEvent("click", this, e) !== false
96                 && this.parentMenu.fireEvent("itemclick", this, e) !== false){
97             this.handleClick(e);
98         }else{
99             e.stopEvent();
100         }
101     },
102
103     // private
104     activate : function(){
105         if(this.disabled){
106             return false;
107         }
108         var li = this.container;
109         li.addClass(this.activeClass);
110         this.region = li.getRegion().adjust(2, 2, -2, -2);
111         this.fireEvent("activate", this);
112         return true;
113     },
114
115     // private
116     deactivate : function(){
117         this.container.removeClass(this.activeClass);
118         this.fireEvent("deactivate", this);
119     },
120
121     // private
122     shouldDeactivate : function(e){
123         return !this.region || !this.region.contains(e.getPoint());
124     },
125
126     // private
127     handleClick : function(e){
128         if(this.hideOnClick){
129             this.parentMenu.hide.defer(this.hideDelay, this.parentMenu, [true]);
130         }
131     },
132
133     // private
134     expandMenu : function(autoActivate){
135         // do nothing
136     },
137
138     // private
139     hideMenu : function(){
140         // do nothing
141     }
142 });