-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtooltip.coffee
41 lines (31 loc) · 1.06 KB
/
tooltip.coffee
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
class Tooltip
constructor: (doc) ->
@cssClass = "suave-tooltip"
node = doc.querySelector("." + @cssClass) || doc.createElement('div')
@tip = d3.select(node)
@tip.attr("class", @cssClass)
doc.body.appendChild(@tip.node())
html: (content) =>
@tip.html(content)
show: (node) =>
offset = @offset(node)
@tip.attr("class", "#{@cssClass} show")
@tip.style({
"position" : "absolute",
"top" : "#{offset.top}px",
"left" : "#{offset.left}px"
})
hide: () =>
@tip.attr("class", @cssClass)
remove: () =>
@tip.remove()
offset: (node) ->
nodeCoords = node.getBoundingClientRect()
nodeCenter = nodeCoords.left + ((nodeCoords.right - nodeCoords.left) / 2)
tipCoords = @tip.node().getBoundingClientRect()
tipWidth = tipCoords.right - tipCoords.left
tipHeight = tipCoords.bottom - tipCoords.top
scrollX = window.scrollX
scrollY = window.scrollY
{ top: nodeCoords.top - (tipHeight + 10) + window.scrollY, left: nodeCenter - (tipWidth / 2) + window.scrollX }
module.exports = Tooltip