1 | /*! |
---|
2 | * Ext JS Library 3.4.0 |
---|
3 | * Copyright(c) 2006-2011 Sencha Inc. |
---|
4 | * licensing@sencha.com |
---|
5 | * http://www.sencha.com/license |
---|
6 | */ |
---|
7 | /** |
---|
8 | * @class Ext.menu.Item |
---|
9 | * @extends Ext.menu.BaseItem |
---|
10 | * A base class for all menu items that require menu-related functionality (like sub-menus) and are not static |
---|
11 | * display items. Item extends the base functionality of {@link Ext.menu.BaseItem} by adding menu-specific |
---|
12 | * activation and click handling. |
---|
13 | * @constructor |
---|
14 | * Creates a new Item |
---|
15 | * @param {Object} config Configuration options |
---|
16 | * @xtype menuitem |
---|
17 | */ |
---|
18 | Ext.menu.Item = Ext.extend(Ext.menu.BaseItem, { |
---|
19 | /** |
---|
20 | * @property menu |
---|
21 | * @type Ext.menu.Menu |
---|
22 | * The submenu associated with this Item if one was configured. |
---|
23 | */ |
---|
24 | /** |
---|
25 | * @cfg {Mixed} menu (optional) Either an instance of {@link Ext.menu.Menu} or the config object for an |
---|
26 | * {@link Ext.menu.Menu} which acts as the submenu when this item is activated. |
---|
27 | */ |
---|
28 | /** |
---|
29 | * @cfg {String} icon The path to an icon to display in this item (defaults to Ext.BLANK_IMAGE_URL). If |
---|
30 | * icon is specified {@link #iconCls} should not be. |
---|
31 | */ |
---|
32 | /** |
---|
33 | * @cfg {String} iconCls A CSS class that specifies a background image that will be used as the icon for |
---|
34 | * this item (defaults to ''). If iconCls is specified {@link #icon} should not be. |
---|
35 | */ |
---|
36 | /** |
---|
37 | * @cfg {String} text The text to display in this item (defaults to ''). |
---|
38 | */ |
---|
39 | /** |
---|
40 | * @cfg {String} href The href attribute to use for the underlying anchor link (defaults to '#'). |
---|
41 | */ |
---|
42 | /** |
---|
43 | * @cfg {String} hrefTarget The target attribute to use for the underlying anchor link (defaults to ''). |
---|
44 | */ |
---|
45 | /** |
---|
46 | * @cfg {String} itemCls The default CSS class to use for menu items (defaults to 'x-menu-item') |
---|
47 | */ |
---|
48 | itemCls : 'x-menu-item', |
---|
49 | /** |
---|
50 | * @cfg {Boolean} canActivate True if this item can be visually activated (defaults to true) |
---|
51 | */ |
---|
52 | canActivate : true, |
---|
53 | /** |
---|
54 | * @cfg {Number} showDelay Length of time in milliseconds to wait before showing this item (defaults to 200) |
---|
55 | */ |
---|
56 | showDelay: 200, |
---|
57 | |
---|
58 | /** |
---|
59 | * @cfg {String} altText The altText to use for the icon, if it exists. Defaults to <tt>''</tt>. |
---|
60 | */ |
---|
61 | altText: '', |
---|
62 | |
---|
63 | // doc'd in BaseItem |
---|
64 | hideDelay: 200, |
---|
65 | |
---|
66 | // private |
---|
67 | ctype: 'Ext.menu.Item', |
---|
68 | |
---|
69 | initComponent : function(){ |
---|
70 | Ext.menu.Item.superclass.initComponent.call(this); |
---|
71 | if(this.menu){ |
---|
72 | // If array of items, turn it into an object config so we |
---|
73 | // can set the ownerCt property in the config |
---|
74 | if (Ext.isArray(this.menu)){ |
---|
75 | this.menu = { items: this.menu }; |
---|
76 | } |
---|
77 | |
---|
78 | // An object config will work here, but an instance of a menu |
---|
79 | // will have already setup its ref's and have no effect |
---|
80 | if (Ext.isObject(this.menu)){ |
---|
81 | this.menu.ownerCt = this; |
---|
82 | } |
---|
83 | |
---|
84 | this.menu = Ext.menu.MenuMgr.get(this.menu); |
---|
85 | this.menu.ownerCt = undefined; |
---|
86 | } |
---|
87 | }, |
---|
88 | |
---|
89 | // private |
---|
90 | onRender : function(container, position){ |
---|
91 | if (!this.itemTpl) { |
---|
92 | this.itemTpl = Ext.menu.Item.prototype.itemTpl = new Ext.XTemplate( |
---|
93 | '<a id="{id}" class="{cls}" hidefocus="true" unselectable="on" href="{href}"', |
---|
94 | '<tpl if="hrefTarget">', |
---|
95 | ' target="{hrefTarget}"', |
---|
96 | '</tpl>', |
---|
97 | '>', |
---|
98 | '<img alt="{altText}" src="{icon}" class="x-menu-item-icon {iconCls}"/>', |
---|
99 | '<span class="x-menu-item-text">{text}</span>', |
---|
100 | '</a>' |
---|
101 | ); |
---|
102 | } |
---|
103 | var a = this.getTemplateArgs(); |
---|
104 | this.el = position ? this.itemTpl.insertBefore(position, a, true) : this.itemTpl.append(container, a, true); |
---|
105 | this.iconEl = this.el.child('img.x-menu-item-icon'); |
---|
106 | this.textEl = this.el.child('.x-menu-item-text'); |
---|
107 | if(!this.href) { // if no link defined, prevent the default anchor event |
---|
108 | this.mon(this.el, 'click', Ext.emptyFn, null, { preventDefault: true }); |
---|
109 | } |
---|
110 | Ext.menu.Item.superclass.onRender.call(this, container, position); |
---|
111 | }, |
---|
112 | |
---|
113 | getTemplateArgs: function() { |
---|
114 | return { |
---|
115 | id: this.id, |
---|
116 | cls: this.itemCls + (this.menu ? ' x-menu-item-arrow' : '') + (this.cls ? ' ' + this.cls : ''), |
---|
117 | href: this.href || '#', |
---|
118 | hrefTarget: this.hrefTarget, |
---|
119 | icon: this.icon || Ext.BLANK_IMAGE_URL, |
---|
120 | iconCls: this.iconCls || '', |
---|
121 | text: this.itemText||this.text||' ', |
---|
122 | altText: this.altText || '' |
---|
123 | }; |
---|
124 | }, |
---|
125 | |
---|
126 | /** |
---|
127 | * Sets the text to display in this menu item |
---|
128 | * @param {String} text The text to display |
---|
129 | */ |
---|
130 | setText : function(text){ |
---|
131 | this.text = text||' '; |
---|
132 | if(this.rendered){ |
---|
133 | this.textEl.update(this.text); |
---|
134 | this.parentMenu.layout.doAutoSize(); |
---|
135 | } |
---|
136 | }, |
---|
137 | |
---|
138 | /** |
---|
139 | * Sets the CSS class to apply to the item's icon element |
---|
140 | * @param {String} cls The CSS class to apply |
---|
141 | */ |
---|
142 | setIconClass : function(cls){ |
---|
143 | var oldCls = this.iconCls; |
---|
144 | this.iconCls = cls; |
---|
145 | if(this.rendered){ |
---|
146 | this.iconEl.replaceClass(oldCls, this.iconCls); |
---|
147 | } |
---|
148 | }, |
---|
149 | |
---|
150 | //private |
---|
151 | beforeDestroy: function(){ |
---|
152 | clearTimeout(this.showTimer); |
---|
153 | clearTimeout(this.hideTimer); |
---|
154 | if (this.menu){ |
---|
155 | delete this.menu.ownerCt; |
---|
156 | this.menu.destroy(); |
---|
157 | } |
---|
158 | Ext.menu.Item.superclass.beforeDestroy.call(this); |
---|
159 | }, |
---|
160 | |
---|
161 | // private |
---|
162 | handleClick : function(e){ |
---|
163 | if(!this.href){ // if no link defined, stop the event automatically |
---|
164 | e.stopEvent(); |
---|
165 | } |
---|
166 | Ext.menu.Item.superclass.handleClick.apply(this, arguments); |
---|
167 | }, |
---|
168 | |
---|
169 | // private |
---|
170 | activate : function(autoExpand){ |
---|
171 | if(Ext.menu.Item.superclass.activate.apply(this, arguments)){ |
---|
172 | this.focus(); |
---|
173 | if(autoExpand){ |
---|
174 | this.expandMenu(); |
---|
175 | } |
---|
176 | } |
---|
177 | return true; |
---|
178 | }, |
---|
179 | |
---|
180 | // private |
---|
181 | shouldDeactivate : function(e){ |
---|
182 | if(Ext.menu.Item.superclass.shouldDeactivate.call(this, e)){ |
---|
183 | if(this.menu && this.menu.isVisible()){ |
---|
184 | return !this.menu.getEl().getRegion().contains(e.getPoint()); |
---|
185 | } |
---|
186 | return true; |
---|
187 | } |
---|
188 | return false; |
---|
189 | }, |
---|
190 | |
---|
191 | // private |
---|
192 | deactivate : function(){ |
---|
193 | Ext.menu.Item.superclass.deactivate.apply(this, arguments); |
---|
194 | this.hideMenu(); |
---|
195 | }, |
---|
196 | |
---|
197 | // private |
---|
198 | expandMenu : function(autoActivate){ |
---|
199 | if(!this.disabled && this.menu){ |
---|
200 | clearTimeout(this.hideTimer); |
---|
201 | delete this.hideTimer; |
---|
202 | if(!this.menu.isVisible() && !this.showTimer){ |
---|
203 | this.showTimer = this.deferExpand.defer(this.showDelay, this, [autoActivate]); |
---|
204 | }else if (this.menu.isVisible() && autoActivate){ |
---|
205 | this.menu.tryActivate(0, 1); |
---|
206 | } |
---|
207 | } |
---|
208 | }, |
---|
209 | |
---|
210 | // private |
---|
211 | deferExpand : function(autoActivate){ |
---|
212 | delete this.showTimer; |
---|
213 | this.menu.show(this.container, this.parentMenu.subMenuAlign || 'tl-tr?', this.parentMenu); |
---|
214 | if(autoActivate){ |
---|
215 | this.menu.tryActivate(0, 1); |
---|
216 | } |
---|
217 | }, |
---|
218 | |
---|
219 | // private |
---|
220 | hideMenu : function(){ |
---|
221 | clearTimeout(this.showTimer); |
---|
222 | delete this.showTimer; |
---|
223 | if(!this.hideTimer && this.menu && this.menu.isVisible()){ |
---|
224 | this.hideTimer = this.deferHide.defer(this.hideDelay, this); |
---|
225 | } |
---|
226 | }, |
---|
227 | |
---|
228 | // private |
---|
229 | deferHide : function(){ |
---|
230 | delete this.hideTimer; |
---|
231 | if(this.menu.over){ |
---|
232 | this.parentMenu.setActiveItem(this, false); |
---|
233 | }else{ |
---|
234 | this.menu.hide(); |
---|
235 | } |
---|
236 | } |
---|
237 | }); |
---|
238 | Ext.reg('menuitem', Ext.menu.Item); |
---|