moar silly changes

This commit is contained in:
Starbeamrainbowlabs 2024-07-14 23:50:06 +01:00
parent 2e9afeff5c
commit 47b446b4e6
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
6 changed files with 121 additions and 9 deletions

View file

@ -50,8 +50,8 @@ for(let txt of tqdm(txts, { total: txts.length })) {
continue; continue;
} }
const result = await throttled(txt, true); // bool is whether we're pretend or not - i.e. not making anthropic/claude api calls 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; result.i = i + 1;
console.log(result); console.log(result);

View file

@ -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`); const js = fs.readFileSync(path.join(__dirname, `../static/index.js`), `utf-8`);
export default function(objs) { 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; total = Math.round(total * 100) / 100;
const values = { const values = {

View file

@ -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.net = obj.paid - obj.vat;
obj.vat = round_silly(obj.vat, 2);
obj.vat = obj.vat.toFixed(2);
}
if(typeof obj.net === "undefined") if(typeof obj.net === "undefined")
obj.net = null; obj.net = null;
obj.vat = round_silly(obj.vat, 2); if(obj.net !== null) {
obj.net = round_silly(obj.net, 2); 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 = round_silly(obj.paid, 2);
obj.paid = obj.paid.toFixed(2);
}
obj.date = convert_date_silly(obj.date); obj.date = convert_date_silly(obj.date);

View file

@ -41,6 +41,10 @@ tr:hover {
text-align: right; text-align: right;
} }
.button.add {
color: #186918;
}
@media print { @media print {
.noprint { .noprint {
display: none; display: none;

View file

@ -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) { function handle_remove_row(event) {
const el_tr = event.target.closest("tr"); const el_tr = event.target.closest("tr");
if (el_tr === null) { if (el_tr === null) {
@ -37,13 +89,23 @@ function handle_add_row(event) {
case "button add-row": case "button add-row":
el_copy.addEventListener("click", handle_add_row); el_copy.addEventListener("click", handle_add_row);
el_copy.addEventListener("touchend", 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); el_next.append(el_copy);
} }
} }
else { else {
let txt = "placeholder"; let txt = "";
switch (i) { switch (i) {
case 0: case 0:
txt = highest_number + 1; 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", () => { window.addEventListener("load", () => {
console.log(`BEGIN`); console.log(`BEGIN`);
const els_remove = [...document.querySelectorAll(".button.remove-row")]; const els_remove = [...document.querySelectorAll(".button.remove-row")];
@ -90,4 +173,17 @@ window.addEventListener("load", () => {
el.addEventListener("click", handle_add_row); el.addEventListener("click", handle_add_row);
el.addEventListener("touchend", 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);
}); });

View file

@ -33,6 +33,8 @@
<td class="actions noprint"> <td class="actions noprint">
<span class="button remove-row"></span> <span class="button remove-row"></span>
<span class="button add-row"></span> <span class="button add-row"></span>
<span class="button up">&#x2b06;&#xfe0f;</span>
<span class="button down">&#x2b07;&#xfe0f;</span>
</td> </td>
</tr> </tr>
{#endeach} {#endeach}
@ -41,7 +43,7 @@
</table> </table>
<div class="align-right"> <div class="align-right">
<strong>All total:</strong> <output contenteditable>{{total}}</output> <strong>Grand Total:</strong> <output class="total" contenteditable>{{total}}</output>
</div> </div>
<!----------------> <!---------------->