forked from chrisdavies/tlite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tlite.js
138 lines (110 loc) · 3.52 KB
/
tlite.js
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
function tlite(getTooltipOpts) {
document.addEventListener('mouseover', function (e) {
var el = e.target;
var opts = getTooltipOpts(el);
if (!opts) {
el = el.parentElement;
opts = el && getTooltipOpts(el);
}
opts && tlite.show(el, opts, true);
});
}
tlite.show = function (el, opts, isAuto) {
var fallbackAttrib = 'data-tlite';
opts = opts || {};
(el.tooltip || Tooltip(el, opts)).show();
function Tooltip(el, opts) {
var tooltipEl;
var showTimer;
var text;
el.addEventListener('mousedown', autoHide);
el.addEventListener('mouseleave', autoHide);
function show() {
text = el.title || el.getAttribute(fallbackAttrib) || text;
el.title = '';
el.setAttribute(fallbackAttrib, '');
text && !showTimer && (showTimer = setTimeout(fadeIn, isAuto ? 150 : 1))
}
function autoHide() {
tlite.hide(el, true);
}
function hide(isAutoHiding) {
if (isAuto === isAutoHiding) {
showTimer = clearTimeout(showTimer);
var parent = tooltipEl && tooltipEl.parentNode;
parent && parent.removeChild(tooltipEl);
tooltipEl = undefined;
}
}
function fadeIn() {
if (!tooltipEl) {
tooltipEl = createTooltip(el, text, opts);
}
}
return el.tooltip = {
show: show,
hide: hide
};
}
function createTooltip(el, text, opts) {
var tooltipEl = document.createElement('div');
tooltipEl.className = 'tlite-container'
var tooltipInner = document.createElement('span');
tooltipInner.className = 'tlite'
tooltipInner.innerHTML = text;
tooltipEl.appendChild(tooltipInner)
el.appendChild(tooltipEl);
var grav = opts.grav || el.getAttribute('data-tlite') || 'n';
var vertGrav = grav[0] || '';
var horzGrav = grav[1] || '';
function positionTooltip() {
tooltipEl.className += ' tlite-' + vertGrav + horzGrav;
var arrowSize = 10;
var top = el.offsetTop;
var left = el.offsetLeft;
if (tooltipEl.offsetParent === el) {
top = left = 0;
}
var width = el.offsetWidth;
var height = el.offsetHeight;
var tooltipHeight = tooltipEl.offsetHeight;
var tooltipWidth = tooltipEl.offsetWidth;
var centerEl = left + (width / 2);
tooltipEl.style.top = (
vertGrav === 's' ? (top - tooltipHeight - arrowSize) :
vertGrav === 'n' ? (top + height + arrowSize) :
(top + (height / 2) - (tooltipHeight / 2))
) + 'px';
tooltipEl.style.left = (
horzGrav === 'w' ? left :
horzGrav === 'e' ? left + width - tooltipWidth :
vertGrav === 'w' ? (left + width + arrowSize) :
vertGrav === 'e' ? (left - tooltipWidth - arrowSize) :
(centerEl - tooltipWidth / 2)
) + 'px';
}
positionTooltip();
var rect = tooltipEl.getBoundingClientRect();
if (vertGrav === 's' && rect.top < 0) {
vertGrav = 'n';
positionTooltip();
} else if (vertGrav === 'n' && rect.bottom > window.innerHeight) {
vertGrav = 's';
positionTooltip();
} else if (vertGrav === 'e' && rect.left < 0) {
vertGrav = 'w';
positionTooltip();
} else if (vertGrav === 'w' && rect.right > window.innerWidth) {
vertGrav = 'e';
positionTooltip();
}
tooltipEl.className += ' tlite-visible';
return tooltipEl;
}
};
tlite.hide = function (el, isAuto) {
el.tooltip && el.tooltip.hide(isAuto);
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = tlite;
}