import { html, nothing } from 'lit'; import { ifDefined } from 'lit/directives/if-defined.js'; import { classMap } from 'lit/directives/class-map.js'; import { when } from 'lit/directives/when.js'; import { BaseView } from './baseView.js'; import { sleep, queryBackgroundColor, setThemeColor } from './helpers.js'; import { cachedCoachSequence } from './coach-sequence/index.js'; import { remarksModal, platformTemplate, stopTemplate, timeTemplate } from './templates.js'; import { processLeg } from './app_functions.js'; import { formatDuration, formatLineAdditionalName, formatLineDisplayName, formatTrainTypes } from './formatters.js'; import { getHafasClient } from './hafasClient.js'; import { t } from './translate.js'; import { headerStyles, tableStyles, journeyViewStyles } from './styles.js'; class TripView extends BaseView { static styles = [ super.styles, headerStyles, tableStyles, journeyViewStyles ]; static properties = { profile: { attribute: true }, refreshToken: { attribute: true, converter: (value) => decodeURIComponent(value)}, viewState: { state: true }, }; constructor () { super(); this.viewState = null; } async connectedCallback () { super.connectedCallback(); await sleep(200); setThemeColor(queryBackgroundColor(this.renderRoot, 'header')); } disconnectedCallback () { super.disconnectedCallback(); this.viewState = null; } async willUpdate (previous) { if (previous.has('stopId')) this.viewState = null; if (!this.viewState && !this.overlayState.visible) await this.updateViewState(); } updated (previous) { super.updated(previous, 'TripView'); if (isDevServer && previous.has('viewState')) console.info('TripView(viewState): ', this.viewState); } updateViewState = async () => { if (this.isOffline !== false) return; this.isUpdating = true; try { const client = await getHafasClient(this.profile); let viewState = await client.trip(this.refreshToken, {stopovers: true}); processLeg(viewState.trip); // needed fix for bug/typo in vendo-client if (viewState.trip.canceled === true) viewState.trip.cancelled = true; if (!viewState.trip.remarks) viewState.trip.remarks = []; const remarksStatus = viewState.trip.remarks.some((obj) => obj.type === 'status'); const remarksWarning = viewState.trip.remarks.some((obj) => obj.type === 'warning'); viewState.trip.remarksIcon = remarksWarning ? 'icon-warning' : (remarksStatus ? 'icon-status' : 'icon-hint'); viewState.trip.bahnExpertUrl = null; if (viewState.trip.line && [ 'nationalExpress', 'national', 'regionalExpress', 'regional' ].includes(viewState.trip.line.product)) { const trainName = formatLineAdditionalName(viewState.trip.line) || viewState.trip.line?.name; if (trainName) viewState.trip.bahnExpertUrl = 'https://bahn.expert/details/' + encodeURIComponent(trainName) + '/' + Number(viewState.trip.plannedDeparture); } this.viewState = viewState; if (this.viewState.trip.line && this.viewState.trip.line.name) { const [category, number] = this.viewState.trip.line.name.split(' '); const info = cachedCoachSequence(category, this.viewState.trip.line.fahrtNr || number, this.viewState.trip.origin.id, this.viewState.trip.plannedDeparture); if (info) this.viewState.trip.line.trainType = formatTrainTypes(info); this.requestUpdate(); } } catch(e) { this.showAlertOverlay( e.toString(), () => { window.location = '#/'; } ); console.error(e); } this.isUpdating = false; }; renderView = () => [ html`
history.back()}>
${when( !this.viewState, () => html`

Trip of ...

`, () => [ when( this.viewState.trip.bahnExpertUrl, () => html`` ), html`

Trip of ${formatLineDisplayName(this.viewState.trip.line)} to ${this.viewState.trip.direction}

` ] )}
`, when( !this.viewState, () => when( !this.isOffline, () => html`
`, () => html`
` ), () => html`
${formatLineDisplayName(this.viewState.trip.line)} ${when( this.viewState.trip.direction, () => html` → ${this.viewState.trip.direction}` )} ${when( this.viewState.trip.remarks.length !== 0, () => html` remarksModal(this, this.viewState.trip.remarks)}>` )}
${when( this.viewState.trip.cancelled, () => html`${t('cancelled')}` )} ${when( formatLineAdditionalName(this.viewState.trip.line), () => html`Trip: ${formatLineAdditionalName(this.viewState.trip.line)}` )} ${when( this.viewState.trip.line.trainType, () => html`Train type: ${this.viewState.trip.line.trainType}` )} ${when( !this.viewState.trip.cancelled, () => html` ${t('duration')}: ${formatDuration( (this.viewState.trip.arrival ? this.viewState.trip.arrival : this.viewState.trip.plannedArrival) - (this.viewState.trip.departure ? this.viewState.trip.departure : this.viewState.trip.plannedDeparture) )} ` )} ${when( this.viewState.trip.loadFactor, () => html`${t("load-"+this.viewState.trip.loadFactor)}` )}
${t('arrival')} ${t('departure')} ${t('station')} ${t('platform')}
${(this.viewState.trip.stopovers || []).map(stop => html`
${timeTemplate(stop, 'arrival')} ${timeTemplate(stop, 'departure')} ${stopTemplate(this.profile, stop.stop)} ${platformTemplate(stop)}
`)}
` ) ]; } customElements.define('trip-view', TripView);