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 
226 
227 
228 
229 
230 
231 
232 
233 
234 
235 
236 
237 
'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;
};