-
Notifications
You must be signed in to change notification settings - Fork 6
/
exhibit-helpers.js
96 lines (85 loc) · 2.3 KB
/
exhibit-helpers.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
function createExhibitHelpers() {
function elWidth(el) {
var width = el.clientWidth;
if (width < 1) {
// This is necessary on Firefox.
width = el.parentElement.clientWidth
}
return width;
}
function elHeight(el) {
var height = el.clientHeight;
if (height < 1) {
// This is necessary on Firefox.
height = el.parentElement.clientHeight;
}
return height;
}
function wrapInEventHandler(useEventDetail) {
return function respondToEvent(e) {
useEventDetail(e.detail);
};
}
function respondToEventWithFn(eventName, fn) {
document.addEventListener(eventName, wrapInEventHandler(fn));
}
function compose(f, g) {
return function composed(x) {
return g(f(x));
};
}
function randomPointFunctor(pointFieldBounds) {
return function randomPoint() {
return [
~~(Math.random() * pointFieldBounds[0]),
~~(Math.random() * pointFieldBounds[1])
];
};
}
function animateHalo(target) {
var enterDuration = 700;
var exitDuration = 1000;
var originalRadius = +target.attr('r');
var originalBorderWidth = target.style('stroke-width').replace('px', '');
target.transition()
.duration(enterDuration)
.attr('r', originalRadius + 4)
.style('stroke-width', 10);
target.transition()
.delay(enterDuration)
.duration(exitDuration)
.attr('r', originalRadius)
.style('stroke-width', originalBorderWidth);
}
function hideElement() {
var idToHide = this.dataset.target;
var fadeLength = 500;
d3.select('#' + idToHide).transition()
.duration(fadeLength)
.style('opacity', 0)
.transition()
.delay(fadeLength)
.style('display', 'none');
}
function showElement() {
var idToShow = this.dataset.target;
var fadeLength = 500;
d3.select('#' + idToShow)
.style('opacity', 0)
.style('display', 'block')
.transition()
.duration(fadeLength)
.style('opacity', 1);
}
return {
elWidth: elWidth,
elHeight: elHeight,
wrapInEventHandler: wrapInEventHandler,
respondToEventWithFn: respondToEventWithFn,
compose: compose,
randomPointFunctor: randomPointFunctor,
animateHalo: animateHalo,
hideElement: hideElement,
showElement: showElement
};
}