import { html, nothing, render } from 'lit-html'; import { ElementById, setThemeColor, queryBackgroundColor, padZeros } from './helpers.js'; import { getJourneys, getMoreJourneys, refreshJourneys, getFrom, getTo } from './app_functions.js'; import { formatName, formatDuration, formatFromTo, formatPrice } from './formatters.js'; import { timeTemplate, footerTemplate } from './templates.js'; import { settings, modifySettings } from './settings.js'; import { setupCanvas } from './journeysViewCanvas.js'; import { go } from './router.js'; import { showAlertModal, showLoader, hideOverlay } from './overlays.js'; import { t } from './languages.js'; const journeysTemplate = (data) => html`
${settings.journeysViewMode === 'canvas' ? html`
` : nothing} ${settings.journeysViewMode === 'table' ? html`
${data.earlierRef ? html` moreJourneys(data.slug, 'earlier')}>` : nothing}
${settings.showPrices && settings.profile === 'db' ? html`` : nothing} ${Object.entries(data.journeys).map(([key, value]) => journeyOverviewTemplate(data.profile || "db", value, data.slug, key - data.indexOffset))}
${t('departure')} ${t('arrival')} ${t('duration')} ${t('changes')} ${t('products')}${t('price')}
${data.laterRef ? html` moreJourneys(data.slug, 'later')}>` : nothing}
${footerTemplate} ` : nothing} `; const journeyOverviewTemplate = (profile, entry, slug, key) => { const firstLeg = entry.legs[0]; const lastLeg = entry.legs[entry.legs.length - 1]; let changes = 0; const products = {}; let productsString = ''; const changesDuration = 0; let cancelled = false; const duration = Number(lastLeg.arrival || lastLeg.plannedArrival) - Number(firstLeg.departure || firstLeg.plannedDeparture); for (const leg of entry.legs) { if (leg.cancelled) cancelled = true; if (leg.walking || leg.transfer) continue; changes = changes+1; if (leg.line) { const productName = leg.line.name.split(' ')[0]; if (!products[productName]) products[productName] = []; } //if (leg.line && leg.line.trainTypeShort) products[leg.line.productName].push(leg.line.trainTypeShort); } productsString = Object.entries(products).map(([prod, types]) => { if (types.length >= 2) { prod += ' (' + types.join(', ') + ')'; } else if (types.length) { prod += ' ' + types[0]; } return prod; }).join(', '); return html` go(`/j/${profile}/${entry.refreshToken}`)}> ${timeTemplate(firstLeg, 'departure')} ${cancelled ? html` ${t('cancelled-ride')} ` : html` ${timeTemplate(lastLeg, 'arrival')} `} ${formatDuration(duration)} ${changes-1} ${productsString} ${settings.showPrices && settings.profile === 'db' ? html`${formatPrice(entry.price)}` : nothing} `; }; export const journeysView = async (match, isUpdate) => { const slug = match[0]; const mode = match[1]; if (settings.journeysViewMode != mode) { await modifySettings(settings => { settings.journeysViewMode = mode; return settings; }); } let data; if (!isUpdate) showLoader(); try { data = await getJourneys(slug); } catch(e) { await showAlertModal(e.toString()); go('/'); throw e; } if (!data) { await showAlertModal(html`journeys overview id invalid.
journeys overview links can not be shared across devices in ${APPNAME}.`); go('/'); return; } hideOverlay(); render(journeysTemplate(data), ElementById('content')); setThemeColor(queryBackgroundColor('header')); if (settings.journeysViewMode === 'canvas') return setupCanvas(data, isUpdate); }; export const moreJourneys = async (slug, mode) => { showLoader(); try { await getMoreJourneys(slug, mode); } catch(e) { showAlertModal(e.toString()); throw e; } hideOverlay(); journeysView([slug, settings.journeysViewMode], true); }; const refreshJourneysView = async (slug) => { document.querySelector('.icon-reload').classList.add('spinning'); try { await refreshJourneys(slug, true); } catch(e) { showAlertModal(e.toString()); document.querySelector('.icon-reload').classList.remove('spinning'); throw e; } journeysView([slug, settings.journeysViewMode], true); document.querySelector('.icon-reload').classList.remove('spinning'); };