I have been working on two main themes of logic. But, I have been unable to implement a more sane solution, due to my incapability to defer the evaluation of some parts of the code.
This is one of the bits. This logic repeats itself for three different layouts. It’s a “click me and change border” functionality.
function toggleUp(){
buyImagesDrink[0].classList.remove("buyDrink");
showDrink[0].classList.remove("showDrink");
}
function toggleDown(){
this.classList.add("buyDrink");
this.getElementsByClassName("animation-hidden")[0].classList.add("showDrink");
}
function toggleThumbnail(){
if (buyImagesDrink.length > 0){
toggleUp();
}
toggleDown();
}
for (var j=0; j < selectedDrink.length; j++){
selectedDrink[j].addEventListener("click", toggleThumbnail());
};
Now, there is this other piece, which get’s activated (footer), when these three thumbails are active.
let combinedVar = buyImages.length + buyImagesDrink.length + buyImagesDessert.length;
// foot style changer function.
function styleChange() {
if (combinedVar === 3){
footerStyle[0].classList.add("order");
footerStyleSpan.innerHTML = "Fechar pedido";
}
};
//add event listener in all elements, so to make style change be independent of order of layout selection.
for (var m=0; m < selectedDessert.length; m++){
selectedDessert[m].addEventListener("click", styleChange());
selected[m].addEventListener("click", styleChange());
selectedDrink[m].addEventListener("click", styleChange());
};
Demo running: https://jsfiddle.net/BuddhiLW/q3mwe9ky/13/