-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpopup.ts
102 lines (86 loc) · 2.05 KB
/
popup.ts
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
import { Directive, Input, ElementRef } from '@angular/core';
@Directive({
selector: '[lsu-popup]',
host: {
'(click)': 'onClick()',
'(focus)': 'onFocus()',
'(focusout)': 'onFocusOut()',
'(mouseenter)': 'onMouseEnter()',
'(mouseleave)': 'onMouseLeave()',
'(window:resize)': 'onResize()'
}
})
export class PopupDirective {
@Input()
content: string = "";
@Input()
trigger: string = "hover";
element: any;
popupEle: any;
timeout: any;
constructor(el: ElementRef) {
this.element = el.nativeElement;
}
ngOnInit() {
let id = `lsu_popup_${Math.random()}`;
let div: any = document.createElement('div');
div.id = id;
div.className = "ui custom popup top left transition hidden";
div.style = `word-wrap: break-word; bottom: auto; right: auto;`;
div.innerHTML = this.content;
this.element.parentElement.appendChild(div);
this.popupEle = document.getElementById(id);
this.setPosition();
}
setPosition(): void {
let top = this.element.offsetTop;
let left = this.element.offsetLeft;
let height = this.popupEle.offsetHeight;
this.popupEle.style.top = top - height - 10 + 'px';
this.popupEle.style.left = left + 'px';
}
show(): void {
this.popupEle.classList.remove('hidden');
this.popupEle.classList.add('visible');
this.setPosition();
}
hidden(): void {
this.popupEle.classList.remove('visible');
this.popupEle.classList.add('hidden');
}
isActived(): boolean {
return this.popupEle.classList.contains('visible');
}
onClick() {
if (this.trigger === 'click') {
if (this.isActived()) {
this.hidden();
} else {
this.show();
}
}
}
onFocus() {
if (this.trigger === 'focus') {
this.show();
}
}
onFocusOut() {
if (this.trigger === 'focus') {
this.hidden();
}
}
onMouseEnter() {
if (this.trigger === 'hover') {
this.show();
}
}
onMouseLeave() {
if (this.trigger === 'hover') {
this.hidden();
}
}
onResize() {
this.setPosition();
}
}