-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.js
108 lines (94 loc) · 2.35 KB
/
init.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
97
98
99
100
101
102
103
104
105
106
107
108
window.onbeforeunload = function() {
window.scrollTo(0, 0);
};
var narrative = d3.select('#sections');
var narrativeDiv = narrative
.selectAll('div')
.data(sections)
.enter()
.append('div')
.attr('id', 'section');
narrativeDiv
.append('div')
.attr('id', 'section-text')
.html(function(d) {
return (
'<h1>' +
d.title +
'</h1><h2>' +
d.subTitle +
'</h2><p>' +
d.body +
'</p><p class="comment">' +
d.comment +
'</p>'
);
});
charts.forEach(function(element) {
var chartSections = sections.filter(function(section) {
return section.viz === element.title;
});
element.firstSectionIndex = sections.indexOf(chartSections[0]);
element.lastSectionIndex = sections.indexOf(chartSections[chartSections.length - 1]);
element.currentPos = 0;
element.vert_margin = 0;
element.transitionIndex = 0;
element.active = false;
});
console.log(charts);
var vizzes = d3.select('#charts');
var chart = vizzes
.selectAll('div')
.data(charts)
.enter()
.append('div')
.attr('id', 'chart');
var chartLabel = chart.append('div').attr('id', 'chart-text');
chart.attr('id', function(d) {
d.update(d3.select(this));
return 'chart';
});
function updateCharts() {
chart.attr('class', function(d, i) {
if (d.active) {
d.update(d3.select(this));
return 'activeChart';
}
return 'inactiveChart';
});
chart.style('margin-top', function(d) {
return d.vert_margin + 'px';
});
}
var transitionScale = d3
.scaleLinear()
.domain([window.innerHeight * 0.2, window.innerHeight - window.innerHeight * 0.2])
.range([0, 100])
.clamp(true);
updateCharts();
function setActiveSection(index, scroll) {
sections.forEach(function(sec) {
sec.active = false;
});
sections[index].active = true;
charts.forEach(function(element) {
if (index === element.lastSectionIndex) {
//moving to next element
element.vert_margin = -scroll;
} else if (index >= element.firstSectionIndex && index < element.lastSectionIndex) {
//active
element.currentPos = transitionScale(scroll);
element.transitionIndex = index - element.firstSectionIndex;
element.vert_margin = 0;
} else if (index > element.lastSectionIndex) {
//passed
element.vert_margin = -window.innerHeight;
}
if (index >= element.firstSectionIndex && index < element.lastSectionIndex) {
element.active = true;
} else {
element.active = false;
}
});
updateCharts();
}