import { FocusableOption , FocusMonitor , FocusOrigin } from '@angular/cdk/a11y' ; import { DOCUMENT } from '@angular/common' ; import { Directive , ElementRef , EventEmitter , HostListener , Inject , Input , OnDestroy , Output , } from '@angular/core' ; import { mixinDisabled , TypeRef } from '@sbb-esta/angular/core' ; docs-private const _HeaderMenuItemBase = mixinDisabled ( class { } ) ; @ Directive ( { selector : '[sbbHeaderMenuItem]' , host : { '[attr.role]' : 'this.role' , class : 'sbb-header-menu-item sbb-menu-item' , } , } ) export class SbbHeaderMenuItem extends _HeaderMenuItemBase implements FocusableOption , OnDestroy { /** ARIA role for the menu item. */ @ Input ( ) role : 'menuitem' | 'menuitemradio' | 'menuitemcheckbox' = 'menuitem' ; private _document : Document ; /** Whether the menu item is highlighted. */ _highlighted : boolean = false ; /** Whether the menu item acts as a trigger for a sub-menu. */ _triggersSubmenu : boolean = false ; /** Emits whenever a this item is clicked when enabled. */ @ Output ( ) click : EventEmitter < Event > = new EventEmitter < Event > ( ) ; constructor ( private _elementRef , private _focusMonitor ? , @ Inject ( DOCUMENT ) document ? ) { super ( ) ; if ( _focusMonitor ) { _focusMonitor . monitor ( this . _elementRef , false ) ; } this . _document = document ; } /** Focuses the menu item. */ focus ( origin = 'program' , options ? ) { if ( this . _focusMonitor ) { this . _focusMonitor . focusVia ( this . _getHostElement ( ) , origin , options ) ; } else { this . _getHostElement ( ) . focus ( options ) ; } } ngOnDestroy ( ) { if ( this . _focusMonitor ) { this . _focusMonitor . stopMonitoring ( this . _elementRef ) ; } } /** 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. */ @ HostListener ( 'click' , [ '$event' ] ) _checkDisabled ( event ) { if ( this . disabled ) { event . preventDefault ( ) ; event . stopPropagation ( ) ; } } /** 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 ( ) ; } }