Files
traveldrafter/web/datastore.js
2025-07-06 18:39:49 +02:00

129 lines
2.2 KiB
JavaScript

export class LocationsDataStore {
constructor() {
this.data = {};
this.read();
}
read() {
this.data = DataStore.get("locations") || {};
}
write() {
DataStore.set("locations", this.data);
}
rememberAll(location_list) {
for (let location of location_list) {
this.data[location.id] = location;
}
this.write();
}
rememberAllIfNotExist(location_list) {
for (let location of location_list) {
if (!(location.id in this.data)) {
this.data[location.id] = location;
}
}
this.write();
}
}
export class RecentLocationsDataStore {
constructor() {
this.data = [];
this.read();
}
read() {
this.data = DataStore.get("recent-locations") || [];
}
write() {
DataStore.set("recent-locations", this.data);
}
remember(location_id) {
this.data = this.data.filter((item) => {
return item != location_id;
});
this.data.push(location_id);
this.write();
}
}
export class LegsDataStore {
constructor() {
this.data = [];
this.read();
}
read() {
this.data = DataStore.get("legs") || [];
}
write() {
DataStore.set("legs", this.data);
}
remember(trip_id, origin_location_id, destination_location_id) {
this.data = this.data.filter((leg) => {
return !(leg.trip_id == trip_id && leg.origin_location_id == origin_location_id && leg.destination_location_id == destination_location_id);
});
this.data.push({
trip_id: trip_id,
origin_location_id: origin_location_id,
destination_location_id: destination_location_id,
});
this.write();
}
}
export class TripsDataStore {
constructor() {
this.data = {};
this.read();
}
read() {
this.data = DataStore.get("trips") || {};
}
write() {
DataStore.set("trips", this.data);
}
remember(trip) {
this.rememberAll([trip]);
}
rememberAll(trip_list) {
for (let trip of trip_list) {
this.data[trip.id] = trip;
}
this.write();
}
}
export class DataStore {
static get(key) {
return JSON.parse(window.localStorage.getItem(key));
}
static set(key, value) {
window.localStorage.setItem(key, JSON.stringify(value));
}
constructor() {
this.locations = new LocationsDataStore();
this.recent_locations = new RecentLocationsDataStore();
this.legs = new LegsDataStore();
this.trips = new TripsDataStore();
}
}