import { html, nothing, render } from 'lit-html'; import { settings } from './settings.js'; import { remarksModalTemplate, platformTemplate, stopTemplate, timeTemplate } from './templates.js'; import { ElementById, showElement, setThemeColor, queryBackgroundColor } from './helpers.js'; import { processLeg } from './app_functions.js'; import { formatDateTime, formatDuration, formatPrice, formatLineAdditionalName, formatLineDisplayName } from './formatters.js'; import { showAlertModal, showLoader, hideOverlay, showModal } from './overlays.js'; import { go } from './router.js'; import { getHafasClient, client } from './hafasClient.js'; import { t } from './languages.js'; const tripTemplate = (data, profile, refreshToken) => { let changes = 0; let lastArrival; const remarks = data.remarks || []; const remarksStatus = remarks.some((obj) => obj.type === 'status'); const remarksWarning = remarks.some((obj) => obj.type === 'warning'); const remarksIcon = remarksWarning ? 'icon-warning' : (remarksStatus ? 'icon-status' : 'icon-hint'); let bahnExpertUrl = null; if (data.line && (data.line.product == 'nationalExpress' || data.line.product == 'national' || data.line.product == 'regionalExpress' || data.line.product == 'regional')) { const trainName = formatLineAdditionalName(data.line) || data.line?.name; if (trainName) { bahnExpertUrl = 'https://bahn.expert/details/' + encodeURIComponent(trainName) + '/' + Number(data.plannedDeparture); } } return html`

Trip of ${formatLineDisplayName(data.line)} to ${data.direction}

refreshTripView(profile, refreshToken)}>
${(data.stopovers || []).map(stop => html` `)}
${bahnExpertUrl ? html` ${formatLineDisplayName(data.line)}${data.direction ? html` → ${data.direction}` : ''} ` : html ` ${formatLineDisplayName(data.line)}${data.direction ? html` → ${data.direction}` : ''} `} ${data.cancelled ? html`${t('cancelled-ride')}` : ''} ${!!remarks.length ? html` showModal(t('remarks'), remarksModalTemplate(remarks))}>` : nothing}
${formatLineAdditionalName(data.line) ? html`
Trip: ${formatLineAdditionalName(data.line)}
` : nothing} ${data.line.trainType ? html`
Train type: ${data.line.trainType}
` : nothing}
${t('duration')}: ${formatDuration(data.arrival - (data.departure ? data.departure : data.plannedDeparture))} ${data.departure ? '' : ('(' + t('planned') + ')')}
${data.loadFactor ? html`
` : nothing}
${t('arrival')} ${t('departure')} ${t('station')} ${t('platform')}
${timeTemplate(stop, 'arrival')} ${timeTemplate(stop, 'departure')} ${stopTemplate(profile, stop.stop)} ${platformTemplate(stop)}
`; }; export const tripView = async (match, isUpdate) => { if (!isUpdate) showLoader(); let profile, refreshToken, data; try { profile = match[0]; refreshToken = decodeURIComponent(match[1]); const client = await getHafasClient(profile); data = await client.trip(refreshToken, {stopovers: true}); processLeg(data.trip); } catch(e) { showAlertModal(e.toString()); throw e; } hideOverlay(); render(tripTemplate(data.trip, profile, refreshToken), ElementById('content')); setThemeColor(queryBackgroundColor('header')); console.log(data) if (history.length > 0) showElement(ElementById('back')); }; const refreshTripView = async (profile, refreshToken) => { document.querySelector('.icon-reload').classList.add('spinning'); let data; try { const client = await getHafasClient(profile); data = await client.trip(refreshToken, {stopovers: true}); processLeg(data.trip); } catch(e) { showAlertModal(e.toString()); throw e; } render(tripTemplate(data.trip, profile, refreshToken), ElementById('content')); document.querySelector('.icon-reload').classList.remove('spinning'); };