-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
access pokemon details on creating and updating
- Loading branch information
1 parent
bb8c77e
commit 2c9a117
Showing
8 changed files
with
122 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,98 @@ | ||
export function initializePokemonSelection(slotSelector, pokemonCardSelector, modalSelector, hiddenInputSelector) { | ||
export function initializePokemonSelection(slotSelector, addPokemonBtnSelector, modalSelector, hiddenInputSelector) { | ||
const selectedPokemons = {}; | ||
let currentSlotId = null; | ||
|
||
function updateSlotDisplay(slotId, pokemonName) { | ||
document.getElementById(`slot-${slotId}`).textContent = pokemonName || "Empty"; | ||
function updateSlotDisplay(slotId, pokemonName, pokemonSprite) { | ||
const slotElement = document.getElementById(`slot-${slotId}`); | ||
if (pokemonName) { | ||
slotElement.innerHTML = ` | ||
<div class="card text-center"> | ||
<img class="card-img-top" src="${pokemonSprite}" alt="Sprite of ${pokemonName}" style="object-fit: contain; aspect-ratio: 1/1" height="200px"> | ||
<div class="card-body p-2"> | ||
<h5 class="card-title mb-0">${pokemonName}</h5> | ||
</div> | ||
</div> | ||
`; | ||
} else { | ||
slotElement.innerHTML = ""; | ||
} | ||
} | ||
|
||
function updateHiddenInput() { | ||
document.querySelector(hiddenInputSelector).value = Object.values(selectedPokemons).join(','); | ||
const hiddenInput = document.querySelector(hiddenInputSelector); | ||
hiddenInput.value = Object.values(selectedPokemons).join(','); | ||
} | ||
|
||
|
||
function updateCardVisuals() { | ||
document.querySelectorAll(pokemonCardSelector).forEach(card => { | ||
document.querySelectorAll('.pokemon-card').forEach(card => { | ||
card.classList.remove('selected-green', 'selected-blue'); | ||
const pokemonId = card.querySelector('.add-pokemon-btn').dataset.pokemonId; | ||
const addButton = card.querySelector('.add-pokemon-btn'); | ||
|
||
if (Object.values(selectedPokemons).includes(card.dataset.pokemonId)) { | ||
card.classList.add('selected-blue'); | ||
} | ||
if (selectedPokemons[currentSlotId] === card.dataset.pokemonId) { | ||
card.classList.remove('selected-blue'); | ||
card.classList.add('selected-green'); | ||
if (selectedPokemons[currentSlotId] === pokemonId) { | ||
updateCardIfSelected(card, addButton); | ||
} else if (Object.values(selectedPokemons).includes(pokemonId)) { | ||
updateCardIfSelectedOnAnotherSlot(card, addButton); | ||
} else { | ||
updateCardIfNotSelected(card, addButton); | ||
} | ||
}); | ||
} | ||
|
||
function updateCardIfSelected(card, addButton) { | ||
card.classList.add('selected-green'); | ||
addButton.classList.remove('btn-success'); | ||
addButton.classList.add('btn-danger'); | ||
addButton.innerHTML = '<i class="bi bi-dash"></i>'; | ||
} | ||
|
||
function updateCardIfSelectedOnAnotherSlot(card, addButton) { | ||
card.classList.add('selected-blue'); | ||
addButton.classList.remove('btn-danger'); | ||
addButton.classList.add('btn-success'); | ||
addButton.innerHTML = '<i class="bi bi-plus"></i>'; | ||
} | ||
|
||
function updateCardIfNotSelected(card, addButton) { | ||
addButton.classList.remove('btn-danger'); | ||
addButton.classList.add('btn-success'); | ||
addButton.innerHTML = '<i class="bi bi-plus"></i>'; | ||
} | ||
|
||
function handleSlotClick(event) { | ||
currentSlotId = event.currentTarget.dataset.slotId; | ||
updateCardVisuals(); | ||
$(modalSelector).modal('show'); | ||
} | ||
|
||
function handleCardClick(event) { | ||
const card = event.currentTarget; | ||
const pokemonId = card.dataset.pokemonId; | ||
const pokemonName = card.dataset.pokemonName; | ||
function handleAddButtonClick(event) { | ||
const button = event.currentTarget; | ||
const pokemonId = button.dataset.pokemonId; | ||
const pokemonName = button.dataset.pokemonName; | ||
const pokemonSprite = button.dataset.pokemonSprite; | ||
|
||
if (selectedPokemons[currentSlotId] === pokemonId) { | ||
delete selectedPokemons[currentSlotId]; | ||
updateSlotDisplay(currentSlotId); | ||
} else if (!Object.values(selectedPokemons).includes(pokemonId)) { | ||
selectedPokemons[currentSlotId] = pokemonId; | ||
updateSlotDisplay(currentSlotId, pokemonName); | ||
updateSlotDisplay(currentSlotId, pokemonName, pokemonSprite); | ||
} else { | ||
alert("This Pokemon is already selected in another slot."); | ||
return; | ||
} | ||
|
||
updateHiddenInput(); | ||
updateCardVisuals(); | ||
$(modalSelector).modal('hide'); | ||
} | ||
|
||
|
||
document.querySelectorAll(slotSelector).forEach(slot => { | ||
slot.addEventListener('click', handleSlotClick); | ||
}); | ||
|
||
document.querySelectorAll(pokemonCardSelector).forEach(card => { | ||
card.addEventListener('click', handleCardClick); | ||
document.querySelectorAll(addPokemonBtnSelector).forEach(button => { | ||
button.addEventListener('click', handleAddButtonClick); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters