-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
82 lines (82 loc) · 2.9 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
"use strict";
/// <reference path="d/d3.d.ts" />
exports.__esModule = true;
var windowDimensions = {
width: window.innerWidth,
height: window.innerHeight
};
function setWindowDimmensions() {
windowDimensions = {
width: window.innerWidth,
height: window.innerHeight
};
}
var windowResize = window.addEventListener('resize', setWindowDimmensions);
function getOffset(event, bounds, offset) {
var collideVertically = (windowDimensions.height + window.scrollY) - event.pageY - offset.top - bounds.height < 0;
var collideHorizontally = (windowDimensions.width + window.scrollX) - event.pageX - offset.left - bounds.width < 0;
return {
top: collideVertically ? event.pageY - bounds.height - offset.top : offset.top + event.pageY,
left: collideHorizontally ? event.pageX - bounds.width - offset.left : event.pageX + offset.left
};
}
var defaultOffset = { top: 10, left: 10 };
function getOffsetSettings(offset) {
if (!offset)
return defaultOffset;
return {
top: offset.top === undefined ? defaultOffset.top : offset.top,
left: offset.left === undefined ? defaultOffset.left : offset.left
};
}
function d3scription(contentGetter, options) {
if (options === void 0) { options = {}; }
var offsetSettings = getOffsetSettings(options.offset);
return function () {
var tip = d3.select('body')
.append('div')
.attr('class', options["class"] || 'd3scription-tip')
.style('position', 'absolute')
.style('z-index', options.zIndex || 100)
.style('visibility', 'hidden');
function updateTipPosition() {
var bounds = tip.node().getBoundingClientRect();
var position = getOffset(d3.event, bounds, offsetSettings);
tip
.style("top", position.top + "px")
.style("left", position.left + "px");
}
function setupTracking(element) {
element.on('mousemove', updateTipPosition);
}
var publicMethods = {
element: function (element) {
setupTracking(element);
return publicMethods;
},
show: function (data) {
updateTipPosition();
tip.html(contentGetter(data));
tip.style('visibility', 'visible');
return publicMethods;
},
update: function (data) {
tip.html(contentGetter(data));
return publicMethods;
},
hide: function () {
tip.style('visibility', 'hidden');
return publicMethods;
},
remove: function () {
tip.remove();
}
};
return publicMethods;
};
}
exports["default"] = d3scription;
// export as Global Object
if (typeof window === 'object') {
window['d3scription'] = d3scription;
}