To modify the code to display the timeline and station with a green color from departure to the destination, we'll add some logic to the updateSchedule function to check the station against departure and destination and to apply the appropriate colors. We'll also introduce a new CSS class for the green color:
Here's the updated JavaScript part (right inside the <script> tag):
// ... [existing code up to updateSchedule function]
function updateSchedule(scheduleData) {
const departure = document.getElementById('departure').value.toUpperCase();
const destination = document.getElementById('destination').value.toUpperCase();
const scheduleContainer = document.getElementById('scheduleContainer');
scheduleContainer.innerHTML = ''; // Clear previous schedule
scheduleData.forEach((trip, index, array) => {
let vehicleDiv = document.createElement('div');
vehicleDiv.textContent = `Vehicle Number: ${trip.vehicle_number}, Trip: ${trip.trip}`;
scheduleContainer.appendChild(vehicleDiv);
let hasDeparted = false;
let hasArrived = false;
trip.stations.forEach((station, stationIndex) => {
let stationDiv = document.createElement('div');
stationDiv.classList.add('station');
if(station.station.toUpperCase() === departure){
hasDeparted = true;
}
let arrivalTimeDiv = document.createElement('div');
arrivalTimeDiv.classList.add('station-time', hasDeparted && !hasArrived ? 'green-bg' : '');
arrivalTimeDiv.textContent = station.arrivalTime;
stationDiv.appendChild(arrivalTimeDiv);
// Create time-wrapper and its children elements: hoi and timeline
let timeWrapperDiv = document.createElement('div');
timeWrapperDiv.classList.add('time-wrapper');
let hoiDiv = document.createElement('div');
hoiDiv.classList.add('hoi', hasDeparted && !hasArrived ? 'green-border' : '');
timeWrapperDiv.appendChild(hoiDiv);
// Only add a timeline if it is not the last station of the current trip
if (stationIndex !== trip.stations.length - 1) {
let timelineDiv = document.createElement('div');
timelineDiv.classList.add('timeline', hasDeparted && !hasArrived ? 'green-line' : '');
timeWrapperDiv.appendChild(timelineDiv);
}
stationDiv.appendChild(timeWrapperDiv);
let stationNameDiv = document.createElement('div');
stationNameDiv.classList.add('station-name');
stationNameDiv.textContent = station.station;
stationDiv.appendChild(stationNameDiv);
let departureTimeDiv = document.createElement('div');
departureTimeDiv.classList.add('station-time', hasDeparted && !hasArrived ? 'green-bg' : '');
departureTimeDiv.textContent = station.departureTime;
stationDiv.appendChild(departureTimeDiv);
scheduleContainer.appendChild(stationDiv);
if(station.station.toUpperCase() === destination) {
hasArrived = true;
}
});
});
}
And here's the updated CSS to include the new green classes (inside the <style> tag):
/* ... [existing CSS] ... */
/* Define green classes for highlighting */
.green-bg {
background-color: #10B981; /* green-500 from Tailwind Color Palette */
}
.green-border {
border-color: #10B981; /* green-500 from Tailwind Color Palette */
}
.green-line {
border-left-color: #10B981; /* green-500 from Tailwind Color Palette */
}
Note that these code snippets assume that the scheduleData JSON response is an array of trip objects, and each trip object contains an array of stations. Each station object should have station, arrivalTime, and departureTime attributes. Additionally, I've added toUpperCase() to ensure case-insensitive comparison between the station names and the input values for departure and destination.
Make sure to replace the dummy API URL with an actual one, and adjust the processing logic according to the structure of the response from your backend or data source.