-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
241 lines (220 loc) · 5.49 KB
/
script.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// You should not need to modify anything in this file to set up your own survey website
const container = d3.select(".grid");
const facets = !taxonomy || Object.keys(taxonomy);
// create checkboxes to filter techniques
if (taxonomy != null) {
var filters = d3
.select("#filters")
.selectAll("div")
.data(facets)
.enter()
.append("div")
.attr("id", (d) => "select_" + d)
.attr(
"class",
(d) =>
"select_width" +
((taxonomy[d].length == 2) | (taxonomy[d].length == 4) ? 2 : 3)
);
filters.append("h3").html((d) => formatText(d));
var checkboxes = filters
.selectAll("input")
.data((d) => taxonomy[d])
.enter()
.append("div")
.classed("checkbox-container", true);
checkboxes
.append("input")
.attr("type", "checkbox")
.attr("class", "input")
.attr("id", function (d) {
return (
"check_" +
d3.select(this.parentNode.parentNode).datum() +
"_" +
d
);
})
.attr("value", (d) => d);
checkboxes
.append("label")
.attr("for", function (d) {
return (
"check_" +
d3.select(this.parentNode.parentNode).datum() +
"_" +
d
);
})
.append("span")
.text((d) => formatText(d));
}
if (tags) {
// checkboxes for data types
var checkData = d3
.select("#filters_data")
.selectAll("div")
.data(tags)
.enter()
.append("div");
checkData
.append("input")
.attr("type", "checkbox")
.attr("class", "input")
.attr("id", (d) => "check_" + d)
.attr("value", (d) => d);
checkData
.append("label")
.attr("for", (d) => "check_" + d)
.append("span")
.text((d) => sentenceCase(d));
}
d3.select("#showall").on("click", function () {
d3.selectAll("input").property("checked", false);
// dispatch event to reload techniques
let event = new Event("change");
eventHandler.dispatchEvent(event);
});
d3.csv(url, (d, i) => {
d.id = i;
return d;
})
.then(function (data) {
// display count
d3.selectAll("#count, #total").text(data.length);
// listen for changes in filters
d3.selectAll(".input").on("change", function () {
// get filter values
if (taxonomy != null) {
var filters = facets.map(function (facet) {
var cats = [];
taxonomy[facet].forEach(function (cat) {
console.log(facet, " ", cat);
if (d3.select("#check_" + facet + "_" + cat).property("checked")) {
cats.push(cat);
}
});
return [facet, cats];
});
} else {
var filters = null;
}
if (tags != null) {
var dataFilters = tags.filter(function (d) {
return d3.select("#check_" + d).property("checked");
});
} else {
var dataFilters = null;
}
// update
refreshTechniques(filters, dataFilters);
});
function refreshTechniques(filters, dataFilters) {
// filter
var fData = data.filter((d) => filterData(d, filters, dataFilters));
// update count in heading
d3.select("#count").text(fData.length);
// get IDs of techniques matching filter
var ids = fData.map((d) => d.id);
// hide all non-matching ones
d3.selectAll(".grid-item").style("display", (d) =>
ids.indexOf(d.id) != -1 ? null : "none"
);
// update layout
msnry.layout();
}
// draw boxes for papers
var div = container
.selectAll("div")
.data(data)
.enter()
.append("div")
.classed("grid-item", true);
// show image if defined
div.filter((d) => {
console.log(d);
return d.image != "";
})
.append("img")
.attr("src", (d) => "img/" + d.image);
// add div for text content (modifiable in config)
div.append("div").call(cardContent);
// add taxonomy tags
var tax_tags = div.append("div").style("margin-top", "7px");
// add tags on technique cards (if taxonomy is defined)
if (taxonomy) {
facets.forEach(function (facet) {
tax_tags
.append("div")
.classed("tag", true)
.classed(facet, true)
.html((d) => {
console.log("d[facet]:", d[facet]);
return d[facet];
});
});
}
})
.then(function () {
imagesLoaded(".grid", function () {
var elem = document.querySelector(".grid");
window.msnry = new Masonry(elem, {
// options
itemSelector: ".grid-item",
columnWidth: 241,
gutter: 15,
});
});
})
.catch(function (error) {
throw error;
});
function filterData(d, filters, dataFilters) {
return (
(filters == null ||
filters.every(function (fil) {
// facet: fil[0]
// selected: fil[1]
// check if either array is empty or category is selected
return fil[1].length == 0 || fil[1].indexOf(d[fil[0]]) != -1;
})) &&
(dataFilters == null ||
dataFilters.every(function (fil) {
return d[fil] == "yes";
}))
);
}
function unique(arr, acc) {
return arr.map(acc).filter(function (value, index, self) {
return self.indexOf(value) === index;
});
}
function formatText(str) {
// capitalise and replace underscores by spaces
// replace first letter
str = str.slice(0, 1).toUpperCase() + str.slice(1);
// find all underscores, replace by spaces and capitalise following letter
while (str.indexOf("_") != -1) {
str =
str.slice(0, str.indexOf("_")) +
" " +
str
.slice(str.indexOf("_") + 1, str.indexOf("_") + 2)
.toUpperCase() +
str.slice(str.indexOf("_") + 2);
}
return str;
}
function sentenceCase(str) {
// capitalise first word and replace underscores by spaces
// replace first letter
str = str.slice(0, 1).toUpperCase() + str.slice(1);
// find all underscores, replace by spaces and capitalise following letter
while (str.indexOf("_") != -1) {
str =
str.slice(0, str.indexOf("_")) +
" " +
str.slice(str.indexOf("_") + 1);
}
return str;
}