'use strict'; import { devMode, dataStorage } from './app.js'; import { showModal } from './overlays.js'; import { languages } from './languages.js'; import { vsprintf } from './sprintf.min.js'; let ds100 = {}; export const addCache = (mode, data) => { if (data === undefined) { return false; } switch (mode) { case 'journeys': if (dataStorage.journeys[data.reqId] !== undefined) { ConsoleLog(data); dataStorage.journeys[data.reqId].lastUpdated = data.lastUpdated; dataStorage.journeys[data.reqId].journeys = {...dataStorage.journeys[data.reqId].journeys, ...data.journeys}; } else { dataStorage.journeys[data.reqId] = data; const lastHistoryEntry = dataStorage.journeysHistory.slice(-1); if (lastHistoryEntry[0] !== undefined) { if (JSON.stringify(lastHistoryEntry[0].fromPoint) !== JSON.stringify(data.params.fromPoint) || JSON.stringify(lastHistoryEntry[0].toPoint) !== JSON.stringify(data.params.toPoint)) { dataStorage.journeysHistory.push({ fromPoint: data.params.fromPoint, toPoint: data.params.toPoint, reqId: data.reqId, journeyId: '' }); } else { dataStorage.journeysHistory[dataStorage.journeysHistory.length-1].reqId = data.reqId; dataStorage.journeysHistory[dataStorage.journeysHistory.length-1].journeyId = ''; } } else { dataStorage.journeysHistory.push({ fromPoint: data.params.fromPoint, toPoint: data.params.toPoint, reqId: data.reqId, journeyId: '' }); } } break; case 'journeysHistory': if (data !== undefined) { dataStorage.journeysHistory = data; } else { return false; } break; default: return false; break; } return saveDataStorage(); }; export const getCache = (mode, reqId) => { switch (mode) { case 'journeys': if (reqId !== undefined) { if (dataStorage.journeys[reqId] !== undefined) { return dataStorage.journeys[reqId]; } else { return null; } } else { return dataStorage.journeys; } break; case 'journeysHistory': return dataStorage.journeysHistory; break; default: return null; break; } }; export const t = (key, params) => { let language = 'en'; if (dataStorage.settings.language !== undefined) { language = dataStorage.settings.language; } if (languages[language][key] !== undefined) { return vsprintf(languages[language][key], params); } else { return key; } } export const parseName = (point) => { let nameHTML = ''; if (point.stop) { nameHTML += point.stop.name+ds100Names(point.stop.id); } else if (point.location) { if (point.location.name) { nameHTML += point.location.name; } else if (point.location.address) { nameHTML += point.location.address; } } else { return ""; } return nameHTML; }; export const ds100Names = (id) => { if (dataStorage.settings.showRIL100Names !== false) { if (ds100[Number(id)] !== undefined) { return '('+ds100[Number(id)]+')'; } else { return ''; } } else { return ''; } } export const ConsoleLog = (data) => { if (dataStorage.settings.writeDebugLog !== false) { console.log(data); } }; export const clearDataStorage = () => { localStorage.removeItem('dataStorage'); localStorage.removeItem('dataStorage_dev'); location.reload(); }; export const saveDataStorage = () => { let name = 'dataStorage_dev'; if (devMode !== true) { name = 'dataStorage'; } try { localStorage.setItem(name, JSON.stringify(dataStorage)); } catch (e) { ConsoleLog("yikes, localstorage full, clear it...") dataStorage.journeys = {}; saveDataStorage(); } }; export const restoreDataStorage = () => { let data; if (devMode !== true) { data = localStorage.getItem('dataStorage'); } else { data = localStorage.getItem('dataStorage_dev'); } if (data !== null) { let dataStorage = JSON.parse(data); if (dataStorage.dbVer < 0.5) { showModal('Lokale Datenbank veraltet - Datenbank wird neu erstellt.'); if (devMode !== true) { localStorage.removeItem('dataStorage'); } else { localStorage.removeItem('dataStorage_dev'); } dataStorage = restoreDataStorage(); } return dataStorage; } else { let userLang = navigator.language || navigator.userLanguage; let language = 'en'; if (['en', 'de'].includes(userLang)) { language = userLang; } const data = { dbVer: 0.5, journeysHistory: [], journeys: {}, ignored: [], favorites: [], settings: { provider: 'DB', products: { "nationalExp": true, "national": true, "regionalExp": true, "regional": true, "suburban": true, "bus": true, "ferry": true, "subway": true, "tram": true, "taxi": true }, accessibility: 'none', showRIL100Names: false, writeDebugLog: false, enableCanvas: false, language: language, travelynx: false } } if (devMode !== true) { localStorage.setItem('dataStorage', JSON.stringify(data)); } else { localStorage.setItem('dataStorage_dev', JSON.stringify(data)); } return data; } }; export const loadDS100 = async () => { const module = await import('./ds100.js'); ds100 = module.ds100; };