1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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`
<div class="header-container">
<header>
<a class="icon-back ${classMap({ invisible: history.length === 1 })}" title=${t('back')} @click=${() => history.back()}></a>
<div class="container">
${when(
this.viewState,
() => html`<a class="icon-bahnexpert" href="https://bahn.expert/${encodeURIComponent(this.viewState.name)}"></a>`
)}
<h3>Departures from ${when(!this.viewState, () => '...', () => this.viewState.name)}</h3>
</div>
<a class="icon-reload ${classMap({ spinning: this.isUpdating, invisible: this.isOffline })}" title=${t("refresh")} @click=${this.updateViewState}></a>
</header>
</div>
`,
when(!this.viewState,
() => when(
!this.isOffline,
() => html`<div class="spinner"></div>`,
() => html`<div class="offline"></div>`
),
() => html`
<div class="container">
<div class="table" style="grid-template-columns: 1fr .5fr 3.5fr 1fr">
<div class="row head">
<span>Time</span>
<span></span>
<span></span>
<span>${t('platform')}</span>
</div>
${(this.viewState.departures || []).map(departure => html`
<a class="row" href="#/t/${this.profile}/${departure.tripId}">
<span class=${classMap({ cancelled: departure.cancelled })}>
${timeTemplate(departure)}
</span>
<span class="direction ${classMap({ cancelled: departure.cancelled })}">
${departure.line.name}
</span>
<span class="direction ${classMap({ cancelled: departure.cancelled })}">
${when(
departure.direction,
() => html` → ${departure.direction}`
)}
</span>
${when(
!departure.cancelled,
() => html`<span>${platformTemplate(departure)}</span>`,
() => html`<span class="cancelled-text">${t('cancelled')}</span>`
)}
</a>
`)}
</tbody>
</div>
</div>
<footer-component></footer-component>
`
)
];
}
customElements.define('departures-view', DeparturesView);