import {Directive, ElementRef, HostListener, Input, Renderer} from '@angular/core'; @Directive({ selector: '[dcPopover]' }) export class PopoverDirective { @Input() dcPopover: string; @Input() options: any; titleBox: any; htmlStr: string; space: any; constructor(private el: ElementRef) { } @HostListener('mouseenter') onMouseEnter() { this.showTitle(); } @HostListener('mouseleave') onMouseLeave() { setTimeout(() => { this.hideTitle(); }, 20); } showTitle() { if (!this.titleBox) { const ele = document.createElement('div'); const offset = this.options.offset || 'down'; let empos = ''; ele.className = 'dc-popover-tip'; ele.style.position = 'absolute'; ele.style.zIndex = '1'; ele.style.width = this.options.width || '280px'; this.space = this.options.space || '10px'; this.el.nativeElement.style.position = 'relative'; this.el.nativeElement.style.overflow = "visible"; this.titleBox = ele; if (offset === 'up') { ele.style.paddingLeft = this.options.left || '0px'; const height = this.titleBox.offsetHeight; if (this.options.tablePop) { ele.style.bottom = '40px'; } else { ele.style.bottom = (height + 20) + 'px'; } ele.style.paddingBottom = this.space; empos = 'transform:rotate(-135deg);bottom:-5px;left:10px;'; } else if (offset === 'left') { ele.style.left = '-' + ele.style.width; ele.style.top = '-10px'; ele.style.paddingRight = this.options.space || '10px'; if (this.options.tablePop) { empos = 'transform:rotate(135deg);right:-5px;top:24px;'; } else { empos = 'transform:rotate(135deg);right:-5px;top:15px;'; } } else if (offset === 'leftTop') { ele.style.left = '-' + ele.style.width; ele.style.top = '5px'; ele.style.paddingRight = this.options.space || '10px'; empos = 'transform:rotate(135deg);right:-5px;top:10px;'; } else if (offset === 'leftBottom') { ele.style.left = '-' + ele.style.width; ele.style.bottom = '5px'; ele.style.paddingRight = this.options.space || '10px'; empos = 'transform:rotate(135deg);right:-5px;bottom:20px;'; } else if (offset === 'right') { ele.style.right = '-' + (ele.style.width); ele.style.top = '-10px'; ele.style.paddingLeft = this.options.space || '10px'; if (this.options.tablePop) { empos = 'transform:rotate(-45deg);left:-5px;top:24px;'; } else { empos = 'transform:rotate(-45deg);left:-5px;top:15px;'; } } else if (offset === 'rightTop') { ele.style.right = '-' + (ele.style.width); ele.style.top = '5px'; ele.style.paddingLeft = this.options.space || '10px'; empos = 'transform:rotate(-45deg);left:-5px;top:10px;'; } else if (offset === 'rightBottom') { ele.style.right = '-' + (ele.style.width); ele.style.bottom = '5px'; ele.style.paddingLeft = this.options.space || '10px'; empos = 'transform:rotate(-45deg);left:-5px;bottom:20px;'; } else { ele.style.paddingLeft = this.options.left || '0px'; ele.style.paddingTop = this.options.space || '10px'; if (this.options.tablePop) { ele.style.bottom = -(this.options.data.length * 24 + 20) + 'px'; } empos = 'transform:rotate(45deg);top: -5px;left:10px;'; } if (this.options.text) { this.htmlStr = '
' + this.options.text + ' 
'; } else { this.htmlStr = '
' + ' ' + '
'; this.options.data.map((item: any) => { this.htmlStr += '
' + item['key'] + ':
' + '
' + item['value'] + '
'; }); this.htmlStr += '
'; } this.titleBox.innerHTML = this.htmlStr; this.el.nativeElement.appendChild(this.titleBox); } this.titleBox.hidden = false; } hideTitle() { this.titleBox.hidden = true; } }