import { Component, Directive, Input, Output, EventEmitter, ChangeDetectionStrategy, OnInit, OnDestroy, Injector, Renderer2, ComponentRef, ElementRef, TemplateRef, ViewContainerRef, ComponentFactoryResolver, NgZone } from '@angular/core'; import {listenToTriggers} from '../util/triggers'; import {positionElements} from '../util/positioning'; import {PopupService} from '../util/popup'; import {Ng2vTooltipConfig} from './tooltip-config'; let nextId = 0; @Component({ selector: 'ng2v-tooltip-window', changeDetection: ChangeDetectionStrategy.OnPush, host: {'[class]': '"tooltip show tooltip-" + placement', 'role': 'tooltip', '[id]': 'id'}, template: `
` }) export class Ng2vTooltipWindow { @Input() placement: 'top' | 'bottom' | 'left' | 'right' = 'top'; @Input() id: string; } /** * A lightweight, extensible directive for fancy tooltip creation. */ @Directive({selector: '[ng2vTooltip]', exportAs: 'ng2vTooltip'}) export class Ng2vTooltip implements OnInit, OnDestroy { /** * Placement of a tooltip. Accepts: "top", "bottom", "left", "right" */ @Input() placement: 'top' | 'bottom' | 'left' | 'right'; /** * Specifies events that should trigger. Supports a space separated list of event names. */ @Input() triggers: string; /** * A selector specifying the element the tooltip should be appended to. * Currently only supports "body". */ @Input() container: string; /** * Emits an event when the tooltip is shown */ @Output() shown = new EventEmitter(); /** * Emits an event when the tooltip is hidden */ @Output() hidden = new EventEmitter(); private _ng2vTooltip: string | TemplateRef; private _ng2vTooltipWindowId = `ng2v-tooltip-${nextId++}`; private _popupService: PopupService; private _windowRef: ComponentRef; private _unregisterListenersFn; private _zoneSubscription: any; constructor( private _elementRef: ElementRef, private _renderer: Renderer2, injector: Injector, componentFactoryResolver: ComponentFactoryResolver, viewContainerRef: ViewContainerRef, config: Ng2vTooltipConfig, ngZone: NgZone) { this.placement = config.placement; this.triggers = config.triggers; this.container = config.container; this._popupService = new PopupService( Ng2vTooltipWindow, injector, viewContainerRef, _renderer, componentFactoryResolver); this._zoneSubscription = ngZone.onStable.subscribe(() => { if (this._windowRef) { positionElements( this._elementRef.nativeElement, this._windowRef.location.nativeElement, this.placement, this.container === 'body'); } }); } /** * Content to be displayed as tooltip. If falsy, the tooltip won't open. */ @Input() set ng2vTooltip(value: string | TemplateRef) { this._ng2vTooltip = value; if (!value && this._windowRef) { this.close(); } } get ng2vTooltip() { return this._ng2vTooltip; } /** * Opens an element’s tooltip. This is considered a “manual” triggering of the tooltip. * The context is an optional value to be injected into the tooltip template when it is created. */ open(context?: any) { if (!this._windowRef && this._ng2vTooltip) { this._windowRef = this._popupService.open(this._ng2vTooltip, context); this._windowRef.instance.placement = this.placement; this._windowRef.instance.id = this._ng2vTooltipWindowId; this._renderer.setAttribute(this._elementRef.nativeElement, 'aria-describedby', this._ng2vTooltipWindowId); if (this.container === 'body') { window.document.querySelector(this.container).appendChild(this._windowRef.location.nativeElement); } // we need to manually invoke change detection since events registered via // Renderer::listen() - to be determined if this is a bug in the Angular itself this._windowRef.changeDetectorRef.markForCheck(); this.shown.emit(); } } /** * Closes an element’s tooltip. This is considered a “manual” triggering of the tooltip. */ close(): void { if (this._windowRef != null) { this._renderer.removeAttribute(this._elementRef.nativeElement, 'aria-describedby'); this._popupService.close(); this._windowRef = null; this.hidden.emit(); } } /** * Toggles an element’s tooltip. This is considered a “manual” triggering of the tooltip. */ toggle(): void { if (this._windowRef) { this.close(); } else { this.open(); } } /** * Returns whether or not the tooltip is currently being shown */ isOpen(): boolean { return this._windowRef != null; } ngOnInit() { this._unregisterListenersFn = listenToTriggers( this._renderer, this._elementRef.nativeElement, this.triggers, this.open.bind(this), this.close.bind(this), this.toggle.bind(this)); } ngOnDestroy() { this.close(); this._unregisterListenersFn(); this._zoneSubscription.unsubscribe(); } }