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