-
Notifications
You must be signed in to change notification settings - Fork 4
/
tableload.js
183 lines (163 loc) · 4.94 KB
/
tableload.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
function init(params) {
var key = params.key;
var account = params.account;
var repo = params.repo;
var pagination = params.pagination || 20;
var data;
var currentData;
var filters = {};
var subjectFilter = document.getElementById("subject");
var coverageFilter = document.getElementById("coverage");
var fromFilter = document.getElementById("from");
var toFilter = document.getElementById("to");
var fromPicker, toPicker;
Tabletop.init({
key: key,
callback: loadData,
simpleSheet: true})
function loadData(csv, tabletop) {
data = csv.map(function(d) {d.date = new Date(d.date); return d;});
initFilters();
filterData();
}
function initFilters() {
var subjects = {}, coverage = {};
var minYear, maxYear;
var minDate = new Date(), maxDate = new Date(0);
var i;
var myOption;
data.forEach(function(d) {
subjects[d.subject] = true;
coverage[d.coverage] = true;
if (minDate > d.date) {minDate = d.date;}
if (maxDate < d.date) {maxDate = d.date;}
})
minYear = minDate.getUTCFullYear();
maxYear = maxDate.getUTCFullYear();
subjects = ["all"].concat(Object.keys(subjects));
coverage = ["all"].concat(Object.keys(coverage));
fromPicker = new Pikaday({
field: fromFilter,
position: 'top left',
firstDay: 1,
minDate: minDate,
maxDate: maxDate,
yearRange: [minYear, maxYear]
});
toPicker = new Pikaday({
field: toFilter,
position: 'top left',
firstDay: 1,
minDate: minDate,
maxDate: maxDate,
yearRange: [minYear, maxYear]
});
while (subjectFilter.firstChild) {
subjectFilter.remove(subjectFilter.firstChild);
}
while (coverageFilter.firstChild) {
coverageFilter.remove(coverageFilter.firstChild);
}
subjects.forEach(function(d, i) {
myOption = document.createElement("option");
myOption.value = d;
myOption.innerHTML = d;
subjectFilter.appendChild(myOption);
})
coverage.forEach(function(d, i) {
myOption = document.createElement("option");
myOption.value = d;
myOption.innerHTML = d;
coverageFilter.appendChild(myOption);
})
subjectFilter.addEventListener("change", function() {
var val = subjectFilter.value;
if (val === "all") {
delete filters.subject;
} else {
filters.subject = val;
}
filterData();
})
coverageFilter.addEventListener("change", function() {
var val = coverageFilter.value;
if (val === "all") {
delete filters.coverage;
} else {
filters.coverage = val;
}
filterData();
})
fromFilter.addEventListener("change", function() {
var val = fromFilter.value;
if (val === "") {
delete filters.from;
} else {
filters.from = new Date(val);
}
filterData();
})
toFilter.addEventListener("change", function() {
var val = toFilter.value;
if (val === "") {
delete filters.to;
} else {
filters.to = new Date(val);
}
filterData();
})
}
function filterData() {
console.log(filters);
currentData = data.filter(function(d) {
var keep = true;
Object.keys(filters).some(function(k) {
if (["coverage", "subject"].indexOf(k) >=0) {
if (d[k] !== filters[k]) {return (keep = false);}
}
if (k === "from" && d.date < filters.from) {
return (keep = false);
}
if (k === "to" && d.date > filters.to) {
return (keep = false);
}
});
return keep;
})
showData();
}
function showData() {
console.log(currentData);
var i;
var tableRecords = document.querySelectorAll("#files tr.record");
var table = document.getElementById("files")
for (i = 0; i < tableRecords.length; i ++) {
table.removeChild(tableRecords[i]);
}
currentData.forEach(function(d) {
var newRow = document.createElement("tr");
newRow.classList.add("record");
["id", "title", "date", "records", "coverage", "subject", "link"].forEach(function(k) {
var newCell = document.createElement("td");
if (k !== "link") {
if (k === "date") {
var date = new Date(d[k]);
newCell.innerHTML = (date.getMonth() + 1) + "/" + date.getDate() + "/" + date.getUTCFullYear();
} else {
newCell.innerHTML = d[k];
}
} else {
https://raw.githubusercontent.com/jckr/drunken-sansa/master/
var a = document.createElement("a");
a.setAttribute("href", "https://raw.githubusercontent.com/" + account + "/" + repo + "/master/" + d[k]);
a.setAttribute("download", "");
a.setAttribute("target", "_blank");
a.innerHTML = "Download"
newCell.appendChild(a);
}
newRow.appendChild(newCell);
})
table.appendChild(newRow);
})
}
}