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'>/**
19 </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}.
35 * {@img Ext.menu.Item/Ext.menu.Item.png Ext.menu.Item component}
39 * Ext.create('Ext.menu.Menu', {
43 * floating: false, // usually you want this set to True (default)
44 * renderTo: Ext.getBody(), // usually rendered by it's containing component
46 * text: 'regular item 1'
48 * text: 'regular item 2'
50 * text: 'regular item 3'
54 * Ext.create('Ext.menu.Menu', {
58 * floating: false, // usually you want this set to True (default)
59 * renderTo: Ext.getBody(), // usually rendered by it's containing component
61 * text: 'plain item 1'
63 * text: 'plain item 2'
65 * text: 'plain item 3'
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 * Menus are constrained to the document body by default
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 hideMode: 'visibility',
133 <span id='Ext-menu-Menu-cfg-ignoreParentClicks'> /**
134 </span> * @cfg {Boolean} ignoreParentClicks
135 * True to ignore clicks on any item in this menu that is a parent item (displays a submenu)
136 * so that the submenu is not dismissed when clicking the parent item. Defaults to `false`.
139 ignoreParentClicks: false,
143 <span id='Ext-menu-Menu-cfg-layout'> /**
144 </span> * @cfg {String/Object} layout @hide
147 <span id='Ext-menu-Menu-cfg-showSeparator'> /**
148 </span> * @cfg {Boolean} showSeparator True to show the icon separator. (defaults to true).
150 showSeparator : true,
152 <span id='Ext-menu-Menu-cfg-minWidth'> /**
153 </span> * @cfg {Number} minWidth
154 * The minimum width of the Menu. Defaults to `120`.
159 <span id='Ext-menu-Menu-cfg-plain'> /**
160 </span> * @cfg {Boolean} plain
161 * True to remove the incised line down the left side of the menu and to not
162 * indent general Component items. Defaults to `false`.
166 initComponent: function() {
168 prefix = Ext.baseCSSPrefix,
169 cls = [prefix + 'menu'],
170 bodyCls = me.bodyCls ? [me.bodyCls] : [];
173 <span id='Ext-menu-Menu-event-click'> /**
174 </span> * @event click
175 * Fires when this menu is clicked
176 * @param {Ext.menu.Menu} menu The menu which has been clicked
177 * @param {Ext.Component} item The menu item that was clicked. `undefined` if not applicable.
178 * @param {Ext.EventObject} e The underlying {@link Ext.EventObject}.
183 <span id='Ext-menu-Menu-event-mouseenter'> /**
184 </span> * @event mouseenter
185 * Fires when the mouse enters this menu
186 * @param {Ext.menu.Menu} menu The menu
187 * @param {Ext.EventObject} e The underlying {@link Ext.EventObject}
192 <span id='Ext-menu-Menu-event-mouseleave'> /**
193 </span> * @event mouseleave
194 * Fires when the mouse leaves this menu
195 * @param {Ext.menu.Menu} menu The menu
196 * @param {Ext.EventObject} e The underlying {@link Ext.EventObject}
201 <span id='Ext-menu-Menu-event-mouseover'> /**
202 </span> * @event mouseover
203 * Fires when the mouse is hovering over this menu
204 * @param {Ext.menu.Menu} menu The menu
205 * @param {Ext.Component} item The menu item that the mouse is over. `undefined` if not applicable.
206 * @param {Ext.EventObject} e The underlying {@link Ext.EventObject}
211 Ext.menu.Manager.register(me);
215 cls.push(prefix + 'menu-plain');
217 me.cls = cls.join(' ');
220 bodyCls.unshift(prefix + 'menu-body');
221 me.bodyCls = bodyCls.join(' ');
223 // Internal vbox layout, with scrolling overflow
224 // Placed in initComponent (rather than prototype) in order to support dynamic layout/scroller
225 // options if we wish to allow for such configurations on the Menu.
226 // e.g., scrolling speed, vbox align stretch, etc.
231 clearInnerCtOnLayout: true,
232 overflowHandler: 'Scroller'
235 // hidden defaults to false if floating is configured as false
236 if (me.floating === false && me.initialConfig.hidden !== true) {
240 me.callParent(arguments);
242 me.on('beforeshow', function() {
243 var hasItems = !!me.items.length;
244 // FIXME: When a menu has its show cancelled because of no items, it
245 // gets a visibility: hidden applied to it (instead of the default display: none)
246 // Not sure why, but we remove this style when we want to show again.
247 if (hasItems && me.rendered) {
248 me.el.setStyle('visibility', null);
254 afterRender: function(ct) {
256 prefix = Ext.baseCSSPrefix,
257 space = '&#160;';
259 me.callParent(arguments);
261 // TODO: Move this to a subTemplate When we support them in the future
262 if (me.showSeparator) {
263 me.iconSepEl = me.layout.getRenderTarget().insertFirst({
264 cls: prefix + 'menu-icon-separator',
269 me.focusEl = me.el.createChild({
270 cls: prefix + 'menu-focus',
277 mouseover: me.onMouseOver,
280 me.mouseMonitor = me.el.monitorMouseLeave(100, me.onMouseLeave, me);
282 if (me.showSeparator && ((!Ext.isStrict && Ext.isIE) || Ext.isIE6)) {
283 me.iconSepEl.setHeight(me.el.getHeight());
286 me.keyNav = Ext.create('Ext.menu.KeyNav', me);
289 afterLayout: function() {
291 me.callParent(arguments);
293 // For IE6 & IE quirks, we have to resize the el and body since position: absolute
294 // floating elements inherit their parent's width, making them the width of
295 // document.body instead of the width of their contents.
296 // This includes left/right dock items.
297 if ((!Ext.iStrict && Ext.isIE) || Ext.isIE6) {
298 var innerCt = me.layout.getRenderTarget(),
300 dis = me.dockedItems,
305 innerCtWidth = innerCt.getWidth();
307 newWidth = innerCtWidth + me.body.getBorderWidth('lr') + me.body.getPadding('lr');
309 // First set the body to the new width
310 me.body.setWidth(newWidth);
312 // Now we calculate additional width (docked items) and set the el's width
313 for (; i < l, di = dis.getAt(i); i++) {
314 if (di.dock == 'left' || di.dock == 'right') {
315 newWidth += di.getWidth();
318 me.el.setWidth(newWidth);
322 <span id='Ext-menu-Menu-method-canActivateItem'> /**
323 </span> * Returns whether a menu item can be activated or not.
326 canActivateItem: function(item) {
327 return item && !item.isDisabled() && item.isVisible() && (item.canActivate || item.getXTypes().indexOf('menuitem') < 0);
330 <span id='Ext-menu-Menu-method-deactivateActiveItem'> /**
331 </span> * Deactivates the current active item on the menu, if one exists.
333 deactivateActiveItem: function() {
337 me.activeItem.deactivate();
338 if (!me.activeItem.activated) {
339 delete me.activeItem;
342 if (me.focusedItem) {
343 me.focusedItem.blur();
344 if (!me.focusedItem.$focused) {
345 delete me.focusedItem;
350 clearStretch: function () {
351 // the vbox/stretchmax will set the el sizes and subsequent layouts will not
352 // reconsider them unless we clear the dimensions on the el's here:
354 this.items.each(function (item) {
355 // each menuItem component needs to layout again, so clear its cache
356 if (item.componentLayout) {
357 delete item.componentLayout.lastComponentSize;
360 item.el.setWidth(null);
370 me.callParent(arguments);
372 if (Ext.isIE6 || Ext.isIE7) {
373 // TODO - why does this need to be done (and not ok to do now)?
374 Ext.Function.defer(me.doComponentLayout, 10, me);
378 onRemove: function () {
380 this.callParent(arguments);
384 redoComponentLayout: function () {
387 this.doComponentLayout();
392 getFocusEl: function() {
398 this.deactivateActiveItem();
399 this.callParent(arguments);
403 getItemFromEvent: function(e) {
404 return this.getChildByElement(e.getTarget());
407 lookupComponent: function(cmp) {
410 if (Ext.isString(cmp)) {
411 cmp = me.lookupItemFromString(cmp);
412 } else if (Ext.isObject(cmp)) {
413 cmp = me.lookupItemFromObject(cmp);
416 // Apply our minWidth to all of our child components so it's accounted
417 // for in our VBox layout
418 cmp.minWidth = cmp.minWidth || me.minWidth;
424 lookupItemFromObject: function(cmp) {
426 prefix = Ext.baseCSSPrefix,
430 if (!cmp.isComponent) {
432 cmp = Ext.create('Ext.menu.' + (Ext.isBoolean(cmp.checked) ? 'Check': '') + 'Item', cmp);
434 cmp = Ext.ComponentManager.create(cmp, cmp.xtype);
438 if (cmp.isMenuItem) {
442 if (!cmp.isMenuItem && !cmp.dock) {
443 cls = [prefix + 'menu-item', prefix + 'menu-item-cmp'];
444 intercept = Ext.Function.createInterceptor;
446 // Wrap focus/blur to control component focus
447 cmp.focus = intercept(cmp.focus, function() {
448 this.$focused = true;
450 cmp.blur = intercept(cmp.blur, function() {
451 this.$focused = false;
454 if (!me.plain && (cmp.indent === true || cmp.iconCls === 'no-icon')) {
455 cls.push(prefix + 'menu-item-indent');
461 cmp.cls = (cmp.cls ? cmp.cls : '') + ' ' + cls.join(' ');
463 cmp.isMenuItem = true;
469 lookupItemFromString: function(cmp) {
470 return (cmp == 'separator' || cmp == '-') ?
471 Ext.createWidget('menuseparator')
472 : Ext.createWidget('menuitem', {
480 onClick: function(e) {
489 if ((e.getTarget() == me.focusEl.dom) || e.within(me.layout.getRenderTarget())) {
490 item = me.getItemFromEvent(e) || me.activeItem;
493 if (item.getXTypes().indexOf('menuitem') >= 0) {
494 if (!item.menu || !me.ignoreParentClicks) {
501 me.fireEvent('click', me, item, e);
505 onDestroy: function() {
508 Ext.menu.Manager.unregister(me);
510 me.el.un(me.mouseMonitor);
514 me.callParent(arguments);
517 onMouseLeave: function(e) {
520 me.deactivateActiveItem();
526 me.fireEvent('mouseleave', me, e);
529 onMouseOver: function(e) {
531 fromEl = e.getRelatedTarget(),
532 mouseEnter = !me.el.contains(fromEl),
533 item = me.getItemFromEvent(e);
535 if (mouseEnter && me.parentMenu) {
536 me.parentMenu.setActiveItem(me.parentItem);
537 me.parentMenu.mouseMonitor.mouseenter();
545 me.setActiveItem(item);
546 if (item.activated && item.expandMenu) {
551 me.fireEvent('mouseenter', me, e);
553 me.fireEvent('mouseover', me, item, e);
556 setActiveItem: function(item) {
559 if (item && (item != me.activeItem && item != me.focusedItem)) {
560 me.deactivateActiveItem();
561 if (me.canActivateItem(item)) {
564 if (item.activated) {
565 me.activeItem = item;
566 me.focusedItem = item;
571 me.focusedItem = item;
574 item.el.scrollIntoView(me.layout.getRenderTarget());
578 <span id='Ext-menu-Menu-method-showBy'> /**
579 </span> * Shows the floating menu by the specified {@link Ext.Component Component} or {@link Ext.core.Element Element}.
580 * @param {Mixed component} The {@link Ext.Component} or {@link Ext.core.Element} to show the menu by.
581 * @param {String} position (optional) Alignment position as used by {@link Ext.core.Element#getAlignToXY Ext.core.Element.getAlignToXY}. Defaults to `{@link #defaultAlign}`.
582 * @param {Array} offsets (optional) Alignment offsets as used by {@link Ext.core.Element#getAlignToXY Ext.core.Element.getAlignToXY}. Defaults to `undefined`.
583 * @return {Menu} This Menu.
586 showBy: function(cmp, pos, off) {
591 if (me.floating && cmp) {
592 me.layout.autoSize = true;
594 // show off-screen first so that we can calc position without causing a visual jump
597 // Component or Element
600 // Convert absolute to floatParent-relative coordinates if necessary.
601 xy = me.el.getAlignToXY(cmp, pos || me.defaultAlign, off);
602 if (me.floatParent) {
603 region = me.floatParent.getTargetEl().getViewRegion();
614 this.callParent(arguments);
620 doConstrain : function() {
625 returnY = y, normalY, parentEl, scrollTop, viewHeight;
629 full = me.getHeight();
631 parentEl = Ext.fly(me.el.dom.parentNode);
632 scrollTop = parentEl.getScroll().top;
633 viewHeight = parentEl.getViewSize().height;
634 //Normalize y by the scroll position for the parent element. Need to move it into the coordinate space
636 normalY = y - scrollTop;
637 max = me.maxHeight ? me.maxHeight : viewHeight - normalY;
638 if (full > viewHeight) {
640 //Set returnY equal to (0,0) in view space by reducing y by the value of normalY
641 returnY = y - normalY;
642 } else if (max < full) {
643 returnY = y - (full - max);
647 max = me.getHeight();
649 // Always respect maxHeight
651 max = Math.min(me.maxHeight, max);
653 if (full > max && max > 0){
654 me.layout.autoSize = false;
656 if (me.showSeparator){
657 me.iconSepEl.setHeight(me.layout.getRenderTarget().dom.scrollHeight);
660 vector = me.getConstrainVector(me.el.dom.parentNode);
662 me.setPosition(me.getPosition()[0] + vector[0]);