-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcsv.js
223 lines (205 loc) · 6.94 KB
/
csv.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
import {
getDetails,
getDetailTitle,
existsDetail,
getCurrentShotTypes,
getTypeIndex,
saveCurrentSetup,
} from "./details/details-functions.js";
import {
createFilterRow,
select2Filter,
existFilters,
} from "./table/filter.js";
import {
clearTable,
getHeaderRow,
getFilteredRows,
getRows,
} from "./table/table-functions.js";
import { updateTableFooter } from "./table/table.js";
import { createShotFromData } from "./shots/shot.js";
import { shotTypeLegend, teamLegend } from "./shots/legend.js";
import { downloadArea, uploadArea } from "./components/upload-download.js";
import { cfgSportA } from "../setup.js";
function setUpCSVDownloadUpload() {
// Custom Filename
const d = new Date(Date.now());
const defaultFileName = `${(
d.getMonth() + 1
).toString()}.${d.getDate()}.${d.getFullYear()}-${d
.getHours()
.toString()
.padStart(2, "0")}.${d.getMinutes().toString().padStart(2, "0")}`;
downloadArea(
"#csv-upload-download",
defaultFileName,
() => downloadCSV("#csv-upload-download"),
".csv"
);
uploadArea(
"#csv-upload-download",
"csv-upload",
(e) => uploadCSV("#csv-upload-download", "#csv-upload", e),
"Only .csv files are allowed. The column headers in the .csv file must be identical to the column headers in the table, excluding #. Order matters."
);
}
export function toggleDownloadText() {
const node = d3
.select("#csv-upload-download")
.select("button")
.text(existFilters() ? "Download Filtered" : "Download");
}
function downloadCSV(id) {
// set up header row
let csv = "";
let header = [];
d3.select("#shot-table")
.select("thead")
.selectAll("th")
.each(function () {
header.push(d3.select(this).attr("data-id"));
let text = d3.select(this).text();
if (text !== "" && text !== "#") {
csv += text + ",";
}
});
csv = csv.slice(0, -1) + "\n";
const rows = getFilteredRows();
for (let row of rows) {
for (let col of _.compact(header)) {
if (col !== "shot-number") {
csv += escape(row.rowData[col].toString()) + ",";
}
}
// remove trailing comma
csv = csv.slice(0, -1) + "\n";
}
csv = csv.slice(0, -1); // remove trailing new line
let fileName = d3.select(id).select(".download-name").property("value");
if (!fileName) {
fileName = d3.select(id).select(".download-name").attr("placeholder");
}
download(csv, fileName + ".csv", "text/csv");
}
function escape(text) {
return text.includes(",") ? '"' + text + '"' : text;
}
async function uploadCSV(id, uploadId, e) {
if (/.csv$/i.exec(d3.select(uploadId).property("value"))) {
const f = e.target.files[0];
if (f) {
// change text and wipe value to allow for same file upload
// while preserving name
d3.select(id).select(".upload-name-text").text(f.name);
d3.select(id).select(".upload").property("value", "");
// remove invalid class if necessary
d3.select(uploadId).classed("is-invalid", false);
let swapTeamColor = "blueTeam";
clearTable();
Papa.parse(f, {
header: true,
skipEmptyLines: true,
step: function (row) {
swapTeamColor = processCSV(
uploadId,
row.data,
swapTeamColor
);
},
});
updateTableFooter();
}
} else {
d3.select(uploadId).classed("is-invalid", true);
}
}
function processCSV(uploadId, row, swapTeamColor) {
// only process if current table header (minus shot) is Identical to the current header
let tableHeader = [];
d3.select("#shot-table")
.select("thead")
.selectAll("th")
.each(function () {
let text = d3.select(this).text();
if (text.length > 0 && text !== "#") {
tableHeader.push(text);
}
});
const csvHeader = Object.keys(row);
if (!_.isEqual(tableHeader, csvHeader)) {
d3.select(uploadId).classed("is-invalid", true);
return swapTeamColor;
}
// add any new shot type options
if (existsDetail("#shot-type")) {
const value = row[getDetailTitle("#shot-type")];
const typeOptions = getCurrentShotTypes().map((x) => x.value);
if (typeOptions.indexOf(value) === -1) {
d3.select("#shot-type-select").append("option").text(value);
shotTypeLegend();
saveCurrentSetup();
createFilterRow(getDetails());
select2Filter();
}
}
let newSwapTeam = swapTeamColor;
let teamColor;
if (existsDetail("#team")) {
const team = row[getDetailTitle("#team")];
// add any new team name
if (!team) {
teamColor = "blueTeam";
} else if (team === d3.select("#blue-team-name").property("value")) {
teamColor = "blueTeam";
} else if (team === d3.select("#orange-team-name").property("value")) {
teamColor = "orangeTeam";
} else {
const swapTeamId =
swapTeamColor === "blueTeam"
? "#blue-team-name"
: "#orange-team-name";
d3.select(swapTeamId).property("value", team);
teamLegend();
saveCurrentSetup();
createFilterRow(getDetails());
select2Filter();
teamColor = swapTeamColor;
// alternate changing team names
newSwapTeam =
swapTeamColor === "blueTeam" ? "orangeTeam" : "blueTeam";
}
}
// add additional attributes to row
let id = uuidv4();
let specialData = {
typeIndex: getTypeIndex(row.Type),
teamColor: teamColor,
coords: [
parseFloat(row.X) + cfgSportA.width / 2,
-1 * parseFloat(row.Y) + cfgSportA.height / 2,
], // undo coordinate adjustment
player: row.Player,
numberCol: _.findIndex(getHeaderRow(), { type: "shot-number" }) - 1,
};
if (row.X2 && row.Y2) {
specialData.coords2 = [
parseFloat(row.X2) + cfgSportA.width / 2,
-1 * parseFloat(row.Y2) + cfgSportA.height / 2,
]; // undo coordinate adjustment
}
let headerIds = _.without(
_.compact(getHeaderRow().map((x) => x.id)),
"shot-number"
);
let rowData =
specialData.numberCol !== -2
? { "shot-number": getRows().length + 1 }
: {};
_.forEach(_.zip(headerIds, Object.values(row)), function ([header, value]) {
rowData[header] = value;
});
createShotFromData(id, rowData, specialData);
return newSwapTeam;
}
export { setUpCSVDownloadUpload };