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

167 lines
5.3 KiB
JavaScript
Raw Permalink Normal View History

2021-11-23 06:23:57 +00:00
import * as Data from "./data.js";
2022-03-07 19:27:10 +00:00
export async function populateSports(sportDropdown, selectedSportID = undefined, data = undefined) {
2021-11-23 06:23:57 +00:00
sportDropdown.innerHTML = "";
2022-03-07 19:27:10 +00:00
let sportsList;
if(data) {
sportsList = data.sports;
} else {
sportsList = await Data.getSports();
}
2021-11-23 06:23:57 +00:00
let currentIndex = 0;
let selectedSportIndex;
sportsList.forEach(sport => {
const option = document.createElement('option');
option.text = sport.name;
option.value = sport.id;
sportDropdown.appendChild(option);
2022-03-07 19:27:10 +00:00
if(sport.id == selectedSportID || (data && sport.id == data.latestGame.sportID)) selectedSportIndex = currentIndex;
2021-11-23 06:23:57 +00:00
currentIndex++;
});
if(selectedSportIndex) sportDropdown.selectedIndex = selectedSportIndex;
}
2022-03-07 19:21:17 +00:00
export async function populateSeasons(seasonDropdown, selectedSeasonID = undefined, data = undefined) {
2021-11-23 07:49:11 +00:00
seasonDropdown.innerHTML = "";
2022-03-07 19:21:17 +00:00
let seasonsList;
if(data) {
seasonsList = data.seasons;
} else {
seasonsList = await Data.getSeasons();
}
2022-03-07 19:27:10 +00:00
2021-11-23 07:49:11 +00:00
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);
2022-03-07 19:21:17 +00:00
if(season.id == selectedSeasonID || (data && season.id == data.latestGame.seasonID)) selectedSeasonIndex = currentIndex;
2021-11-23 07:49:11 +00:00
currentIndex++;
});
if(selectedSeasonIndex) seasonDropdown.selectedIndex = selectedSeasonIndex;
}
export async function populateGenders(genderDropdown, selectedSportID, selectedGender = undefined, data = undefined) {
2021-11-23 07:49:11 +00:00
genderDropdown.innerHTML = "";
let gendersList;
if(data) {
gendersList = data.genders;
selectedSportID = data.latestGame.sportID;
} else {
gendersList = await Data.getGenders(selectedSportID);
}
2021-11-23 07:49:11 +00:00
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 || (data && gender.name == data.latestGame.gender.name)) selectedGenderIndex = currentIndex;
2021-11-23 07:49:11 +00:00
currentIndex++;
});
if(selectedGenderIndex) genderDropdown.selectedIndex = selectedGenderIndex;
}
}
export async function populateDivisions (divisionDropdown, selectedSportID, selectedGender, selectedDivisionID = undefined, data = undefined) {
2021-11-23 07:49:11 +00:00
divisionDropdown.innerHTML = "";
if(data) {
selectedSportID = data.latestGame.sportID;
selectedGender = data.latestGame.gender;
}
2021-11-23 07:49:11 +00:00
if(selectedSportID && selectedGender) {
let divisionsList;
if(data) {
divisionsList = data.divisions;
} else {
divisionsList = await Data.getDivisions(selectedSportID, selectedGender);
}
2021-11-23 07:49:11 +00:00
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 || (data && division.id == data.latestGame.divisionID)) selectedDivisionIndex = currentIndex;
2021-11-23 07:49:11 +00:00
currentIndex++;
});
if(selectedDivisionIndex) divisionDropdown.selectedIndex = selectedDivisionIndex;
}
}
2022-03-08 17:55:23 +00:00
export async function populateTeams(teamDropdown, selectedSportID, selectedTeamID = undefined, data = undefined, useOpponent = false) {
2021-11-23 07:49:11 +00:00
teamDropdown.innerHTML = "";
if(data) {
selectedSportID = data.latestGame.sportID;
}
2021-11-23 07:49:11 +00:00
if(selectedSportID) {
let teamsList;
if(data) {
teamsList = data.teams;
} else {
teamsList = await Data.getTeams(selectedSportID);
}
2021-11-23 07:49:11 +00:00
let currentIndex = 0;
let selectedTeamIndex;
2022-03-08 17:55:23 +00:00
if(data) {
selectedTeamID = useOpponent ? data.latestGame.team2ID : data.latestGame.team1ID;
}
2021-11-23 07:49:11 +00:00
teamsList.forEach(team => {
const option = document.createElement('option');
option.text = team.name;
option.value = team.id;
teamDropdown.appendChild(option);
2022-03-08 17:55:23 +00:00
if(team.id == selectedTeamID) selectedTeamIndex = currentIndex;
2021-11-23 07:49:11 +00:00
currentIndex++;
});
if(selectedTeamIndex) teamDropdown.selectedIndex = selectedTeamIndex;
}
}
2021-11-23 06:23:57 +00:00
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();
}
});
}