3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
8 * @class Ext.layout.MenuLayout
\r
9 * @extends Ext.layout.ContainerLayout
\r
10 * <p>Layout manager used by {@link Ext.menu.Menu}. Generally this class should not need to be used directly.</p>
\r
12 Ext.layout.MenuLayout = Ext.extend(Ext.layout.ContainerLayout, {
\r
13 monitorResize : true,
\r
15 setContainer : function(ct){
\r
16 this.monitorResize = !ct.floating;
\r
17 // This event is only fired by the menu in IE, used so we don't couple
\r
18 // the menu with the layout.
\r
19 ct.on('autosize', this.doAutoSize, this);
\r
20 Ext.layout.MenuLayout.superclass.setContainer.call(this, ct);
\r
23 renderItem : function(c, position, target){
\r
24 if (!this.itemTpl) {
\r
25 this.itemTpl = Ext.layout.MenuLayout.prototype.itemTpl = new Ext.XTemplate(
\r
26 '<li id="{itemId}" class="{itemCls}">',
\r
27 '<tpl if="needsIcon">',
\r
28 '<img src="{icon}" class="{iconCls}"/>',
\r
34 if(c && !c.rendered){
\r
35 if(Ext.isNumber(position)){
\r
36 position = target.dom.childNodes[position];
\r
38 var a = this.getItemArgs(c);
\r
40 // The Component's positionEl is the <li> it is rendered into
\r
41 c.render(c.positionEl = position ?
\r
42 this.itemTpl.insertBefore(position, a, true) :
\r
43 this.itemTpl.append(target, a, true));
\r
45 // Link the containing <li> to the item.
\r
46 c.positionEl.menuItemId = c.getItemId();
\r
48 // If rendering a regular Component, and it needs an icon,
\r
49 // move the Component rightwards.
\r
50 if (!a.isMenuItem && a.needsIcon) {
\r
51 c.positionEl.addClass('x-menu-list-item-indent');
\r
53 this.configureItem(c, position);
\r
54 }else if(c && !this.isValidParent(c, target)){
\r
55 if(Ext.isNumber(position)){
\r
56 position = target.dom.childNodes[position];
\r
58 target.dom.insertBefore(c.getActionEl().dom, position || null);
\r
62 getItemArgs : function(c) {
\r
63 var isMenuItem = c instanceof Ext.menu.Item;
\r
65 isMenuItem: isMenuItem,
\r
66 needsIcon: !isMenuItem && (c.icon || c.iconCls),
\r
67 icon: c.icon || Ext.BLANK_IMAGE_URL,
\r
68 iconCls: 'x-menu-item-icon ' + (c.iconCls || ''),
\r
69 itemId: 'x-menu-el-' + c.id,
\r
70 itemCls: 'x-menu-list-item '
\r
74 // Valid if the Component is in a <li> which is part of our target <ul>
\r
75 isValidParent : function(c, target) {
\r
76 return c.el.up('li.x-menu-list-item', 5).dom.parentNode === (target.dom || target);
\r
79 onLayout : function(ct, target){
\r
80 this.renderAll(ct, target);
\r
84 doAutoSize : function(){
\r
85 var ct = this.container, w = ct.width;
\r
90 ct.setWidth(Ext.isStrict && (Ext.isIE7 || Ext.isIE8) ? 'auto' : ct.minWidth);
\r
91 var el = ct.getEl(), t = el.dom.offsetWidth; // force recalc
\r
92 ct.setWidth(ct.getLayoutTarget().getWidth() + el.getFrameWidth('lr'));
\r
97 Ext.Container.LAYOUTS['menu'] = Ext.layout.MenuLayout;
\r
100 * @class Ext.menu.Menu
\r
101 * @extends Ext.Container
\r
102 * <p>A menu object. This is the container to which you may add menu items. Menu can also serve as a base class
\r
103 * when you want a specialized menu based off of another component (like {@link Ext.menu.DateMenu} for example).</p>
\r
104 * <p>Menus may contain either {@link Ext.menu.Item menu items}, or general {@link Ext.Component Component}s.</p>
\r
105 * <p>To make a contained general {@link Ext.Component Component} line up with other {@link Ext.menu.Item menu items}
\r
106 * specify <tt>iconCls: 'no-icon'</tt>. This reserves a space for an icon, and indents the Component in line
\r
107 * with the other menu items. See {@link Ext.form.ComboBox}.{@link Ext.form.ComboBox#getListParent getListParent}
\r
108 * for an example.</p>
\r
109 * <p>By default, Menus are absolutely positioned, floating Components. By configuring a Menu with
\r
110 * <b><tt>{@link #floating}:false</tt></b>, a Menu may be used as child of a Container.</p>
\r
114 Ext.menu.Menu = Ext.extend(Ext.Container, {
\r
116 * @cfg {Object} defaults
\r
117 * A config object that will be applied to all items added to this container either via the {@link #items}
\r
118 * config or via the {@link #add} method. The defaults config can contain any number of
\r
119 * name/value property pairs to be added to each item, and should be valid for the types of items
\r
120 * being added to the menu.
\r
123 * @cfg {Mixed} items
\r
124 * An array of items to be added to this menu. Menus may contain either {@link Ext.menu.Item menu items},
\r
125 * or general {@link Ext.Component Component}s.
\r
128 * @cfg {Number} minWidth The minimum width of the menu in pixels (defaults to 120)
\r
132 * @cfg {Boolean/String} shadow True or 'sides' for the default effect, 'frame' for 4-way shadow, and 'drop'
\r
133 * for bottom-right shadow (defaults to 'sides')
\r
137 * @cfg {String} subMenuAlign The {@link Ext.Element#alignTo} anchor position value to use for submenus of
\r
138 * this menu (defaults to 'tl-tr?')
\r
140 subMenuAlign : 'tl-tr?',
\r
142 * @cfg {String} defaultAlign The default {@link Ext.Element#alignTo} anchor position value for this menu
\r
143 * relative to its element of origin (defaults to 'tl-bl?')
\r
145 defaultAlign : 'tl-bl?',
\r
147 * @cfg {Boolean} allowOtherMenus True to allow multiple menus to be displayed at the same time (defaults to false)
\r
149 allowOtherMenus : false,
\r
151 * @cfg {Boolean} ignoreParentClicks True to ignore clicks on any item in this menu that is a parent item (displays
\r
152 * a submenu) so that the submenu is not dismissed when clicking the parent item (defaults to false).
\r
154 ignoreParentClicks : false,
\r
156 * @cfg {Boolean} enableScrolling True to allow the menu container to have scroller controls if the menu is too long (defaults to true).
\r
158 enableScrolling : true,
\r
160 * @cfg {Number} maxHeight The maximum height of the menu. Only applies when enableScrolling is set to True (defaults to null).
\r
164 * @cfg {Number} scrollIncrement The amount to scroll the menu. Only applies when enableScrolling is set to True (defaults to 24).
\r
166 scrollIncrement : 24,
\r
168 * @cfg {Boolean} showSeparator True to show the icon separator. (defaults to true).
\r
170 showSeparator : true,
\r
172 * @cfg {Array} defaultOffsets An array specifying the [x, y] offset in pixels by which to
\r
173 * change the default Menu popup position after aligning according to the {@link #defaultAlign}
\r
174 * configuration. Defaults to <tt>[0, 0]</tt>.
\r
176 defaultOffsets : [0, 0],
\r
179 * @cfg {Boolean} plain
\r
180 * True to remove the incised line down the left side of the menu. Defaults to <tt>false</tt>.
\r
185 * @cfg {Boolean} floating
\r
186 * <p>By default, a Menu configured as <b><code>floating:true</code></b>
\r
187 * will be rendered as an {@link Ext.Layer} (an absolutely positioned,
\r
188 * floating Component with zindex=15000).
\r
189 * If configured as <b><code>floating:false</code></b>, the Menu may be
\r
190 * used as child item of another Container instead of a free-floating
\r
191 * {@link Ext.Layer Layer}.
\r
199 * @cfg {String/Object} layout
\r
200 * This class assigns a default layout (<code>layout:'<b>menu</b>'</code>).
\r
201 * Developers <i>may</i> override this configuration option if another layout is required.
\r
202 * See {@link Ext.Container#layout} for additional information.
\r
205 hideMode : 'offsets', // Important for laying out Components
\r
206 scrollerHeight : 8,
\r
207 autoLayout : true, // Provided for backwards compat
\r
208 defaultType : 'menuitem',
\r
210 initComponent : function(){
\r
211 if(Ext.isArray(this.initialConfig)){
\r
212 Ext.apply(this, {items:this.initialConfig});
\r
217 * Fires when this menu is clicked (or when the enter key is pressed while it is active)
\r
218 * @param {Ext.menu.Menu} this
\r
219 * @param {Ext.menu.Item} menuItem The menu item that was clicked
\r
220 * @param {Ext.EventObject} e
\r
225 * Fires when the mouse is hovering over this menu
\r
226 * @param {Ext.menu.Menu} this
\r
227 * @param {Ext.EventObject} e
\r
228 * @param {Ext.menu.Item} menuItem The menu item that was clicked
\r
233 * Fires when the mouse exits this menu
\r
234 * @param {Ext.menu.Menu} this
\r
235 * @param {Ext.EventObject} e
\r
236 * @param {Ext.menu.Item} menuItem The menu item that was clicked
\r
241 * Fires when a menu item contained in this menu is clicked
\r
242 * @param {Ext.menu.BaseItem} baseItem The BaseItem that was clicked
\r
243 * @param {Ext.EventObject} e
\r
247 Ext.menu.MenuMgr.register(this);
\r
249 Ext.EventManager.onWindowResize(this.hide, this);
\r
251 if(this.initialConfig.hidden !== false){
\r
252 this.hidden = false;
\r
254 this.internalDefaults = {hideOnClick: false};
\r
256 Ext.menu.Menu.superclass.initComponent.call(this);
\r
257 if(this.autoLayout){
\r
259 add: this.doLayout,
\r
260 remove: this.doLayout,
\r
267 getLayoutTarget : function() {
\r
272 onRender : function(ct, position){
\r
274 ct = Ext.getBody();
\r
279 cls: 'x-menu ' + ((this.floating) ? 'x-menu-floating x-layer ' : '') + (this.cls || '') + (this.plain ? ' x-menu-plain' : '') + (this.showSeparator ? '' : ' x-menu-nosep'),
\r
282 {tag: 'a', cls: 'x-menu-focus', href: '#', onclick: 'return false;', tabIndex: '-1'},
\r
283 {tag: 'ul', cls: 'x-menu-list'}
\r
287 this.el = new Ext.Layer({
\r
288 shadow: this.shadow,
\r
295 this.el = ct.createChild(dh);
\r
297 Ext.menu.Menu.superclass.onRender.call(this, ct, position);
\r
300 this.keyNav = new Ext.menu.MenuNav(this);
\r
302 // generic focus element
\r
303 this.focusEl = this.el.child('a.x-menu-focus');
\r
304 this.ul = this.el.child('ul.x-menu-list');
\r
305 this.mon(this.ul, {
\r
307 click: this.onClick,
\r
308 mouseover: this.onMouseOver,
\r
309 mouseout: this.onMouseOut
\r
311 if(this.enableScrolling){
\r
312 this.mon(this.el, {
\r
314 delegate: '.x-menu-scroller',
\r
315 click: this.onScroll,
\r
316 mouseover: this.deactivateActive
\r
322 findTargetItem : function(e){
\r
323 var t = e.getTarget('.x-menu-list-item', this.ul, true);
\r
324 if(t && t.menuItemId){
\r
325 return this.items.get(t.menuItemId);
\r
330 onClick : function(e){
\r
331 var t = this.findTargetItem(e);
\r
334 this.setActiveItem(t);
\r
335 }else if(t instanceof Ext.menu.BaseItem){
\r
336 if(t.menu && this.ignoreParentClicks){
\r
338 e.preventDefault();
\r
339 }else if(t.onClick){
\r
341 this.fireEvent('click', this, t, e);
\r
348 setActiveItem : function(item, autoExpand){
\r
349 if(item != this.activeItem){
\r
350 this.deactivateActive();
\r
351 if((this.activeItem = item).isFormField){
\r
354 item.activate(autoExpand);
\r
356 }else if(autoExpand){
\r
361 deactivateActive : function(){
\r
362 var a = this.activeItem;
\r
365 //Fields cannot deactivate, but Combos must collapse
\r
372 delete this.activeItem;
\r
377 tryActivate : function(start, step){
\r
378 var items = this.items;
\r
379 for(var i = start, len = items.length; i >= 0 && i < len; i+= step){
\r
380 var item = items.get(i);
\r
381 if(!item.disabled && (item.canActivate || item.isFormField)){
\r
382 this.setActiveItem(item, false);
\r
390 onMouseOver : function(e){
\r
391 var t = this.findTargetItem(e);
\r
393 if(t.canActivate && !t.disabled){
\r
394 this.setActiveItem(t, true);
\r
398 this.fireEvent('mouseover', this, e, t);
\r
402 onMouseOut : function(e){
\r
403 var t = this.findTargetItem(e);
\r
405 if(t == this.activeItem && t.shouldDeactivate && t.shouldDeactivate(e)){
\r
406 this.activeItem.deactivate();
\r
407 delete this.activeItem;
\r
411 this.fireEvent('mouseout', this, e, t);
\r
415 onScroll : function(e, t){
\r
419 var ul = this.ul.dom, top = Ext.fly(t).is('.x-menu-scroller-top');
\r
420 ul.scrollTop += this.scrollIncrement * (top ? -1 : 1);
\r
421 if(top ? ul.scrollTop <= 0 : ul.scrollTop + this.activeMax >= ul.scrollHeight){
\r
422 this.onScrollerOut(null, t);
\r
427 onScrollerIn : function(e, t){
\r
428 var ul = this.ul.dom, top = Ext.fly(t).is('.x-menu-scroller-top');
\r
429 if(top ? ul.scrollTop > 0 : ul.scrollTop + this.activeMax < ul.scrollHeight){
\r
430 Ext.fly(t).addClass(['x-menu-item-active', 'x-menu-scroller-active']);
\r
435 onScrollerOut : function(e, t){
\r
436 Ext.fly(t).removeClass(['x-menu-item-active', 'x-menu-scroller-active']);
\r
440 * If <code>{@link #floating}=true</code>, shows this menu relative to
\r
441 * another element using {@link #showat}, otherwise uses {@link Ext.Component#show}.
\r
442 * @param {Mixed} element The element to align to
\r
443 * @param {String} position (optional) The {@link Ext.Element#alignTo} anchor position to use in aligning to
\r
444 * the element (defaults to this.defaultAlign)
\r
445 * @param {Ext.menu.Menu} parentMenu (optional) This menu's parent menu, if applicable (defaults to undefined)
\r
447 show : function(el, pos, parentMenu){
\r
449 this.parentMenu = parentMenu;
\r
452 this.doLayout(false, true);
\r
454 this.showAt(this.el.getAlignToXY(el, pos || this.defaultAlign, this.defaultOffsets), parentMenu);
\r
456 Ext.menu.Menu.superclass.show.call(this);
\r
461 * Displays this menu at a specific xy position and fires the 'show' event if a
\r
462 * handler for the 'beforeshow' event does not return false cancelling the operation.
\r
463 * @param {Array} xyPosition Contains X & Y [x, y] values for the position at which to show the menu (coordinates are page-based)
\r
464 * @param {Ext.menu.Menu} parentMenu (optional) This menu's parent menu, if applicable (defaults to undefined)
\r
466 showAt : function(xy, parentMenu){
\r
467 if(this.fireEvent('beforeshow', this) !== false){
\r
468 this.parentMenu = parentMenu;
\r
472 if(this.enableScrolling){
\r
473 // set the position so we can figure out the constrain value.
\r
475 //constrain the value, keep the y coordinate the same
\r
476 this.constrainScroll(xy[1]);
\r
477 xy = [this.el.adjustForConstraints(xy)[0], xy[1]];
\r
479 //constrain to the viewport.
\r
480 xy = this.el.adjustForConstraints(xy);
\r
484 Ext.menu.Menu.superclass.onShow.call(this);
\r
486 // internal event, used so we don't couple the layout to the menu
\r
487 this.fireEvent('autosize', this);
\r
492 this.hidden = false;
\r
494 this.fireEvent('show', this);
\r
498 constrainScroll : function(y){
\r
499 var max, full = this.ul.setHeight('auto').getHeight();
\r
501 max = this.maxHeight ? this.maxHeight : Ext.fly(this.el.dom.parentNode).getViewSize().height - y;
\r
503 max = this.getHeight();
\r
505 if(full > max && max > 0){
\r
506 this.activeMax = max - this.scrollerHeight * 2 - this.el.getFrameWidth('tb') - Ext.num(this.el.shadowOffset, 0);
\r
507 this.ul.setHeight(this.activeMax);
\r
508 this.createScrollers();
\r
509 this.el.select('.x-menu-scroller').setDisplayed('');
\r
511 this.ul.setHeight(full);
\r
512 this.el.select('.x-menu-scroller').setDisplayed('none');
\r
514 this.ul.dom.scrollTop = 0;
\r
517 createScrollers : function(){
\r
518 if(!this.scroller){
\r
521 top: this.el.insertFirst({
\r
523 cls: 'x-menu-scroller x-menu-scroller-top',
\r
526 bottom: this.el.createChild({
\r
528 cls: 'x-menu-scroller x-menu-scroller-bottom',
\r
532 this.scroller.top.hover(this.onScrollerIn, this.onScrollerOut, this);
\r
533 this.scroller.topRepeater = new Ext.util.ClickRepeater(this.scroller.top, {
\r
535 click: this.onScroll.createDelegate(this, [null, this.scroller.top], false)
\r
538 this.scroller.bottom.hover(this.onScrollerIn, this.onScrollerOut, this);
\r
539 this.scroller.bottomRepeater = new Ext.util.ClickRepeater(this.scroller.bottom, {
\r
541 click: this.onScroll.createDelegate(this, [null, this.scroller.bottom], false)
\r
547 onLayout : function(){
\r
548 if(this.isVisible()){
\r
549 if(this.enableScrolling){
\r
550 this.constrainScroll(this.el.getTop());
\r
558 focus : function(){
\r
560 this.doFocus.defer(50, this);
\r
564 doFocus : function(){
\r
566 this.focusEl.focus();
\r
571 * Hides this menu and optionally all parent menus
\r
572 * @param {Boolean} deep (optional) True to hide all parent menus recursively, if any (defaults to false)
\r
574 hide : function(deep){
\r
575 this.deepHide = deep;
\r
576 Ext.menu.Menu.superclass.hide.call(this);
\r
577 delete this.deepHide;
\r
581 onHide : function(){
\r
582 Ext.menu.Menu.superclass.onHide.call(this);
\r
583 this.deactivateActive();
\r
584 if(this.el && this.floating){
\r
587 var pm = this.parentMenu;
\r
588 if(this.deepHide === true && pm){
\r
592 pm.deactivateActive();
\r
598 lookupComponent : function(c){
\r
599 if(Ext.isString(c)){
\r
600 c = (c == 'separator' || c == '-') ? new Ext.menu.Separator() : new Ext.menu.TextItem(c);
\r
601 this.applyDefaults(c);
\r
603 if(Ext.isObject(c)){
\r
604 c = this.getMenuItem(c);
\r
605 }else if(c.tagName || c.el){ // element. Wrap it.
\r
606 c = new Ext.BoxComponent({
\r
614 applyDefaults : function(c){
\r
615 if(!Ext.isString(c)){
\r
616 c = Ext.menu.Menu.superclass.applyDefaults.call(this, c);
\r
617 var d = this.internalDefaults;
\r
620 Ext.applyIf(c.initialConfig, d);
\r
631 getMenuItem : function(config){
\r
632 if(!config.isXType){
\r
633 if(!config.xtype && Ext.isBoolean(config.checked)){
\r
634 return new Ext.menu.CheckItem(config)
\r
636 return Ext.create(config, this.defaultType);
\r
642 * Adds a separator bar to the menu
\r
643 * @return {Ext.menu.Item} The menu item that was added
\r
645 addSeparator : function(){
\r
646 return this.add(new Ext.menu.Separator());
\r
650 * Adds an {@link Ext.Element} object to the menu
\r
651 * @param {Mixed} el The element or DOM node to add, or its id
\r
652 * @return {Ext.menu.Item} The menu item that was added
\r
654 addElement : function(el){
\r
655 return this.add(new Ext.menu.BaseItem(el));
\r
659 * Adds an existing object based on {@link Ext.menu.BaseItem} to the menu
\r
660 * @param {Ext.menu.Item} item The menu item to add
\r
661 * @return {Ext.menu.Item} The menu item that was added
\r
663 addItem : function(item){
\r
664 return this.add(item);
\r
668 * Creates a new {@link Ext.menu.Item} based an the supplied config object and adds it to the menu
\r
669 * @param {Object} config A MenuItem config object
\r
670 * @return {Ext.menu.Item} The menu item that was added
\r
672 addMenuItem : function(config){
\r
673 return this.add(this.getMenuItem(config));
\r
677 * Creates a new {@link Ext.menu.TextItem} with the supplied text and adds it to the menu
\r
678 * @param {String} text The text to display in the menu item
\r
679 * @return {Ext.menu.Item} The menu item that was added
\r
681 addText : function(text){
\r
682 return this.add(new Ext.menu.TextItem(text));
\r
686 onDestroy : function(){
\r
687 Ext.menu.Menu.superclass.onDestroy.call(this);
\r
688 Ext.menu.MenuMgr.unregister(this);
\r
689 Ext.EventManager.removeResizeListener(this.hide, this);
\r
691 this.keyNav.disable();
\r
693 var s = this.scroller;
\r
695 Ext.destroy(s.topRepeater, s.bottomRepeater, s.top, s.bottom);
\r
705 Ext.reg('menu', Ext.menu.Menu);
\r
707 // MenuNav is a private utility class used internally by the Menu
\r
708 Ext.menu.MenuNav = Ext.extend(Ext.KeyNav, function(){
\r
710 if(!m.tryActivate(m.items.indexOf(m.activeItem)-1, -1)){
\r
711 m.tryActivate(m.items.length-1, -1);
\r
714 function down(e, m){
\r
715 if(!m.tryActivate(m.items.indexOf(m.activeItem)+1, 1)){
\r
716 m.tryActivate(0, 1);
\r
720 constructor : function(menu){
\r
721 Ext.menu.MenuNav.superclass.constructor.call(this, menu.el);
\r
722 this.scope = this.menu = menu;
\r
725 doRelay : function(e, h){
\r
726 var k = e.getKey();
\r
727 // Keystrokes within a form Field (e.g.: down in a Combo) do not navigate. Allow only TAB
\r
728 if (this.menu.activeItem && this.menu.activeItem.isFormField && k != e.TAB) {
\r
731 if(!this.menu.activeItem && e.isNavKeyPress() && k != e.SPACE && k != e.RETURN){
\r
732 this.menu.tryActivate(0, 1);
\r
735 return h.call(this.scope || this, e, this.menu);
\r
738 tab: function(e, m) {
\r
751 right : function(e, m){
\r
753 m.activeItem.expandMenu(true);
\r
757 left : function(e, m){
\r
759 if(m.parentMenu && m.parentMenu.activeItem){
\r
760 m.parentMenu.activeItem.activate();
\r
764 enter : function(e, m){
\r
766 e.stopPropagation();
\r
767 m.activeItem.onClick(e);
\r
768 m.fireEvent('click', this, m.activeItem);
\r