'use strict';
import { showDiv, hideDiv, ElementById, parseDateTime, formatDuration } from './helpers.js';
import { getCache, addCache, parseName, ConsoleLog, t } from './app_functions.js';
import { dataStorage } from './app.js';
import { setupCanvas } from './canvas.js';
import { get } from './api.js';
import { go } from './router.js';
import { html, render } from './lit-html.js';
import { showAlertModal } from './overlays.js';
let data;
const journeysTemplate = (data) => html`
${dataStorage.settings.enableCanvas ? html`
` : html`
moreJourneys(data.reqId, 'earlier')}>
${t('departure')} | ${t('arrival')} | ${t('duration')} | ${t('changes')} | ${t('products')} |
${Object.keys(data.journeys).sort((a, b) => a - b).map(key => journeyOverviewTemplate(data, key))}
moreJourneys(data.reqId, "later")}>
`}
`;
const journeyOverviewTemplate = (data, key) => {
let departure = data.journeys[key].legs[0].departure;
let arrival = data.journeys[key].legs[data.journeys[key].legs.length - 1].arrival;
let changes = 0;
let products = [];
let changesDuration = 0;
let cancelled = false;
let departureTime = departure.prognosedTime ? departure.prognosedTime : departure.plannedTime;
let arrivalTime = arrival.prognosedTime ? arrival.prognosedTime : arrival.plannedTime;
let duration = arrivalTime - departureTime;
for (let legKey in data.journeys[key].legs) {
let leg = data.journeys[key].legs[legKey];
if (leg.cancelled !== false) cancelled = true;
if (leg.isWalking || leg.isTransfer) continue;
changes = changes+1;
products.push(leg.line.productName);
}
products = [...new Set(products)];
return html`
go('/'+data.reqId + '/' + key)}">
${timeTemplate(departure, 'departure')} |
${timeTemplate(arrival, 'arrival')} |
${formatDuration(duration)} |
${changes-1} |
${products.join(', ')} |
`;
}
export const journeysView = async (match) => {
const reqId = match[0];
data = getCache('journeys', reqId);
if (!data) {
data = await get("/journeys", {"reqId": reqId});
addCache('journeys', data);
}
render(journeysTemplate(data), ElementById('content'))
if (dataStorage.settings.enableCanvas) {
setupCanvas(data);
}
};
const timeTemplate = (data, mode) => {
let delay = 0;
if (data.prognosedTime !== null) {
delay = (data.prognosedTime - data.plannedTime)/60;
}
return html`
${delay > 0 ? html`
${parseDateTime(data.prognosedTime)} (+${delay})
` : html`
${parseDateTime(data.plannedTime)}
`}`;
}
export const moreJourneys = async (reqId, mode) => {
let data = getCache('journeys', reqId);
const newData = await get('/moreJourneys', {"reqId": reqId, "mode": mode});
data.lastUpdated = newData.lastUpdated;
data.journeys = {...data.journeys, ...newData.journeys};
addCache('journeys', data);
journeysView([reqId]);
};
export const reloadJourneys = async (requestId) => {
showAlertModal("not implemented yet");
};