Dove vuoi
andare?
CERCA
→
3 VIAGGI
6 DESTINAZIONI
(function () {
var continent = document.getElementById('nw-continent');
var destination = document.getElementById('nw-destination');
var month = document.getElementById('nw-month');
var searchButton = document.getElementById('nw-search-button');
var cards = document.querySelectorAll('.nw-trip-card');
var availableCount = document.getElementById('nw-available-count');
var comingCount = document.getElementById('nw-coming-count');
/* ===================================================== DESTINAZIONI DINAMICHE ===================================================== */
var allDestinationOptions = Array.prototype.slice.call( destination.querySelectorAll('option') );
continent.addEventListener('change', function () {
var selectedContinent = continent.value;
destination.innerHTML = '';
var firstOption = document.createElement('option');
firstOption.value = 'all'; firstOption.textContent = 'Tutte le destinazioni';
destination.appendChild(firstOption);
allDestinationOptions.forEach(function (option) {
if (option.value === 'all') { return; }
var optionContinent = option.getAttribute('data-continent');
if ( selectedContinent === 'all' || optionContinent === selectedContinent ) {
var newOption = option.cloneNode(true);
destination.appendChild(newOption);
}
});
});
/* ===================================================== FILTRO ===================================================== */
function filterTrips() {
var selectedContinent = continent.value;
var selectedDestination = destination.value;
var selectedMonth = month.value;
var availableVisible = 0; var comingVisible = 0;
cards.forEach(function (card) {
var cardContinent = card.getAttribute('data-continent');
var cardDestination = card.getAttribute('data-destination');
var cardMonth = card.getAttribute('data-month');
var cardStatus = card.getAttribute('data-status');
var continentMatch = selectedContinent === 'all' || cardContinent === selectedContinent;
var destinationMatch = selectedDestination === 'all' || cardDestination === selectedDestination;
var monthMatch = selectedMonth === 'all' || cardMonth === selectedMonth || cardMonth === 'all';
var visible = continentMatch && destinationMatch && monthMatch;
if (visible) {
card.classList.remove('nw-hidden');
if (cardStatus === 'available') { availableVisible++; }
if (cardStatus === 'coming') { comingVisible++; }
} else {
card.classList.add('nw-hidden');
}
});
availableCount.textContent = availableVisible + (availableVisible === 1 ? ' VIAGGIO' : ' VIAGGI');
comingCount.textContent = comingVisible + (comingVisible === 1 ? ' DESTINAZIONE' : ' DESTINAZIONI');
}
searchButton.addEventListener( 'click', filterTrips );
})();