|
| 1 | +import { |
| 2 | + Directive, |
| 3 | + ElementRef, |
| 4 | + Input, |
| 5 | + Output, |
| 6 | + EventEmitter, |
| 7 | + HostListener, |
| 8 | + ViewContainerRef, |
| 9 | + AfterViewInit, |
| 10 | + OnDestroy |
| 11 | +} from '@angular/core'; |
| 12 | +import {MdMenu} from './menu'; |
| 13 | +import {MdMenuItem, MdMenuAnchor} from './menu-item'; |
| 14 | +import {MdMenuMissingError} from './menu-errors'; |
| 15 | +import { |
| 16 | + Overlay, |
| 17 | + OverlayState, |
| 18 | + OverlayRef, |
| 19 | + OVERLAY_PROVIDERS, |
| 20 | + TemplatePortal |
| 21 | +} from '@angular2-material/core/core'; |
| 22 | +import { |
| 23 | + ConnectedPositionStrategy |
| 24 | +} from '@angular2-material/core/overlay/position/connected-position-strategy'; |
| 25 | + |
| 26 | +/** |
| 27 | + * This directive is intended to be used in conjunction with an md-menu tag. It is |
| 28 | + * responsible for toggling the display of the provided menu instance. |
| 29 | + */ |
| 30 | +@Directive({ |
| 31 | + selector: '[md-menu-trigger-for]', |
| 32 | + host: {'aria-haspopup': 'true'}, |
| 33 | + providers: [OVERLAY_PROVIDERS], |
| 34 | + exportAs: 'mdMenuTrigger' |
| 35 | +}) |
| 36 | +export class MdMenuTrigger implements AfterViewInit, OnDestroy { |
| 37 | + private _portal: TemplatePortal; |
| 38 | + private _overlayRef: OverlayRef; |
| 39 | + menuOpen: boolean = false; |
| 40 | + |
| 41 | + @Input('md-menu-trigger-for') menu: MdMenu; |
| 42 | + @Output() onMenuOpen = new EventEmitter(); |
| 43 | + @Output() onMenuClose = new EventEmitter(); |
| 44 | + |
| 45 | + constructor(private _overlay: Overlay, private _element: ElementRef, |
| 46 | + private _viewContainerRef: ViewContainerRef) {} |
| 47 | + |
| 48 | + ngAfterViewInit() { |
| 49 | + this._checkMenu(); |
| 50 | + this.menu.close.subscribe(() => this.closeMenu()); |
| 51 | + } |
| 52 | + |
| 53 | + ngOnDestroy() { this.destroyMenu(); } |
| 54 | + |
| 55 | + @HostListener('click') |
| 56 | + toggleMenu(): Promise<void> { |
| 57 | + return this.menuOpen ? this.closeMenu() : this.openMenu(); |
| 58 | + } |
| 59 | + |
| 60 | + openMenu(): Promise<void> { |
| 61 | + return this._createOverlay() |
| 62 | + .then(() => this._overlayRef.attach(this._portal)) |
| 63 | + .then(() => this._setIsMenuOpen(true)); |
| 64 | + } |
| 65 | + |
| 66 | + closeMenu(): Promise<void> { |
| 67 | + if (!this._overlayRef) { return Promise.resolve(); } |
| 68 | + |
| 69 | + return this._overlayRef.detach() |
| 70 | + .then(() => this._setIsMenuOpen(false)); |
| 71 | + } |
| 72 | + |
| 73 | + destroyMenu(): void { |
| 74 | + this._overlayRef.dispose(); |
| 75 | + } |
| 76 | + |
| 77 | + // set state rather than toggle to support triggers sharing a menu |
| 78 | + private _setIsMenuOpen(isOpen: boolean): void { |
| 79 | + this.menuOpen = isOpen; |
| 80 | + this.menu._setClickCatcher(isOpen); |
| 81 | + this.menuOpen ? this.onMenuOpen.emit(null) : this.onMenuClose.emit(null); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * This method checks that a valid instance of MdMenu has been passed into |
| 86 | + * md-menu-trigger-for. If not, an exception is thrown. |
| 87 | + */ |
| 88 | + private _checkMenu() { |
| 89 | + if (!this.menu || !(this.menu instanceof MdMenu)) { |
| 90 | + throw new MdMenuMissingError(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * This method creates the overlay from the provided menu's template and saves its |
| 96 | + * OverlayRef so that it can be attached to the DOM when openMenu is called. |
| 97 | + */ |
| 98 | + private _createOverlay(): Promise<any> { |
| 99 | + if (this._overlayRef) { return Promise.resolve(); } |
| 100 | + |
| 101 | + this._portal = new TemplatePortal(this.menu.templateRef, this._viewContainerRef); |
| 102 | + return this._overlay.create(this._getOverlayConfig()) |
| 103 | + .then(overlay => this._overlayRef = overlay); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * This method builds the configuration object needed to create the overlay, the OverlayState. |
| 108 | + * @returns OverlayState |
| 109 | + */ |
| 110 | + private _getOverlayConfig(): OverlayState { |
| 111 | + const overlayState = new OverlayState(); |
| 112 | + overlayState.positionStrategy = this._getPosition(); |
| 113 | + return overlayState; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * This method builds the position strategy for the overlay, so the menu is properly connected |
| 118 | + * to the trigger. |
| 119 | + * @returns ConnectedPositionStrategy |
| 120 | + */ |
| 121 | + private _getPosition(): ConnectedPositionStrategy { |
| 122 | + return this._overlay.position().connectedTo( |
| 123 | + this._element, |
| 124 | + {originX: 'start', originY: 'top'}, |
| 125 | + {overlayX: 'start', overlayY: 'top'} |
| 126 | + ); |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +export const MD_MENU_DIRECTIVES = [MdMenu, MdMenuItem, MdMenuTrigger, MdMenuAnchor]; |
0 commit comments