dwm-app/static/js/index.js

75 lines
2.5 KiB
JavaScript
Raw Normal View History

2024-01-30 16:06:00 +00:00
document.addEventListener("DOMContentLoaded", function () {
const familyDropdown = document.getElementById("familyDropdown");
const monsterDropdown = document.getElementById("monsterDropdown");
2024-01-31 01:17:15 +00:00
const monsterIframe = document.getElementById("monsterIframe");
const breedingIframe = document.getElementById("breedingIframe");
2024-01-30 16:06:00 +00:00
2024-01-31 01:17:15 +00:00
// Initialize dropdowns and iframes
2024-01-30 16:06:00 +00:00
updateMonstersDropdown();
2024-01-31 01:17:15 +00:00
updateIframes();
2024-01-30 16:06:00 +00:00
// Fetch families data from the server
fetch("/get_families")
.then(response => response.json())
.then(data => {
populateDropdown(familyDropdown, data);
updateMonstersDropdown();
2024-01-31 01:17:15 +00:00
updateIframes();
2024-01-30 16:06:00 +00:00
})
.catch(error => console.error("Error fetching families:", error));
familyDropdown.addEventListener("change", function () {
updateMonstersDropdown();
2024-01-31 01:17:15 +00:00
updateIframes();
2024-01-30 16:06:00 +00:00
});
monsterDropdown.addEventListener("change", function () {
2024-01-31 01:17:15 +00:00
updateIframes();
2024-01-30 16:06:00 +00:00
});
function populateDropdown(dropdown, data) {
// Clear existing options
dropdown.innerHTML = '<option value="">All</option>';
// Populate dropdown options
data.forEach(item => {
const option = document.createElement("option");
option.value = item;
option.text = item;
dropdown.appendChild(option);
});
}
function updateMonstersDropdown() {
const selectedFamily = familyDropdown.value;
// Fetch monsters data from the server based on the selected family
fetch(`/get_monsters?family=${selectedFamily}`)
.then(response => response.json())
.then(data => populateDropdown(monsterDropdown, data))
.catch(error => console.error("Error fetching monsters:", error));
}
2024-01-31 01:17:15 +00:00
function updateIframes() {
2024-01-30 16:06:00 +00:00
const selectedFamily = familyDropdown.value;
const selectedMonster = monsterDropdown.value;
2024-01-31 01:17:15 +00:00
// Update monsterIframe src based on selected family and monster
const monsterIframeSrc = selectedMonster
2024-01-30 16:06:00 +00:00
? `/monster/${selectedMonster}`
: selectedFamily
? `/monster/${selectedFamily}`
: "about:blank";
2024-01-31 01:17:15 +00:00
monsterIframe.src = monsterIframeSrc;
// Update breedingIframe src based on the selected monster
const breedingIframeSrc = selectedMonster
? `/get_breeding_combinations?monster=${selectedMonster}`
: "about:blank";
breedingIframe.src = breedingIframeSrc;
2024-01-30 16:06:00 +00:00
}
2024-01-31 02:27:02 +00:00
2024-01-30 16:06:00 +00:00
});