license import { FocusableOption , FocusMonitor , FocusOrigin } from '@angular/cdk/a11y' ; import { ChangeDetectionStrategy , Component , ElementRef , OnDestroy , ViewEncapsulation , Inject , Optional , Input , } from '@angular/core' ; import { CanDisable , CanDisableCtor , CanDisableRipple , CanDisableRippleCtor , mixinDisabled , mixinDisableRipple , } from '@angular/material/core' ; import { Subject } from 'rxjs' ; import { DOCUMENT } from '@angular/common' ; import { MAT_MENU_PANEL , MatMenuPanel } from './menu-panel' ; docs-private export class MatMenuItemBase { } export const _MatMenuItemMixinBase = mixinDisableRipple ( mixinDisabled ( MatMenuItemBase ) ) ; /**
 * This directive is intended to be used inside an mat-menu tag.
 * It exists mostly to set the role attribute.
 */ @ Component ( { moduleId : module . id , selector : '[mat-menu-item]' , exportAs : 'matMenuItem' , inputs : [ 'disabled' , 'disableRipple' ] , host : { '[attr.role]' : 'role' , '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)' : '_handleMouseEnter()' , } , changeDetection : ChangeDetectionStrategy . OnPush , encapsulation : ViewEncapsulation . None , templateUrl : 'menu-item.html' , } ) export class MatMenuItem extends _MatMenuItemMixinBase implements FocusableOption , CanDisable , CanDisableRipple , OnDestroy { /** ARIA role for the menu item. */ @ Input ( ) role : 'menuitem' | 'menuitemradio' | 'menuitemcheckbox' = 'menuitem' ; private _document : Document ; /** Stream that emits when the menu item is hovered. */ readonly _hovered : Subject < MatMenuItem > = new Subject < MatMenuItem > ( ) ; /** Whether the menu item is highlighted. */ _highlighted : boolean = false ; /** Whether the menu item acts as a trigger for a sub-menu. */ _triggersSubmenu : boolean = false ; constructor ( private _elementRef , @ Inject ( DOCUMENT ) document ? , private _focusMonitor ? , @ Inject ( MAT_MENU_PANEL ) @ Optional ( ) private _parentMenu ? ) { super ( ) ; if ( _focusMonitor ) { _focusMonitor . monitor ( this . _elementRef , false ) ; } if ( _parentMenu && _parentMenu . addItem ) { _parentMenu . addItem ( this ) ; } this . _document = document ; } /** Focuses the menu item. */ focus ( origin = 'program' ) { if ( this . _focusMonitor ) { this . _focusMonitor . focusVia ( this . _getHostElement ( ) , origin ) ; } else { this . _getHostElement ( ) . focus ( ) ; } } ngOnDestroy ( ) { if ( this . _focusMonitor ) { this . _focusMonitor . stopMonitoring ( this . _elementRef ) ; } if ( this . _parentMenu && this . _parentMenu . removeItem ) { this . _parentMenu . removeItem ( this ) ; } this . _hovered . complete ( ) ; } /** Used to set the `tabindex`. */ _getTabIndex ( ) { return this . disabled ? '-1' : '0' ; } /** Returns the host DOM element. */ _getHostElement ( ) { return this . _elementRef . nativeElement ; } /** Prevents the default element actions if it is disabled. */ _checkDisabled ( event ) { if ( this . disabled ) { event . preventDefault ( ) ; event . stopPropagation ( ) ; } } /** Emits to the hover stream. */ _handleMouseEnter ( ) { this . _hovered . next ( this ) ; } /** Gets the label to be used when determining whether the option should be focused. */ getLabel ( ) { const element = this . _elementRef . nativeElement ; const textNodeType = this . _document ? this . _document . TEXT_NODE : 3 ; let output = '' ; if ( element . childNodes ) { const length = element . childNodes . length ; for ( let i = 0 ; i < length ; i ++ ) { if ( element . childNodes [ i ] . nodeType === textNodeType ) { output += element . childNodes [ i ] . textContent ; } } } return output . trim ( ) ; } }