To update the code so that it displays the timeline and stations in green color from departure to destination, we'll need to modify the updateSchedule function to apply different styles depending on whether a station is before the departure, between the departure and destination, or after the destination.
Firstly, we'll update the styles within the <style> tag, adding a new color definition for the selected route portion (green):
/* Add these styles below your existing styles */
.selected-route {
border-left: 4px solid #10b981; /* Green border for the selected route's timeline */
}
.selected-hoi {
border: 4px solid #10b981; /* Green border for the selected route's hoi */
}
.selected-station-time {
background-color: #10b981; /* Green background for the selected route's station time */
}
Now, let's modify the updateSchedule function in the <script> section to apply these styles based on the indexes:
function updateSchedule(scheduleData) {
const scheduleContainer = document.getElementById('scheduleContainer');
scheduleContainer.innerHTML = ''; // Clear previous schedule
const departureIndex = scheduleData.findIndex(station => station.station === departure);
const destinationIndex = scheduleData.findIndex(station => station.station === destination);
scheduleData.forEach((trip, index, array) => {
// Existing code for creating vehicleDiv, etc. ...
trip.stations.forEach((station, stationIndex) => {
let stationDiv = document.createElement('div');
stationDiv.classList.add('station');
// Existing code for creating arrivalTimeDiv, etc. ...
let hoiDiv = document.createElement('div');
hoiDiv.classList.add('hoi');
// Apply green color for stations between departure and destination
if (stationIndex >= departureIndex && stationIndex <= destinationIndex) {
hoiDiv.classList.add('selected-hoi');
}
timeWrapperDiv.appendChild(hoiDiv);
if (stationIndex !== trip.stations.length - 1) {
let timelineDiv = document.createElement('div');
timelineDiv.classList.add('timeline');
// Apply green color for the timeline between departure and destination
if (stationIndex >= departureIndex && stationIndex < destinationIndex) {
timelineDiv.classList.add('selected-route');
}
timeWrapperDiv.appendChild(timelineDiv);
}
stationDiv.appendChild(timeWrapperDiv);
// Existing code for creating stationNameDiv, etc. ...
let departureTimeDiv = document.createElement('div');
departureTimeDiv.classList.add('station-time');
// Apply green color for stations between departure and destination
if (stationIndex >= departureIndex && stationIndex < destinationIndex) {
departureTimeDiv.classList.add('selected-station-time');
}
departureTimeDiv.textContent = station.departureTime;
stationDiv.appendChild(departureTimeDiv);
scheduleContainer.appendChild(stationDiv);
});
});
}
This updated updateSchedule function now checks if a station is between the departure and destination indexes, and if so, it adds the new green color classes to the hoi, timeline, and station-time elements. Make sure to insert the new styles in your existing <style> block, and adjust the script as shown above.
Please note that I have used findIndex to determine the departure and destination indexes. If the scheduleData does not contain the stations as an array or if the array does not contain objects with a station property that matches the departure and destination strings, then findIndex would return -1 and this code would not function as expected. Adjust the index-finding logic accordingly based on your actual data structure.