3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
8 * @class Ext.layout.MenuLayout
9 * @extends Ext.layout.ContainerLayout
10 * <p>Layout manager used by {@link Ext.menu.Menu}. Generally this class should not need to be used directly.</p>
12 Ext.layout.MenuLayout = Ext.extend(Ext.layout.ContainerLayout, {
15 setContainer : function(ct){
16 this.monitorResize = !ct.floating;
17 // This event is only fired by the menu in IE, used so we don't couple
18 // the menu with the layout.
19 ct.on('autosize', this.doAutoSize, this);
20 Ext.layout.MenuLayout.superclass.setContainer.call(this, ct);
23 renderItem : function(c, position, target){
25 this.itemTpl = Ext.layout.MenuLayout.prototype.itemTpl = new Ext.XTemplate(
26 '<li id="{itemId}" class="{itemCls}">',
27 '<tpl if="needsIcon">',
28 '<img src="{icon}" class="{iconCls}"/>',
35 if(Ext.isNumber(position)){
36 position = target.dom.childNodes[position];
38 var a = this.getItemArgs(c);
40 // The Component's positionEl is the <li> it is rendered into
41 c.render(c.positionEl = position ?
42 this.itemTpl.insertBefore(position, a, true) :
43 this.itemTpl.append(target, a, true));
45 // Link the containing <li> to the item.
46 c.positionEl.menuItemId = c.getItemId();
48 // If rendering a regular Component, and it needs an icon,
49 // move the Component rightwards.
50 if (!a.isMenuItem && a.needsIcon) {
51 c.positionEl.addClass('x-menu-list-item-indent');
53 this.configureItem(c, position);
54 }else if(c && !this.isValidParent(c, target)){
55 if(Ext.isNumber(position)){
56 position = target.dom.childNodes[position];
58 target.dom.insertBefore(c.getActionEl().dom, position || null);
62 getItemArgs : function(c) {
63 var isMenuItem = c instanceof Ext.menu.Item;
65 isMenuItem: isMenuItem,
66 needsIcon: !isMenuItem && (c.icon || c.iconCls),
67 icon: c.icon || Ext.BLANK_IMAGE_URL,
68 iconCls: 'x-menu-item-icon ' + (c.iconCls || ''),
69 itemId: 'x-menu-el-' + c.id,
70 itemCls: 'x-menu-list-item '
74 // Valid if the Component is in a <li> which is part of our target <ul>
75 isValidParent : function(c, target) {
76 return c.el.up('li.x-menu-list-item', 5).dom.parentNode === (target.dom || target);
79 onLayout : function(ct, target){
80 this.renderAll(ct, target);
84 doAutoSize : function(){
85 var ct = this.container, w = ct.width;
90 ct.setWidth(Ext.isStrict && (Ext.isIE7 || Ext.isIE8) ? 'auto' : ct.minWidth);
91 var el = ct.getEl(), t = el.dom.offsetWidth; // force recalc
92 ct.setWidth(ct.getLayoutTarget().getWidth() + el.getFrameWidth('lr'));
97 Ext.Container.LAYOUTS['menu'] = Ext.layout.MenuLayout;
100 * @class Ext.menu.Menu
101 * @extends Ext.Container
102 * <p>A menu object. This is the container to which you may add menu items. Menu can also serve as a base class
103 * when you want a specialized menu based off of another component (like {@link Ext.menu.DateMenu} for example).</p>
104 * <p>Menus may contain either {@link Ext.menu.Item menu items}, or general {@link Ext.Component Component}s.</p>
105 * <p>To make a contained general {@link Ext.Component Component} line up with other {@link Ext.menu.Item menu items}
106 * specify <tt>iconCls: 'no-icon'</tt>. This reserves a space for an icon, and indents the Component in line
107 * with the other menu items. See {@link Ext.form.ComboBox}.{@link Ext.form.ComboBox#getListParent getListParent}
108 * for an example.</p>
109 * <p>By default, Menus are absolutely positioned, floating Components. By configuring a Menu with
110 * <b><tt>{@link #floating}:false</tt></b>, a Menu may be used as child of a Container.</p>
114 Ext.menu.Menu = Ext.extend(Ext.Container, {
116 * @cfg {Object} defaults
117 * A config object that will be applied to all items added to this container either via the {@link #items}
118 * config or via the {@link #add} method. The defaults config can contain any number of
119 * name/value property pairs to be added to each item, and should be valid for the types of items
120 * being added to the menu.
124 * An array of items to be added to this menu. Menus may contain either {@link Ext.menu.Item menu items},
125 * or general {@link Ext.Component Component}s.
128 * @cfg {Number} minWidth The minimum width of the menu in pixels (defaults to 120)
132 * @cfg {Boolean/String} shadow True or 'sides' for the default effect, 'frame' for 4-way shadow, and 'drop'
133 * for bottom-right shadow (defaults to 'sides')
137 * @cfg {String} subMenuAlign The {@link Ext.Element#alignTo} anchor position value to use for submenus of
138 * this menu (defaults to 'tl-tr?')
140 subMenuAlign : 'tl-tr?',
142 * @cfg {String} defaultAlign The default {@link Ext.Element#alignTo} anchor position value for this menu
143 * relative to its element of origin (defaults to 'tl-bl?')
145 defaultAlign : 'tl-bl?',
147 * @cfg {Boolean} allowOtherMenus True to allow multiple menus to be displayed at the same time (defaults to false)
149 allowOtherMenus : false,
151 * @cfg {Boolean} ignoreParentClicks True to ignore clicks on any item in this menu that is a parent item (displays
152 * a submenu) so that the submenu is not dismissed when clicking the parent item (defaults to false).
154 ignoreParentClicks : false,
156 * @cfg {Boolean} enableScrolling True to allow the menu container to have scroller controls if the menu is too long (defaults to true).
158 enableScrolling : true,
160 * @cfg {Number} maxHeight The maximum height of the menu. Only applies when enableScrolling is set to True (defaults to null).
164 * @cfg {Number} scrollIncrement The amount to scroll the menu. Only applies when enableScrolling is set to True (defaults to 24).
166 scrollIncrement : 24,
168 * @cfg {Boolean} showSeparator True to show the icon separator. (defaults to true).
170 showSeparator : true,
172 * @cfg {Array} defaultOffsets An array specifying the [x, y] offset in pixels by which to
173 * change the default Menu popup position after aligning according to the {@link #defaultAlign}
174 * configuration. Defaults to <tt>[0, 0]</tt>.
176 defaultOffsets : [0, 0],
179 * @cfg {Boolean} plain
180 * True to remove the incised line down the left side of the menu. Defaults to <tt>false</tt>.
185 * @cfg {Boolean} floating
186 * <p>By default, a Menu configured as <b><code>floating:true</code></b>
187 * will be rendered as an {@link Ext.Layer} (an absolutely positioned,
188 * floating Component with zindex=15000).
189 * If configured as <b><code>floating:false</code></b>, the Menu may be
190 * used as child item of another Container instead of a free-floating
191 * {@link Ext.Layer Layer}.
199 * @cfg {String/Object} layout
200 * This class assigns a default layout (<code>layout:'<b>menu</b>'</code>).
201 * Developers <i>may</i> override this configuration option if another layout is required.
202 * See {@link Ext.Container#layout} for additional information.
205 hideMode : 'offsets', // Important for laying out Components
207 autoLayout : true, // Provided for backwards compat
208 defaultType : 'menuitem',
209 bufferResize : false,
211 initComponent : function(){
212 if(Ext.isArray(this.initialConfig)){
213 Ext.apply(this, {items:this.initialConfig});
218 * Fires when this menu is clicked (or when the enter key is pressed while it is active)
219 * @param {Ext.menu.Menu} this
220 * @param {Ext.menu.Item} menuItem The menu item that was clicked
221 * @param {Ext.EventObject} e
226 * Fires when the mouse is hovering over this menu
227 * @param {Ext.menu.Menu} this
228 * @param {Ext.EventObject} e
229 * @param {Ext.menu.Item} menuItem The menu item that was clicked
234 * Fires when the mouse exits this menu
235 * @param {Ext.menu.Menu} this
236 * @param {Ext.EventObject} e
237 * @param {Ext.menu.Item} menuItem The menu item that was clicked
242 * Fires when a menu item contained in this menu is clicked
243 * @param {Ext.menu.BaseItem} baseItem The BaseItem that was clicked
244 * @param {Ext.EventObject} e
248 Ext.menu.MenuMgr.register(this);
250 Ext.EventManager.onWindowResize(this.hide, this);
252 if(this.initialConfig.hidden !== false){
255 this.internalDefaults = {hideOnClick: false};
257 Ext.menu.Menu.superclass.initComponent.call(this);
261 remove: this.doLayout,
268 getLayoutTarget : function() {
273 onRender : function(ct, position){
280 cls: 'x-menu ' + ((this.floating) ? 'x-menu-floating x-layer ' : '') + (this.cls || '') + (this.plain ? ' x-menu-plain' : '') + (this.showSeparator ? '' : ' x-menu-nosep'),
283 {tag: 'a', cls: 'x-menu-focus', href: '#', onclick: 'return false;', tabIndex: '-1'},
284 {tag: 'ul', cls: 'x-menu-list'}
288 this.el = new Ext.Layer({
296 this.el = ct.createChild(dh);
298 Ext.menu.Menu.superclass.onRender.call(this, ct, position);
301 this.keyNav = new Ext.menu.MenuNav(this);
303 // generic focus element
304 this.focusEl = this.el.child('a.x-menu-focus');
305 this.ul = this.el.child('ul.x-menu-list');
309 mouseover: this.onMouseOver,
310 mouseout: this.onMouseOut
312 if(this.enableScrolling){
315 delegate: '.x-menu-scroller',
316 click: this.onScroll,
317 mouseover: this.deactivateActive
323 findTargetItem : function(e){
324 var t = e.getTarget('.x-menu-list-item', this.ul, true);
325 if(t && t.menuItemId){
326 return this.items.get(t.menuItemId);
331 onClick : function(e){
332 var t = this.findTargetItem(e);
335 this.setActiveItem(t);
336 }else if(t instanceof Ext.menu.BaseItem){
337 if(t.menu && this.ignoreParentClicks){
342 this.fireEvent('click', this, t, e);
349 setActiveItem : function(item, autoExpand){
350 if(item != this.activeItem){
351 this.deactivateActive();
352 if((this.activeItem = item).isFormField){
355 item.activate(autoExpand);
357 }else if(autoExpand){
362 deactivateActive : function(){
363 var a = this.activeItem;
366 //Fields cannot deactivate, but Combos must collapse
373 delete this.activeItem;
378 tryActivate : function(start, step){
379 var items = this.items;
380 for(var i = start, len = items.length; i >= 0 && i < len; i+= step){
381 var item = items.get(i);
382 if(!item.disabled && (item.canActivate || item.isFormField)){
383 this.setActiveItem(item, false);
391 onMouseOver : function(e){
392 var t = this.findTargetItem(e);
394 if(t.canActivate && !t.disabled){
395 this.setActiveItem(t, true);
399 this.fireEvent('mouseover', this, e, t);
403 onMouseOut : function(e){
404 var t = this.findTargetItem(e);
406 if(t == this.activeItem && t.shouldDeactivate && t.shouldDeactivate(e)){
407 this.activeItem.deactivate();
408 delete this.activeItem;
412 this.fireEvent('mouseout', this, e, t);
416 onScroll : function(e, t){
420 var ul = this.ul.dom, top = Ext.fly(t).is('.x-menu-scroller-top');
421 ul.scrollTop += this.scrollIncrement * (top ? -1 : 1);
422 if(top ? ul.scrollTop <= 0 : ul.scrollTop + this.activeMax >= ul.scrollHeight){
423 this.onScrollerOut(null, t);
428 onScrollerIn : function(e, t){
429 var ul = this.ul.dom, top = Ext.fly(t).is('.x-menu-scroller-top');
430 if(top ? ul.scrollTop > 0 : ul.scrollTop + this.activeMax < ul.scrollHeight){
431 Ext.fly(t).addClass(['x-menu-item-active', 'x-menu-scroller-active']);
436 onScrollerOut : function(e, t){
437 Ext.fly(t).removeClass(['x-menu-item-active', 'x-menu-scroller-active']);
441 * If <code>{@link #floating}=true</code>, shows this menu relative to
442 * another element using {@link #showat}, otherwise uses {@link Ext.Component#show}.
443 * @param {Mixed} element The element to align to
444 * @param {String} position (optional) The {@link Ext.Element#alignTo} anchor position to use in aligning to
445 * the element (defaults to this.defaultAlign)
446 * @param {Ext.menu.Menu} parentMenu (optional) This menu's parent menu, if applicable (defaults to undefined)
448 show : function(el, pos, parentMenu){
450 this.parentMenu = parentMenu;
453 this.doLayout(false, true);
455 this.showAt(this.el.getAlignToXY(el, pos || this.defaultAlign, this.defaultOffsets), parentMenu);
457 Ext.menu.Menu.superclass.show.call(this);
462 * Displays this menu at a specific xy position and fires the 'show' event if a
463 * handler for the 'beforeshow' event does not return false cancelling the operation.
464 * @param {Array} xyPosition Contains X & Y [x, y] values for the position at which to show the menu (coordinates are page-based)
465 * @param {Ext.menu.Menu} parentMenu (optional) This menu's parent menu, if applicable (defaults to undefined)
467 showAt : function(xy, parentMenu){
468 if(this.fireEvent('beforeshow', this) !== false){
469 this.parentMenu = parentMenu;
473 if(this.enableScrolling){
474 // set the position so we can figure out the constrain value.
476 //constrain the value, keep the y coordinate the same
477 this.constrainScroll(xy[1]);
478 xy = [this.el.adjustForConstraints(xy)[0], xy[1]];
480 //constrain to the viewport.
481 xy = this.el.adjustForConstraints(xy);
485 Ext.menu.Menu.superclass.onShow.call(this);
487 // internal event, used so we don't couple the layout to the menu
488 this.fireEvent('autosize', this);
495 this.fireEvent('show', this);
499 constrainScroll : function(y){
500 var max, full = this.ul.setHeight('auto').getHeight();
502 max = this.maxHeight ? this.maxHeight : Ext.fly(this.el.dom.parentNode).getViewSize(false).height - y;
504 max = this.getHeight();
506 if(full > max && max > 0){
507 this.activeMax = max - this.scrollerHeight * 2 - this.el.getFrameWidth('tb') - Ext.num(this.el.shadowOffset, 0);
508 this.ul.setHeight(this.activeMax);
509 this.createScrollers();
510 this.el.select('.x-menu-scroller').setDisplayed('');
512 this.ul.setHeight(full);
513 this.el.select('.x-menu-scroller').setDisplayed('none');
515 this.ul.dom.scrollTop = 0;
518 createScrollers : function(){
522 top: this.el.insertFirst({
524 cls: 'x-menu-scroller x-menu-scroller-top',
527 bottom: this.el.createChild({
529 cls: 'x-menu-scroller x-menu-scroller-bottom',
533 this.scroller.top.hover(this.onScrollerIn, this.onScrollerOut, this);
534 this.scroller.topRepeater = new Ext.util.ClickRepeater(this.scroller.top, {
536 click: this.onScroll.createDelegate(this, [null, this.scroller.top], false)
539 this.scroller.bottom.hover(this.onScrollerIn, this.onScrollerOut, this);
540 this.scroller.bottomRepeater = new Ext.util.ClickRepeater(this.scroller.bottom, {
542 click: this.onScroll.createDelegate(this, [null, this.scroller.bottom], false)
548 onLayout : function(){
549 if(this.isVisible()){
550 if(this.enableScrolling){
551 this.constrainScroll(this.el.getTop());
561 this.doFocus.defer(50, this);
565 doFocus : function(){
567 this.focusEl.focus();
572 * Hides this menu and optionally all parent menus
573 * @param {Boolean} deep (optional) True to hide all parent menus recursively, if any (defaults to false)
575 hide : function(deep){
576 this.deepHide = deep;
577 Ext.menu.Menu.superclass.hide.call(this);
578 delete this.deepHide;
583 Ext.menu.Menu.superclass.onHide.call(this);
584 this.deactivateActive();
585 if(this.el && this.floating){
588 var pm = this.parentMenu;
589 if(this.deepHide === true && pm){
593 pm.deactivateActive();
599 lookupComponent : function(c){
601 c = (c == 'separator' || c == '-') ? new Ext.menu.Separator() : new Ext.menu.TextItem(c);
602 this.applyDefaults(c);
605 c = this.getMenuItem(c);
606 }else if(c.tagName || c.el){ // element. Wrap it.
607 c = new Ext.BoxComponent({
615 applyDefaults : function(c){
616 if(!Ext.isString(c)){
617 c = Ext.menu.Menu.superclass.applyDefaults.call(this, c);
618 var d = this.internalDefaults;
621 Ext.applyIf(c.initialConfig, d);
632 getMenuItem : function(config){
634 if(!config.xtype && Ext.isBoolean(config.checked)){
635 return new Ext.menu.CheckItem(config)
637 return Ext.create(config, this.defaultType);
643 * Adds a separator bar to the menu
644 * @return {Ext.menu.Item} The menu item that was added
646 addSeparator : function(){
647 return this.add(new Ext.menu.Separator());
651 * Adds an {@link Ext.Element} object to the menu
652 * @param {Mixed} el The element or DOM node to add, or its id
653 * @return {Ext.menu.Item} The menu item that was added
655 addElement : function(el){
656 return this.add(new Ext.menu.BaseItem(el));
660 * Adds an existing object based on {@link Ext.menu.BaseItem} to the menu
661 * @param {Ext.menu.Item} item The menu item to add
662 * @return {Ext.menu.Item} The menu item that was added
664 addItem : function(item){
665 return this.add(item);
669 * Creates a new {@link Ext.menu.Item} based an the supplied config object and adds it to the menu
670 * @param {Object} config A MenuItem config object
671 * @return {Ext.menu.Item} The menu item that was added
673 addMenuItem : function(config){
674 return this.add(this.getMenuItem(config));
678 * Creates a new {@link Ext.menu.TextItem} with the supplied text and adds it to the menu
679 * @param {String} text The text to display in the menu item
680 * @return {Ext.menu.Item} The menu item that was added
682 addText : function(text){
683 return this.add(new Ext.menu.TextItem(text));
687 onDestroy : function(){
688 var pm = this.parentMenu;
689 if(pm && pm.activeChild == this){
690 delete pm.activeChild;
692 delete this.parentMenu;
693 Ext.menu.Menu.superclass.onDestroy.call(this);
694 Ext.menu.MenuMgr.unregister(this);
695 Ext.EventManager.removeResizeListener(this.hide, this);
697 this.keyNav.disable();
699 var s = this.scroller;
701 Ext.destroy(s.topRepeater, s.bottomRepeater, s.top, s.bottom);
711 Ext.reg('menu', Ext.menu.Menu);
713 // MenuNav is a private utility class used internally by the Menu
714 Ext.menu.MenuNav = Ext.extend(Ext.KeyNav, function(){
716 if(!m.tryActivate(m.items.indexOf(m.activeItem)-1, -1)){
717 m.tryActivate(m.items.length-1, -1);
721 if(!m.tryActivate(m.items.indexOf(m.activeItem)+1, 1)){
726 constructor : function(menu){
727 Ext.menu.MenuNav.superclass.constructor.call(this, menu.el);
728 this.scope = this.menu = menu;
731 doRelay : function(e, h){
733 // Keystrokes within a form Field (e.g.: down in a Combo) do not navigate. Allow only TAB
734 if (this.menu.activeItem && this.menu.activeItem.isFormField && k != e.TAB) {
737 if(!this.menu.activeItem && e.isNavKeyPress() && k != e.SPACE && k != e.RETURN){
738 this.menu.tryActivate(0, 1);
741 return h.call(this.scope || this, e, this.menu);
744 tab: function(e, m) {
757 right : function(e, m){
759 m.activeItem.expandMenu(true);
763 left : function(e, m){
765 if(m.parentMenu && m.parentMenu.activeItem){
766 m.parentMenu.activeItem.activate();
770 enter : function(e, m){
773 m.activeItem.onClick(e);
774 m.fireEvent('click', this, m.activeItem);