import { html, nothing } from 'lit'; 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 { platformTemplate, timeTemplate } from './templates.js'; import { processLeg } from './app_functions.js'; import { getHafasClient } from './hafasClient.js'; import { t } from './translate.js'; import { headerStyles, tableStyles, departuresViewStyles } from './styles.js'; class DeparturesView extends BaseView { static styles = [ super.styles, headerStyles, tableStyles, departuresViewStyles ]; static properties = { profile: { attribute: true }, stopId: { attribute: true }, when: { attribute: true, type: Number }, 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, 'DeparturesView'); if (isDevServer && previous.has('viewState')) console.info('DeparturesView(viewState):', this.viewState); } updateViewState = async () => { if (this.isOffline !== false) return; this.isUpdating = true; try { const when = this.when; const client = await getHafasClient(this.profile); const [ { departures }, stopInfo ] = await Promise.all([ client.departures(this.stopId, { when }), client.stop(this.stopId), ]); departures.forEach(departure => processLeg(departure)); this.viewState = { ...stopInfo, departures }; } catch(e) { this.showAlertOverlay( e.toString(), () => { window.location = '#/'; } ); console.error(e); } this.isUpdating = false; } renderView = () => [ html`
history.back()}>
${when( this.viewState, () => html`` )}

Departures from ${when(!this.viewState, () => '...', () => this.viewState.name)}

`, when(!this.viewState, () => when( !this.isOffline, () => html`
`, () => html`
` ), () => html`
Time ${t('platform')}
${(this.viewState.departures || []).map(departure => html` ${timeTemplate(departure)} ${departure.line.name} ${when( departure.direction, () => html` → ${departure.direction}` )} ${when( !departure.cancelled, () => html`${platformTemplate(departure)}`, () => html`${t('cancelled')}` )} `)}
` ) ]; } customElements.define('departures-view', DeparturesView);