2 Directive, ComponentRef, ViewContainerRef, ComponentFactoryResolver, Input, HostListener
3 } from "@angular/core";
4 import {TooltipContentComponent} from "./tooltip-content.component";
9 export class TooltipComponent {
11 // -------------------------------------------------------------------------
13 // -------------------------------------------------------------------------
15 private tooltip: ComponentRef<TooltipContentComponent>;
16 private visible: boolean;
17 private delayInProgress: boolean = false;
19 // -------------------------------------------------------------------------
21 // -------------------------------------------------------------------------
23 constructor(private viewContainerRef: ViewContainerRef,
24 private resolver: ComponentFactoryResolver) {
27 // -------------------------------------------------------------------------
29 // -------------------------------------------------------------------------
31 @Input("tooltip") content: string|TooltipContentComponent;
32 @Input() tooltipDisabled: boolean;
33 @Input() tooltipAnimation: boolean = true;
34 @Input() tooltipPlacement: "top"|"bottom"|"left"|"right" = "bottom";
35 @Input() tooltipDelay: number = 1500;
37 // -------------------------------------------------------------------------
39 // -------------------------------------------------------------------------
41 @HostListener("mouseenter")
43 if(this.tooltipDisabled || this.visible || this.content === "") {
46 if (this.tooltipDelay && !this.delayInProgress) {
47 this.delayInProgress = true;
48 setTimeout(() => { this.delayInProgress && this.show() }, this.tooltipDelay);
53 if (typeof this.content === "string") {
54 const factory = this.resolver.resolveComponentFactory(TooltipContentComponent);
59 this.tooltip = this.viewContainerRef.createComponent(factory);
60 this.tooltip.instance.hostElement = this.viewContainerRef.element.nativeElement;
61 this.tooltip.instance.content = this.content as string;
62 this.tooltip.instance.placement = this.tooltipPlacement;
63 this.tooltip.instance.animation = this.tooltipAnimation;
65 const tooltip = this.content as TooltipContentComponent;
66 tooltip.hostElement = this.viewContainerRef.element.nativeElement;
67 tooltip.placement = this.tooltipPlacement;
68 tooltip.animation = this.tooltipAnimation;
73 @HostListener("mouseleave")
75 this.delayInProgress = false;
82 this.tooltip.destroy();
84 if (this.content instanceof TooltipContentComponent) {
85 (this.content as TooltipContentComponent).hide();