-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
132 lines (107 loc) · 3.72 KB
/
index.ts
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
/// <reference path="d/d3.d.ts" />
export interface Offset {
top? : number;
left? : number;
}
export interface Options {
zIndex? : number;
class? : string;
offset? : Offset;
}
export interface ContentGetter<T> extends Function {
(data : T) : string;
}
export interface Tip<T> {
element(element : d3.Selection<any>) : Tip<T>;
show(data : T) : Tip<T>;
update(data : T) : Tip<T>;
hide() : Tip<T>;
remove() : void;
}
export interface TipFactory<T> extends Function {
() : Tip<T>
}
interface WindowDimensions {
width : number,
height : number
}
interface Position {
top : number,
left : number,
}
let windowDimensions : WindowDimensions = {
width: window.innerWidth,
height: window.innerHeight
}
function setWindowDimmensions() : void {
windowDimensions = {
width: window.innerWidth,
height: window.innerHeight
}
}
const windowResize = window.addEventListener('resize', setWindowDimmensions);
function getOffset(event : MouseEvent, bounds : ClientRect, offset : Position) : Position {
const collideVertically : boolean = (windowDimensions.height + window.scrollY) - event.pageY - offset.top - bounds.height < 0;
const collideHorizontally : boolean = (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
};
}
const defaultOffset : Position = { top: 10, left: 10 };
function getOffsetSettings(offset? : Offset) : Position {
if (!offset) return defaultOffset;
return {
top: offset.top === undefined ? defaultOffset.top : offset.top,
left: offset.left === undefined ? defaultOffset.left : offset.left
};
}
export default function d3scription<T> (contentGetter : ContentGetter<T>, options:Options = {}) : TipFactory<T> {
const offsetSettings : Position = getOffsetSettings(options.offset);
return function () : Tip<T> {
const tip : d3.Selection<any> = 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() {
const bounds : ClientRect = tip.node().getBoundingClientRect();
const position : Position = getOffset(d3.event, bounds, offsetSettings)
tip
.style("top", `${position.top}px`)
.style("left", `${position.left}px`);
}
function setupTracking(element : d3.Selection<any>) : void {
element.on('mousemove', updateTipPosition);
}
const publicMethods : Tip<T> = {
element(element : d3.Selection<any>) : Tip<T> {
setupTracking(element);
return publicMethods;
},
show(data : T) : Tip<T> {
updateTipPosition();
tip.html(contentGetter(data));
tip.style('visibility', 'visible');
return publicMethods;
},
update(data : T) : Tip<T> {
tip.html(contentGetter(data));
return publicMethods;
},
hide() : Tip<T> {
tip.style('visibility', 'hidden');
return publicMethods;
},
remove() : void {
tip.remove();
}
};
return publicMethods;
};
}
// export as Global Object
if (typeof window === 'object') {
window['d3scription'] = d3scription;
}