3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
\r
4 <title>The source code</title>
\r
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
8 <body onload="prettyPrint();">
\r
9 <pre class="prettyprint lang-js"><div id="cls-Ext.layout.MenuLayout"></div>/**
10 * @class Ext.layout.MenuLayout
11 * @extends Ext.layout.ContainerLayout
12 * <p>Layout manager used by {@link Ext.menu.Menu}. Generally this class should not need to be used directly.</p>
14 Ext.layout.MenuLayout = Ext.extend(Ext.layout.ContainerLayout, {
17 setContainer : function(ct){
18 this.monitorResize = !ct.floating;
19 // This event is only fired by the menu in IE, used so we don't couple
20 // the menu with the layout.
21 ct.on('autosize', this.doAutoSize, this);
22 Ext.layout.MenuLayout.superclass.setContainer.call(this, ct);
25 renderItem : function(c, position, target){
27 this.itemTpl = Ext.layout.MenuLayout.prototype.itemTpl = new Ext.XTemplate(
28 '<li id="{itemId}" class="{itemCls}">',
29 '<tpl if="needsIcon">',
30 '<img src="{icon}" class="{iconCls}"/>',
37 if(Ext.isNumber(position)){
38 position = target.dom.childNodes[position];
40 var a = this.getItemArgs(c);
42 // The Component's positionEl is the <li> it is rendered into
43 c.render(c.positionEl = position ?
44 this.itemTpl.insertBefore(position, a, true) :
45 this.itemTpl.append(target, a, true));
47 // Link the containing <li> to the item.
48 c.positionEl.menuItemId = c.getItemId();
50 // If rendering a regular Component, and it needs an icon,
51 // move the Component rightwards.
52 if (!a.isMenuItem && a.needsIcon) {
53 c.positionEl.addClass('x-menu-list-item-indent');
55 this.configureItem(c, position);
56 }else if(c && !this.isValidParent(c, target)){
57 if(Ext.isNumber(position)){
58 position = target.dom.childNodes[position];
60 target.dom.insertBefore(c.getActionEl().dom, position || null);
64 getItemArgs : function(c) {
65 var isMenuItem = c instanceof Ext.menu.Item;
67 isMenuItem: isMenuItem,
68 needsIcon: !isMenuItem && (c.icon || c.iconCls),
69 icon: c.icon || Ext.BLANK_IMAGE_URL,
70 iconCls: 'x-menu-item-icon ' + (c.iconCls || ''),
71 itemId: 'x-menu-el-' + c.id,
72 itemCls: 'x-menu-list-item '
76 // Valid if the Component is in a <li> which is part of our target <ul>
77 isValidParent : function(c, target) {
78 return c.el.up('li.x-menu-list-item', 5).dom.parentNode === (target.dom || target);
81 onLayout : function(ct, target){
82 this.renderAll(ct, target);
86 doAutoSize : function(){
87 var ct = this.container, w = ct.width;
92 ct.setWidth(Ext.isStrict && (Ext.isIE7 || Ext.isIE8) ? 'auto' : ct.minWidth);
93 var el = ct.getEl(), t = el.dom.offsetWidth; // force recalc
94 ct.setWidth(ct.getLayoutTarget().getWidth() + el.getFrameWidth('lr'));
99 Ext.Container.LAYOUTS['menu'] = Ext.layout.MenuLayout;
101 <div id="cls-Ext.menu.Menu"></div>/**
102 * @class Ext.menu.Menu
103 * @extends Ext.Container
104 * <p>A menu object. This is the container to which you may add menu items. Menu can also serve as a base class
105 * when you want a specialized menu based off of another component (like {@link Ext.menu.DateMenu} for example).</p>
106 * <p>Menus may contain either {@link Ext.menu.Item menu items}, or general {@link Ext.Component Component}s.</p>
107 * <p>To make a contained general {@link Ext.Component Component} line up with other {@link Ext.menu.Item menu items}
108 * specify <tt>iconCls: 'no-icon'</tt>. This reserves a space for an icon, and indents the Component in line
109 * with the other menu items. See {@link Ext.form.ComboBox}.{@link Ext.form.ComboBox#getListParent getListParent}
110 * for an example.</p>
111 * <p>By default, Menus are absolutely positioned, floating Components. By configuring a Menu with
112 * <b><tt>{@link #floating}:false</tt></b>, a Menu may be used as child of a Container.</p>
116 Ext.menu.Menu = Ext.extend(Ext.Container, {
117 <div id="cfg-Ext.menu.Menu-defaults"></div>/**
118 * @cfg {Object} defaults
119 * A config object that will be applied to all items added to this container either via the {@link #items}
120 * config or via the {@link #add} method. The defaults config can contain any number of
121 * name/value property pairs to be added to each item, and should be valid for the types of items
122 * being added to the menu.
124 <div id="cfg-Ext.menu.Menu-items"></div>/**
126 * An array of items to be added to this menu. Menus may contain either {@link Ext.menu.Item menu items},
127 * or general {@link Ext.Component Component}s.
129 <div id="cfg-Ext.menu.Menu-minWidth"></div>/**
130 * @cfg {Number} minWidth The minimum width of the menu in pixels (defaults to 120)
133 <div id="cfg-Ext.menu.Menu-shadow"></div>/**
134 * @cfg {Boolean/String} shadow True or 'sides' for the default effect, 'frame' for 4-way shadow, and 'drop'
135 * for bottom-right shadow (defaults to 'sides')
138 <div id="cfg-Ext.menu.Menu-subMenuAlign"></div>/**
139 * @cfg {String} subMenuAlign The {@link Ext.Element#alignTo} anchor position value to use for submenus of
140 * this menu (defaults to 'tl-tr?')
142 subMenuAlign : 'tl-tr?',
143 <div id="cfg-Ext.menu.Menu-defaultAlign"></div>/**
144 * @cfg {String} defaultAlign The default {@link Ext.Element#alignTo} anchor position value for this menu
145 * relative to its element of origin (defaults to 'tl-bl?')
147 defaultAlign : 'tl-bl?',
148 <div id="cfg-Ext.menu.Menu-allowOtherMenus"></div>/**
149 * @cfg {Boolean} allowOtherMenus True to allow multiple menus to be displayed at the same time (defaults to false)
151 allowOtherMenus : false,
152 <div id="cfg-Ext.menu.Menu-ignoreParentClicks"></div>/**
153 * @cfg {Boolean} ignoreParentClicks True to ignore clicks on any item in this menu that is a parent item (displays
154 * a submenu) so that the submenu is not dismissed when clicking the parent item (defaults to false).
156 ignoreParentClicks : false,
157 <div id="cfg-Ext.menu.Menu-enableScrolling"></div>/**
158 * @cfg {Boolean} enableScrolling True to allow the menu container to have scroller controls if the menu is too long (defaults to true).
160 enableScrolling : true,
161 <div id="cfg-Ext.menu.Menu-maxHeight"></div>/**
162 * @cfg {Number} maxHeight The maximum height of the menu. Only applies when enableScrolling is set to True (defaults to null).
165 <div id="cfg-Ext.menu.Menu-scrollIncrement"></div>/**
166 * @cfg {Number} scrollIncrement The amount to scroll the menu. Only applies when enableScrolling is set to True (defaults to 24).
168 scrollIncrement : 24,
169 <div id="cfg-Ext.menu.Menu-showSeparator"></div>/**
170 * @cfg {Boolean} showSeparator True to show the icon separator. (defaults to true).
172 showSeparator : true,
173 <div id="cfg-Ext.menu.Menu-defaultOffsets"></div>/**
174 * @cfg {Array} defaultOffsets An array specifying the [x, y] offset in pixels by which to
175 * change the default Menu popup position after aligning according to the {@link #defaultAlign}
176 * configuration. Defaults to <tt>[0, 0]</tt>.
178 defaultOffsets : [0, 0],
180 <div id="cfg-Ext.menu.Menu-plain"></div>/**
181 * @cfg {Boolean} plain
182 * True to remove the incised line down the left side of the menu. Defaults to <tt>false</tt>.
186 <div id="cfg-Ext.menu.Menu-floating"></div>/**
187 * @cfg {Boolean} floating
188 * <p>By default, a Menu configured as <b><code>floating:true</code></b>
189 * will be rendered as an {@link Ext.Layer} (an absolutely positioned,
190 * floating Component with zindex=15000).
191 * If configured as <b><code>floating:false</code></b>, the Menu may be
192 * used as child item of another Container instead of a free-floating
193 * {@link Ext.Layer Layer}.
200 <div id="cfg-Ext.menu.Menu-layout"></div>/**
201 * @cfg {String/Object} layout
202 * This class assigns a default layout (<code>layout:'<b>menu</b>'</code>).
203 * Developers <i>may</i> override this configuration option if another layout is required.
204 * See {@link Ext.Container#layout} for additional information.
207 hideMode : 'offsets', // Important for laying out Components
209 autoLayout : true, // Provided for backwards compat
210 defaultType : 'menuitem',
211 bufferResize : false,
213 initComponent : function(){
214 if(Ext.isArray(this.initialConfig)){
215 Ext.apply(this, {items:this.initialConfig});
218 <div id="event-Ext.menu.Menu-click"></div>/**
220 * Fires when this menu is clicked (or when the enter key is pressed while it is active)
221 * @param {Ext.menu.Menu} this
222 * @param {Ext.menu.Item} menuItem The menu item that was clicked
223 * @param {Ext.EventObject} e
226 <div id="event-Ext.menu.Menu-mouseover"></div>/**
228 * Fires when the mouse is hovering over this menu
229 * @param {Ext.menu.Menu} this
230 * @param {Ext.EventObject} e
231 * @param {Ext.menu.Item} menuItem The menu item that was clicked
234 <div id="event-Ext.menu.Menu-mouseout"></div>/**
236 * Fires when the mouse exits this menu
237 * @param {Ext.menu.Menu} this
238 * @param {Ext.EventObject} e
239 * @param {Ext.menu.Item} menuItem The menu item that was clicked
242 <div id="event-Ext.menu.Menu-itemclick"></div>/**
244 * Fires when a menu item contained in this menu is clicked
245 * @param {Ext.menu.BaseItem} baseItem The BaseItem that was clicked
246 * @param {Ext.EventObject} e
250 Ext.menu.MenuMgr.register(this);
252 Ext.EventManager.onWindowResize(this.hide, this);
254 if(this.initialConfig.hidden !== false){
257 this.internalDefaults = {hideOnClick: false};
259 Ext.menu.Menu.superclass.initComponent.call(this);
263 remove: this.doLayout,
270 getLayoutTarget : function() {
275 onRender : function(ct, position){
282 cls: 'x-menu ' + ((this.floating) ? 'x-menu-floating x-layer ' : '') + (this.cls || '') + (this.plain ? ' x-menu-plain' : '') + (this.showSeparator ? '' : ' x-menu-nosep'),
285 {tag: 'a', cls: 'x-menu-focus', href: '#', onclick: 'return false;', tabIndex: '-1'},
286 {tag: 'ul', cls: 'x-menu-list'}
290 this.el = new Ext.Layer({
298 this.el = ct.createChild(dh);
300 Ext.menu.Menu.superclass.onRender.call(this, ct, position);
303 this.keyNav = new Ext.menu.MenuNav(this);
305 // generic focus element
306 this.focusEl = this.el.child('a.x-menu-focus');
307 this.ul = this.el.child('ul.x-menu-list');
311 mouseover: this.onMouseOver,
312 mouseout: this.onMouseOut
314 if(this.enableScrolling){
317 delegate: '.x-menu-scroller',
318 click: this.onScroll,
319 mouseover: this.deactivateActive
325 findTargetItem : function(e){
326 var t = e.getTarget('.x-menu-list-item', this.ul, true);
327 if(t && t.menuItemId){
328 return this.items.get(t.menuItemId);
333 onClick : function(e){
334 var t = this.findTargetItem(e);
337 this.setActiveItem(t);
338 }else if(t instanceof Ext.menu.BaseItem){
339 if(t.menu && this.ignoreParentClicks){
344 this.fireEvent('click', this, t, e);
351 setActiveItem : function(item, autoExpand){
352 if(item != this.activeItem){
353 this.deactivateActive();
354 if((this.activeItem = item).isFormField){
357 item.activate(autoExpand);
359 }else if(autoExpand){
364 deactivateActive : function(){
365 var a = this.activeItem;
368 //Fields cannot deactivate, but Combos must collapse
375 delete this.activeItem;
380 tryActivate : function(start, step){
381 var items = this.items;
382 for(var i = start, len = items.length; i >= 0 && i < len; i+= step){
383 var item = items.get(i);
384 if(!item.disabled && (item.canActivate || item.isFormField)){
385 this.setActiveItem(item, false);
393 onMouseOver : function(e){
394 var t = this.findTargetItem(e);
396 if(t.canActivate && !t.disabled){
397 this.setActiveItem(t, true);
401 this.fireEvent('mouseover', this, e, t);
405 onMouseOut : function(e){
406 var t = this.findTargetItem(e);
408 if(t == this.activeItem && t.shouldDeactivate && t.shouldDeactivate(e)){
409 this.activeItem.deactivate();
410 delete this.activeItem;
414 this.fireEvent('mouseout', this, e, t);
418 onScroll : function(e, t){
422 var ul = this.ul.dom, top = Ext.fly(t).is('.x-menu-scroller-top');
423 ul.scrollTop += this.scrollIncrement * (top ? -1 : 1);
424 if(top ? ul.scrollTop <= 0 : ul.scrollTop + this.activeMax >= ul.scrollHeight){
425 this.onScrollerOut(null, t);
430 onScrollerIn : function(e, t){
431 var ul = this.ul.dom, top = Ext.fly(t).is('.x-menu-scroller-top');
432 if(top ? ul.scrollTop > 0 : ul.scrollTop + this.activeMax < ul.scrollHeight){
433 Ext.fly(t).addClass(['x-menu-item-active', 'x-menu-scroller-active']);
438 onScrollerOut : function(e, t){
439 Ext.fly(t).removeClass(['x-menu-item-active', 'x-menu-scroller-active']);
442 <div id="method-Ext.menu.Menu-show"></div>/**
443 * If <code>{@link #floating}=true</code>, shows this menu relative to
444 * another element using {@link #showat}, otherwise uses {@link Ext.Component#show}.
445 * @param {Mixed} element The element to align to
446 * @param {String} position (optional) The {@link Ext.Element#alignTo} anchor position to use in aligning to
447 * the element (defaults to this.defaultAlign)
448 * @param {Ext.menu.Menu} parentMenu (optional) This menu's parent menu, if applicable (defaults to undefined)
450 show : function(el, pos, parentMenu){
452 this.parentMenu = parentMenu;
455 this.doLayout(false, true);
457 this.showAt(this.el.getAlignToXY(el, pos || this.defaultAlign, this.defaultOffsets), parentMenu);
459 Ext.menu.Menu.superclass.show.call(this);
463 <div id="method-Ext.menu.Menu-showAt"></div>/**
464 * Displays this menu at a specific xy position and fires the 'show' event if a
465 * handler for the 'beforeshow' event does not return false cancelling the operation.
466 * @param {Array} xyPosition Contains X & Y [x, y] values for the position at which to show the menu (coordinates are page-based)
467 * @param {Ext.menu.Menu} parentMenu (optional) This menu's parent menu, if applicable (defaults to undefined)
469 showAt : function(xy, parentMenu){
470 if(this.fireEvent('beforeshow', this) !== false){
471 this.parentMenu = parentMenu;
475 if(this.enableScrolling){
476 // set the position so we can figure out the constrain value.
478 //constrain the value, keep the y coordinate the same
479 this.constrainScroll(xy[1]);
480 xy = [this.el.adjustForConstraints(xy)[0], xy[1]];
482 //constrain to the viewport.
483 xy = this.el.adjustForConstraints(xy);
487 Ext.menu.Menu.superclass.onShow.call(this);
489 // internal event, used so we don't couple the layout to the menu
490 this.fireEvent('autosize', this);
497 this.fireEvent('show', this);
501 constrainScroll : function(y){
502 var max, full = this.ul.setHeight('auto').getHeight();
504 max = this.maxHeight ? this.maxHeight : Ext.fly(this.el.dom.parentNode).getViewSize(false).height - y;
506 max = this.getHeight();
508 if(full > max && max > 0){
509 this.activeMax = max - this.scrollerHeight * 2 - this.el.getFrameWidth('tb') - Ext.num(this.el.shadowOffset, 0);
510 this.ul.setHeight(this.activeMax);
511 this.createScrollers();
512 this.el.select('.x-menu-scroller').setDisplayed('');
514 this.ul.setHeight(full);
515 this.el.select('.x-menu-scroller').setDisplayed('none');
517 this.ul.dom.scrollTop = 0;
520 createScrollers : function(){
524 top: this.el.insertFirst({
526 cls: 'x-menu-scroller x-menu-scroller-top',
529 bottom: this.el.createChild({
531 cls: 'x-menu-scroller x-menu-scroller-bottom',
535 this.scroller.top.hover(this.onScrollerIn, this.onScrollerOut, this);
536 this.scroller.topRepeater = new Ext.util.ClickRepeater(this.scroller.top, {
538 click: this.onScroll.createDelegate(this, [null, this.scroller.top], false)
541 this.scroller.bottom.hover(this.onScrollerIn, this.onScrollerOut, this);
542 this.scroller.bottomRepeater = new Ext.util.ClickRepeater(this.scroller.bottom, {
544 click: this.onScroll.createDelegate(this, [null, this.scroller.bottom], false)
550 onLayout : function(){
551 if(this.isVisible()){
552 if(this.enableScrolling){
553 this.constrainScroll(this.el.getTop());
563 this.doFocus.defer(50, this);
567 doFocus : function(){
569 this.focusEl.focus();
573 <div id="method-Ext.menu.Menu-hide"></div>/**
574 * Hides this menu and optionally all parent menus
575 * @param {Boolean} deep (optional) True to hide all parent menus recursively, if any (defaults to false)
577 hide : function(deep){
578 this.deepHide = deep;
579 Ext.menu.Menu.superclass.hide.call(this);
580 delete this.deepHide;
585 Ext.menu.Menu.superclass.onHide.call(this);
586 this.deactivateActive();
587 if(this.el && this.floating){
590 var pm = this.parentMenu;
591 if(this.deepHide === true && pm){
595 pm.deactivateActive();
601 lookupComponent : function(c){
603 c = (c == 'separator' || c == '-') ? new Ext.menu.Separator() : new Ext.menu.TextItem(c);
604 this.applyDefaults(c);
607 c = this.getMenuItem(c);
608 }else if(c.tagName || c.el){ // element. Wrap it.
609 c = new Ext.BoxComponent({
617 applyDefaults : function(c){
618 if(!Ext.isString(c)){
619 c = Ext.menu.Menu.superclass.applyDefaults.call(this, c);
620 var d = this.internalDefaults;
623 Ext.applyIf(c.initialConfig, d);
634 getMenuItem : function(config){
636 if(!config.xtype && Ext.isBoolean(config.checked)){
637 return new Ext.menu.CheckItem(config)
639 return Ext.create(config, this.defaultType);
644 <div id="method-Ext.menu.Menu-addSeparator"></div>/**
645 * Adds a separator bar to the menu
646 * @return {Ext.menu.Item} The menu item that was added
648 addSeparator : function(){
649 return this.add(new Ext.menu.Separator());
652 <div id="method-Ext.menu.Menu-addElement"></div>/**
653 * Adds an {@link Ext.Element} object to the menu
654 * @param {Mixed} el The element or DOM node to add, or its id
655 * @return {Ext.menu.Item} The menu item that was added
657 addElement : function(el){
658 return this.add(new Ext.menu.BaseItem(el));
661 <div id="method-Ext.menu.Menu-addItem"></div>/**
662 * Adds an existing object based on {@link Ext.menu.BaseItem} to the menu
663 * @param {Ext.menu.Item} item The menu item to add
664 * @return {Ext.menu.Item} The menu item that was added
666 addItem : function(item){
667 return this.add(item);
670 <div id="method-Ext.menu.Menu-addMenuItem"></div>/**
671 * Creates a new {@link Ext.menu.Item} based an the supplied config object and adds it to the menu
672 * @param {Object} config A MenuItem config object
673 * @return {Ext.menu.Item} The menu item that was added
675 addMenuItem : function(config){
676 return this.add(this.getMenuItem(config));
679 <div id="method-Ext.menu.Menu-addText"></div>/**
680 * Creates a new {@link Ext.menu.TextItem} with the supplied text and adds it to the menu
681 * @param {String} text The text to display in the menu item
682 * @return {Ext.menu.Item} The menu item that was added
684 addText : function(text){
685 return this.add(new Ext.menu.TextItem(text));
689 onDestroy : function(){
690 var pm = this.parentMenu;
691 if(pm && pm.activeChild == this){
692 delete pm.activeChild;
694 delete this.parentMenu;
695 Ext.menu.Menu.superclass.onDestroy.call(this);
696 Ext.menu.MenuMgr.unregister(this);
697 Ext.EventManager.removeResizeListener(this.hide, this);
699 this.keyNav.disable();
701 var s = this.scroller;
703 Ext.destroy(s.topRepeater, s.bottomRepeater, s.top, s.bottom);
713 Ext.reg('menu', Ext.menu.Menu);
715 // MenuNav is a private utility class used internally by the Menu
716 Ext.menu.MenuNav = Ext.extend(Ext.KeyNav, function(){
718 if(!m.tryActivate(m.items.indexOf(m.activeItem)-1, -1)){
719 m.tryActivate(m.items.length-1, -1);
723 if(!m.tryActivate(m.items.indexOf(m.activeItem)+1, 1)){
728 constructor : function(menu){
729 Ext.menu.MenuNav.superclass.constructor.call(this, menu.el);
730 this.scope = this.menu = menu;
733 doRelay : function(e, h){
735 // Keystrokes within a form Field (e.g.: down in a Combo) do not navigate. Allow only TAB
736 if (this.menu.activeItem && this.menu.activeItem.isFormField && k != e.TAB) {
739 if(!this.menu.activeItem && e.isNavKeyPress() && k != e.SPACE && k != e.RETURN){
740 this.menu.tryActivate(0, 1);
743 return h.call(this.scope || this, e, this.menu);
746 tab: function(e, m) {
759 right : function(e, m){
761 m.activeItem.expandMenu(true);
765 left : function(e, m){
767 if(m.parentMenu && m.parentMenu.activeItem){
768 m.parentMenu.activeItem.activate();
772 enter : function(e, m){
775 m.activeItem.onClick(e);
776 m.fireEvent('click', this, m.activeItem);