Refactor naming of tracked trip to leg

This commit is contained in:
2025-07-06 16:05:03 +02:00
parent ca35dbf92b
commit 4871052a4b
3 changed files with 73 additions and 77 deletions

View File

@@ -55,25 +55,25 @@ export class RecentLocationsDataStore {
} }
} }
export class TrackedTripsDataStore { export class LegsDataStore {
constructor() { constructor() {
this.data = []; this.data = [];
this.read(); this.read();
} }
read() { read() {
this.data = DataStore.get("tracked-trips") || []; this.data = DataStore.get("legs") || [];
} }
write() { write() {
DataStore.set("tracked-trips", this.data); DataStore.set("legs", this.data);
} }
remember(trip_id, origin_id, destination_id) { remember(trip_id, origin_id, destination_id) {
this.data.push({ this.data.push({
trip: trip_id, trip_id: trip_id,
origin: origin_id, origin_location_id: origin_id,
destination: destination_id, destination_location_id: destination_id,
}); });
this.write(); this.write();
@@ -119,7 +119,7 @@ export class DataStore {
constructor() { constructor() {
this.locations = new LocationsDataStore(); this.locations = new LocationsDataStore();
this.recent_locations = new RecentLocationsDataStore(); this.recent_locations = new RecentLocationsDataStore();
this.tracked_trips = new TrackedTripsDataStore(); this.legs = new LegsDataStore();
this.trips = new TripsDataStore(); this.trips = new TripsDataStore();
} }
} }

View File

@@ -6,7 +6,7 @@ let element_board = document.querySelector("#drafting-board-content");
export function addJourneyToDraftingBoard(journey) { export function addJourneyToDraftingBoard(journey) {
for (let leg of journey.legs.filter(item => !("walking" in item))) { for (let leg of journey.legs.filter(item => !("walking" in item))) {
window.dataStore.tracked_trips.remember(leg.tripId, leg.origin.id, leg.destination.id); window.dataStore.legs.remember(leg.tripId, leg.origin.id, leg.destination.id);
fetchTrip(leg.tripId).then(result => { fetchTrip(leg.tripId).then(result => {
window.dataStore.trips.remember(result.trip); window.dataStore.trips.remember(result.trip);
}); });
@@ -38,11 +38,8 @@ export function drawDraftingBoard() {
} }
let trip_data = window.dataStore.trips.data; function getStopFromTrip(trip_id, location_id) {
console.log(trip_data); for (let stop of window.dataStore.trips.data[trip_id]?.stopovers || []) {
function getStopFromTrip(tripId, location_id) {
for (let stop of trip_data[tripId]?.stopovers || []) {
if (stop.stop.id == location_id) { if (stop.stop.id == location_id) {
return stop; return stop;
} }
@@ -53,18 +50,16 @@ export function drawDraftingBoard() {
} }
let display_trips = window.dataStore.tracked_trips.data.map(item => { let display_legs = window.dataStore.legs.data.map(leg => {
item["trip_data"] = trip_data[item.trip]; leg.trip = window.dataStore.trips.data[leg.trip_id];
item.originStop = getStopFromTrip(item.trip, item.origin); leg.origin_location = getStopFromTrip(leg.trip_id, leg.origin_location_id);
item.destinationStop = getStopFromTrip(item.trip, item.destination); leg.destination_location = getStopFromTrip(leg.trip_id, leg.destination_location_id);
return item; return leg;
}); });
console.log(display_trips); let rows = display_legs.length;
let rows = display_trips.length;
for (let display_location of display_locations) { for (let display_location of display_locations) {
let el_location_name = EL("div", { let el_location_name = EL("div", {
@@ -93,72 +88,72 @@ export function drawDraftingBoard() {
element_board.appendChild(el_location_column); element_board.appendChild(el_location_column);
} }
let trip_offset = 0; let leg_offset = 0;
for (let display_trip of display_trips) { for (let display_leg of display_legs) {
console.log(display_trip); console.log(display_leg);
let el_trip_left = EL("div", { let el_leg_left = EL("div", {
class: [ "trip-left" ], class: [ "leg-left" ],
style: { style: {
"grid-column-start": grid_location_indexes[display_trip.origin]["name-start"], "grid-column-start": grid_location_indexes[display_leg.origin_location_id]["name-start"],
"grid-column-end": grid_location_indexes[display_trip.origin]["column-start"], "grid-column-end": grid_location_indexes[display_leg.origin_location_id]["column-start"],
"grid-row-start": trip_offset + 2, "grid-row-start": leg_offset + 2,
"grid-row-end": trip_offset + 2, "grid-row-end": leg_offset + 2,
}, },
}); });
el_trip_left.appendChild(document.createTextNode(new Date(display_trip.originStop.departure).toLocaleTimeString())); el_leg_left.appendChild(document.createTextNode(new Date(display_leg.origin_location.departure).toLocaleTimeString()));
el_trip_left.appendChild(EL("br", {})); el_leg_left.appendChild(EL("br", {}));
el_trip_left.appendChild(document.createTextNode("Gleis " + display_trip.originStop.departurePlatform)); el_leg_left.appendChild(document.createTextNode("Gleis " + display_leg.origin_location.departurePlatform));
element_board.appendChild(el_trip_left); element_board.appendChild(el_leg_left);
let el_trip = EL("div", { let el_leg = EL("div", {
class: [ "trip" ], class: [ "leg" ],
style: { style: {
"grid-column-start": grid_location_indexes[display_trip.origin]["column-end"], "grid-column-start": grid_location_indexes[display_leg.origin_location_id]["column-end"],
"grid-column-end": grid_location_indexes[display_trip.destination]["column-start"], "grid-column-end": grid_location_indexes[display_leg.destination_location_id]["column-start"],
"grid-row-start": trip_offset + 2, "grid-row-start": leg_offset + 2,
"grid-row-end": trip_offset + 2, "grid-row-end": leg_offset + 2,
}, },
}); });
el_trip.innerText = display_trip.trip_data?.line?.name; el_leg.innerText = display_leg.trip?.line?.name;
el_trip.addEventListener("click", event => { el_leg.addEventListener("click", event => {
console.log(display_trip.tripId); console.log(display_leg.trip_id);
displayTripDetails(display_trip.tripId); displayTripDetails(display_leg.trip_id);
}); });
element_board.appendChild(el_trip); element_board.appendChild(el_leg);
let el_trip_right = EL("div", { let el_leg_right = EL("div", {
class: [ "trip-right" ], class: [ "leg-right" ],
style: { style: {
"grid-column-start": grid_location_indexes[display_trip.destination]["column-end"], "grid-column-start": grid_location_indexes[display_leg.destination_location_id]["column-end"],
"grid-column-end": grid_location_indexes[display_trip.destination]["name-end"], "grid-column-end": grid_location_indexes[display_leg.destination_location_id]["name-end"],
"grid-row-start": trip_offset + 2, "grid-row-start": leg_offset + 2,
"grid-row-end": trip_offset + 2, "grid-row-end": leg_offset + 2,
}, },
}); });
el_trip_right.appendChild(document.createTextNode(new Date(display_trip.destinationStop.arrival).toLocaleTimeString())); el_leg_right.appendChild(document.createTextNode(new Date(display_leg.destination_location.arrival).toLocaleTimeString()));
el_trip_right.appendChild(EL("br", {})); el_leg_right.appendChild(EL("br", {}));
el_trip_right.appendChild(document.createTextNode("Gleis " + display_trip.destinationStop.arrivalPlatform)); el_leg_right.appendChild(document.createTextNode("Gleis " + display_leg.destination_location.arrivalPlatform));
element_board.appendChild(el_trip_right); element_board.appendChild(el_leg_right);
trip_offset += 1; leg_offset += 1;
} }
} }
export function getLocationWithLeastInboundTrips(locations, trips) { export function getLocationWithLeastInboundTrips(locations, legs) {
function numberOfInboundTrips(location) { function numberOfInboundLegs(location) {
return trips.filter(trip => trip.destination == location).length; return legs.filter(leg => leg.destination_location_id == location).length;
} }
let locations_sorted_by_number_of_inbound_trips = locations.toSorted((location_a, location_b) => { let locations_sorted_by_number_of_inbound_legs = locations.toSorted((location_a, location_b) => {
let n_a = numberOfInboundTrips(location_a); let n_a = numberOfInboundLegs(location_a);
let n_b = numberOfInboundTrips(location_b); let n_b = numberOfInboundLegs(location_b);
if (n_a == n_b) { if (n_a == n_b) {
return 0; return 0;
@@ -169,30 +164,31 @@ export function getLocationWithLeastInboundTrips(locations, trips) {
} }
}); });
return locations_sorted_by_number_of_inbound_trips[0]; return locations_sorted_by_number_of_inbound_legs[0];
} }
export function sortLocations(sorted_locations, unsorted_locations, trips) { export function sortLocations(sorted_locations, unsorted_locations, legs) {
if (unsorted_locations.length == 0) { if (unsorted_locations.length == 0) {
return sorted_locations; return sorted_locations;
} }
let selected_location = getLocationWithLeastInboundTrips(unsorted_locations, trips); let selected_location_id = getLocationWithLeastInboundTrips(unsorted_locations, legs);
unsorted_locations = unsorted_locations.filter(location => location != selected_location); unsorted_locations = unsorted_locations.filter(location_id => location_id != selected_location_id);
trips = trips.filter(trip => trip.destination != selected_location && trip.origin != selected_location); legs = legs.filter(leg => leg.destination_id != selected_location_id && leg.origin_id != selected_location_id);
return sortLocations(sorted_locations.concat([ selected_location ]), unsorted_locations, trips); return sortLocations(sorted_locations.concat([ selected_location_id ]), unsorted_locations, legs);
} }
export function trackedTripsLocationsSorted() { export function trackedTripsLocationsSorted() {
let tracked_trips = window.dataStore.tracked_trips.data; let passed_locations = Array.from(new Set(
let tracked_locations = Array.from(new Set(tracked_trips.map(trip => trip.origin).concat(tracked_trips.map(trip => trip.destination)))); window.dataStore.legs.data.map(leg => leg.origin_location_id)
.concat(
window.dataStore.legs.data.map(leg => leg.destination_location_id)
)
));
console.log(tracked_trips); let sorted_locations = sortLocations([], passed_locations, window.dataStore.legs.data);
console.log(tracked_locations);
let sorted_locations = sortLocations([], tracked_locations, tracked_trips);
console.log(sorted_locations); console.log(sorted_locations);

View File

@@ -104,14 +104,14 @@ input.form-control {
background-color: black; background-color: black;
} }
#drafting-board .trip-left { #drafting-board .leg-left {
margin-top: auto; margin-top: auto;
margin-bottom: auto; margin-bottom: auto;
margin-right: 10px; margin-right: 10px;
text-align: right; text-align: right;
} }
#drafting-board .trip { #drafting-board .leg {
margin: 10px; margin: 10px;
border-style: solid; border-style: solid;
@@ -124,7 +124,7 @@ input.form-control {
background-color: #ff4bb0; background-color: #ff4bb0;
} }
#drafting-board .trip-right { #drafting-board .leg-right {
margin-top: auto; margin-top: auto;
margin-bottom: auto; margin-bottom: auto;
margin-left: 10px; margin-left: 10px;