@swimlane/ngx-charts
Version:
Declarative Charting Framework for Angular2 and beyond!
116 lines (95 loc) • 3.18 kB
text/typescript
import {
Input, Component, ElementRef, AfterViewInit,
HostListener, ViewChild, HostBinding, Renderer
} from '@angular/core';
import { throttleable } from '../../utils/throttle';
import { PositionHelper, PlacementTypes } from './position';
import { StyleTypes } from './style.type';
import { AlignmentTypes } from './alignment.type';
export class TooltipContentComponent implements AfterViewInit {
host: any;
showCaret: boolean;
type: StyleTypes;
placement: PlacementTypes;
alignment: AlignmentTypes;
spacing: number;
cssClass: string;
title: string;
caretElm;
get cssClasses(): string {
let clz = 'ngx-tooltip-content';
clz += ` position-${this.placement}`;
clz += ` type-${this.type}`;
clz += ` ${this.cssClass}`;
return clz;
}
constructor(
public element: ElementRef,
private renderer: Renderer) {
}
ngAfterViewInit(): void {
setTimeout(this.position.bind(this));
}
position(): void {
const nativeElm = this.element.nativeElement;
const hostDim = this.host.nativeElement.getBoundingClientRect();
// if no dims were found, never show
if(!hostDim.height && !hostDim.width) return;
const elmDim = nativeElm.getBoundingClientRect();
this.checkFlip(hostDim, elmDim);
this.positionContent(nativeElm, hostDim, elmDim);
if(this.showCaret) {
this.positionCaret(hostDim, elmDim);
}
// animate its entry
setTimeout(() => this.renderer.setElementClass(nativeElm, 'animate', true), 1);
}
positionContent(nativeElm, hostDim, elmDim): void {
const { top, left } = PositionHelper.positionContent(
this.placement, elmDim, hostDim, this.spacing, this.alignment);
this.renderer.setElementStyle(nativeElm, 'top', `${top}px`);
this.renderer.setElementStyle(nativeElm, 'left', `${left}px`);
}
positionCaret(hostDim, elmDim): void {
const caretElm = this.caretElm.nativeElement;
const caretDimensions = caretElm.getBoundingClientRect();
const { top, left } = PositionHelper.positionCaret(
this.placement, elmDim, hostDim, caretDimensions, this.alignment);
this.renderer.setElementStyle(caretElm, 'top', `${top}px`);
this.renderer.setElementStyle(caretElm, 'left', `${left}px`);
}
checkFlip(hostDim, elmDim): void {
this.placement = PositionHelper.determinePlacement(
this.placement, elmDim, hostDim, this.spacing, this.alignment);
}
onWindowResize(): void {
this.position();
}
}