ctucx.git: trainsearch

web based trip-planner, fork of https://cyberchaos.dev/yuka/trainsearch

commit 233f9b7890c2b5add9460d750bc6bf32bc9c552f
parent ae593ebf48d6b8e5a83594bcb64eb208a1067f7f
Author: Katja (ctucx) <git@ctu.cx>
Date: Tue, 21 Jan 2025 12:20:12 +0100

searchView: implement via
2 files changed, 44 insertions(+), 6 deletions(-)
M
src/app_functions.js
|
7
+++----
M
src/searchView.js
|
43
+++++++++++++++++++++++++++++++++++++++++--
diff --git a/src/app_functions.js b/src/app_functions.js
@@ -21,10 +21,9 @@ const addJourneys = async data => {
 
 	const historyEntry = {
 		fromPoint: getFrom(data.journeys),
-		//viaPoint: data.params.via,
-		toPoint: getTo(data.journeys),
-		slug: data.slug,
-		journeyId: ''
+		viaPoint:  data.params.via,
+		toPoint:   getTo(data.journeys),
+		slug:      data.slug
 	};
 
 	const journeyEntries = data.journeys.map(j => {
diff --git a/src/searchView.js b/src/searchView.js
@@ -173,6 +173,8 @@ export const searchView = async () => {
 	const journeysHistory = (await db.getJourneysHistory()).slice().reverse();	
 	render(searchTemplate(journeysHistory), ElementById('content'));
 
+	if (viaValue !== '') toggleVia('show');
+
 	ElementById('from').focus();
 
 	for (const [product, enabled] of Object.entries(settings.products)) {

@@ -277,7 +279,7 @@ export const search = async (requestId) => {
 		products: settings.products,
 	};
 
-	if (via) params.viaPoint = via;
+	if (via) params.via = via;
 
 	if (isDep)
 		params.departure = timestamp * 1000;

@@ -334,7 +336,11 @@ export const setSuggestion = (data, inputId) => {
 
 	if (inputId === 'from') {
 		ElementById('fromSuggestions').classList.remove('mouseover');
-		ElementById('to').focus();
+		if (!ElementById('via').classList.contains('hidden')) {
+			ElementById('via').focus();
+		} else {
+			ElementById('to').focus();
+		}
 	} else if (inputId === 'via') {
 		ElementById('viaSuggestions').classList.remove('mouseover');
 		ElementById('to').focus();

@@ -344,6 +350,32 @@ export const setSuggestion = (data, inputId) => {
 	}
 };
 
+export const toggleVia = ( mode ) => {
+	const rowElement    = ElementById('viaRow');
+	const buttonElement = ElementById('viaButton');
+
+	const show = () => {
+		buttonElement.classList.add('flipped');
+		rowElement.classList.remove('hidden');
+	}
+
+	const hide = () => {
+		buttonElement.classList.remove('flipped');
+		rowElement.classList.add('hidden');
+		ElementById('via').value = '';
+	}
+
+	if (mode === 'show') {
+		show();
+	} else if (mode === 'hide') {
+		hide();
+	} else if (rowElement.classList.contains('hidden')) {
+		show();
+	} else {
+		hide();
+	}
+}
+
 export const swapFromTo = () => {
 	suggestions.from = [suggestions.to, suggestions.to = suggestions.from][0];
 

@@ -356,7 +388,14 @@ export const swapFromTo = () => {
 export const setFromHistory = async id => {
 	const entry = (await db.getJourneysHistory())[id];
 	if (!entry) return;
+
 	setSuggestion(entry.fromPoint, 'from');
+
+	if (entry.viaPoint !== undefined) {
+		setSuggestion(entry.viaPoint, 'via');
+		toggleVia('show');
+	};
+
 	setSuggestion(entry.toPoint, 'to');
 };