3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4 <title>The source code</title>
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <body onload="prettyPrint();">
9 <pre class="prettyprint lang-js">/*!
10 * Ext JS Library 3.2.0
11 * Copyright(c) 2006-2010 Ext JS, Inc.
13 * http://www.extjs.com/license
15 <div id="cls-Ext.menu.Menu"></div>/**
16 * @class Ext.menu.Menu
17 * @extends Ext.Container
18 * <p>A menu object. This is the container to which you may add menu items. Menu can also serve as a base class
19 * when you want a specialized menu based off of another component (like {@link Ext.menu.DateMenu} for example).</p>
20 * <p>Menus may contain either {@link Ext.menu.Item menu items}, or general {@link Ext.Component Component}s.</p>
21 * <p>To make a contained general {@link Ext.Component Component} line up with other {@link Ext.menu.Item menu items}
22 * specify <tt>iconCls: 'no-icon'</tt>. This reserves a space for an icon, and indents the Component in line
23 * with the other menu items. See {@link Ext.form.ComboBox}.{@link Ext.form.ComboBox#getListParent getListParent}
25 * <p>By default, Menus are absolutely positioned, floating Components. By configuring a Menu with
26 * <b><tt>{@link #floating}:false</tt></b>, a Menu may be used as child of a Container.</p>
30 Ext.menu.Menu = Ext.extend(Ext.Container, {
31 <div id="cfg-Ext.menu.Menu-defaults"></div>/**
32 * @cfg {Object} defaults
33 * A config object that will be applied to all items added to this container either via the {@link #items}
34 * config or via the {@link #add} method. The defaults config can contain any number of
35 * name/value property pairs to be added to each item, and should be valid for the types of items
36 * being added to the menu.
38 <div id="cfg-Ext.menu.Menu-items"></div>/**
40 * An array of items to be added to this menu. Menus may contain either {@link Ext.menu.Item menu items},
41 * or general {@link Ext.Component Component}s.
43 <div id="cfg-Ext.menu.Menu-minWidth"></div>/**
44 * @cfg {Number} minWidth The minimum width of the menu in pixels (defaults to 120)
47 <div id="cfg-Ext.menu.Menu-shadow"></div>/**
48 * @cfg {Boolean/String} shadow True or 'sides' for the default effect, 'frame' for 4-way shadow, and 'drop'
49 * for bottom-right shadow (defaults to 'sides')
52 <div id="cfg-Ext.menu.Menu-subMenuAlign"></div>/**
53 * @cfg {String} subMenuAlign The {@link Ext.Element#alignTo} anchor position value to use for submenus of
54 * this menu (defaults to 'tl-tr?')
56 subMenuAlign : 'tl-tr?',
57 <div id="cfg-Ext.menu.Menu-defaultAlign"></div>/**
58 * @cfg {String} defaultAlign The default {@link Ext.Element#alignTo} anchor position value for this menu
59 * relative to its element of origin (defaults to 'tl-bl?')
61 defaultAlign : 'tl-bl?',
62 <div id="cfg-Ext.menu.Menu-allowOtherMenus"></div>/**
63 * @cfg {Boolean} allowOtherMenus True to allow multiple menus to be displayed at the same time (defaults to false)
65 allowOtherMenus : false,
66 <div id="cfg-Ext.menu.Menu-ignoreParentClicks"></div>/**
67 * @cfg {Boolean} ignoreParentClicks True to ignore clicks on any item in this menu that is a parent item (displays
68 * a submenu) so that the submenu is not dismissed when clicking the parent item (defaults to false).
70 ignoreParentClicks : false,
71 <div id="cfg-Ext.menu.Menu-enableScrolling"></div>/**
72 * @cfg {Boolean} enableScrolling True to allow the menu container to have scroller controls if the menu is too long (defaults to true).
74 enableScrolling : true,
75 <div id="cfg-Ext.menu.Menu-maxHeight"></div>/**
76 * @cfg {Number} maxHeight The maximum height of the menu. Only applies when enableScrolling is set to True (defaults to null).
79 <div id="cfg-Ext.menu.Menu-scrollIncrement"></div>/**
80 * @cfg {Number} scrollIncrement The amount to scroll the menu. Only applies when enableScrolling is set to True (defaults to 24).
83 <div id="cfg-Ext.menu.Menu-showSeparator"></div>/**
84 * @cfg {Boolean} showSeparator True to show the icon separator. (defaults to true).
87 <div id="cfg-Ext.menu.Menu-defaultOffsets"></div>/**
88 * @cfg {Array} defaultOffsets An array specifying the [x, y] offset in pixels by which to
89 * change the default Menu popup position after aligning according to the {@link #defaultAlign}
90 * configuration. Defaults to <tt>[0, 0]</tt>.
92 defaultOffsets : [0, 0],
94 <div id="cfg-Ext.menu.Menu-plain"></div>/**
95 * @cfg {Boolean} plain
96 * True to remove the incised line down the left side of the menu. Defaults to <tt>false</tt>.
100 <div id="cfg-Ext.menu.Menu-floating"></div>/**
101 * @cfg {Boolean} floating
102 * <p>By default, a Menu configured as <b><code>floating:true</code></b>
103 * will be rendered as an {@link Ext.Layer} (an absolutely positioned,
104 * floating Component with zindex=15000).
105 * If configured as <b><code>floating:false</code></b>, the Menu may be
106 * used as child item of another Container instead of a free-floating
107 * {@link Ext.Layer Layer}.
112 <div id="cfg-Ext.menu.Menu-zIndex"></div>/**
113 * @cfg {Number} zIndex
114 * zIndex to use when the menu is floating.
121 <div id="cfg-Ext.menu.Menu-layout"></div>/**
122 * @cfg {String/Object} layout
123 * This class assigns a default layout (<code>layout:'<b>menu</b>'</code>).
124 * Developers <i>may</i> override this configuration option if another layout is required.
125 * See {@link Ext.Container#layout} for additional information.
128 hideMode : 'offsets', // Important for laying out Components
130 autoLayout : true, // Provided for backwards compat
131 defaultType : 'menuitem',
132 bufferResize : false,
134 initComponent : function(){
135 if(Ext.isArray(this.initialConfig)){
136 Ext.apply(this, {items:this.initialConfig});
139 <div id="event-Ext.menu.Menu-click"></div>/**
141 * Fires when this menu is clicked (or when the enter key is pressed while it is active)
142 * @param {Ext.menu.Menu} this
143 * @param {Ext.menu.Item} menuItem The menu item that was clicked
144 * @param {Ext.EventObject} e
147 <div id="event-Ext.menu.Menu-mouseover"></div>/**
149 * Fires when the mouse is hovering over this menu
150 * @param {Ext.menu.Menu} this
151 * @param {Ext.EventObject} e
152 * @param {Ext.menu.Item} menuItem The menu item that was clicked
155 <div id="event-Ext.menu.Menu-mouseout"></div>/**
157 * Fires when the mouse exits this menu
158 * @param {Ext.menu.Menu} this
159 * @param {Ext.EventObject} e
160 * @param {Ext.menu.Item} menuItem The menu item that was clicked
163 <div id="event-Ext.menu.Menu-itemclick"></div>/**
165 * Fires when a menu item contained in this menu is clicked
166 * @param {Ext.menu.BaseItem} baseItem The BaseItem that was clicked
167 * @param {Ext.EventObject} e
171 Ext.menu.MenuMgr.register(this);
173 Ext.EventManager.onWindowResize(this.hide, this);
175 if(this.initialConfig.hidden !== false){
178 this.internalDefaults = {hideOnClick: false};
180 Ext.menu.Menu.superclass.initComponent.call(this);
182 var fn = this.doLayout.createDelegate(this, []);
191 getLayoutTarget : function() {
196 onRender : function(ct, position){
203 cls: 'x-menu ' + ((this.floating) ? 'x-menu-floating x-layer ' : '') + (this.cls || '') + (this.plain ? ' x-menu-plain' : '') + (this.showSeparator ? '' : ' x-menu-nosep'),
206 {tag: 'a', cls: 'x-menu-focus', href: '#', onclick: 'return false;', tabIndex: '-1'},
207 {tag: 'ul', cls: 'x-menu-list'}
211 this.el = new Ext.Layer({
219 this.el = ct.createChild(dh);
221 Ext.menu.Menu.superclass.onRender.call(this, ct, position);
224 this.keyNav = new Ext.menu.MenuNav(this);
226 // generic focus element
227 this.focusEl = this.el.child('a.x-menu-focus');
228 this.ul = this.el.child('ul.x-menu-list');
232 mouseover: this.onMouseOver,
233 mouseout: this.onMouseOut
235 if(this.enableScrolling){
238 delegate: '.x-menu-scroller',
239 click: this.onScroll,
240 mouseover: this.deactivateActive
246 findTargetItem : function(e){
247 var t = e.getTarget('.x-menu-list-item', this.ul, true);
248 if(t && t.menuItemId){
249 return this.items.get(t.menuItemId);
254 onClick : function(e){
255 var t = this.findTargetItem(e);
258 this.setActiveItem(t);
259 }else if(t instanceof Ext.menu.BaseItem){
260 if(t.menu && this.ignoreParentClicks){
265 this.fireEvent('click', this, t, e);
272 setActiveItem : function(item, autoExpand){
273 if(item != this.activeItem){
274 this.deactivateActive();
275 if((this.activeItem = item).isFormField){
278 item.activate(autoExpand);
280 }else if(autoExpand){
285 deactivateActive : function(){
286 var a = this.activeItem;
289 //Fields cannot deactivate, but Combos must collapse
296 delete this.activeItem;
301 tryActivate : function(start, step){
302 var items = this.items;
303 for(var i = start, len = items.length; i >= 0 && i < len; i+= step){
304 var item = items.get(i);
305 if(!item.disabled && (item.canActivate || item.isFormField)){
306 this.setActiveItem(item, false);
314 onMouseOver : function(e){
315 var t = this.findTargetItem(e);
317 if(t.canActivate && !t.disabled){
318 this.setActiveItem(t, true);
322 this.fireEvent('mouseover', this, e, t);
326 onMouseOut : function(e){
327 var t = this.findTargetItem(e);
329 if(t == this.activeItem && t.shouldDeactivate && t.shouldDeactivate(e)){
330 this.activeItem.deactivate();
331 delete this.activeItem;
335 this.fireEvent('mouseout', this, e, t);
339 onScroll : function(e, t){
343 var ul = this.ul.dom, top = Ext.fly(t).is('.x-menu-scroller-top');
344 ul.scrollTop += this.scrollIncrement * (top ? -1 : 1);
345 if(top ? ul.scrollTop <= 0 : ul.scrollTop + this.activeMax >= ul.scrollHeight){
346 this.onScrollerOut(null, t);
351 onScrollerIn : function(e, t){
352 var ul = this.ul.dom, top = Ext.fly(t).is('.x-menu-scroller-top');
353 if(top ? ul.scrollTop > 0 : ul.scrollTop + this.activeMax < ul.scrollHeight){
354 Ext.fly(t).addClass(['x-menu-item-active', 'x-menu-scroller-active']);
359 onScrollerOut : function(e, t){
360 Ext.fly(t).removeClass(['x-menu-item-active', 'x-menu-scroller-active']);
363 <div id="method-Ext.menu.Menu-show"></div>/**
364 * If <code>{@link #floating}=true</code>, shows this menu relative to
365 * another element using {@link #showat}, otherwise uses {@link Ext.Component#show}.
366 * @param {Mixed} element The element to align to
367 * @param {String} position (optional) The {@link Ext.Element#alignTo} anchor position to use in aligning to
368 * the element (defaults to this.defaultAlign)
369 * @param {Ext.menu.Menu} parentMenu (optional) This menu's parent menu, if applicable (defaults to undefined)
371 show : function(el, pos, parentMenu){
373 this.parentMenu = parentMenu;
376 this.doLayout(false, true);
378 this.showAt(this.el.getAlignToXY(el, pos || this.defaultAlign, this.defaultOffsets), parentMenu);
380 Ext.menu.Menu.superclass.show.call(this);
384 <div id="method-Ext.menu.Menu-showAt"></div>/**
385 * Displays this menu at a specific xy position and fires the 'show' event if a
386 * handler for the 'beforeshow' event does not return false cancelling the operation.
387 * @param {Array} xyPosition Contains X & Y [x, y] values for the position at which to show the menu (coordinates are page-based)
388 * @param {Ext.menu.Menu} parentMenu (optional) This menu's parent menu, if applicable (defaults to undefined)
390 showAt : function(xy, parentMenu){
391 if(this.fireEvent('beforeshow', this) !== false){
392 this.parentMenu = parentMenu;
396 if(this.enableScrolling){
397 // set the position so we can figure out the constrain value.
399 //constrain the value, keep the y coordinate the same
400 xy[1] = this.constrainScroll(xy[1]);
401 xy = [this.el.adjustForConstraints(xy)[0], xy[1]];
403 //constrain to the viewport.
404 xy = this.el.adjustForConstraints(xy);
408 Ext.menu.Menu.superclass.onShow.call(this);
410 // internal event, used so we don't couple the layout to the menu
411 this.fireEvent('autosize', this);
418 this.fireEvent('show', this);
422 constrainScroll : function(y){
423 var max, full = this.ul.setHeight('auto').getHeight(),
424 returnY = y, normalY, parentEl, scrollTop, viewHeight;
426 parentEl = Ext.fly(this.el.dom.parentNode);
427 scrollTop = parentEl.getScroll().top;
428 viewHeight = parentEl.getViewSize().height;
429 //Normalize y by the scroll position for the parent element. Need to move it into the coordinate space
431 normalY = y - scrollTop;
432 max = this.maxHeight ? this.maxHeight : viewHeight - normalY;
433 if(full > viewHeight) {
435 //Set returnY equal to (0,0) in view space by reducing y by the value of normalY
436 returnY = y - normalY;
437 } else if(max < full) {
438 returnY = y - (full - max);
442 max = this.getHeight();
444 // Always respect maxHeight
446 max = Math.min(this.maxHeight, max);
448 if(full > max && max > 0){
449 this.activeMax = max - this.scrollerHeight * 2 - this.el.getFrameWidth('tb') - Ext.num(this.el.shadowOffset, 0);
450 this.ul.setHeight(this.activeMax);
451 this.createScrollers();
452 this.el.select('.x-menu-scroller').setDisplayed('');
454 this.ul.setHeight(full);
455 this.el.select('.x-menu-scroller').setDisplayed('none');
457 this.ul.dom.scrollTop = 0;
461 createScrollers : function(){
465 top: this.el.insertFirst({
467 cls: 'x-menu-scroller x-menu-scroller-top',
470 bottom: this.el.createChild({
472 cls: 'x-menu-scroller x-menu-scroller-bottom',
476 this.scroller.top.hover(this.onScrollerIn, this.onScrollerOut, this);
477 this.scroller.topRepeater = new Ext.util.ClickRepeater(this.scroller.top, {
479 click: this.onScroll.createDelegate(this, [null, this.scroller.top], false)
482 this.scroller.bottom.hover(this.onScrollerIn, this.onScrollerOut, this);
483 this.scroller.bottomRepeater = new Ext.util.ClickRepeater(this.scroller.bottom, {
485 click: this.onScroll.createDelegate(this, [null, this.scroller.bottom], false)
491 onLayout : function(){
492 if(this.isVisible()){
493 if(this.enableScrolling){
494 this.constrainScroll(this.el.getTop());
504 this.doFocus.defer(50, this);
508 doFocus : function(){
510 this.focusEl.focus();
514 <div id="method-Ext.menu.Menu-hide"></div>/**
515 * Hides this menu and optionally all parent menus
516 * @param {Boolean} deep (optional) True to hide all parent menus recursively, if any (defaults to false)
518 hide : function(deep){
519 if (!this.isDestroyed) {
520 this.deepHide = deep;
521 Ext.menu.Menu.superclass.hide.call(this);
522 delete this.deepHide;
528 Ext.menu.Menu.superclass.onHide.call(this);
529 this.deactivateActive();
530 if(this.el && this.floating){
533 var pm = this.parentMenu;
534 if(this.deepHide === true && pm){
538 pm.deactivateActive();
544 lookupComponent : function(c){
546 c = (c == 'separator' || c == '-') ? new Ext.menu.Separator() : new Ext.menu.TextItem(c);
547 this.applyDefaults(c);
550 c = this.getMenuItem(c);
551 }else if(c.tagName || c.el){ // element. Wrap it.
552 c = new Ext.BoxComponent({
560 applyDefaults : function(c){
561 if(!Ext.isString(c)){
562 c = Ext.menu.Menu.superclass.applyDefaults.call(this, c);
563 var d = this.internalDefaults;
566 Ext.applyIf(c.initialConfig, d);
577 getMenuItem : function(config){
579 if(!config.xtype && Ext.isBoolean(config.checked)){
580 return new Ext.menu.CheckItem(config)
582 return Ext.create(config, this.defaultType);
587 <div id="method-Ext.menu.Menu-addSeparator"></div>/**
588 * Adds a separator bar to the menu
589 * @return {Ext.menu.Item} The menu item that was added
591 addSeparator : function(){
592 return this.add(new Ext.menu.Separator());
595 <div id="method-Ext.menu.Menu-addElement"></div>/**
596 * Adds an {@link Ext.Element} object to the menu
597 * @param {Mixed} el The element or DOM node to add, or its id
598 * @return {Ext.menu.Item} The menu item that was added
600 addElement : function(el){
601 return this.add(new Ext.menu.BaseItem({
606 <div id="method-Ext.menu.Menu-addItem"></div>/**
607 * Adds an existing object based on {@link Ext.menu.BaseItem} to the menu
608 * @param {Ext.menu.Item} item The menu item to add
609 * @return {Ext.menu.Item} The menu item that was added
611 addItem : function(item){
612 return this.add(item);
615 <div id="method-Ext.menu.Menu-addMenuItem"></div>/**
616 * Creates a new {@link Ext.menu.Item} based an the supplied config object and adds it to the menu
617 * @param {Object} config A MenuItem config object
618 * @return {Ext.menu.Item} The menu item that was added
620 addMenuItem : function(config){
621 return this.add(this.getMenuItem(config));
624 <div id="method-Ext.menu.Menu-addText"></div>/**
625 * Creates a new {@link Ext.menu.TextItem} with the supplied text and adds it to the menu
626 * @param {String} text The text to display in the menu item
627 * @return {Ext.menu.Item} The menu item that was added
629 addText : function(text){
630 return this.add(new Ext.menu.TextItem(text));
634 onDestroy : function(){
635 Ext.EventManager.removeResizeListener(this.hide, this);
636 var pm = this.parentMenu;
637 if(pm && pm.activeChild == this){
638 delete pm.activeChild;
640 delete this.parentMenu;
641 Ext.menu.Menu.superclass.onDestroy.call(this);
642 Ext.menu.MenuMgr.unregister(this);
644 this.keyNav.disable();
646 var s = this.scroller;
648 Ext.destroy(s.topRepeater, s.bottomRepeater, s.top, s.bottom);
658 Ext.reg('menu', Ext.menu.Menu);
660 // MenuNav is a private utility class used internally by the Menu
661 Ext.menu.MenuNav = Ext.extend(Ext.KeyNav, function(){
663 if(!m.tryActivate(m.items.indexOf(m.activeItem)-1, -1)){
664 m.tryActivate(m.items.length-1, -1);
668 if(!m.tryActivate(m.items.indexOf(m.activeItem)+1, 1)){
673 constructor : function(menu){
674 Ext.menu.MenuNav.superclass.constructor.call(this, menu.el);
675 this.scope = this.menu = menu;
678 doRelay : function(e, h){
680 // Keystrokes within a form Field (e.g.: down in a Combo) do not navigate. Allow only TAB
681 if (this.menu.activeItem && this.menu.activeItem.isFormField && k != e.TAB) {
684 if(!this.menu.activeItem && e.isNavKeyPress() && k != e.SPACE && k != e.RETURN){
685 this.menu.tryActivate(0, 1);
688 return h.call(this.scope || this, e, this.menu);
691 tab: function(e, m) {
704 right : function(e, m){
706 m.activeItem.expandMenu(true);
710 left : function(e, m){
712 if(m.parentMenu && m.parentMenu.activeItem){
713 m.parentMenu.activeItem.activate();
717 enter : function(e, m){
720 m.activeItem.onClick(e);
721 m.fireEvent('click', this, m.activeItem);