moar silly changes
This commit is contained in:
parent
2e9afeff5c
commit
47b446b4e6
6 changed files with 121 additions and 9 deletions
|
@ -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);
|
||||||
|
|
||||||
|
|
|
@ -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 = {
|
||||||
|
|
|
@ -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);
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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);
|
||||||
});
|
});
|
|
@ -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">⬆️</span>
|
||||||
|
<span class="button down">⬇️</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>
|
||||||
|
|
||||||
<!---------------->
|
<!---------------->
|
||||||
|
|
Loading…
Reference in a new issue