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