All files / src/tooltip tooltip.ts

8.33% Statements 5/60
0% Branches 0/14
0% Functions 0/26
6.78% Lines 4/59
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 2001x   1x                                               1x   1x                                                                                                                                                                                                                                                                                                                                                      
import { Component, ElementRef, ViewChild } from '@angular/core';
import { TooltipOptions } from './tooltipOptions';
import { transition, trigger, style, animate } from '@angular/animations';
 
@Component( {
    selector: 'tl-tooltip',
    template: `
        <div #tooltip [@enterAnimation]="show" class="tooltip-text" [ngStyle]="getStyleTooltip()"><span>{{text}}</span>
            <div [ngClass]="'-'+placement" [ngStyle]="getStyleTooltipArrow()"></div>
        </div>`,
    styles: [],
    animations: [
        trigger(
            'enterAnimation', [
                transition( ':enter', [
                    style( { opacity: 0 } ),
                    animate( '200ms', style( { opacity: 1 } ) )
                ] ),
                transition( ':leave', [
                    style( { opacity: 1 } ),
                    animate( '200ms', style( { opacity: 0 } ) )
                ] )
            ]
        )
    ],
} )
export class TlToolTip implements TooltipOptions {
 
    @ViewChild( 'tooltip' ) tooltip;
 
    public text: string;
 
    public color = '#000';
 
    public placement = 'top';
 
    public fontColor = '#FFF';
 
    public width = '';
 
    public show = true;
 
    private tooltipWidth = 0;
 
    private elementWidth = 0;
 
    private elementHeight = 0;
 
    private tooltipPadding = 10;
 
    private element;
 
    setOptions( options: TooltipOptions ) {
        const self = this;
        Object.keys( options ).forEach( function ( key ) {
            self[ key ] = options[ key ];
        } );
    }
 
    setPlacementTop() {
        const self = this;
        this.getElementWidth();
        this.getElementHeight();
        setTimeout( function () {
            self.getTopMeasureForTopTooltip();
            self.setAlignCenter();
        }, 0 );
 
    }
 
    setPlacementLeft() {
        const self = this;
        setTimeout( function () {
            self.getTopMeasureForLeftAndRight();
            self.setAlignLeft();
        }, 0 );
    }
 
    setPlacementRight() {
        const self = this;
        this.getElementWidth();
        setTimeout( function () {
            self.getTopMeasureForLeftAndRight();
            self.setAlignRight();
        }, 0 );
    }
 
    setPlacementBottom() {
        const self = this;
        this.getElementWidth();
        this.getElementHeight();
        setTimeout( function () {
            self.setTopMeasureForBottom();
            self.setAlignCenter();
        }, 0 );
    }
 
    getStyleTooltip() {
        return { 'background-color': this.color, 'color': this.fontColor, 'width': this.width };
    }
 
    getStyleTooltipArrow() {
        switch ( this.placement ) {
            case 'right':
                return {
                    'border-color': 'transparent ' + this.color + ' transparent transparent'
                };
            case 'left':
                return {
                    'border-color': 'transparent transparent transparent' + this.color
                };
            case 'top':
                return {
                    'border-color': this.color + ' transparent transparent transparent'
                };
            case 'bottom':
                return {
                    'border-color': 'transparent transparent ' + this.color + ' transparent'
                };
        }
    }
 
    setPosition( element: ElementRef ) {
        this.getElement( element );
        switch ( this.placement ) {
            case 'top':
                this.setPlacementTop();
                break;
            case 'left':
                this.setPlacementLeft();
                break;
            case 'right':
                this.setPlacementRight();
                break;
            case 'bottom':
                this.setPlacementBottom();
                break;
        }
    }
 
    private getElement( element ) {
        this.element = element;
    }
 
    private existsMeasureOnNativeElement(): boolean {
        return this.element.nativeElement.offsetWidth > 0 && this.element.nativeElement.offsetHeight > 0;
    }
 
    private isLessOrEqualThanNormalWidth(): boolean {
        return this.tooltip.nativeElement.offsetHeight <= 27;
    }
 
    private getElementWidth() {
        this.existsMeasureOnNativeElement() ? this.elementWidth = this.element.nativeElement.offsetWidth
            : this.elementWidth = this.element.nativeElement.firstChild.clientWidth;
    }
 
    private getTopMeasureForTopTooltip() {
        this.isLessOrEqualThanNormalWidth() ? this.tooltip.nativeElement.style.top =
            this.getElementTop() - this.elementHeight - this.tooltipPadding + 'px' :
            this.tooltip.nativeElement.style.top = this.getElementTop() -
                this.tooltip.nativeElement.offsetHeight - this.tooltipPadding + 'px';
    }
 
    private getTopMeasureForLeftAndRight() {
        this.isLessOrEqualThanNormalWidth() ? this.tooltip.nativeElement.style.top = this.getElementTop() - 2.5 + 'px'
            : this.tooltip.nativeElement.style.top =
            this.getElementTop() - this.tooltip.nativeElement.offsetHeight / 2 + this.tooltipPadding + 'px';
    }
 
 
    private getElementHeight() {
        this.existsMeasureOnNativeElement() ? this.elementHeight = this.element.nativeElement.offsetHeight :
            this.elementHeight = this.element.nativeElement.firstChild.clientHeight;
    }
 
    private setTopMeasureForBottom() {
        this.tooltip.nativeElement.style.top = this.getElementTop() + this.elementHeight + this.tooltipPadding + 'px';
    }
 
    private setAlignCenter() {
        this.tooltip.nativeElement.style.left = this.element.nativeElement.offsetLeft +
            (this.elementWidth / 2) - this.tooltip.nativeElement.offsetWidth / 2 + 'px';
    }
 
    private setAlignRight() {
        this.tooltip.nativeElement.style.left = this.element.nativeElement.offsetLeft +
            this.elementWidth + this.tooltipPadding + 'px';
    }
 
    private setAlignLeft() {
        this.tooltip.nativeElement.style.left = this.element.nativeElement.offsetLeft -
            this.tooltip.nativeElement.offsetWidth - this.tooltipPadding + 'px';
    }
 
    private getElementTop() {
        return this.element.nativeElement.offsetTop;
    }
}