Skip to content

Commit 457371c

Browse files
committed
Polished the CSV download feature
1 parent c51b3f0 commit 457371c

3 files changed

Lines changed: 55 additions & 44 deletions

File tree

src/Sim/write.ts

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import jsonData from "../Data/data.json" assert { type: "json" };
22
import { convertTime, formatNumber, isMainTheory, logToExp } from "../Utils/helpers";
3-
import { qs, qsa, ce, event, removeAllChilds, downloadString } from "../Utils/DOMhelpers";
3+
import { qs, qsa, ce, event, removeAllChilds, downloadString, getTableHeaders, tau } from "../Utils/DOMhelpers";
44

55
// Settings
66
const generateTotalPurchaseList = qs<HTMLInputElement>(".totalPurchaseList");
@@ -14,10 +14,6 @@ const varBuyDialog = qs<HTMLDialogElement>(".boughtVars");
1414
const varBuyTable = qs<HTMLTableSectionElement>(".boughtVarsOtp");
1515
const varBuyListCloseBtn = qs<HTMLButtonElement>(".boughtVarsCloseBtn");
1616

17-
// Consts
18-
const tau = `<span style="font-size:0.9rem; font-style:italics">&tau;</span>`;
19-
const rho = `<span style="font-size:0.9rem; font-style:italics">&rho;</span>`;
20-
2117
const downloadIcon = '<svg xmlns="http://www.w3.org" width="24" height="24" viewBox="0 0 24 24" ' +
2218
'fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"' +
2319
'class="feather feather-download">\n' +
@@ -299,35 +295,8 @@ export function writeSimResponse(response: SimResponse) {
299295
setTableMode(mode);
300296
clearTable();
301297
}
302-
if (mode === "all") {
303-
let headers = [
304-
`${response.sigma}<span style="font-size:0.9rem;">&sigma;</span><sub>t</sub>`,
305-
'Input'
306-
];
307-
if (response.stratType == "all") headers.push('Ratio');
308-
headers.push(
309-
`${tau}/h`,
310-
'Multi',
311-
'Strat',
312-
'Time',
313-
`&Delta;${tau}`,
314-
`Pub ${rho}`,
315-
'Var Buys'
316-
)
317-
setTableHeaders(...headers);
318-
}
319-
else setTableHeaders(
320-
'<span style="padding-inline: 0.5rem">Theory</span>',
321-
'<span style="font-size:0.9rem;">&sigma;</span><sub>t</sub>',
322-
'Last Pub',
323-
'Max Rho',
324-
`&Delta;${tau}`,
325-
'Multi',
326-
'Strat',
327-
`${tau}/h`,
328-
'Pub Time',
329-
'Var Buys'
330-
);
298+
if (mode === "all") setTableHeaders(...getTableHeaders(response.stratType === "all" ? "all" : "all_one", "html", response.sigma), 'Var Buys')
299+
else setTableHeaders(...getTableHeaders("single", "html"), 'Var Buys');
331300

332301
totalBuys = [];
333302

src/UI/buttonEvents.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import html2canvas from "html2canvas";
2-
import { qs, qsa, ce, event, removeAllChilds, downloadString } from "../Utils/DOMhelpers";
2+
import { qs, qsa, ce, event, removeAllChilds, downloadString, tau, rho, sigma_t, getTableHeaders } from "../Utils/DOMhelpers";
33

44
//Buttons
55
const clear = qs(".clear");
@@ -95,10 +95,12 @@ function makeTableCsv(): string {
9595
let csvTotal = "";
9696

9797
if (table.classList.contains("big")) {
98-
for (let i = 0; i < theadRow.children.length - 1; i++) {
99-
csvTotal += theadRow.children[i].innerHTML + ",";
100-
}
101-
csvTotal += "\n";
98+
let headers = getTableHeaders(
99+
theadRow.children.length == 10 ? "all" : "all_one",
100+
"text",
101+
Number(theadRow.children[0].innerHTML.match(/^\d+/))
102+
)
103+
csvTotal += headers.join(",") + ",\n";
102104
let rowIndex = 0;
103105
while (rowIndex < tbody.children.length) {
104106
let row = tbody.children[rowIndex];
@@ -124,7 +126,7 @@ function makeTableCsv(): string {
124126
csvTotal += row2Content.join(",") + ",\n";
125127
}
126128
else {
127-
for (let i = 0; i < 9; i++) {
129+
for (let i = 0; i < theadRow.children.length - 1; i++) {
128130
csvTotal += row.children[i].innerHTML + ",";
129131
}
130132
csvTotal += "\n";
@@ -134,11 +136,12 @@ function makeTableCsv(): string {
134136
}
135137
}
136138
else {
137-
for (let i = 0; i < 9; i++) {
138-
csvTotal += theadRow.children[i].innerHTML + ",";
139-
}
140-
csvTotal += "\n";
139+
let headers = getTableHeaders("single", "text")
140+
csvTotal += headers.join(",") + ",\n";
141141
for (let row of tbody.children) {
142+
if (row.children[0].innerHTML.trim().length == 0) {
143+
continue;
144+
}
142145
for (let i = 0; i < 9; i++) {
143146
csvTotal += row.children[i].innerHTML + ",";
144147
}

src/Utils/DOMhelpers.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
const raise = (err: string) => {
33
throw new Error(err);
44
};
5+
6+
// Consts
7+
export const tau = `<span style="font-size:0.9rem; font-style:italics">&tau;</span>`;
8+
export const rho = `<span style="font-size:0.9rem; font-style:italics">&rho;</span>`;
9+
export const sigma_t = `<span style="font-size:0.9rem;">&sigma;</span><sub>t</sub>`;
10+
511
/** Alias of document.querySelector */
612
export const qs = <T extends HTMLElement>(name: string) => document.querySelector<T>(name) ?? raise(`HtmlElement ${name} not found.`);
713
/** Alias of document.querySelectorAll */
@@ -37,4 +43,37 @@ export function downloadString(str: string, filename: string) {
3743
link.click();
3844
document.body.removeChild(link); // Clean up the DOM element
3945
}
46+
}
47+
48+
export function getTableHeaders(tableType: "single" | "all" | "all_one", headerType: "html" | "text", sigma?: number): string[] {
49+
if (tableType !== "single") {
50+
let headers = [
51+
headerType === "html" ? `${sigma}${sigma_t}` : `${sigma}σ`,
52+
'Input'
53+
];
54+
if (tableType == "all") headers.push('Ratio');
55+
headers.push(
56+
headerType === "html" ? `${tau}/h` : "τ/h",
57+
'Multi',
58+
'Strat',
59+
'Time',
60+
headerType === "html" ? `&Delta;${tau}` : "Δτ",
61+
headerType === "html" ? `Pub ${rho}` : "Pub ρ"
62+
)
63+
return headers;
64+
}
65+
else {
66+
let headers = [
67+
'<span style="padding-inline: 0.5rem">Theory</span>',
68+
headerType === "html" ? sigma_t : "σ",
69+
'Last Pub',
70+
'Max Rho',
71+
headerType === "html" ? `&Delta;${tau}` : "Δτ",
72+
'Multi',
73+
'Strat',
74+
headerType === "html" ? `${tau}/h` : "τ/h",
75+
'Pub Time'
76+
];
77+
return headers;
78+
};
4079
}

0 commit comments

Comments
 (0)