This repository has been archived on 2024-04-05. You can view files and clone it, but cannot push or open issues/pull-requests.
score-tracker/public/scripts/form.js

126 lines
4.2 KiB
JavaScript

import * as Data from "./data.js";
export async function populateSports(sportDropdown, selectedSportID = undefined) {
sportDropdown.innerHTML = "";
const sportsList = await Data.getSports();
let currentIndex = 0;
let selectedSportIndex;
sportsList.forEach(sport => {
const option = document.createElement('option');
option.text = sport.name;
option.value = sport.id;
sportDropdown.appendChild(option);
if(sport.id == selectedSportID) selectedSportIndex = currentIndex;
currentIndex++;
});
if(selectedSportIndex) sportDropdown.selectedIndex = selectedSportIndex;
}
export async function populateSeasons(seasonDropdown, selectedSeasonID = undefined) {
seasonDropdown.innerHTML = "";
const seasonsList = await Data.getSeasons();
let currentIndex = 0;
let selectedSeasonIndex;
seasonsList.forEach(season => {
const option = document.createElement('option');
option.text = (season.year - 1) + "-" + season.year;
option.value = season.id;
seasonDropdown.appendChild(option);
if(season.id == selectedSeasonID) selectedSeasonIndex = currentIndex;
currentIndex++;
});
if(selectedSeasonIndex) seasonDropdown.selectedIndex = selectedSeasonIndex;
}
export async function populateGenders(genderDropdown, selectedSportID, selectedGender = undefined) {
genderDropdown.innerHTML = "";
const gendersList = await Data.getGenders(selectedSportID);
if(selectedSportID) {
let currentIndex = 0;
let selectedGenderIndex;
gendersList.forEach(gender => {
const option = document.createElement('option');
option.text = (gender.name == "female") ? "Female" : (gender.name == "male") ? "Male" : "";
option.value = gender.name;
genderDropdown.appendChild(option);
if(gender.name == selectedGender) selectedGenderIndex = currentIndex;
currentIndex++;
});
if(selectedGenderIndex) genderDropdown.selectedIndex = selectedGenderIndex;
}
}
export async function populateDivisions (divisionDropdown, selectedSportID, selectedGender, selectedDivisionID = undefined) {
divisionDropdown.innerHTML = "";
if(selectedSportID && selectedGender) {
const divisionsList = await Data.getDivisions(selectedSportID, selectedGender);
let currentIndex = 0;
let selectedDivisionIndex;
divisionsList.forEach(division => {
const option = document.createElement('option');
option.text = division.name;
option.value = division.id;
divisionDropdown.appendChild(option);
if(division.id == selectedDivisionID) selectedDivisionIndex = currentIndex;
currentIndex++;
});
if(selectedDivisionIndex) divisionDropdown.selectedIndex = selectedDivisionIndex;
}
}
export async function populateTeams(teamDropdown, selectedSportID, selectedTeamID = undefined) {
teamDropdown.innerHTML = "";
if(selectedSportID) {
const teamsList = await Data.getTeams(selectedSportID);
let currentIndex = 0;
let selectedTeamIndex;
teamsList.forEach(team => {
const option = document.createElement('option');
option.text = team.name;
option.value = team.id;
teamDropdown.appendChild(option);
if(team.id == selectedTeamID) selectedTeamIndex = currentIndex;
currentIndex++;
});
if(selectedTeamIndex) teamDropdown.selectedIndex = selectedTeamIndex;
}
}
export async function addHiddenValue(name, value, form) {
const valueInput = document.createElement('input');
valueInput.setAttribute('name', name);
valueInput.setAttribute('value', value);
valueInput.setAttribute('type', 'hidden');
form.appendChild(valueInput);
}
export async function addRemoveFunction(removeButton, form, objectTitle) {
removeButton.addEventListener('click', async () => {
const verified = confirm(`This ${objectTitle} will be removed.`);
if(verified) {
await addHiddenValue('remove', 1, form);
form.submit();
}
});
}