ctucx.git: oeffi-web

[nimlang] oeffisearch fork that works without javascript

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 
163 
164 
165 
166 
167 
168 
169 
170 
171 
172 
173 
174 
175 
176 
177 
178 
179 
180 
181 
182 
183 
184 
185 
186 
187 
188 
189 
190 
191 
192 
193 
194 
195 
196 
197 
198 
199 
200 
201 
202 
203 
204 
205 
206 
207 
208 
209 
210 
211 
212 
213 
214 
215 
216 
217 
218 
219 
220 
221 
222 
223 
224 
225 
'use strict';

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

const remarksModalTemplate = (type, remarks) => html`
	<table class="remarks">
		${remarks.map(element => html`
			<tr>
			<td>
				<span class="remark ${type}"></span>
				<span>${element.text}</span>
			</td>
			</tr>
		`)}
	</table>
`;

const showRemarksModal = (type, remarks) => {
	showModal(t('remarks'), remarksModalTemplate(type, remarks));
};
const remarksTemplate = ([type, remarks]) => !!remarks.length ? html`
	<a class="showremarks ${type}" @click=${() => showRemarksModal(type, remarks)}></a>
` : '';

const legTemplate = (element) => {
	const allRemarks = element.remarks || [];
	const remarks = {
		"status": allRemarks.filter(r => r.type === 'status'),
		"hint": allRemarks.filter(r => r.type === 'hint'),
		"other": allRemarks.filter(r => r.type !== 'status' && r.type !== 'hint'),
	};

	return html`
		${element.isWalking ? html`
			<p class="walk">${t('walkinfo', [parseName(element.arrival.point), element.distance])}</p>
		` : element.isTransfer ? html`
			<p class="transfer">${t('transferinfo', [parseName(element.arrival.point)])}</p>
		` : element.isChange ? html`
			<p class="change">${t('changeinfo', [formatDuration(element.duration)])}</p>
		` : html`
			<table>
				<thead>
					<tr>
						<td colspan="4">
							${element.line.name} ${element.line.additionalName ? '('+element.line.additionalName+')' : ''} → ${element.direction} ${element.cancelled ? html`<b style="color:red;">${t('cancelled-ride')}</b>` : ''}
							${Object.entries(remarks).map(remarksTemplate)}
							${dataStorage.settings.travelynx && element.line.mode == 'Train' ? html`<a class="travelynx" href="https://travelynx.de/s/${element.departure.point.stop.id}?train=${element.line.additionalName}"></a>` : ''}
						</td>
					</tr>
					<tr>
						<th>${t('arrival')}</th>
						<th>${t('departure')}</th>
						<th>${t('station')}</th>
						<th>${t('platform')}</th>
					</tr>
				</thead>
				<tbody>
					${element.stopovers.map(stop => html`
						<tr class="stop ${stop.cancelled ? 'cancelled' : ''}" @click=${() => {location.href = "https://marudor.de/"+stop.stop.id+"?searchType=hafas"}}>
							<td>${timeTemplate(stop.arrival)}</td>
							<td>${timeTemplate(stop.departure)}</td>
							<td>${stop.stop.name} ${ds100Names(stop.stop.id)}</td>
							<td>${stopPlatformTemplate(stop)}</td>
						</tr>
					`)}
				</tbody>
			</table>
		`}
	`;
};

const journeyTemplate = (data, requestId, journeyId) => {
	const departure       = data.legs[0].departure;
	const arrival         = data.legs[data.legs.length - 1].arrival;

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

	const legs = [];
	let changes = 0;
	let lastArrival;
	for (let leg of data.legs) {
		if (!leg.isWalking && !leg.isTransfer) {

			// add change
			if (lastArrival) {
				const departure = leg.departure;
				const arrival = lastArrival;
				const departureTime = departure.prognosedTime ? departure.prognosedTime : departure.plannedTime;
				const arrivalTime   = arrival.prognosedTime   ? arrival.prognosedTime   : arrival.plannedTime;
				const duration      = departureTime - arrivalTime;

				legs.push({
					isChange: true,
					duration: duration,
				});
			}
			changes++;

			lastArrival = leg.arrival;
		} else if (legs.length) {

			// if this is a walking leg and it is the first one, we don't want to
			// insert a 0 minutes change entry for this
			lastArrival = leg.arrival;
		}
		legs.push(leg);
	}

	return html`
		<div id="journeyView">
			<div id="header">
				<a class="back" href="#/${requestId}"></a>
				<p>
					<h2>
						<span id="fromName2">${parseName(departure.point)}</span>
						<span> - </span>
						<span id="toName2">${parseName(arrival.point)}</span>
					</h2>
				</p>
				<p>
					<b>
						<span>${t('duration')}: </span>
						<span id="duration">${formatDuration(duration)}</span>
						<span> | ${t('changes')}: </span>
						<span id="changes">${changes-1}</span>
						<span> | ${t('date')}: </span>
						<span id="date2">${parseDateTime(departure.plannedTime, 'date')}</span>
					</b>
				</p>
				<div class="reload" id="reload-journey" @click=${() => reloadJourney(requestId, journeyId)}></div>
			</div>
	
			<div id="connection">
				${legs.map(legTemplate)}
			</div>
		</div>
	`;
};

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

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

	if (!data.plannedTime  && !data.prognosedTime) return '-';

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

const platformTemplate = (data) => html`
	${data.prognosedPlatform ? html`
		<b>${data.prognosedPlatform}</b>
	` : (data.plannedPlatform ? data.plannedPlatform : '-')}
`;

const stopPlatformTemplate = (data) => {
	if (data.departure.plannedPlatform | data.departure.prognosedPlatform) {
		if (!data.departure.prognosedPlatform) {
			return data.departure.plannedPlatform;
		} else {
			return html`<b>${data.departure.prognosedPlatform}</b>`;
		}
	} else if (data.arrival.plannedPlatform | data.arrival.prognosedPlatform) {
		if (!data.arrival.prognosedPlatform) {
			return data.arrival.plannedPlatform;
		} else {
			return html`<b>${data.arrival.prognosedPlatform}</b>`;
		}
	} else {
		return '-'
	}
};

export const journeyView = async (match) => {
	const reqId     = match[0];
	const journeyId = match[1];

	let data;
	let all = getCache('journeys', reqId)
	if (all && all.journeys && journeyId in all.journeys) {
		data = all.journeys[journeyId];
	} else {
		const request = await get("/journeys", {"reqId": reqId});
		addCache('journeys', request);
		data = request.journeys[journeyId];
	}

	ConsoleLog(data);
	render(journeyTemplate(data, reqId, journeyId), ElementById('content'));

	const journeysHistory = getCache('journeysHistory');	
	const history_id = journeysHistory.findIndex(obj => obj.reqId === reqId);

	if (journeysHistory[history_id] !== undefined) {
		journeysHistory[history_id].journeyId = journeyId;
		addCache('journeysHistory', journeysHistory);
	}
};

const reloadJourney = async (reqId, journeyId) => {
	document.querySelector('.reload').classList.add('spinning');
	try {
		let request = await get("/refreshJourney", {"reqId": reqId, "journeyId": journeyId});
		addCache('journeys', request);
		journeyView([reqId, journeyId]);
	} catch(err) {
		ConsoleLog(err);
	}
	document.querySelector('.reload').classList.remove('spinning');
};