Roo/menu/Item.js
[roojs1] / Roo / menu / Item.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  * @class Roo.menu.Item
13  * @extends Roo.menu.BaseItem
14  * A base class for all menu items that require menu-related functionality (like sub-menus) and are not static
15  * display items.  Item extends the base functionality of {@link Roo.menu.BaseItem} by adding menu-specific
16  * activation and click handling.
17  * @constructor
18  * Creates a new Item
19  * @param {Object} config Configuration options
20  */
21 Roo.menu.Item = function(config){
22     Roo.menu.Item.superclass.constructor.call(this, config);
23     if(this.menu){
24         this.menu = Roo.menu.MenuMgr.get(this.menu);
25     }
26 };
27 Roo.extend(Roo.menu.Item, Roo.menu.BaseItem, {
28     
29     /**
30      * @cfg {String} text
31      * The text to show on the menu item.
32      */
33     text: '',
34     /**
35      * @cfg {String} icon
36      * The path to an icon to display in this menu item (defaults to Roo.BLANK_IMAGE_URL)
37      */
38     icon: undefined,
39     /**
40      * @cfg {String} itemCls The default CSS class to use for menu items (defaults to "x-menu-item")
41      */
42     itemCls : "x-menu-item",
43     /**
44      * @cfg {Boolean} canActivate True if this item can be visually activated (defaults to true)
45      */
46     canActivate : true,
47     /**
48      * @cfg {Number} showDelay Length of time in milliseconds to wait before showing this item (defaults to 200)
49      */
50     showDelay: 200,
51     // doc'd in BaseItem
52     hideDelay: 200,
53
54     // private
55     ctype: "Roo.menu.Item",
56     
57     // private
58     onRender : function(container, position){
59         var el = document.createElement("a");
60         el.hideFocus = true;
61         el.unselectable = "on";
62         el.href = this.href || "#";
63         if(this.hrefTarget){
64             el.target = this.hrefTarget;
65         }
66         el.className = this.itemCls + (this.menu ?  " x-menu-item-arrow" : "") + (this.cls ?  " " + this.cls : "");
67         el.innerHTML = String.format(
68                 '<img src="{0}" class="x-menu-item-icon {2}" />{1}',
69                 this.icon || Roo.BLANK_IMAGE_URL, this.text, this.iconCls || '');
70         this.el = el;
71         Roo.menu.Item.superclass.onRender.call(this, container, position);
72     },
73
74     /**
75      * Sets the text to display in this menu item
76      * @param {String} text The text to display
77      */
78     setText : function(text){
79         this.text = text;
80         if(this.rendered){
81             this.el.update(String.format(
82                 '<img src="{0}" class="x-menu-item-icon {2}">{1}',
83                 this.icon || Roo.BLANK_IMAGE_URL, this.text, this.iconCls || ''));
84             this.parentMenu.autoWidth();
85         }
86     },
87
88     // private
89     handleClick : function(e){
90         if(!this.href){ // if no link defined, stop the event automatically
91             e.stopEvent();
92         }
93         Roo.menu.Item.superclass.handleClick.apply(this, arguments);
94     },
95
96     // private
97     activate : function(autoExpand){
98         if(Roo.menu.Item.superclass.activate.apply(this, arguments)){
99             this.focus();
100             if(autoExpand){
101                 this.expandMenu();
102             }
103         }
104         return true;
105     },
106
107     // private
108     shouldDeactivate : function(e){
109         if(Roo.menu.Item.superclass.shouldDeactivate.call(this, e)){
110             if(this.menu && this.menu.isVisible()){
111                 return !this.menu.getEl().getRegion().contains(e.getPoint());
112             }
113             return true;
114         }
115         return false;
116     },
117
118     // private
119     deactivate : function(){
120         Roo.menu.Item.superclass.deactivate.apply(this, arguments);
121         this.hideMenu();
122     },
123
124     // private
125     expandMenu : function(autoActivate){
126         if(!this.disabled && this.menu){
127             clearTimeout(this.hideTimer);
128             delete this.hideTimer;
129             if(!this.menu.isVisible() && !this.showTimer){
130                 this.showTimer = this.deferExpand.defer(this.showDelay, this, [autoActivate]);
131             }else if (this.menu.isVisible() && autoActivate){
132                 this.menu.tryActivate(0, 1);
133             }
134         }
135     },
136
137     // private
138     deferExpand : function(autoActivate){
139         delete this.showTimer;
140         this.menu.show(this.container, this.parentMenu.subMenuAlign || "tl-tr?", this.parentMenu);
141         if(autoActivate){
142             this.menu.tryActivate(0, 1);
143         }
144     },
145
146     // private
147     hideMenu : function(){
148         clearTimeout(this.showTimer);
149         delete this.showTimer;
150         if(!this.hideTimer && this.menu && this.menu.isVisible()){
151             this.hideTimer = this.deferHide.defer(this.hideDelay, this);
152         }
153     },
154
155     // private
156     deferHide : function(){
157         delete this.hideTimer;
158         this.menu.hide();
159     }
160 });