4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-menu-Menu-method-constructor'><span id='Ext-menu-Menu'>/**
19 </span></span> * @class Ext.menu.Menu
20 * @extends Ext.panel.Panel
22 * A menu object. This is the container to which you may add {@link Ext.menu.Item menu items}.
24 * Menus may contain either {@link Ext.menu.Item menu items}, or general {@link Ext.Component Components}.
25 * Menus may also contain {@link Ext.panel.AbstractPanel#dockedItems docked items} because it extends {@link Ext.panel.Panel}.
27 * To make a contained general {@link Ext.Component Component} line up with other {@link Ext.menu.Item menu items},
28 * specify `{@link Ext.menu.Item#iconCls iconCls}: 'no-icon'` _or_ `{@link Ext.menu.Item#indent indent}: true`.
29 * This reserves a space for an icon, and indents the Component in line with the other menu items.
30 * See {@link Ext.form.field.ComboBox}.{@link Ext.form.field.ComboBox#getListParent getListParent} for an example.
32 * By default, Menus are absolutely positioned, floating Components. By configuring a Menu with `{@link #floating}:false`,
33 * a Menu may be used as a child of a {@link Ext.container.Container Container}.
34 * {@img Ext.menu.Item/Ext.menu.Item.png Ext.menu.Item component}
36 Ext.create('Ext.menu.Menu', {
40 floating: false, // usually you want this set to True (default)
41 renderTo: Ext.getBody(), // usually rendered by it's containing component
43 text: 'regular item 1'
45 text: 'regular item 2'
47 text: 'regular item 3'
51 Ext.create('Ext.menu.Menu', {
55 floating: false, // usually you want this set to True (default)
56 renderTo: Ext.getBody(), // usually rendered by it's containing component
68 * @param {Object} config The config object
70 Ext.define('Ext.menu.Menu', {
71 extend: 'Ext.panel.Panel',
74 'Ext.layout.container.Fit',
75 'Ext.layout.container.VBox',
83 <span id='Ext-menu-Menu-cfg-allowOtherMenus'> /**
84 </span> * @cfg {Boolean} allowOtherMenus
85 * True to allow multiple menus to be displayed at the same time. Defaults to `false`.
88 allowOtherMenus: false,
90 <span id='Ext-menu-Menu-cfg-ariaRole'> /**
91 </span> * @cfg {String} ariaRole @hide
95 <span id='Ext-menu-Menu-cfg-autoRender'> /**
96 </span> * @cfg {Boolean} autoRender @hide
97 * floating is true, so autoRender always happens
100 <span id='Ext-menu-Menu-cfg-defaultAlign'> /**
101 </span> * @cfg {String} defaultAlign
102 * The default {@link Ext.core.Element#getAlignToXY Ext.core.Element#getAlignToXY} anchor position value for this menu
103 * relative to its element of origin. Defaults to `'tl-bl?'`.
106 defaultAlign: 'tl-bl?',
108 <span id='Ext-menu-Menu-cfg-floating'> /**
109 </span> * @cfg {Boolean} floating
110 * A Menu configured as `floating: true` (the default) will be rendered as an absolutely positioned,
111 * {@link Ext.Component#floating floating} {@link Ext.Component Component}. If configured as `floating: false`, the Menu may be
112 * used as a child item of another {@link Ext.container.Container Container}.
117 <span id='Ext-menu-Menu-cfg-constrain'> /**
118 </span> * @cfg {Boolean} @hide
119 * Menu performs its own size changing constraining, so ensure Component's constraining is not applied
123 <span id='Ext-menu-Menu-cfg-hidden'> /**
124 </span> * @cfg {Boolean} hidden
125 * True to initially render the Menu as hidden, requiring to be shown manually.
126 * Defaults to `true` when `floating: true`, and defaults to `false` when `floating: false`.
131 <span id='Ext-menu-Menu-cfg-ignoreParentClicks'> /**
132 </span> * @cfg {Boolean} ignoreParentClicks
133 * True to ignore clicks on any item in this menu that is a parent item (displays a submenu)
134 * so that the submenu is not dismissed when clicking the parent item. Defaults to `false`.
137 ignoreParentClicks: false,
141 <span id='Ext-menu-Menu-cfg-layout'> /**
142 </span> * @cfg {String/Object} layout @hide
145 <span id='Ext-menu-Menu-cfg-showSeparator'> /**
146 </span> * @cfg {Boolean} showSeparator True to show the icon separator. (defaults to true).
148 showSeparator : true,
150 <span id='Ext-menu-Menu-cfg-minWidth'> /**
151 </span> * @cfg {Number} minWidth
152 * The minimum width of the Menu. Defaults to `120`.
157 <span id='Ext-menu-Menu-cfg-plain'> /**
158 </span> * @cfg {Boolean} plain
159 * True to remove the incised line down the left side of the menu and to not
160 * indent general Component items. Defaults to `false`.
164 initComponent: function() {
166 prefix = Ext.baseCSSPrefix,
167 cls = [prefix + 'menu'],
168 bodyCls = me.bodyCls ? [me.bodyCls] : [];
171 <span id='Ext-menu-Menu-event-click'> /**
172 </span> * @event click
173 * Fires when this menu is clicked
174 * @param {Ext.menu.Menu} menu The menu which has been clicked
175 * @param {Ext.Component} item The menu item that was clicked. `undefined` if not applicable.
176 * @param {Ext.EventObject} e The underlying {@link Ext.EventObject}.
181 <span id='Ext-menu-Menu-event-mouseenter'> /**
182 </span> * @event mouseenter
183 * Fires when the mouse enters this menu
184 * @param {Ext.menu.Menu} menu The menu
185 * @param {Ext.EventObject} e The underlying {@link Ext.EventObject}
190 <span id='Ext-menu-Menu-event-mouseleave'> /**
191 </span> * @event mouseleave
192 * Fires when the mouse leaves this menu
193 * @param {Ext.menu.Menu} menu The menu
194 * @param {Ext.EventObject} e The underlying {@link Ext.EventObject}
199 <span id='Ext-menu-Menu-event-mouseover'> /**
200 </span> * @event mouseover
201 * Fires when the mouse is hovering over this menu
202 * @param {Ext.menu.Menu} menu The menu
203 * @param {Ext.Component} item The menu item that the mouse is over. `undefined` if not applicable.
204 * @param {Ext.EventObject} e The underlying {@link Ext.EventObject}
209 Ext.menu.Manager.register(me);
213 cls.push(prefix + 'menu-plain');
215 me.cls = cls.join(' ');
218 bodyCls.unshift(prefix + 'menu-body');
219 me.bodyCls = bodyCls.join(' ');
221 // Internal vbox layout, with scrolling overflow
222 // Placed in initComponent (rather than prototype) in order to support dynamic layout/scroller
223 // options if we wish to allow for such configurations on the Menu.
224 // e.g., scrolling speed, vbox align stretch, etc.
229 clearInnerCtOnLayout: true,
230 overflowHandler: 'Scroller'
233 // hidden defaults to false if floating is configured as false
234 if (me.floating === false && me.initialConfig.hidden !== true) {
238 me.callParent(arguments);
240 me.on('beforeshow', function() {
241 var hasItems = !!me.items.length;
242 // FIXME: When a menu has its show cancelled because of no items, it
243 // gets a visibility: hidden applied to it (instead of the default display: none)
244 // Not sure why, but we remove this style when we want to show again.
245 if (hasItems && me.rendered) {
246 me.el.setStyle('visibility', null);
252 afterRender: function(ct) {
254 prefix = Ext.baseCSSPrefix,
255 space = '&#160;';
257 me.callParent(arguments);
259 // TODO: Move this to a subTemplate When we support them in the future
260 if (me.showSeparator) {
261 me.iconSepEl = me.layout.getRenderTarget().insertFirst({
262 cls: prefix + 'menu-icon-separator',
267 me.focusEl = me.el.createChild({
268 cls: prefix + 'menu-focus',
275 mouseover: me.onMouseOver,
278 me.mouseMonitor = me.el.monitorMouseLeave(100, me.onMouseLeave, me);
280 if (me.showSeparator && ((!Ext.isStrict && Ext.isIE) || Ext.isIE6)) {
281 me.iconSepEl.setHeight(me.el.getHeight());
284 me.keyNav = Ext.create('Ext.menu.KeyNav', me);
287 afterLayout: function() {
289 me.callParent(arguments);
291 // For IE6 & IE quirks, we have to resize the el and body since position: absolute
292 // floating elements inherit their parent's width, making them the width of
293 // document.body instead of the width of their contents.
294 // This includes left/right dock items.
295 if ((!Ext.iStrict && Ext.isIE) || Ext.isIE6) {
296 var innerCt = me.layout.getRenderTarget(),
298 dis = me.dockedItems,
303 innerCtWidth = innerCt.getWidth();
305 newWidth = innerCtWidth + me.body.getBorderWidth('lr') + me.body.getPadding('lr');
307 // First set the body to the new width
308 me.body.setWidth(newWidth);
310 // Now we calculate additional width (docked items) and set the el's width
311 for (; i < l, di = dis.getAt(i); i++) {
312 if (di.dock == 'left' || di.dock == 'right') {
313 newWidth += di.getWidth();
316 me.el.setWidth(newWidth);
320 <span id='Ext-menu-Menu-method-canActivateItem'> /**
321 </span> * Returns whether a menu item can be activated or not.
324 canActivateItem: function(item) {
325 return item && !item.isDisabled() && item.isVisible() && (item.canActivate || item.getXTypes().indexOf('menuitem') < 0);
328 <span id='Ext-menu-Menu-method-deactivateActiveItem'> /**
329 </span> * Deactivates the current active item on the menu, if one exists.
331 deactivateActiveItem: function() {
335 me.activeItem.deactivate();
336 if (!me.activeItem.activated) {
337 delete me.activeItem;
340 if (me.focusedItem) {
341 me.focusedItem.blur();
342 if (!me.focusedItem.$focused) {
343 delete me.focusedItem;
349 getFocusEl: function() {
355 this.deactivateActiveItem();
356 this.callParent(arguments);
360 getItemFromEvent: function(e) {
361 return this.getChildByElement(e.getTarget());
364 lookupComponent: function(cmp) {
367 if (Ext.isString(cmp)) {
368 cmp = me.lookupItemFromString(cmp);
369 } else if (Ext.isObject(cmp)) {
370 cmp = me.lookupItemFromObject(cmp);
373 // Apply our minWidth to all of our child components so it's accounted
374 // for in our VBox layout
375 cmp.minWidth = cmp.minWidth || me.minWidth;
381 lookupItemFromObject: function(cmp) {
383 prefix = Ext.baseCSSPrefix,
387 if (!cmp.isComponent) {
389 cmp = Ext.create('Ext.menu.' + (Ext.isBoolean(cmp.checked) ? 'Check': '') + 'Item', cmp);
391 cmp = Ext.ComponentManager.create(cmp, cmp.xtype);
395 if (cmp.isMenuItem) {
399 if (!cmp.isMenuItem && !cmp.dock) {
400 cls = [prefix + 'menu-item', prefix + 'menu-item-cmp'];
401 intercept = Ext.Function.createInterceptor;
403 // Wrap focus/blur to control component focus
404 cmp.focus = intercept(cmp.focus, function() {
405 this.$focused = true;
407 cmp.blur = intercept(cmp.blur, function() {
408 this.$focused = false;
411 if (!me.plain && (cmp.indent === true || cmp.iconCls === 'no-icon')) {
412 cls.push(prefix + 'menu-item-indent');
418 cmp.cls = (cmp.cls ? cmp.cls : '') + ' ' + cls.join(' ');
420 cmp.isMenuItem = true;
426 lookupItemFromString: function(cmp) {
427 return (cmp == 'separator' || cmp == '-') ?
428 Ext.createWidget('menuseparator')
429 : Ext.createWidget('menuitem', {
437 onClick: function(e) {
446 if ((e.getTarget() == me.focusEl.dom) || e.within(me.layout.getRenderTarget())) {
447 item = me.getItemFromEvent(e) || me.activeItem;
450 if (item.getXTypes().indexOf('menuitem') >= 0) {
451 if (!item.menu || !me.ignoreParentClicks) {
458 me.fireEvent('click', me, item, e);
462 onDestroy: function() {
465 Ext.menu.Manager.unregister(me);
467 me.el.un(me.mouseMonitor);
471 me.callParent(arguments);
474 onMouseLeave: function(e) {
477 me.deactivateActiveItem();
483 me.fireEvent('mouseleave', me, e);
486 onMouseOver: function(e) {
488 fromEl = e.getRelatedTarget(),
489 mouseEnter = !me.el.contains(fromEl),
490 item = me.getItemFromEvent(e);
492 if (mouseEnter && me.parentMenu) {
493 me.parentMenu.setActiveItem(me.parentItem);
494 me.parentMenu.mouseMonitor.mouseenter();
502 me.setActiveItem(item);
503 if (item.activated && item.expandMenu) {
508 me.fireEvent('mouseenter', me, e);
510 me.fireEvent('mouseover', me, item, e);
513 setActiveItem: function(item) {
516 if (item && (item != me.activeItem && item != me.focusedItem)) {
517 me.deactivateActiveItem();
518 if (me.canActivateItem(item)) {
521 if (item.activated) {
522 me.activeItem = item;
523 me.focusedItem = item;
528 me.focusedItem = item;
531 item.el.scrollIntoView(me.layout.getRenderTarget());
535 <span id='Ext-menu-Menu-method-showBy'> /**
536 </span> * Shows the floating menu by the specified {@link Ext.Component Component} or {@link Ext.core.Element Element}.
537 * @param {Mixed component} The {@link Ext.Component} or {@link Ext.core.Element} to show the menu by.
538 * @param {String} position (optional) Alignment position as used by {@link Ext.core.Element#getAlignToXY Ext.core.Element.getAlignToXY}. Defaults to `{@link #defaultAlign}`.
539 * @param {Array} offsets (optional) Alignment offsets as used by {@link Ext.core.Element#getAlignToXY Ext.core.Element.getAlignToXY}. Defaults to `undefined`.
540 * @return {Menu} This Menu.
543 showBy: function(cmp, pos, off) {
548 if (me.floating && cmp) {
549 me.layout.autoSize = true;
552 // Component or Element
555 // Convert absolute to floatParent-relative coordinates if necessary.
556 xy = me.el.getAlignToXY(cmp, pos || me.defaultAlign, off);
557 if (me.floatParent) {
558 region = me.floatParent.getTargetEl().getViewRegion();
569 this.callParent(arguments);
575 doConstrain : function() {
580 returnY = y, normalY, parentEl, scrollTop, viewHeight;
584 full = me.getHeight();
586 parentEl = Ext.fly(me.el.dom.parentNode);
587 scrollTop = parentEl.getScroll().top;
588 viewHeight = parentEl.getViewSize().height;
589 //Normalize y by the scroll position for the parent element. Need to move it into the coordinate space
591 normalY = y - scrollTop;
592 max = me.maxHeight ? me.maxHeight : viewHeight - normalY;
593 if (full > viewHeight) {
595 //Set returnY equal to (0,0) in view space by reducing y by the value of normalY
596 returnY = y - normalY;
597 } else if (max < full) {
598 returnY = y - (full - max);
602 max = me.getHeight();
604 // Always respect maxHeight
606 max = Math.min(me.maxHeight, max);
608 if (full > max && max > 0){
609 me.layout.autoSize = false;
611 if (me.showSeparator){
612 me.iconSepEl.setHeight(me.layout.getRenderTarget().dom.scrollHeight);
615 vector = me.getConstrainVector();
617 me.setPosition(me.getPosition()[0] + vector[0]);