timetracker/games/static/js/add_purchase.js

48 lines
1.2 KiB
JavaScript
Raw Normal View History

import {
syncSelectInputUntilChanged,
getEl,
disableElementsWhenTrue,
2023-11-20 20:20:10 +00:00
disableElementsWhenValueNotEqual,
} from "./utils.js";
let syncData = [
{
2023-11-14 18:27:00 +00:00
source: "#id_edition",
source_value: "dataset.platform",
target: "#id_platform",
target_value: "value",
},
];
syncSelectInputUntilChanged(syncData, "form");
function setupElementHandlers() {
disableElementsWhenTrue("#id_type", "game", [
"#id_name",
"#id_related_purchase",
]);
2023-11-20 20:20:10 +00:00
disableElementsWhenValueNotEqual(
"#id_type",
["game", "dlc"],
["#id_date_finished"]
);
}
2023-11-14 18:27:00 +00:00
document.addEventListener("DOMContentLoaded", setupElementHandlers);
2023-11-16 18:03:16 +00:00
document.addEventListener("htmx:afterSwap", setupElementHandlers);
2023-11-14 18:27:00 +00:00
getEl("#id_type").onchange = () => {
setupElementHandlers();
};
2023-11-20 20:15:18 +00:00
document.body.addEventListener("htmx:beforeRequest", function (event) {
// Assuming 'Purchase1' is the element that triggers the HTMX request
2023-11-20 20:15:18 +00:00
if (event.target.id === "id_edition") {
var idEditionValue = document.getElementById("id_edition").value;
2023-11-20 20:15:18 +00:00
// Condition to check - replace this with your actual logic
if (idEditionValue != "") {
event.preventDefault(); // This cancels the HTMX request
}
}
});