@angular/material
Version:
Angular Material
963 lines (954 loc) • 38.2 kB
JavaScript
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { ChangeDetectionStrategy, Component, ContentChildren, Directive, ElementRef, EventEmitter, Inject, InjectionToken, Input, NgModule, NgZone, Optional, Output, Self, TemplateRef, ViewChild, ViewContainerRef, ViewEncapsulation } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MatCommonModule, MatRippleModule, mixinDisabled } from '@angular/material/core';
import { Overlay, OverlayConfig, OverlayModule } from '@angular/cdk/overlay';
import { FocusKeyManager, isFakeMousedownFromScreenReader } from '@angular/cdk/a11y';
import { ESCAPE, LEFT_ARROW, RIGHT_ARROW } from '@angular/cdk/keycodes';
import { RxChain, filter, first, startWith, switchMap } from '@angular/cdk/rxjs';
import { merge } from 'rxjs/observable/merge';
import { Subscription } from 'rxjs/Subscription';
import { animate, state, style, transition, trigger } from '@angular/animations';
import { __extends } from 'tslib';
import * as tslib_1 from 'tslib';
import { Subject } from 'rxjs/Subject';
import { Directionality } from '@angular/cdk/bidi';
import { TemplatePortal } from '@angular/cdk/portal';
import { of } from 'rxjs/observable/of';
/**
* Below are all the animations for the mat-menu component.
* Animation duration and timing values are based on:
* https://material.io/guidelines/components/menus.html#menus-usage
*/
/**
* This animation controls the menu panel's entry and exit from the page.
*
* When the menu panel is added to the DOM, it scales in and fades in its border.
*
* When the menu panel is removed from the DOM, it simply fades out after a brief
* delay to display the ripple.
*/
// TODO(kara): switch to :enter and :leave once Mobile Safari is sorted out.
var transformMenu = trigger('transformMenu', [
state('void', style({
opacity: 0,
// This starts off from 0.01, instead of 0, because there's an issue in the Angular animations
// as of 4.2, which causes the animation to be skipped if it starts from 0.
transform: 'scale(0.01, 0.01)'
})),
state('enter-start', style({
opacity: 1,
transform: 'scale(1, 0.5)'
})),
state('enter', style({
transform: 'scale(1, 1)'
})),
transition('void => enter-start', animate('100ms linear')),
transition('enter-start => enter', animate('300ms cubic-bezier(0.25, 0.8, 0.25, 1)')),
transition('* => void', animate('150ms 50ms linear', style({ opacity: 0 })))
]);
/**
* This animation fades in the background color and content of the menu panel
* after its containing element is scaled in.
*/
var fadeInItems = trigger('fadeInItems', [
state('showing', style({ opacity: 1 })),
transition('void => *', [
style({ opacity: 0 }),
animate('400ms 100ms cubic-bezier(0.55, 0, 0.55, 0.2)')
])
]);
/**
* Throws an exception for the case when menu trigger doesn't have a valid mat-menu instance
* \@docs-private
* @return {?}
*/
function throwMatMenuMissingError() {
throw Error("mat-menu-trigger: must pass in an mat-menu instance.\n\n Example:\n <mat-menu #menu=\"matMenu\"></mat-menu>\n <button [matMenuTriggerFor]=\"menu\"></button>");
}
/**
* Throws an exception for the case when menu's x-position value isn't valid.
* In other words, it doesn't match 'before' or 'after'.
* \@docs-private
* @return {?}
*/
function throwMatMenuInvalidPositionX() {
throw Error("x-position value must be either 'before' or after'.\n Example: <mat-menu x-position=\"before\" #menu=\"matMenu\"></mat-menu>");
}
/**
* Throws an exception for the case when menu's y-position value isn't valid.
* In other words, it doesn't match 'above' or 'below'.
* \@docs-private
* @return {?}
*/
function throwMatMenuInvalidPositionY() {
throw Error("y-position value must be either 'above' or below'.\n Example: <mat-menu y-position=\"above\" #menu=\"matMenu\"></mat-menu>");
}
/**
* \@docs-private
*/
var MatMenuItemBase = (function () {
function MatMenuItemBase() {
}
return MatMenuItemBase;
}());
var _MatMenuItemMixinBase = mixinDisabled(MatMenuItemBase);
/**
* This directive is intended to be used inside an mat-menu tag.
* It exists mostly to set the role attribute.
*/
var MatMenuItem = (function (_super) {
__extends(MatMenuItem, _super);
/**
* @param {?} _elementRef
*/
function MatMenuItem(_elementRef) {
var _this = _super.call(this) || this;
_this._elementRef = _elementRef;
/**
* Stream that emits when the menu item is hovered.
*/
_this.hover = new Subject();
/**
* Whether the menu item is highlighted.
*/
_this._highlighted = false;
/**
* Whether the menu item acts as a trigger for a sub-menu.
*/
_this._triggersSubmenu = false;
return _this;
}
/**
* Focuses the menu item.
* @return {?}
*/
MatMenuItem.prototype.focus = function () {
this._getHostElement().focus();
};
/**
* @return {?}
*/
MatMenuItem.prototype.ngOnDestroy = function () {
this.hover.complete();
};
/**
* Used to set the `tabindex`.
* @return {?}
*/
MatMenuItem.prototype._getTabIndex = function () {
return this.disabled ? '-1' : '0';
};
/**
* Returns the host DOM element.
* @return {?}
*/
MatMenuItem.prototype._getHostElement = function () {
return this._elementRef.nativeElement;
};
/**
* Prevents the default element actions if it is disabled.
* @param {?} event
* @return {?}
*/
MatMenuItem.prototype._checkDisabled = function (event) {
if (this.disabled) {
event.preventDefault();
event.stopPropagation();
}
};
/**
* Emits to the hover stream.
* @return {?}
*/
MatMenuItem.prototype._emitHoverEvent = function () {
if (!this.disabled) {
this.hover.next(this);
}
};
/**
* Gets the label to be used when determining whether the option should be focused.
* @return {?}
*/
MatMenuItem.prototype.getLabel = function () {
var /** @type {?} */ element = this._elementRef.nativeElement;
var /** @type {?} */ output = '';
if (element.childNodes) {
var /** @type {?} */ length_1 = element.childNodes.length;
// Go through all the top-level text nodes and extract their text.
// We skip anything that's not a text node to prevent the text from
// being thrown off by something like an icon.
for (var /** @type {?} */ i = 0; i < length_1; i++) {
if (element.childNodes[i].nodeType === Node.TEXT_NODE) {
output += element.childNodes[i].textContent;
}
}
}
return output.trim();
};
MatMenuItem.decorators = [
{ type: Component, args: [{selector: '[mat-menu-item]',
exportAs: 'matMenuItem',
inputs: ['disabled'],
host: {
'role': 'menuitem',
'class': 'mat-menu-item',
'[class.mat-menu-item-highlighted]': '_highlighted',
'[class.mat-menu-item-submenu-trigger]': '_triggersSubmenu',
'[attr.tabindex]': '_getTabIndex()',
'[attr.aria-disabled]': 'disabled.toString()',
'[attr.disabled]': 'disabled || null',
'(click)': '_checkDisabled($event)',
'(mouseenter)': '_emitHoverEvent()',
},
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
preserveWhitespaces: false,
template: "<ng-content></ng-content><div class=\"mat-menu-ripple\" *ngIf=\"!disabled\" mat-ripple [matRippleTrigger]=\"_getHostElement()\"></div>",
},] },
];
/**
* @nocollapse
*/
MatMenuItem.ctorParameters = function () { return [
{ type: ElementRef, },
]; };
return MatMenuItem;
}(_MatMenuItemMixinBase));
/**
* Injection token to be used to override the default options for `mat-menu`.
*/
var MAT_MENU_DEFAULT_OPTIONS = new InjectionToken('mat-menu-default-options');
/**
* Start elevation for the menu panel.
* \@docs-private
*/
var MAT_MENU_BASE_ELEVATION = 2;
var MatMenu = (function () {
/**
* @param {?} _elementRef
* @param {?} _ngZone
* @param {?} _defaultOptions
*/
function MatMenu(_elementRef, _ngZone, _defaultOptions) {
this._elementRef = _elementRef;
this._ngZone = _ngZone;
this._defaultOptions = _defaultOptions;
this._xPosition = this._defaultOptions.xPosition;
this._yPosition = this._defaultOptions.yPosition;
/**
* Subscription to tab events on the menu panel
*/
this._tabSubscription = Subscription.EMPTY;
/**
* Config object to be passed into the menu's ngClass
*/
this._classList = {};
/**
* Current state of the panel animation.
*/
this._panelAnimationState = 'void';
/**
* Whether the menu should overlap its trigger.
*/
this.overlapTrigger = this._defaultOptions.overlapTrigger;
/**
* Event emitted when the menu is closed.
*/
this.close = new EventEmitter();
}
Object.defineProperty(MatMenu.prototype, "xPosition", {
/**
* Position of the menu in the X axis.
* @return {?}
*/
get: function () { return this._xPosition; },
/**
* @param {?} value
* @return {?}
*/
set: function (value) {
if (value !== 'before' && value !== 'after') {
throwMatMenuInvalidPositionX();
}
this._xPosition = value;
this.setPositionClasses();
},
enumerable: true,
configurable: true
});
Object.defineProperty(MatMenu.prototype, "yPosition", {
/**
* Position of the menu in the Y axis.
* @return {?}
*/
get: function () { return this._yPosition; },
/**
* @param {?} value
* @return {?}
*/
set: function (value) {
if (value !== 'above' && value !== 'below') {
throwMatMenuInvalidPositionY();
}
this._yPosition = value;
this.setPositionClasses();
},
enumerable: true,
configurable: true
});
Object.defineProperty(MatMenu.prototype, "classList", {
/**
* This method takes classes set on the host mat-menu element and applies them on the
* menu template that displays in the overlay container. Otherwise, it's difficult
* to style the containing menu from outside the component.
* @param {?} classes list of class names
* @return {?}
*/
set: function (classes) {
if (classes && classes.length) {
this._classList = classes.split(' ').reduce(function (obj, className) {
obj[className] = true;
return obj;
}, {});
this._elementRef.nativeElement.className = '';
this.setPositionClasses();
}
},
enumerable: true,
configurable: true
});
/**
* @return {?}
*/
MatMenu.prototype.ngAfterContentInit = function () {
var _this = this;
this._keyManager = new FocusKeyManager(this.items).withWrap().withTypeAhead();
this._tabSubscription = this._keyManager.tabOut.subscribe(function () { return _this.close.emit('keydown'); });
};
/**
* @return {?}
*/
MatMenu.prototype.ngOnDestroy = function () {
this._tabSubscription.unsubscribe();
this.close.emit();
this.close.complete();
};
/**
* Stream that emits whenever the hovered menu item changes.
* @return {?}
*/
MatMenu.prototype.hover = function () {
var _this = this;
if (this.items) {
return RxChain.from(this.items.changes)
.call(startWith, this.items)
.call(switchMap, function (items) { return merge.apply(void 0, items.map(function (item) { return item.hover; })); })
.result();
}
return RxChain.from(this._ngZone.onStable.asObservable())
.call(first)
.call(switchMap, function () { return _this.hover(); })
.result();
};
/**
* Handle a keyboard event from the menu, delegating to the appropriate action.
* @param {?} event
* @return {?}
*/
MatMenu.prototype._handleKeydown = function (event) {
switch (event.keyCode) {
case ESCAPE:
this.close.emit('keydown');
event.stopPropagation();
break;
case LEFT_ARROW:
if (this.parentMenu && this.direction === 'ltr') {
this.close.emit('keydown');
}
break;
case RIGHT_ARROW:
if (this.parentMenu && this.direction === 'rtl') {
this.close.emit('keydown');
}
break;
default:
this._keyManager.onKeydown(event);
}
};
/**
* Focus the first item in the menu. This method is used by the menu trigger
* to focus the first item when the menu is opened by the ENTER key.
* @return {?}
*/
MatMenu.prototype.focusFirstItem = function () {
this._keyManager.setFirstItemActive();
};
/**
* It's necessary to set position-based classes to ensure the menu panel animation
* folds out from the correct direction.
* @param {?=} posX
* @param {?=} posY
* @return {?}
*/
MatMenu.prototype.setPositionClasses = function (posX, posY) {
if (posX === void 0) { posX = this.xPosition; }
if (posY === void 0) { posY = this.yPosition; }
this._classList['mat-menu-before'] = posX === 'before';
this._classList['mat-menu-after'] = posX === 'after';
this._classList['mat-menu-above'] = posY === 'above';
this._classList['mat-menu-below'] = posY === 'below';
};
/**
* Sets the menu panel elevation.
* @param {?} depth Number of parent menus that come before the menu.
* @return {?}
*/
MatMenu.prototype.setElevation = function (depth) {
// The elevation starts at the base and increases by one for each level.
var /** @type {?} */ newElevation = "mat-elevation-z" + (MAT_MENU_BASE_ELEVATION + depth);
var /** @type {?} */ customElevation = Object.keys(this._classList).find(function (c) { return c.startsWith('mat-elevation-z'); });
if (!customElevation || customElevation === this._previousElevation) {
if (this._previousElevation) {
this._classList[this._previousElevation] = false;
}
this._classList[newElevation] = true;
this._previousElevation = newElevation;
}
};
/**
* Starts the enter animation.
* @return {?}
*/
MatMenu.prototype._startAnimation = function () {
this._panelAnimationState = 'enter-start';
};
/**
* Resets the panel animation to its initial state.
* @return {?}
*/
MatMenu.prototype._resetAnimation = function () {
this._panelAnimationState = 'void';
};
/**
* Callback that is invoked when the panel animation completes.
* @param {?} event
* @return {?}
*/
MatMenu.prototype._onAnimationDone = function (event) {
// After the initial expansion is done, trigger the second phase of the enter animation.
if (event.toState === 'enter-start') {
this._panelAnimationState = 'enter';
}
};
MatMenu.decorators = [
{ type: Component, args: [{selector: 'mat-menu',
template: "<ng-template><div class=\"mat-menu-panel\" [ngClass]=\"_classList\" (keydown)=\"_handleKeydown($event)\" (click)=\"close.emit('click')\" [@transformMenu]=\"_panelAnimationState\" (@transformMenu.done)=\"_onAnimationDone($event)\" role=\"menu\"><div class=\"mat-menu-content\" [@fadeInItems]=\"'showing'\"><ng-content></ng-content></div></div></ng-template>",
styles: [".mat-menu-panel{min-width:112px;max-width:280px;overflow:auto;-webkit-overflow-scrolling:touch;max-height:calc(100vh - 48px);border-radius:2px}.mat-menu-panel:not([class*=mat-elevation-z]){box-shadow:0 3px 1px -2px rgba(0,0,0,.2),0 2px 2px 0 rgba(0,0,0,.14),0 1px 5px 0 rgba(0,0,0,.12)}.mat-menu-panel.mat-menu-after.mat-menu-below{transform-origin:left top}.mat-menu-panel.mat-menu-after.mat-menu-above{transform-origin:left bottom}.mat-menu-panel.mat-menu-before.mat-menu-below{transform-origin:right top}.mat-menu-panel.mat-menu-before.mat-menu-above{transform-origin:right bottom}[dir=rtl] .mat-menu-panel.mat-menu-after.mat-menu-below{transform-origin:right top}[dir=rtl] .mat-menu-panel.mat-menu-after.mat-menu-above{transform-origin:right bottom}[dir=rtl] .mat-menu-panel.mat-menu-before.mat-menu-below{transform-origin:left top}[dir=rtl] .mat-menu-panel.mat-menu-before.mat-menu-above{transform-origin:left bottom}.mat-menu-panel.ng-animating{pointer-events:none}@media screen and (-ms-high-contrast:active){.mat-menu-panel{outline:solid 1px}}.mat-menu-content{padding-top:8px;padding-bottom:8px}.mat-menu-item{-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;cursor:pointer;outline:0;border:none;-webkit-tap-highlight-color:transparent;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;display:block;line-height:48px;height:48px;padding:0 16px;text-align:left;text-decoration:none;position:relative}.mat-menu-item[disabled]{cursor:default}[dir=rtl] .mat-menu-item{text-align:right}.mat-menu-item .mat-icon{margin-right:16px}[dir=rtl] .mat-menu-item .mat-icon{margin-left:16px;margin-right:0}.mat-menu-item .mat-icon{vertical-align:middle}.mat-menu-item-submenu-trigger{padding-right:32px}.mat-menu-item-submenu-trigger::after{width:0;height:0;border-style:solid;border-width:5px 0 5px 5px;border-color:transparent transparent transparent currentColor;content:'';display:inline-block;position:absolute;top:50%;right:16px;transform:translateY(-50%)}[dir=rtl] .mat-menu-item-submenu-trigger{padding-right:8px;padding-left:32px}[dir=rtl] .mat-menu-item-submenu-trigger::after{right:auto;left:16px;transform:rotateY(180deg) translateY(-50%)}button.mat-menu-item{width:100%}.mat-menu-ripple{top:0;left:0;right:0;bottom:0;position:absolute}"],
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
preserveWhitespaces: false,
animations: [
transformMenu,
fadeInItems
],
exportAs: 'matMenu'
},] },
];
/**
* @nocollapse
*/
MatMenu.ctorParameters = function () { return [
{ type: ElementRef, },
{ type: NgZone, },
{ type: undefined, decorators: [{ type: Inject, args: [MAT_MENU_DEFAULT_OPTIONS,] },] },
]; };
MatMenu.propDecorators = {
'xPosition': [{ type: Input },],
'yPosition': [{ type: Input },],
'templateRef': [{ type: ViewChild, args: [TemplateRef,] },],
'items': [{ type: ContentChildren, args: [MatMenuItem,] },],
'overlapTrigger': [{ type: Input },],
'classList': [{ type: Input, args: ['class',] },],
'close': [{ type: Output },],
};
return MatMenu;
}());
/**
* Injection token that determines the scroll handling while the menu is open.
*/
var MAT_MENU_SCROLL_STRATEGY = new InjectionToken('mat-menu-scroll-strategy');
/**
* \@docs-private
* @param {?} overlay
* @return {?}
*/
function MAT_MENU_SCROLL_STRATEGY_PROVIDER_FACTORY(overlay) {
return function () { return overlay.scrollStrategies.reposition(); };
}
/**
* \@docs-private
*/
var MAT_MENU_SCROLL_STRATEGY_PROVIDER = {
provide: MAT_MENU_SCROLL_STRATEGY,
deps: [Overlay],
useFactory: MAT_MENU_SCROLL_STRATEGY_PROVIDER_FACTORY,
};
/**
* Default top padding of the menu panel.
*/
var MENU_PANEL_TOP_PADDING = 8;
/**
* This directive is intended to be used in conjunction with an mat-menu tag. It is
* responsible for toggling the display of the provided menu instance.
*/
var MatMenuTrigger = (function () {
/**
* @param {?} _overlay
* @param {?} _element
* @param {?} _viewContainerRef
* @param {?} _scrollStrategy
* @param {?} _parentMenu
* @param {?} _menuItemInstance
* @param {?} _dir
*/
function MatMenuTrigger(_overlay, _element, _viewContainerRef, _scrollStrategy, _parentMenu, _menuItemInstance, _dir) {
this._overlay = _overlay;
this._element = _element;
this._viewContainerRef = _viewContainerRef;
this._scrollStrategy = _scrollStrategy;
this._parentMenu = _parentMenu;
this._menuItemInstance = _menuItemInstance;
this._dir = _dir;
this._overlayRef = null;
this._menuOpen = false;
this._closeSubscription = Subscription.EMPTY;
this._positionSubscription = Subscription.EMPTY;
this._hoverSubscription = Subscription.EMPTY;
this._openedByMouse = false;
/**
* Event emitted when the associated menu is opened.
*/
this.onMenuOpen = new EventEmitter();
/**
* Event emitted when the associated menu is closed.
*/
this.onMenuClose = new EventEmitter();
if (_menuItemInstance) {
_menuItemInstance._triggersSubmenu = this.triggersSubmenu();
}
}
Object.defineProperty(MatMenuTrigger.prototype, "_deprecatedMatMenuTriggerFor", {
/**
* @deprecated
* @return {?}
*/
get: function () {
return this.menu;
},
/**
* @param {?} v
* @return {?}
*/
set: function (v) {
this.menu = v;
},
enumerable: true,
configurable: true
});
/**
* @return {?}
*/
MatMenuTrigger.prototype.ngAfterContentInit = function () {
var _this = this;
this._checkMenu();
this.menu.close.subscribe(function (reason) {
_this._destroyMenu();
// If a click closed the menu, we should close the entire chain of nested menus.
if (reason === 'click' && _this._parentMenu) {
_this._parentMenu.close.emit(reason);
}
});
if (this.triggersSubmenu()) {
// Subscribe to changes in the hovered item in order to toggle the panel.
this._hoverSubscription = filter
.call(this._parentMenu.hover(), function (active) { return active === _this._menuItemInstance; })
.subscribe(function () {
_this._openedByMouse = true;
_this.openMenu();
});
}
};
/**
* @return {?}
*/
MatMenuTrigger.prototype.ngOnDestroy = function () {
if (this._overlayRef) {
this._overlayRef.dispose();
this._overlayRef = null;
}
this._cleanUpSubscriptions();
};
Object.defineProperty(MatMenuTrigger.prototype, "menuOpen", {
/**
* Whether the menu is open.
* @return {?}
*/
get: function () {
return this._menuOpen;
},
enumerable: true,
configurable: true
});
Object.defineProperty(MatMenuTrigger.prototype, "dir", {
/**
* The text direction of the containing app.
* @return {?}
*/
get: function () {
return this._dir && this._dir.value === 'rtl' ? 'rtl' : 'ltr';
},
enumerable: true,
configurable: true
});
/**
* Whether the menu triggers a sub-menu or a top-level one.
* @return {?}
*/
MatMenuTrigger.prototype.triggersSubmenu = function () {
return !!(this._menuItemInstance && this._parentMenu);
};
/**
* Toggles the menu between the open and closed states.
* @return {?}
*/
MatMenuTrigger.prototype.toggleMenu = function () {
return this._menuOpen ? this.closeMenu() : this.openMenu();
};
/**
* Opens the menu.
* @return {?}
*/
MatMenuTrigger.prototype.openMenu = function () {
var _this = this;
if (!this._menuOpen) {
this._createOverlay().attach(this._portal);
this._closeSubscription = this._menuClosingActions().subscribe(function () {
_this.menu.close.emit();
});
this._initMenu();
if (this.menu instanceof MatMenu) {
this.menu._startAnimation();
}
}
};
/**
* Closes the menu.
* @return {?}
*/
MatMenuTrigger.prototype.closeMenu = function () {
this.menu.close.emit();
};
/**
* Focuses the menu trigger.
* @return {?}
*/
MatMenuTrigger.prototype.focus = function () {
this._element.nativeElement.focus();
};
/**
* Closes the menu and does the necessary cleanup.
* @return {?}
*/
MatMenuTrigger.prototype._destroyMenu = function () {
if (this._overlayRef && this.menuOpen) {
this._resetMenu();
this._overlayRef.detach();
this._closeSubscription.unsubscribe();
if (this.menu instanceof MatMenu) {
this.menu._resetAnimation();
}
}
};
/**
* This method sets the menu state to open and focuses the first item if
* the menu was opened via the keyboard.
* @return {?}
*/
MatMenuTrigger.prototype._initMenu = function () {
this.menu.parentMenu = this.triggersSubmenu() ? this._parentMenu : undefined;
this.menu.direction = this.dir;
this._setMenuElevation();
this._setIsMenuOpen(true);
// Should only set focus if opened via the keyboard, so keyboard users can
// can easily navigate menu items. According to spec, mouse users should not
// see the focus style.
if (!this._openedByMouse) {
this.menu.focusFirstItem();
}
};
/**
* Updates the menu elevation based on the amount of parent menus that it has.
* @return {?}
*/
MatMenuTrigger.prototype._setMenuElevation = function () {
if (this.menu.setElevation) {
var /** @type {?} */ depth = 0;
var /** @type {?} */ parentMenu = this.menu.parentMenu;
while (parentMenu) {
depth++;
parentMenu = parentMenu.parentMenu;
}
this.menu.setElevation(depth);
}
};
/**
* This method resets the menu when it's closed, most importantly restoring
* focus to the menu trigger if the menu was opened via the keyboard.
* @return {?}
*/
MatMenuTrigger.prototype._resetMenu = function () {
this._setIsMenuOpen(false);
// Focus only needs to be reset to the host element if the menu was opened
// by the keyboard and manually shifted to the first menu item.
if (!this._openedByMouse) {
this.focus();
}
this._openedByMouse = false;
};
/**
* @param {?} isOpen
* @return {?}
*/
MatMenuTrigger.prototype._setIsMenuOpen = function (isOpen) {
this._menuOpen = isOpen;
this._menuOpen ? this.onMenuOpen.emit() : this.onMenuClose.emit();
if (this.triggersSubmenu()) {
this._menuItemInstance._highlighted = isOpen;
}
};
/**
* This method checks that a valid instance of MatMenu has been passed into
* matMenuTriggerFor. If not, an exception is thrown.
* @return {?}
*/
MatMenuTrigger.prototype._checkMenu = function () {
if (!this.menu) {
throwMatMenuMissingError();
}
};
/**
* This method creates the overlay from the provided menu's template and saves its
* OverlayRef so that it can be attached to the DOM when openMenu is called.
* @return {?}
*/
MatMenuTrigger.prototype._createOverlay = function () {
if (!this._overlayRef) {
this._portal = new TemplatePortal(this.menu.templateRef, this._viewContainerRef);
var /** @type {?} */ config = this._getOverlayConfig();
this._subscribeToPositions(/** @type {?} */ (config.positionStrategy));
this._overlayRef = this._overlay.create(config);
}
return this._overlayRef;
};
/**
* This method builds the configuration object needed to create the overlay, the OverlayState.
* @return {?} OverlayConfig
*/
MatMenuTrigger.prototype._getOverlayConfig = function () {
return new OverlayConfig({
positionStrategy: this._getPosition(),
hasBackdrop: !this.triggersSubmenu(),
backdropClass: 'cdk-overlay-transparent-backdrop',
direction: this.dir,
scrollStrategy: this._scrollStrategy()
});
};
/**
* Listens to changes in the position of the overlay and sets the correct classes
* on the menu based on the new position. This ensures the animation origin is always
* correct, even if a fallback position is used for the overlay.
* @param {?} position
* @return {?}
*/
MatMenuTrigger.prototype._subscribeToPositions = function (position) {
var _this = this;
this._positionSubscription = position.onPositionChange.subscribe(function (change) {
var /** @type {?} */ posX = change.connectionPair.overlayX === 'start' ? 'after' : 'before';
var /** @type {?} */ posY = change.connectionPair.overlayY === 'top' ? 'below' : 'above';
_this.menu.setPositionClasses(posX, posY);
});
};
/**
* This method builds the position strategy for the overlay, so the menu is properly connected
* to the trigger.
* @return {?} ConnectedPositionStrategy
*/
MatMenuTrigger.prototype._getPosition = function () {
var _a = this.menu.xPosition === 'before' ? ['end', 'start'] : ['start', 'end'], originX = _a[0], originFallbackX = _a[1];
var _b = this.menu.yPosition === 'above' ? ['bottom', 'top'] : ['top', 'bottom'], overlayY = _b[0], overlayFallbackY = _b[1];
var _c = [overlayY, overlayFallbackY], originY = _c[0], originFallbackY = _c[1];
var _d = [originX, originFallbackX], overlayX = _d[0], overlayFallbackX = _d[1];
var /** @type {?} */ offsetY = 0;
if (this.triggersSubmenu()) {
// When the menu is a sub-menu, it should always align itself
// to the edges of the trigger, instead of overlapping it.
overlayFallbackX = originX = this.menu.xPosition === 'before' ? 'start' : 'end';
originFallbackX = overlayX = originX === 'end' ? 'start' : 'end';
// TODO(crisbeto): this should be a function, once the overlay supports it.
// Right now it will be wrong for the fallback positions.
offsetY = overlayY === 'bottom' ? MENU_PANEL_TOP_PADDING : -MENU_PANEL_TOP_PADDING;
}
else if (!this.menu.overlapTrigger) {
originY = overlayY === 'top' ? 'bottom' : 'top';
originFallbackY = overlayFallbackY === 'top' ? 'bottom' : 'top';
}
return this._overlay.position()
.connectedTo(this._element, { originX: originX, originY: originY }, { overlayX: overlayX, overlayY: overlayY })
.withDirection(this.dir)
.withOffsetY(offsetY)
.withFallbackPosition({ originX: originFallbackX, originY: originY }, { overlayX: overlayFallbackX, overlayY: overlayY })
.withFallbackPosition({ originX: originX, originY: originFallbackY }, { overlayX: overlayX, overlayY: overlayFallbackY })
.withFallbackPosition({ originX: originFallbackX, originY: originFallbackY }, { overlayX: overlayFallbackX, overlayY: overlayFallbackY });
};
/**
* Cleans up the active subscriptions.
* @return {?}
*/
MatMenuTrigger.prototype._cleanUpSubscriptions = function () {
this._closeSubscription.unsubscribe();
this._positionSubscription.unsubscribe();
this._hoverSubscription.unsubscribe();
};
/**
* Returns a stream that emits whenever an action that should close the menu occurs.
* @return {?}
*/
MatMenuTrigger.prototype._menuClosingActions = function () {
var _this = this;
var /** @type {?} */ backdrop = ((this._overlayRef)).backdropClick();
var /** @type {?} */ parentClose = this._parentMenu ? this._parentMenu.close : of();
var /** @type {?} */ hover = this._parentMenu ? RxChain.from(this._parentMenu.hover())
.call(filter, function (active) { return active !== _this._menuItemInstance; })
.call(filter, function () { return _this._menuOpen; })
.result() : of();
return merge(backdrop, parentClose, hover);
};
/**
* Handles mouse presses on the trigger.
* @param {?} event
* @return {?}
*/
MatMenuTrigger.prototype._handleMousedown = function (event) {
if (!isFakeMousedownFromScreenReader(event)) {
this._openedByMouse = true;
// Since clicking on the trigger won't close the menu if it opens a sub-menu,
// we should prevent focus from moving onto it via click to avoid the
// highlight from lingering on the menu item.
if (this.triggersSubmenu()) {
event.preventDefault();
}
}
};
/**
* Handles key presses on the trigger.
* @param {?} event
* @return {?}
*/
MatMenuTrigger.prototype._handleKeydown = function (event) {
var /** @type {?} */ keyCode = event.keyCode;
if (this.triggersSubmenu() && ((keyCode === RIGHT_ARROW && this.dir === 'ltr') ||
(keyCode === LEFT_ARROW && this.dir === 'rtl'))) {
this.openMenu();
}
};
/**
* Handles click events on the trigger.
* @param {?} event
* @return {?}
*/
MatMenuTrigger.prototype._handleClick = function (event) {
if (this.triggersSubmenu()) {
// Stop event propagation to avoid closing the parent menu.
event.stopPropagation();
this.openMenu();
}
else {
this.toggleMenu();
}
};
MatMenuTrigger.decorators = [
{ type: Directive, args: [{
selector: "[mat-menu-trigger-for], [matMenuTriggerFor]",
host: {
'aria-haspopup': 'true',
'(mousedown)': '_handleMousedown($event)',
'(keydown)': '_handleKeydown($event)',
'(click)': '_handleClick($event)',
},
exportAs: 'matMenuTrigger'
},] },
];
/**
* @nocollapse
*/
MatMenuTrigger.ctorParameters = function () { return [
{ type: Overlay, },
{ type: ElementRef, },
{ type: ViewContainerRef, },
{ type: undefined, decorators: [{ type: Inject, args: [MAT_MENU_SCROLL_STRATEGY,] },] },
{ type: MatMenu, decorators: [{ type: Optional },] },
{ type: MatMenuItem, decorators: [{ type: Optional }, { type: Self },] },
{ type: Directionality, decorators: [{ type: Optional },] },
]; };
MatMenuTrigger.propDecorators = {
'_deprecatedMatMenuTriggerFor': [{ type: Input, args: ['mat-menu-trigger-for',] },],
'menu': [{ type: Input, args: ['matMenuTriggerFor',] },],
'onMenuOpen': [{ type: Output },],
'onMenuClose': [{ type: Output },],
};
return MatMenuTrigger;
}());
var MatMenuModule = (function () {
function MatMenuModule() {
}
MatMenuModule.decorators = [
{ type: NgModule, args: [{
imports: [
OverlayModule,
CommonModule,
MatRippleModule,
MatCommonModule,
],
exports: [MatMenu, MatMenuItem, MatMenuTrigger, MatCommonModule],
declarations: [MatMenu, MatMenuItem, MatMenuTrigger],
providers: [
MAT_MENU_SCROLL_STRATEGY_PROVIDER,
{
provide: MAT_MENU_DEFAULT_OPTIONS,
useValue: {
overlapTrigger: true,
xPosition: 'after',
yPosition: 'below',
},
}
],
},] },
];
/**
* @nocollapse
*/
MatMenuModule.ctorParameters = function () { return []; };
return MatMenuModule;
}());
/**
* Generated bundle index. Do not edit.
*/
export { MAT_MENU_SCROLL_STRATEGY, fadeInItems, transformMenu, MatMenuModule, MatMenu, MAT_MENU_DEFAULT_OPTIONS, MatMenuItem, MatMenuTrigger, MatMenuItemBase as ɵa22, _MatMenuItemMixinBase as ɵb22, MAT_MENU_SCROLL_STRATEGY_PROVIDER as ɵd22, MAT_MENU_SCROLL_STRATEGY_PROVIDER_FACTORY as ɵc22 };
//# sourceMappingURL=menu.es5.js.map