|
| 1 | +/*! |
| 2 | + * @license MIT |
| 3 | + * Copyright (c) 2017 Bernhard Grünewaldt - codeclou.io |
| 4 | + * https://github.com/cloukit/legal |
| 5 | + */ |
| 6 | +import { |
| 7 | + Directive, Input, HostListener, ViewContainerRef, ComponentFactoryResolver, |
| 8 | +} from '@angular/core'; |
| 9 | +import { DropoutService, DropoutComponentCreationRequest, DropoutComponentRefId, DropoutPlacement } from '@cloukit/dropout'; |
| 10 | +import { CloukitTooltipComponent } from './children/tooltip.component'; |
| 11 | + |
| 12 | +@Directive({ |
| 13 | + selector: '[cloukitTooltip]', |
| 14 | +}) |
| 15 | +export class CloukitTooltipDirective { |
| 16 | + |
| 17 | + @Input('cloukitTooltip') |
| 18 | + cloukitDropout: string; |
| 19 | + |
| 20 | + @Input('cloukitTooltipPlacement') |
| 21 | + cloukitDropoutPlacement: DropoutPlacement = DropoutPlacement.HORIZONTAL_LEFT_BOTTOM; |
| 22 | + |
| 23 | + private dropoutRef: DropoutComponentRefId; |
| 24 | + |
| 25 | + constructor(private dropoutService: DropoutService, |
| 26 | + private viewContainerRef: ViewContainerRef, |
| 27 | + private componentFactoryResolver: ComponentFactoryResolver) { |
| 28 | + } |
| 29 | + |
| 30 | + _doActivate() { |
| 31 | + const request = new DropoutComponentCreationRequest(); |
| 32 | + const componentFactory = this.componentFactoryResolver.resolveComponentFactory(CloukitTooltipComponent); |
| 33 | + const tooltipRef = this.viewContainerRef.createComponent(componentFactory); |
| 34 | + tooltipRef.instance.tooltipText = this.cloukitDropout; |
| 35 | + request.triggerElement = this.viewContainerRef.element.nativeElement; |
| 36 | + request.template = tooltipRef.instance.tooltipTemplate; |
| 37 | + request.placement = this.cloukitDropoutPlacement; |
| 38 | + this.dropoutRef = this.dropoutService.requestDropoutCreation(request); |
| 39 | + } |
| 40 | + |
| 41 | + _doDeactivate() { |
| 42 | + this.dropoutService.destroyComponent(this.dropoutRef); |
| 43 | + this.dropoutRef = undefined; |
| 44 | + } |
| 45 | + |
| 46 | + @HostListener('focusin') |
| 47 | + @HostListener('mouseenter') |
| 48 | + activate() { |
| 49 | + this._doActivate(); |
| 50 | + } |
| 51 | + |
| 52 | + @HostListener('focusout') |
| 53 | + @HostListener('mouseleave') |
| 54 | + deactivate() { |
| 55 | + this._doDeactivate(); |
| 56 | + } |
| 57 | +} |
0 commit comments