allow string based values for comboboxarray
[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('{0}',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         if (isHTML) {
89             this.html = text;
90         } else {
91             this.text = text;
92             this.html = '';
93         }
94         if(this.rendered){
95             var html = this.html.length ? this.html  : String.format('{0}',this.text);
96      
97             this.el.update(String.format(
98                 '<img src="{0}" class="x-menu-item-icon {2}">' + html,
99                 this.icon || Roo.BLANK_IMAGE_URL, this.text, this.iconCls || ''));
100             this.parentMenu.autoWidth();
101         }
102     },
103
104     // private
105     handleClick : function(e){
106         if(!this.href){ // if no link defined, stop the event automatically
107             e.stopEvent();
108         }
109         Roo.menu.Item.superclass.handleClick.apply(this, arguments);
110     },
111
112     // private
113     activate : function(autoExpand){
114         if(Roo.menu.Item.superclass.activate.apply(this, arguments)){
115             this.focus();
116             if(autoExpand){
117                 this.expandMenu();
118             }
119         }
120         return true;
121     },
122
123     // private
124     shouldDeactivate : function(e){
125         if(Roo.menu.Item.superclass.shouldDeactivate.call(this, e)){
126             if(this.menu && this.menu.isVisible()){
127                 return !this.menu.getEl().getRegion().contains(e.getPoint());
128             }
129             return true;
130         }
131         return false;
132     },
133
134     // private
135     deactivate : function(){
136         Roo.menu.Item.superclass.deactivate.apply(this, arguments);
137         this.hideMenu();
138     },
139
140     // private
141     expandMenu : function(autoActivate){
142         if(!this.disabled && this.menu){
143             clearTimeout(this.hideTimer);
144             delete this.hideTimer;
145             if(!this.menu.isVisible() && !this.showTimer){
146                 this.showTimer = this.deferExpand.defer(this.showDelay, this, [autoActivate]);
147             }else if (this.menu.isVisible() && autoActivate){
148                 this.menu.tryActivate(0, 1);
149             }
150         }
151     },
152
153     // private
154     deferExpand : function(autoActivate){
155         delete this.showTimer;
156         this.menu.show(this.container, this.parentMenu.subMenuAlign || "tl-tr?", this.parentMenu);
157         if(autoActivate){
158             this.menu.tryActivate(0, 1);
159         }
160     },
161
162     // private
163     hideMenu : function(){
164         clearTimeout(this.showTimer);
165         delete this.showTimer;
166         if(!this.hideTimer && this.menu && this.menu.isVisible()){
167             this.hideTimer = this.deferHide.defer(this.hideDelay, this);
168         }
169     },
170
171     // private
172     deferHide : function(){
173         delete this.hideTimer;
174         this.menu.hide();
175     }
176 });