Recount-Server/web/static/new_transactions.js
2024-02-27 18:38:00 -08:00

95 lines
2.9 KiB
JavaScript

import {
computePosition,
offset,
} from 'https://cdn.jsdelivr.net/npm/@floating-ui/dom@1.5.4/+esm';
const centerOffset = offset(({rects}) => {
return -rects.reference.height / 2 - rects.floating.height / 2;
});
function open_new_transaction_pane() {
let center = document.querySelector("#main-body-content");
let new_tp = document.querySelector("#new-transaction-pane");
new_tp.style.opacity = "1";
let center_width = center.offsetWidth;
center_width = center_width - (center_width / 10);
new_tp.style.width = center_width + "px";
computePosition(center, new_tp, {
placement: "bottom",
middleware: [
centerOffset,
],
}).then(({x, y}) => {
Object.assign(new_tp.style, {
left: `${x}px`,
top: `${y}px`,
});
});
}
function close_new_transaction_pane() {
let right_column = document.querySelector("#right-col");
let new_tp = document.querySelector("#new-transaction-pane");
new_tp.style.opacity = "0";
computePosition(right_column, new_tp, {
placement: "right",
middleware: [
offset(40),
],
}).then(({x, y}) => {
Object.assign(new_tp.style, {
left: `${x}px`,
top: `${y}px`,
});
});
}
/**
* @param {Event} event
*/
function on_input(event) {
if (event.target.id === "bottom-most-breakdown-input"
|| document.querySelectorAll("#breakdown-tbody input[type='number']").length === 0
) {
add_new_breakdown();
}
let breakdown_inputs = Array.from(document
.querySelectorAll("#breakdown-tbody input[type='number']:not(#bottom-most-breakdown-input)"
));
let breakdown_sum = breakdown_inputs.reduce(function(total, el) {
return total + parseFloat(el.value || 0);
}, 0);
let total_input = document.getElementById("amount-entry");
if (total_input.value) {
let value_total = parseFloat(total_input.value);
document.getElementById("bottom-most-breakdown-input").value = (value_total - breakdown_sum).toFixed(2);
} else {
total_input.placeholder = breakdown_sum;
}
}
function add_new_breakdown() {
let input_latest_old = document.getElementById("bottom-most-breakdown-input");
if (input_latest_old) {
input_latest_old.id = "";
}
let source = document.importNode(document.getElementById("breakdown-template-row").content, true);
let input_latest = source.querySelector("input[type='number']");
input_latest.addEventListener("input", on_input);
input_latest.id = "bottom-most-breakdown-input";
document.getElementById("breakdown-tbody").appendChild(source);
}
function register_breakdown_handlers() {
document.getElementById("amount-entry").addEventListener("input", on_input);
}
export {open_new_transaction_pane, close_new_transaction_pane, add_new_breakdown, register_breakdown_handlers}