3 * @extends Ext.panel.Panel
5 * A menu object. This is the container to which you may add {@link Ext.menu.Item menu items}.
7 * Menus may contain either {@link Ext.menu.Item menu items}, or general {@link Ext.Component Components}.
8 * Menus may also contain {@link Ext.panel.AbstractPanel#dockedItems docked items} because it extends {@link Ext.panel.Panel}.
10 * To make a contained general {@link Ext.Component Component} line up with other {@link Ext.menu.Item menu items},
11 * specify `{@link Ext.menu.Item#iconCls iconCls}: 'no-icon'` _or_ `{@link Ext.menu.Item#indent indent}: true`.
12 * This reserves a space for an icon, and indents the Component in line with the other menu items.
13 * See {@link Ext.form.field.ComboBox}.{@link Ext.form.field.ComboBox#getListParent getListParent} for an example.
15 * By default, Menus are absolutely positioned, floating Components. By configuring a Menu with `{@link #floating}:false`,
16 * a Menu may be used as a child of a {@link Ext.container.Container Container}.
17 * {@img Ext.menu.Item/Ext.menu.Item.png Ext.menu.Item component}
19 Ext.create('Ext.menu.Menu', {
23 floating: false, // usually you want this set to True (default)
24 renderTo: Ext.getBody(), // usually rendered by it's containing component
26 text: 'regular item 1'
28 text: 'regular item 2'
30 text: 'regular item 3'
34 Ext.create('Ext.menu.Menu', {
38 floating: false, // usually you want this set to True (default)
39 renderTo: Ext.getBody(), // usually rendered by it's containing component
51 * @param {Object} config The config object
53 Ext.define('Ext.menu.Menu', {
54 extend: 'Ext.panel.Panel',
57 'Ext.layout.container.Fit',
58 'Ext.layout.container.VBox',
67 * @cfg {Boolean} allowOtherMenus
68 * True to allow multiple menus to be displayed at the same time. Defaults to `false`.
71 allowOtherMenus: false,
74 * @cfg {String} ariaRole @hide
79 * @cfg {Boolean} autoRender @hide
80 * floating is true, so autoRender always happens
84 * @cfg {String} defaultAlign
85 * The default {@link Ext.core.Element#getAlignToXY Ext.core.Element#getAlignToXY} anchor position value for this menu
86 * relative to its element of origin. Defaults to `'tl-bl?'`.
89 defaultAlign: 'tl-bl?',
92 * @cfg {Boolean} floating
93 * A Menu configured as `floating: true` (the default) will be rendered as an absolutely positioned,
94 * {@link Ext.Component#floating floating} {@link Ext.Component Component}. If configured as `floating: false`, the Menu may be
95 * used as a child item of another {@link Ext.container.Container Container}.
101 * @cfg {Boolean} @hide
102 * Menu performs its own size changing constraining, so ensure Component's constraining is not applied
107 * @cfg {Boolean} hidden
108 * True to initially render the Menu as hidden, requiring to be shown manually.
109 * Defaults to `true` when `floating: true`, and defaults to `false` when `floating: false`.
115 * @cfg {Boolean} ignoreParentClicks
116 * True to ignore clicks on any item in this menu that is a parent item (displays a submenu)
117 * so that the submenu is not dismissed when clicking the parent item. Defaults to `false`.
120 ignoreParentClicks: false,
125 * @cfg {String/Object} layout @hide
129 * @cfg {Boolean} showSeparator True to show the icon separator. (defaults to true).
131 showSeparator : true,
134 * @cfg {Number} minWidth
135 * The minimum width of the Menu. Defaults to `120`.
141 * @cfg {Boolean} plain
142 * True to remove the incised line down the left side of the menu and to not
143 * indent general Component items. Defaults to `false`.
147 initComponent: function() {
149 prefix = Ext.baseCSSPrefix,
150 cls = [prefix + 'menu'],
151 bodyCls = me.bodyCls ? [me.bodyCls] : [];
156 * Fires when this menu is clicked
157 * @param {Ext.menu.Menu} menu The menu which has been clicked
158 * @param {Ext.Component} item The menu item that was clicked. `undefined` if not applicable.
159 * @param {Ext.EventObject} e The underlying {@link Ext.EventObject}.
166 * Fires when the mouse enters this menu
167 * @param {Ext.menu.Menu} menu The menu
168 * @param {Ext.EventObject} e The underlying {@link Ext.EventObject}
175 * Fires when the mouse leaves this menu
176 * @param {Ext.menu.Menu} menu The menu
177 * @param {Ext.EventObject} e The underlying {@link Ext.EventObject}
184 * Fires when the mouse is hovering over this menu
185 * @param {Ext.menu.Menu} menu The menu
186 * @param {Ext.Component} item The menu item that the mouse is over. `undefined` if not applicable.
187 * @param {Ext.EventObject} e The underlying {@link Ext.EventObject}
192 Ext.menu.Manager.register(me);
196 cls.push(prefix + 'menu-plain');
198 me.cls = cls.join(' ');
201 bodyCls.unshift(prefix + 'menu-body');
202 me.bodyCls = bodyCls.join(' ');
204 // Internal vbox layout, with scrolling overflow
205 // Placed in initComponent (rather than prototype) in order to support dynamic layout/scroller
206 // options if we wish to allow for such configurations on the Menu.
207 // e.g., scrolling speed, vbox align stretch, etc.
212 clearInnerCtOnLayout: true,
213 overflowHandler: 'Scroller'
216 // hidden defaults to false if floating is configured as false
217 if (me.floating === false && me.initialConfig.hidden !== true) {
221 me.callParent(arguments);
223 me.on('beforeshow', function() {
224 var hasItems = !!me.items.length;
225 // FIXME: When a menu has its show cancelled because of no items, it
226 // gets a visibility: hidden applied to it (instead of the default display: none)
227 // Not sure why, but we remove this style when we want to show again.
228 if (hasItems && me.rendered) {
229 me.el.setStyle('visibility', null);
235 afterRender: function(ct) {
237 prefix = Ext.baseCSSPrefix,
240 me.callParent(arguments);
242 // TODO: Move this to a subTemplate When we support them in the future
243 if (me.showSeparator) {
244 me.iconSepEl = me.layout.getRenderTarget().insertFirst({
245 cls: prefix + 'menu-icon-separator',
250 me.focusEl = me.el.createChild({
251 cls: prefix + 'menu-focus',
258 mouseover: me.onMouseOver,
261 me.mouseMonitor = me.el.monitorMouseLeave(100, me.onMouseLeave, me);
263 if (me.showSeparator && ((!Ext.isStrict && Ext.isIE) || Ext.isIE6)) {
264 me.iconSepEl.setHeight(me.el.getHeight());
267 me.keyNav = Ext.create('Ext.menu.KeyNav', me);
270 afterLayout: function() {
272 me.callParent(arguments);
274 // For IE6 & IE quirks, we have to resize the el and body since position: absolute
275 // floating elements inherit their parent's width, making them the width of
276 // document.body instead of the width of their contents.
277 // This includes left/right dock items.
278 if ((!Ext.iStrict && Ext.isIE) || Ext.isIE6) {
279 var innerCt = me.layout.getRenderTarget(),
281 dis = me.dockedItems,
286 innerCtWidth = innerCt.getWidth();
288 newWidth = innerCtWidth + me.body.getBorderWidth('lr') + me.body.getPadding('lr');
290 // First set the body to the new width
291 me.body.setWidth(newWidth);
293 // Now we calculate additional width (docked items) and set the el's width
294 for (; i < l, di = dis.getAt(i); i++) {
295 if (di.dock == 'left' || di.dock == 'right') {
296 newWidth += di.getWidth();
299 me.el.setWidth(newWidth);
304 * Returns whether a menu item can be activated or not.
307 canActivateItem: function(item) {
308 return item && !item.isDisabled() && item.isVisible() && (item.canActivate || item.getXTypes().indexOf('menuitem') < 0);
312 * Deactivates the current active item on the menu, if one exists.
314 deactivateActiveItem: function() {
318 me.activeItem.deactivate();
319 if (!me.activeItem.activated) {
320 delete me.activeItem;
323 if (me.focusedItem) {
324 me.focusedItem.blur();
325 if (!me.focusedItem.$focused) {
326 delete me.focusedItem;
332 getFocusEl: function() {
338 this.deactivateActiveItem();
339 this.callParent(arguments);
343 getItemFromEvent: function(e) {
344 return this.getChildByElement(e.getTarget());
347 lookupComponent: function(cmp) {
350 if (Ext.isString(cmp)) {
351 cmp = me.lookupItemFromString(cmp);
352 } else if (Ext.isObject(cmp)) {
353 cmp = me.lookupItemFromObject(cmp);
356 // Apply our minWidth to all of our child components so it's accounted
357 // for in our VBox layout
358 cmp.minWidth = cmp.minWidth || me.minWidth;
364 lookupItemFromObject: function(cmp) {
366 prefix = Ext.baseCSSPrefix,
370 if (!cmp.isComponent) {
372 cmp = Ext.create('Ext.menu.' + (Ext.isBoolean(cmp.checked) ? 'Check': '') + 'Item', cmp);
374 cmp = Ext.ComponentManager.create(cmp, cmp.xtype);
378 if (cmp.isMenuItem) {
382 if (!cmp.isMenuItem && !cmp.dock) {
383 cls = [prefix + 'menu-item', prefix + 'menu-item-cmp'];
384 intercept = Ext.Function.createInterceptor;
386 // Wrap focus/blur to control component focus
387 cmp.focus = intercept(cmp.focus, function() {
388 this.$focused = true;
390 cmp.blur = intercept(cmp.blur, function() {
391 this.$focused = false;
394 if (!me.plain && (cmp.indent === true || cmp.iconCls === 'no-icon')) {
395 cls.push(prefix + 'menu-item-indent');
401 cmp.cls = (cmp.cls ? cmp.cls : '') + ' ' + cls.join(' ');
403 cmp.isMenuItem = true;
409 lookupItemFromString: function(cmp) {
410 return (cmp == 'separator' || cmp == '-') ?
411 Ext.createWidget('menuseparator')
412 : Ext.createWidget('menuitem', {
420 onClick: function(e) {
429 if ((e.getTarget() == me.focusEl.dom) || e.within(me.layout.getRenderTarget())) {
430 item = me.getItemFromEvent(e) || me.activeItem;
433 if (item.getXTypes().indexOf('menuitem') >= 0) {
434 if (!item.menu || !me.ignoreParentClicks) {
441 me.fireEvent('click', me, item, e);
445 onDestroy: function() {
448 Ext.menu.Manager.unregister(me);
450 me.el.un(me.mouseMonitor);
454 me.callParent(arguments);
457 onMouseLeave: function(e) {
460 me.deactivateActiveItem();
466 me.fireEvent('mouseleave', me, e);
469 onMouseOver: function(e) {
471 fromEl = e.getRelatedTarget(),
472 mouseEnter = !me.el.contains(fromEl),
473 item = me.getItemFromEvent(e);
475 if (mouseEnter && me.parentMenu) {
476 me.parentMenu.setActiveItem(me.parentItem);
477 me.parentMenu.mouseMonitor.mouseenter();
485 me.setActiveItem(item);
486 if (item.activated && item.expandMenu) {
491 me.fireEvent('mouseenter', me, e);
493 me.fireEvent('mouseover', me, item, e);
496 setActiveItem: function(item) {
499 if (item && (item != me.activeItem && item != me.focusedItem)) {
500 me.deactivateActiveItem();
501 if (me.canActivateItem(item)) {
504 if (item.activated) {
505 me.activeItem = item;
506 me.focusedItem = item;
511 me.focusedItem = item;
514 item.el.scrollIntoView(me.layout.getRenderTarget());
519 * Shows the floating menu by the specified {@link Ext.Component Component} or {@link Ext.core.Element Element}.
520 * @param {Mixed component} The {@link Ext.Component} or {@link Ext.core.Element} to show the menu by.
521 * @param {String} position (optional) Alignment position as used by {@link Ext.core.Element#getAlignToXY Ext.core.Element.getAlignToXY}. Defaults to `{@link #defaultAlign}`.
522 * @param {Array} offsets (optional) Alignment offsets as used by {@link Ext.core.Element#getAlignToXY Ext.core.Element.getAlignToXY}. Defaults to `undefined`.
523 * @return {Menu} This Menu.
526 showBy: function(cmp, pos, off) {
531 if (me.floating && cmp) {
532 me.layout.autoSize = true;
535 // Component or Element
538 // Convert absolute to floatParent-relative coordinates if necessary.
539 xy = me.el.getAlignToXY(cmp, pos || me.defaultAlign, off);
540 if (me.floatParent) {
541 region = me.floatParent.getTargetEl().getViewRegion();
552 this.callParent(arguments);
558 doConstrain : function() {
563 returnY = y, normalY, parentEl, scrollTop, viewHeight;
567 full = me.getHeight();
569 parentEl = Ext.fly(me.el.dom.parentNode);
570 scrollTop = parentEl.getScroll().top;
571 viewHeight = parentEl.getViewSize().height;
572 //Normalize y by the scroll position for the parent element. Need to move it into the coordinate space
574 normalY = y - scrollTop;
575 max = me.maxHeight ? me.maxHeight : viewHeight - normalY;
576 if (full > viewHeight) {
578 //Set returnY equal to (0,0) in view space by reducing y by the value of normalY
579 returnY = y - normalY;
580 } else if (max < full) {
581 returnY = y - (full - max);
585 max = me.getHeight();
587 // Always respect maxHeight
589 max = Math.min(me.maxHeight, max);
591 if (full > max && max > 0){
592 me.layout.autoSize = false;
594 if (me.showSeparator){
595 me.iconSepEl.setHeight(me.layout.getRenderTarget().dom.scrollHeight);
598 vector = me.getConstrainVector();
600 me.setPosition(me.getPosition()[0] + vector[0]);