Cheapest Flights Price Tracker

Cheapest Flights Price Tracker

My Tracked Flights

Please fill in all search fields.

'; return; } const results = allFlightsData.filter(flight => flight.origin === origin && flight.destination === destination && flight.date === departDate ); renderFlightResults(results); } /** * Adds or removes a flight from the tracking list. * @param {number} flightId - The ID of the flight to toggle. */ window.toggleTrackFlight = function(flightId) { const trackButton = document.getElementById(`track-btn-${flightId}`); if (trackedFlightIds.has(flightId)) { trackedFlightIds.delete(flightId); if (trackButton) { trackButton.disabled = false; trackButton.textContent = 'Track this Flight'; } } else { trackedFlightIds.add(flightId); if (trackButton) { trackButton.disabled = true; trackButton.textContent = 'Tracked'; } } renderTrackedFlights(); } /** * Handles tab switching logic. */ window.openTab = function(evt, tabName) { const tabContents = document.getElementsByClassName("tab-content"); Array.from(tabContents).forEach(tab => tab.style.display = "none"); const tabButtons = document.getElementsByClassName("tab-btn"); Array.from(tabButtons).forEach(btn => btn.classList.remove("active")); const tabToShow = document.getElementById(tabName); if (tabToShow) tabToShow.style.display = "block"; if (evt) { evt.currentTarget.classList.add("active"); } else { const btnToActivate = Array.from(tabButtons).find(btn => btn.getAttribute('onclick').includes(`'${tabName}'`)); if (btnToActivate) btnToActivate.classList.add("active"); } updateNavButtons(); } /** * Handles Next/Previous button clicks. */ window.navigateTabs = function(direction) { const tabs = Array.from(document.querySelectorAll('.tab-btn')); const activeTabIndex = tabs.findIndex(tab => tab.classList.contains('active')); let newIndex = (direction === 'next') ? (activeTabIndex + 1) % tabs.length : (activeTabIndex - 1 + tabs.length) % tabs.length; tabs[newIndex].click(); } /** * Updates the visibility of Next/Previous buttons. */ function updateNavButtons() { const tabs = Array.from(document.querySelectorAll('.tab-btn')); const activeTabIndex = tabs.findIndex(tab => tab.classList.contains('active')); const prevBtn = document.getElementById('prev-btn'); const nextBtn = document.getElementById('next-btn'); if (!prevBtn || !nextBtn) return; prevBtn.style.visibility = activeTabIndex === 0 ? 'hidden' : 'visible'; nextBtn.style.visibility = activeTabIndex === tabs.length - 1 ? 'hidden' : 'visible'; } /** * PDF Download Functionality. */ if(downloadPdfBtn) { downloadPdfBtn.addEventListener('click', function() { const { jsPDF } = window.jspdf; const contentToDownload = document.getElementById('tracked-flights-to-download'); if (!contentToDownload || trackedFlightIds.size === 0) { console.warn("No tracked flights to download."); return; } // Clone the node to remove buttons before printing const contentClone = contentToDownload.cloneNode(true); const actionButtons = contentClone.querySelectorAll('button'); actionButtons.forEach(btn => btn.remove()); // Also remove the 'Action' header if it exists const actionHeader = contentClone.querySelector('th:last-child'); if(actionHeader && actionHeader.textContent === 'Action') actionHeader.remove(); const actionCells = contentClone.querySelectorAll('td:last-child'); actionCells.forEach(cell => cell.remove()); // Temporarily append clone to body to render it, but make it invisible contentClone.style.position = 'absolute'; contentClone.style.left = '-9999px'; document.body.appendChild(contentClone); html2canvas(contentClone, { scale: 2, useCORS: true }) .then(canvas => { document.body.removeChild(contentClone); // Clean up the DOM const imgData = canvas.toDataURL('image/png'); const pdf = new jsPDF({ orientation: 'p', unit: 'mm', format: 'a4' }); const pdfWidth = pdf.internal.pageSize.getWidth(); const pdfHeight = pdf.internal.pageSize.getHeight(); const imgProps = pdf.getImageProperties(imgData); const imgWidth = pdfWidth - 20; // with margin const imgHeight = (imgProps.height * imgWidth) / imgProps.width; pdf.addImage(imgData, 'PNG', 10, 10, imgWidth, imgHeight); pdf.save('Tracked-Flights.pdf'); }).catch(err => { document.body.removeChild(contentClone); // Clean up on error console.error("Error generating PDF:", err); }); }); } // --- INITIALIZATION --- function initializeTool() { renderTrackedFlights(); updateNavButtons(); // Set a default date for the date picker const today = new Date().toISOString().split('T')[0]; const departDateInput = document.getElementById('depart-date'); if(departDateInput) departDateInput.value = "2025-10-20"; } initializeTool(); });
Scroll to Top