-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
184 lines (148 loc) · 5.25 KB
/
index.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
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
(function(root, factory) {
if (typeof define === "function" && define.amd) {
define(['jquery'], function($) {
return (root.Tooltip = factory($));
});
} else if (typeof module === "object" && module.exports) {
module.exports = (root.Tooltip = factory(require('jquery')));
} else {
root.Tooltip = factory(root.$);
}
})(this, function($) {
return function() {
var elem;
var html;
var options = {};
var container = document.createElement('div');
var isOver = false;
container.className = 'tooltip';
var tooltipArrow = document.createElement('div');
tooltipArrow.className = "tooltip-arrow";
container.appendChild(tooltipArrow);
var tooltipInner = document.createElement('div');
tooltipInner.className = "tooltip-inner";
container.appendChild(tooltipInner);
var getBounds = function(el) {
var bounds = $(el).offset();
bounds.width = el.offsetWidth;
bounds.height = el.offsetHeight;
return bounds;
};
var getActualOffset = function(placement, pos, actualWidth, actualHeight) {
return placement == 'bottom' ? {
top: pos.top + pos.height,
left: pos.left + pos.width / 2 - actualWidth / 2
} :
placement == 'top' ? {
top: pos.top - actualHeight,
left: pos.left + pos.width / 2 - actualWidth / 2
} :
placement == 'left' ? {
top: pos.top + pos.height / 2 - actualHeight / 2,
left: pos.left - actualWidth
} :
{
top: pos.top + pos.height / 2 - actualHeight / 2,
left: pos.left + pos.width
}
};
var replaceArrow = function(delta, dimension, position) {
$(tooltipArrow).css(position, delta ? (50 * (1 - delta / dimension) + "%") : '')
}
var applyOffset = function(offset, placement) {
var width = container.offsetWidth;
var height = container.offsetHeight;
var marginTop = parseInt($(container).css('margin-top'), 10);
var marginLeft = parseInt($(container).css('margin-left'), 10);
if (isNaN(marginTop))
marginTop = 0;
if (isNaN(marginLeft))
marginLeft = 0;
offset.top = offset.top + marginTop;
offset.left = offset.left + marginLeft;
$(container).offset(offset).addClass('in');
var actualWidth = container.offsetWidth;
var actualHeight = container.offsetHeight;
if (placement == 'top' && actualHeight != height) {
offset.top = offset.top + height - actualHeight;
$(container).offset(offset);
}
if (/bottom|top/.test(placement)) {
var delta = 0
if (offset.left < 0) {
delta = offset.left * -2;
offset.left = 0;
$(container).offset(offset);
actualWidth = container.offsetWidth;
actualHeight = container.offsetHeight;
}
replaceArrow(delta - width + actualWidth, actualWidth, 'left');
} else {
replaceArrow(actualHeight - height, actualHeight, 'top');
}
};
var getTopPositionFromNode = function(node) {
var i = 0;
while (node !== null) {
i += node.offsetTop;
node = node.offsetParent;
}
return i;
};
var showTooltip = function(event) {
var placement = options.placement;
var el = event.currentTarget;
$(container).css({
top: 0,
left: 0,
display: 'block'
}).addClass('fade').addClass(placement);
$(container).insertAfter($(el));
var positionOfElem = getBounds(el);
var actualWidth = container.offsetWidth;
var actualHeight = container.offsetHeight;
var parent = $(el).parent();
var orgPlacement = placement;
var docScroll = document.documentElement.scrollTop || document.body.scrollTop;
var parentWidth = $(parent).outerWidth();
var parentHeight = $(parent).outerHeight();
var parentLeft = $(parent).offset().left;
placement = placement == 'bottom' && positionOfElem.top + positionOfElem.height + actualHeight - docScroll > parentHeight ? 'top' :
placement == 'top' && positionOfElem.top - docScroll - actualHeight < 0 ? 'bottom' :
placement == 'right' && positionOfElem.right + actualWidth > parentWidth ? 'left' :
placement == 'left' && positionOfElem.left - actualWidth < parentLeft ? 'right' :
placement;
$(container).removeClass(orgPlacement).addClass(placement);
var actualOffset = getActualOffset(placement, positionOfElem, actualWidth, actualHeight);
applyOffset(actualOffset, placement);
};
var onMouseOver = function(event) {
if (!isOver) {
showTooltip(event);
isOver = true;
}
};
var onMouseLeave = function(event) {
isOver = false;
$(container).remove();
}
var render = function() {
tooltipInner.innerHTML = html;
};
var bindEvents = function() {
$(elem).on('mouseover', onMouseOver);
$(elem).on('mouseleave', onMouseLeave);
};
var initialize = function(element, htmlStr, opts) {
elem = element;
html = htmlStr;
options = opts || {};
options.placement = opts.placement || 'bottom';
render();
bindEvents();
}
return {
init: initialize
};
}
});