-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtip.js
More file actions
96 lines (78 loc) · 2.68 KB
/
Copy pathtip.js
File metadata and controls
96 lines (78 loc) · 2.68 KB
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
(function() {
function Tip() {
function isTip(element) {
return element.classList.contains('tip');
}
function isInPopover(element) {
if (!element || element.tagName.toLowerCase() === 'body')
return false;
if (element.classList.contains('popover-clone'))
return true;
return isInPopover(element.parentElement);
}
function hidePopovers() {
const popovers = document.querySelectorAll('.popover-clone');
for (let i = 0; i < popovers.length; i++) {
document.body.removeChild(popovers[i]);
}
const links = document.querySelectorAll('.tip');
for (let i = 0; i< links.length; i++) {
links[i].dataOpenPopover = null;
}
}
function toggleTooltip(target) {
//Remove open popover if exists
if (target.dataOpenPopover) {
document.body.removeChild(target.dataOpenPopover);
target.dataOpenPopover = null;
return false;
}
//Or create and show popover
const originalPopover = target.nextElementSibling;
if (!originalPopover)
return false;
const popover = originalPopover.cloneNode(true);
popover.classList.add('popover-clone');
document.body.appendChild(popover);
setPosition(popover, target);
target.dataOpenPopover = popover;
return false;
}
function setPosition(popover, target) {
const scrollY = window.pageYOffset || document.documentElement.scrollTop || 0;
const scrollX = window.pageXOffset || document.documentElement.scrollLeft || 0;
const targetRect = target.getBoundingClientRect();
const targetWidth = targetRect.width || targetRect.left - targetRect.right;
const targetHeight = targetRect.height || targetRect.top - targetRect.bottom;
//Position the popup window
popover.style.display = 'block';
popover.style.position = 'absolute';
let left = targetWidth / 2 + targetRect.left + scrollX - popover.clientWidth / 2;
if (left < 10) { left = 10; }
popover.style.left = left + 'px';
popover.style.bottom = document.documentElement.clientHeight - targetRect.top - scrollY + targetHeight/2 + 'px';
popover.style.top = 'inherit';
popover.style.right = 'inherit';
popover.style.zIndex = 10000;
//Position the arrow over the clicked element
if (popover.querySelector) {
const arrow = popover.querySelector('.arrow');
arrow.style.left = targetRect.left - left + arrow.offsetWidth / 2 + 'px';
}
}
function init() {
document.body.addEventListener('click', function (e) {
const target = e.target || e.srcElement;
if (isTip(target) || isInPopover(target))
return;
hidePopovers();
});
window.addEventListener('resize', hidePopovers);
}
return {
click: toggleTooltip,
init: init
};
};
window.vanillaTip = Tip();
})();