ctucx.git: oeffisearch

[nimlang] fast and simple tripplanner

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 
154 
155 
156 
157 
158 
159 
160 
161 
162 
'use strict';

import { showDiv, hideDiv, ElementById, parseDateTime, formatDuration } from './helpers.js';
import { saveDataStorage, getCache, addCache, parseName, ConsoleLog, t } from './app_functions.js';
import { dataStorage } from './app.js';
import { setupCanvas } from './canvas.js';
import { get } from './api.js';
import { go } from './router.js';
import { html, render } from './lit-html.js';
import { showAlertModal } from './overlays.js';

let data;

const journeysTemplate = (data) => html`
	<div class="journeys">
		<header id="header">
			<a class="back icon-back" href="#/" title="${t('back')}">${t('back')}</a>
			<div class="header-content">
				<h3>${t('from')}: ${parseName(data.params.fromPoint)}</h3>
				<div>
					<h3>${t('to')}: ${parseName(data.params.toPoint)}</h3>
					<div class="mode-changers">
						<a @click=${changeMode('table')} class="${dataStorage.settings.journeysViewMode !== 'canvas' ? 'active' : ''}">
							<div class="icon-table"></div>
							<span>Table</span>
						</a>
						<a @click=${changeMode('canvas')} class="${dataStorage.settings.journeysViewMode === 'canvas' ? 'active' : ''}">
							<div class="icon-canvas"></div>
							<span>Canvas</span>
						</a>
					</div>
				</div>
			</div>
			<a class="back invisible" href="#/"></a>
		</header>

		${dataStorage.settings.journeysViewMode === 'canvas' ? html`
			<div id="journeysCanvas">
				<canvas id="canvas"></canvas>
			</div>
		` : html`
			<a class="loadMore icon-arrow2 flipped" title="${t('label_earlier')}" @click=${() => moreJourneys(data.reqId, 'earlier')}"></a>

			<div class="card">
			<table>
				<thead>
					<tr>
						<th>${t('departure')}</th>
						<th>${t('arrival')}</th>
						<th>${t('duration')}</th>
						<th>${t('changes')}</th>
						<th>${t('products')}</th>
						<th></th>
					</tr>
				</thead>
				<tbody>
					${Object.keys(data.journeys).sort((a, b) => a - b).map(key => journeyOverviewTemplate(data, key))}
				</tbody>
			</table>
			</div>

			<a class="loadMore icon-arrow2" title="${t('label_later')}" @click=${() => moreJourneys(data.reqId, "later")}></a>
		`}
	</div>
`;

const journeyOverviewTemplate = (data, key) => {
	let departure       = data.journeys[key].legs[0].departure;
	let arrival         = data.journeys[key].legs[data.journeys[key].legs.length - 1].arrival;
	let changes         = 0;
	let products        = {};
	let productsString  = "";
	let changesDuration = 0;
	let cancelled       = false;

	let departureTime = departure.prognosedTime ? departure.prognosedTime : departure.plannedTime;
	let arrivalTime   = arrival.prognosedTime   ? arrival.prognosedTime   : arrival.plannedTime;
	let duration      = arrivalTime - departureTime;

	for (let legKey in data.journeys[key].legs) {
		let leg = data.journeys[key].legs[legKey];
		if (leg.cancelled !== false) cancelled = true;
		if (leg.isWalking || leg.isTransfer) continue;

		changes = changes+1;
		if (!products[leg.line.productName]) products[leg.line.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`
	<tr @click=${() => go('/'+data.reqId + '/' + key)}">
		<td><span>${timeTemplate(departure, 'departure')}</span></td>
		<td><span>${timeTemplate(arrival, 'arrival')}</span></td>
		<td title="${changesDuration > 0 ? 'Davon '+formatDuration(changesDuration)+' Umstiegsdauer' : ''}"><span>${formatDuration(duration)}</span></td>
		<td><span>${changes-1}</span></td>
		<td><span>${productsString}</span></td>
		<td><a class="details-button icon-arrow3"></a></td>
	</tr>`;
}

export const journeysView = async (match, isUpdate) => {
	const reqId = match[0];

	data = getCache('journeys', reqId);

	if (!data) {
		data = await get("/journeys", {"reqId": reqId});
		addCache('journeys', data);
	}

	render(journeysTemplate(data), ElementById('content'))

	if (dataStorage.settings.journeysViewMode === 'canvas') {
		setupCanvas(data, isUpdate);
	}
};

const timeTemplate = (data, mode) => {
	let delay = 0;

	if (data.prognosedTime !== null) {
		delay = (data.prognosedTime - data.plannedTime)/60;
	}

	return html`
	${delay > 0 ? html`
		${parseDateTime(data.prognosedTime)} <b>(+${delay})</b>
	` : html`
		${parseDateTime(data.plannedTime)}
	`}`;
};

const changeMode = (mode) => {
	return () => {
		dataStorage.settings.journeysViewMode = mode;
		saveDataStorage();
		render(journeysTemplate(data), ElementById('content'))
		if (dataStorage.settings.journeysViewMode === 'canvas') {
			setupCanvas(data);
		}
	};
};

export const moreJourneys  = async (reqId, mode) => {
	let   data    = getCache('journeys', reqId);
	const newData = await get('/moreJourneys', {"reqId": reqId, "mode": mode});

	data.lastUpdated = newData.lastUpdated; 
	data.journeys    = {...data.journeys, ...newData.journeys};

	addCache('journeys', data);
	journeysView([reqId], true);
};