-
Notifications
You must be signed in to change notification settings - Fork 0
/
directives.js
63 lines (48 loc) · 1.51 KB
/
directives.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
;(function() {
"use strict";
angular.module("myApp.directives", [])
.directive('myTooltip', function(){
// Runs during compile
return {
restrict: "A",
scope: {
text: "@tooltipText"
},
link: function($scope, iElm, iAttrs, controller) {
var elem = iElm[0],
text = $scope.text,
tooltip;
if (!text) return;
addEventHandlers();
function createTooltip(text) {
var tooltip = document.createElement("span");
tooltip.textContent = text;
tooltip.className = "my-tooltip";
return tooltip;
}
function onMouseOver(e) {
var scrollTop = window.pageYOffset || document.documentElement.scrollTop,
elemCoords = elem.getBoundingClientRect();
tooltip = createTooltip(text);
document.body.appendChild(tooltip);
tooltip.style.top = elemCoords.top + scrollTop - tooltip.offsetHeight + "px";
tooltip.style.left = elemCoords.left - tooltip.offsetWidth / 2 + elem.offsetWidth / 2 + "px";
}
function onMouseOut() {
document.body.removeChild(tooltip);
}
function addEventHandlers() {
elem.addEventListener("mouseover", onMouseOver, false);
elem.addEventListener("mouseout", onMouseOut, false);
}
function removeEventHandlers() {
elem.removeEventListener("mouseover", onMouseOver, false);
elem.removeEventListener("mouseout", onMouseOut, false);
}
iElm.on("$destroy", function() {
removeEventHandlers();
});
}
};
})
})();