commit eab817f5e46ce10a84b740ee3b3c119be99d4ce3
parent 91c0cf36a0a555ad4c93e68e5e0090771ea71356
Author: Yureka <yuka@yuka.dev>
Date: Sun, 23 Oct 2022 23:49:33 +0200
parent 91c0cf36a0a555ad4c93e68e5e0090771ea71356
Author: Yureka <yuka@yuka.dev>
Date: Sun, 23 Oct 2022 23:49:33 +0200
ds100 search
2 files changed, 25 insertions(+), 2 deletions(-)
diff --git a/src/app_functions.js b/src/app_functions.js @@ -9,6 +9,7 @@ import { getHafasClient, client } from './hafas_client'; import { trainsearchToHafas, hafasToTrainsearch } from './refresh_token'; let ds100 = {}; +let ds100R = {}; subscribeSettings(async () => { if (settings.showRIL100Names) await loadDS100(); @@ -215,6 +216,11 @@ const ds100Names = (id) => { if (!ds100[Number(id)]) return ''; return '('+ds100[Number(id)]+')'; }; +export const ds100Reverse = (name) => { + if (!settings.showRIL100Names) return null; + if (!ds100R[name]) return null; + return ds100R[name]; +}; export const ConsoleLog = (data) => { if (settings.writeDebugLog) { @@ -225,6 +231,12 @@ export const ConsoleLog = (data) => { export const loadDS100 = async () => { const module = await import('./ds100.js'); ds100 = module.ds100; + ds100R = {}; + for (let [id, names] of Object.entries(module.ds100)) { + for (let name of names.split(", ")) { + ds100R[name] = id; + } + } }; export const timeTemplate = (data, mode) => {
diff --git a/src/searchView.js b/src/searchView.js @@ -1,6 +1,6 @@ import { showDiv, ElementById, padZeros, isValidDate, formatFromTo } from './helpers.js'; import { db } from './dataStorage.js'; -import { parseName, ConsoleLog, t, loadDS100, getJourneys, newJourneys } from './app_functions.js'; +import { parseName, ConsoleLog, t, loadDS100, getJourneys, newJourneys, ds100Reverse } from './app_functions.js'; import { modifySettings, settings } from './settings.js'; import { go } from './router.js'; import { html, render } from 'lit-html'; @@ -297,7 +297,18 @@ const suggestionsTemplate = (data, inputId) => html` const loadSuggestions = async (e, input) => { const val = e.target.value; suggestions[e.target.id] = {}; - const data = val ? await client.locations(val, {'results': 10}) : []; + + const results = val ? await Promise.all([ + (async () => { + const ds100Result = await ds100Reverse(val); + if (ds100Result !== null) { + return await client.locations(ds100Result, {'results': 1}); + } + return []; + }) (), + client.locations(val, {'results': 10}) + ]) : [[], []]; + const data = results[0].concat(results[1]); const suggestionsEl = ElementById(e.target.id+'Suggestions'); render(suggestionsTemplate(data, e.target.id), suggestionsEl); };