167 lines
5.3 KiB
JavaScript
167 lines
5.3 KiB
JavaScript
import * as Data from "./data.js";
|
|
|
|
export async function populateSports(sportDropdown, selectedSportID = undefined, data = undefined) {
|
|
sportDropdown.innerHTML = "";
|
|
|
|
let sportsList;
|
|
if(data) {
|
|
sportsList = data.sports;
|
|
} else {
|
|
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 || (data && sport.id == data.latestGame.sportID)) selectedSportIndex = currentIndex;
|
|
currentIndex++;
|
|
});
|
|
|
|
if(selectedSportIndex) sportDropdown.selectedIndex = selectedSportIndex;
|
|
}
|
|
|
|
export async function populateSeasons(seasonDropdown, selectedSeasonID = undefined, data = undefined) {
|
|
seasonDropdown.innerHTML = "";
|
|
|
|
let seasonsList;
|
|
|
|
if(data) {
|
|
seasonsList = data.seasons;
|
|
} else {
|
|
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 || (data && season.id == data.latestGame.seasonID)) selectedSeasonIndex = currentIndex;
|
|
currentIndex++;
|
|
});
|
|
|
|
if(selectedSeasonIndex) seasonDropdown.selectedIndex = selectedSeasonIndex;
|
|
}
|
|
|
|
export async function populateGenders(genderDropdown, selectedSportID, selectedGender = undefined, data = undefined) {
|
|
genderDropdown.innerHTML = "";
|
|
|
|
let gendersList;
|
|
if(data) {
|
|
gendersList = data.genders;
|
|
selectedSportID = data.latestGame.sportID;
|
|
} else {
|
|
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 || (data && gender.name == data.latestGame.gender.name)) selectedGenderIndex = currentIndex;
|
|
currentIndex++;
|
|
});
|
|
|
|
if(selectedGenderIndex) genderDropdown.selectedIndex = selectedGenderIndex;
|
|
}
|
|
}
|
|
|
|
export async function populateDivisions (divisionDropdown, selectedSportID, selectedGender, selectedDivisionID = undefined, data = undefined) {
|
|
divisionDropdown.innerHTML = "";
|
|
|
|
if(data) {
|
|
selectedSportID = data.latestGame.sportID;
|
|
selectedGender = data.latestGame.gender;
|
|
}
|
|
if(selectedSportID && selectedGender) {
|
|
let divisionsList;
|
|
|
|
if(data) {
|
|
divisionsList = data.divisions;
|
|
} else {
|
|
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 || (data && division.id == data.latestGame.divisionID)) selectedDivisionIndex = currentIndex;
|
|
currentIndex++;
|
|
});
|
|
|
|
if(selectedDivisionIndex) divisionDropdown.selectedIndex = selectedDivisionIndex;
|
|
}
|
|
}
|
|
|
|
export async function populateTeams(teamDropdown, selectedSportID, selectedTeamID = undefined, data = undefined, useOpponent = false) {
|
|
teamDropdown.innerHTML = "";
|
|
|
|
if(data) {
|
|
selectedSportID = data.latestGame.sportID;
|
|
}
|
|
|
|
if(selectedSportID) {
|
|
let teamsList;
|
|
if(data) {
|
|
teamsList = data.teams;
|
|
} else {
|
|
teamsList = await Data.getTeams(selectedSportID);
|
|
}
|
|
|
|
let currentIndex = 0;
|
|
let selectedTeamIndex;
|
|
|
|
if(data) {
|
|
selectedTeamID = useOpponent ? data.latestGame.team2ID : data.latestGame.team1ID;
|
|
}
|
|
|
|
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();
|
|
}
|
|
});
|
|
} |