Files
traveldrafter/web/drafting-board.js

184 lines
5.1 KiB
JavaScript

import { EL } from "./dom.js";
import { displayTripDetails } from "./trip-details.js";
import { fetchTrip } from './api.js';
import { DataStore } from './datastore.js';
let element_board = document.querySelector("#drafting-board-content");
export function addJourneyToDraftingBoard(journey) {
element_board.innerText = "";
for (let leg of journey.legs.filter(item => !("walking" in item))) {
DataStore.tracked_trips.remember(leg.tripId, leg.origin.id, leg.destination.id);
fetchTrip(leg.tripId).then(result => {
DataStore.trips.remember(result.trip);
});
}
trackedTripsLocationsSorted();
let station_offset = 0;
let trip_offset = 1;
let first_station = true;
for (let leg of journey.legs.filter(item => !("walking" in item))) {
if (first_station) {
let el_origin_name = EL("div", {
class: [ "station-name" ],
style: {
"grid-column-start": 1,
"grid-column-end": 4,
"grid-row-start": 1,
"grid-row-end": 1,
},
});
el_origin_name.innerText = leg.origin.name;
element_board.appendChild(el_origin_name);
let el_origin_column = EL("div", {
class: [ "station-column" ],
style: {
"grid-column-start": 2,
"grid-column-end": 3,
"grid-row-start": 2,
"grid-row-end": journey.legs.length + 2,
},
});
element_board.appendChild(el_origin_column);
first_station = false;
}
let el_destination_name = EL("div", {
class: [ "station-name" ],
style: {
"grid-column-start": (station_offset * 3) + 4,
"grid-column-end": (station_offset * 3) + 7,
"grid-row-start": 1,
"grid-row-end": 1,
},
});
el_destination_name.innerText = leg.destination.name;
element_board.appendChild(el_destination_name);
let el_destination_column = EL("div", {
class: [ "station-column" ],
style: {
"grid-column-start": (station_offset * 3) + 5,
"grid-column-end": (station_offset * 3) + 6,
"grid-row-start": 2,
"grid-row-end": journey.legs.length + 2,
},
});
element_board.appendChild(el_destination_column);
let el_trip_left = EL("div", {
class: [ "trip-left" ],
style: {
"grid-column-start": (station_offset * 3) + 1,
"grid-column-end": (station_offset * 3) + 2,
"grid-row-start": (trip_offset) + 1,
"grid-row-end": (trip_offset) + 1,
},
});
el_trip_left.appendChild(document.createTextNode(new Date(leg.departure).toLocaleTimeString()));
el_trip_left.appendChild(EL("br", {}));
el_trip_left.appendChild(document.createTextNode("Gleis " + leg.departurePlatform));
element_board.appendChild(el_trip_left);
let el_trip = EL("div", {
class: [ "trip" ],
style: {
"grid-column-start": (station_offset * 3) + 3,
"grid-column-end": (station_offset * 3) + 5,
"grid-row-start": (trip_offset) + 1,
"grid-row-end": (trip_offset) + 1,
},
});
el_trip.innerText = leg?.line?.name;
el_trip.addEventListener("click", event => {
console.log(leg.tripId);
displayTripDetails(leg.tripId);
});
element_board.appendChild(el_trip);
let el_trip_right = EL("div", {
class: [ "trip-right" ],
style: {
"grid-column-start": (station_offset * 3) + 6,
"grid-column-end": (station_offset * 3) + 7,
"grid-row-start": (trip_offset) + 1,
"grid-row-end": (trip_offset) + 1,
},
});
el_trip_right.appendChild(document.createTextNode(new Date(leg.arrival).toLocaleTimeString()));
el_trip_right.appendChild(EL("br", {}));
el_trip_right.appendChild(document.createTextNode("Gleis " + leg.arrivalPlatform));
element_board.appendChild(el_trip_right);
station_offset += 1;
trip_offset += 1;
}
}
export function getLocationWithLeastInboundTrips(locations, trips) {
function numberOfInboundTrips(location) {
return trips.filter(trip => trip.destination == location).length;
}
let locations_sorted_by_number_of_inbound_trips = locations.toSorted((location_a, location_b) => {
let n_a = numberOfInboundTrips(location_a);
let n_b = numberOfInboundTrips(location_b);
if (n_a == n_b) {
return 0;
} else if (n_a > n_b) {
return 1;
} else {
return -1;
}
});
return locations_sorted_by_number_of_inbound_trips[0];
}
export function sortLocations(sorted_locations, unsorted_locations, trips) {
if (unsorted_locations.length == 0) {
return sorted_locations;
}
let selected_location = getLocationWithLeastInboundTrips(unsorted_locations, trips);
unsorted_locations = unsorted_locations.filter(location => location != selected_location);
trips = trips.filter(trip => trip.destination != selected_location && trip.origin != selected_location);
return sortLocations(sorted_locations.concat([ selected_location ]), unsorted_locations, trips);
}
export function trackedTripsLocationsSorted() {
let tracked_trips = DataStore.tracked_trips.get();
let tracked_locations = Array.from(new Set(tracked_trips.map(trip => trip.origin).concat(tracked_trips.map(trip => trip.destination))));
console.log(tracked_trips);
console.log(tracked_locations);
let sorted_locations = sortLocations([], tracked_locations, tracked_trips);
console.log(sorted_locations);
console.log(sorted_locations.map(item => DataStore.locations.get()[item]?.name));
}