2024-02-09 21:06:09 -08:00
|
|
|
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");
|
2024-02-10 10:52:09 -08:00
|
|
|
let new_tp = document.querySelector("#new-transaction-pane");
|
2024-02-09 21:06:09 -08:00
|
|
|
|
|
|
|
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");
|
2024-02-10 10:52:09 -08:00
|
|
|
let new_tp = document.querySelector("#new-transaction-pane");
|
2024-02-09 21:06:09 -08:00
|
|
|
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`,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-02-27 18:38:00 -08:00
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-27 17:35:33 -08:00
|
|
|
function add_new_breakdown() {
|
2024-02-27 18:38:00 -08:00
|
|
|
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";
|
2024-02-27 17:35:33 -08:00
|
|
|
document.getElementById("breakdown-tbody").appendChild(source);
|
|
|
|
}
|
|
|
|
|
2024-02-27 18:38:00 -08:00
|
|
|
function register_breakdown_handlers() {
|
|
|
|
document.getElementById("amount-entry").addEventListener("input", on_input);
|
2024-02-27 17:35:33 -08:00
|
|
|
}
|
|
|
|
|
2024-02-27 18:38:00 -08:00
|
|
|
export {open_new_transaction_pane, close_new_transaction_pane, add_new_breakdown, register_breakdown_handlers}
|