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