From 47b446b4e6d611cef1f3ab6354b9730a2cfa869c Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Sun, 14 Jul 2024 23:50:06 +0100 Subject: [PATCH] moar silly changes --- src/index.mjs | 4 +- src/lib/make-html.mjs | 4 +- src/lib/parse-result.mjs | 16 +++++-- src/static/index.css | 4 ++ src/static/index.js | 98 +++++++++++++++++++++++++++++++++++++++- src/static/template.html | 4 +- 6 files changed, 121 insertions(+), 9 deletions(-) diff --git a/src/index.mjs b/src/index.mjs index 4ccd2ad..5f13ae1 100755 --- a/src/index.mjs +++ b/src/index.mjs @@ -50,8 +50,8 @@ for(let txt of tqdm(txts, { total: txts.length })) { continue; } - const result = await throttled(txt, true); // bool is whether we're pretend or not - i.e. not making anthropic/claude api calls - result.i = i; + const result = await throttled(txt, false); // bool is whether we're pretend or not - i.e. not making anthropic/claude api calls + result.i = i + 1; console.log(result); diff --git a/src/lib/make-html.mjs b/src/lib/make-html.mjs index 953989c..352acd0 100644 --- a/src/lib/make-html.mjs +++ b/src/lib/make-html.mjs @@ -13,7 +13,9 @@ const css = fs.readFileSync(path.join(__dirname, `../static/index.css`), `utf-8` const js = fs.readFileSync(path.join(__dirname, `../static/index.js`), `utf-8`); export default function(objs) { - let total = objs.reduce((acc, obj) => acc + obj.paid, 0); + // .toFixed() earlier is a P A I N + let total = objs.filter(obj => obj.paid !== null) + .reduce((acc, obj) => acc + parseFloat(obj.paid), 0); total = Math.round(total * 100) / 100; const values = { diff --git a/src/lib/parse-result.mjs b/src/lib/parse-result.mjs index c03aea9..82038c3 100644 --- a/src/lib/parse-result.mjs +++ b/src/lib/parse-result.mjs @@ -41,15 +41,23 @@ export default function parse_result(obj) { } } - if(obj.vat !== null) + if(obj.vat !== null) { obj.net = obj.paid - obj.vat; + obj.vat = round_silly(obj.vat, 2); + obj.vat = obj.vat.toFixed(2); + } if(typeof obj.net === "undefined") obj.net = null; - obj.vat = round_silly(obj.vat, 2); - obj.net = round_silly(obj.net, 2); - obj.paid = round_silly(obj.paid, 2); + if(obj.net !== null) { + obj.net = round_silly(obj.net, 2); + obj.net = obj.net.toFixed(2); + } + if(obj.paid !== null) { + obj.paid = round_silly(obj.paid, 2); + obj.paid = obj.paid.toFixed(2); + } obj.date = convert_date_silly(obj.date); diff --git a/src/static/index.css b/src/static/index.css index 70b3c86..2c30ea0 100644 --- a/src/static/index.css +++ b/src/static/index.css @@ -41,6 +41,10 @@ tr:hover { text-align: right; } +.button.add { + color: #186918; +} + @media print { .noprint { display: none; diff --git a/src/static/index.js b/src/static/index.js index 090ee96..5138bee 100644 --- a/src/static/index.js +++ b/src/static/index.js @@ -1,3 +1,55 @@ +function strip_immediate_text_nodes(el_target) { + for (const el of [...el_target.childNodes].filter(el => el.nodeName === "#text")) + el.remove(); +} + +// Ref https://stackoverflow.com/a/77069029/1460422 +const round_to_dp = (n, p = 2) => (e => Math.round(n * e) / e)(Math.pow(10, p)); + + +///////////////////////////////////////////////////////////////////// + + +function handle_move_dir(dir, event) { + const el_tr_target = event.target.closest("tr"); + const el_tbody = document.querySelector("tbody"); + + strip_immediate_text_nodes(el_tbody); // Without this, we can't swap the order reliably. They may even come back at random intervals so we hafta idiot-proof this + + let target_i = null; + // Ref https://stackoverflow.com/a/34349073/1460422 + for(const [i, el] of [...el_tbody.childNodes].entries()) { + if(el === el_tr_target) { + target_i = i; + break; + } + } + if(target_i === null) { + console.error(`Error: Failed to find tr`, el_tr_target, `...in tbody`, el_tbody); + alert(`Error: Failed to find table row in table body. Check the dev console for more information.`); + return false; + } + + if(target_i === 0 || target_i === el_tbody.childNodes.length - 1) { + console.info(`Suppressing out-of-bounds move for i`, target_i, `el`, el_tr_target); + return; + } + + // Move in the right direction + switch(dir) { + case "up": + el_tbody.childNodes[target_i - 1].before(el_tbody.childNodes[target_i]); + break; + case "down": + el_tbody.childNodes[target_i + 1].after(el_tbody.childNodes[target_i]); + break; + default: + console.error(`Error: Unknown direction '${dir}'`); + return false; + } + +} + function handle_remove_row(event) { const el_tr = event.target.closest("tr"); if (el_tr === null) { @@ -37,13 +89,23 @@ function handle_add_row(event) { case "button add-row": el_copy.addEventListener("click", handle_add_row); el_copy.addEventListener("touchend", handle_add_row); + break; + case "button up": + el_copy.addEventListener("click", handle_move_dir.bind(null, "up")); + el_copy.addEventListener("touchend", handle_move_dir.bind(null, "up")); + break; + case "button down": + el_copy.addEventListener("click", handle_move_dir.bind(null, "down")); + el_copy.addEventListener("touchend", handle_move_dir.bind(null, "down")); + break; + } } el_next.append(el_copy); } } else { - let txt = "placeholder"; + let txt = ""; switch (i) { case 0: txt = highest_number + 1; @@ -76,6 +138,27 @@ function rewrite_indices() { } } +function update_total() { + const el_tbody = document.querySelector("tbody"); + const el_total = document.querySelector("output.total"); + + strip_immediate_text_nodes(el_tbody); + + const total = round_to_dp([...el_tbody.childNodes] + .map(el => { + strip_immediate_text_nodes(el); + return el.childNodes[6].textContent.trim(); + }) + .filter(val => val.length > 0 + && val !== "null" + && !isNaN(parseFloat(val))) + .map(val => parseFloat(val)) + .reduce((acc, next) => acc + next, 0), 2); + + el_total.replaceChildren(); + el_total.append(document.createTextNode(`${total}`)); +} + window.addEventListener("load", () => { console.log(`BEGIN`); const els_remove = [...document.querySelectorAll(".button.remove-row")]; @@ -90,4 +173,17 @@ window.addEventListener("load", () => { el.addEventListener("click", handle_add_row); el.addEventListener("touchend", handle_add_row); } + + const els_up = [...document.querySelectorAll(".button.up")]; + for (const el of els_up) { + el.addEventListener("click", handle_move_dir.bind(null, "up")); + el.addEventListener("touchend", handle_move_dir.bind(null, "up")); + } + const els_down = [...document.querySelectorAll(".button.down")]; + for (const el of els_down) { + el.addEventListener("click", handle_move_dir.bind(null, "down")); + el.addEventListener("touchend", handle_move_dir.bind(null, "down")); + } + + document.addEventListener("keyup", update_total); }); \ No newline at end of file diff --git a/src/static/template.html b/src/static/template.html index 09a110c..4111fe3 100644 --- a/src/static/template.html +++ b/src/static/template.html @@ -33,6 +33,8 @@ + ⬆️ + ⬇️ {#endeach} @@ -41,7 +43,7 @@
- All total: {{total}} + Grand Total: {{total}}