Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
320 lines (261 loc) · 10.3 KB
/
Copy pathscript.js
File metadata and controls
320 lines (261 loc) · 10.3 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
function login() {
// implement your own login logic here
}
const carrierLogos = {
'Železničná spoločnosť Slovensko, a.s.': 'clogo/zssk.png',
'Leo Express Slovensko s.r.o.': 'clogo/leoexpress.png',
'Leo Express s.r.o.': 'clogo/leoexpress.png',
'RegioJet a.s.': 'clogo/regiojet.png',
'Przewozy Regionalne Sp. z o.o.': 'clogo/polregio.png',
'MÁV-Start Zrt.': 'clogo/mavstart.png',
'Default': 'clogo/default_logo.png'
};
const trainTypePriority = {
'EN': 1,
'EC': 2,
'RJ': 3,
'LE': 4,
'IC': 5,
'Ex': 6,
'R': 7,
'REX': 8,
'Zr': 9,
'Os': 10
};
const trainTypeColors = {
'EN': 'dark_green',
'EC': 'green',
'RJ': '#c8c800',
'LE': 'lime',
'IC': 'green',
'Ex': 'dark_orange',
'R': 'red',
'REX': 'orange',
'Zr': 'teal',
'Os': 'gray'
};
let previousDelays = {};
function createTrainDiv(train, showStar = true) {
var trainDiv = $('<div></div>')
.attr('id', train.train_number)
.addClass('train');
const carrierLogo = carrierLogos[train.carrier] || carrierLogos['Default'];
const hasMultipleWords = train.train_name && train.train_name.trim().includes(' ');
var trainNumberEl = $('<p></p>')
.addClass('train_number')
.html(`
<img src="${carrierLogo}" alt="${train.carrier}" style="width: 20px; height: 20px; margin-right: 5px; vertical-align: middle;">
${train.type} ${train.train_number}${hasMultipleWords ? '<br>' : ' '}${train.train_name}
`);
if (trainTypeColors[train.type]) {
trainNumberEl.css('color', trainTypeColors[train.type]);
trainNumberEl.css('font-weight', 'bold');
}
trainDiv.append(trainNumberEl);
var buttonsContainer = $('<div></div>')
.css({
'position': 'absolute',
'right': '10px',
'top': '10px',
'display': 'flex',
'flex-direction': 'column',
'gap': '5px'
});
trainNumberEl.css('position', 'relative');
trainNumberEl.append(buttonsContainer);
var detailsButton = $('<button></button>')
.addClass('details-button')
.html('<i class="fas fa-train"></i>')
.css({
'padding': '5px 10px',
'background-color': '#007bff',
'color': 'white',
'border': 'none',
'border-radius': '4px',
'cursor': 'pointer',
'width': '100%',
'margin-top': '-18px',
})
.on('click', function() {
openInNewTab(`https://www.vagonweb.cz/razeni/vlak.php?zeme=${CarrierCode(train.carrier)}&cislo=${train.train_number}&rok=2025`);
});
buttonsContainer.append(detailsButton);
var delayText = $('<p></p>').addClass('train_delay');
var currentDelay = train.delay;
var previousDelay = previousDelays[train.train_number];
var delayDisplay = $('<span></span>').text(
currentDelay == 0 ? "No delay" :
currentDelay + ' ' + (currentDelay == 1 ? 'minute' : 'minutes')
);
if (typeof previousDelay !== 'undefined' && currentDelay !== previousDelay) {
const diff = currentDelay - previousDelay;
const arrow = diff > 0 ? '↑' : '↓';
const color = diff > 0 ? '#ff4444' : '#44ff44';
$('<span></span>')
.addClass('delay-change')
.text(arrow + Math.abs(diff))
.css('color', color)
.appendTo(delayDisplay);
}
if (currentDelay < 0) {
delayDisplay.css('color', 'lightblue');
} else if (currentDelay <= 5) {
delayDisplay.css('color', 'green');
} else if (currentDelay <= 15) {
delayDisplay.css('color', 'orange');
} else {
delayDisplay.css('color', 'red');
}
delayText.append(delayDisplay);
trainDiv.append(delayText);
previousDelays[train.train_number] = currentDelay;
trainDiv.append($('<p></p>').addClass('train_from').text(train.from));
trainDiv.append($('<p></p>').addClass('train_to').text(train.to));
if (window.routesData && train.line && train.current) {
var progressHtml = createTrainProgressBar(train, window.routesData);
trainDiv.append($('<div></div>').addClass('train_line').html(progressHtml));
} else {
trainDiv.append($('<p></p>').addClass('train_line').css('font-size', '6px').css('text-align', 'center').text('Tracking not available'));
}
return trainDiv;
}
function createTrainProgressBar(train, routesData) {
const route = routesData.find(r => r.route === train.line);
if (!route) return '<p>Route not found</p>';
const stations = route.stations;
const allStationKeys = Object.keys(stations).sort((a, b) => {
const aParts = a.split('_').map(n => parseInt(n) || 0);
const bParts = b.split('_').map(n => parseInt(n) || 0);
if (aParts[0] !== bParts[0]) return aParts[0] - bParts[0];
if (aParts.length === 1 && bParts.length > 1) return -1;
if (bParts.length === 1 && aParts.length > 1) return 1;
if (aParts.length > 1 && bParts.length > 1) return aParts[1] - bParts[1];
return 0;
});
const allStations = allStationKeys.map(key => stations[key]);
const mainStationKeys = allStationKeys.filter(key => !key.includes('_'));
const mainStations = mainStationKeys.map(key => stations[key]);
let displayStations = [...mainStations];
const currentStationIndex = allStations.indexOf(train.current);
if (currentStationIndex === -1) return '<p>Current station not found</p>';
let progressPercent;
if (currentStationIndex === 0) {
progressPercent = 0;
} else if (currentStationIndex === allStations.length - 1) {
progressPercent = 100;
} else {
let prevMainIndex = -1;
let nextMainIndex = -1;
for (let i = 0; i < mainStationKeys.length; i++) {
const stationIndex = allStations.indexOf(stations[mainStationKeys[i]]);
if (stationIndex <= currentStationIndex) {
prevMainIndex = i;
} else {
nextMainIndex = i;
break;
}
}
if (prevMainIndex === -1) {
progressPercent = 0;
} else if (nextMainIndex === -1) {
progressPercent = 100;
} else {
const prevMainStation = stations[mainStationKeys[prevMainIndex]];
const nextMainStation = stations[mainStationKeys[nextMainIndex]];
const prevStationIndex = allStations.indexOf(prevMainStation);
const nextStationIndex = allStations.indexOf(nextMainStation);
const segmentLength = nextStationIndex - prevStationIndex;
const segmentProgress = currentStationIndex - prevStationIndex;
const segmentPercent = segmentProgress / segmentLength;
const mainSegmentPercent = 1 / (mainStationKeys.length - 1);
progressPercent = ((prevMainIndex / (mainStationKeys.length - 1)) +
(segmentPercent * mainSegmentPercent)) * 100;
}
}
let html = '<div class="train-progress">';
displayStations.forEach((station, index) => {
const position = (index / (displayStations.length - 1)) * 100;
const isPassed = position <= progressPercent;
const isCurrent = station === train.current;
html += `
<div class="station-dot ${isPassed ? 'passed' : ''} ${isCurrent ? 'current' : ''}"
style="left: ${position}%">
${index !== 0 && index !== displayStations.length - 1 ? `<div class="station-label">${station}</div>` : ''}
</div>
`;
});
html += `<div class="progress-bar" style="width: ${progressPercent}%"></div>`;
if (!mainStations.includes(train.current)) {
if (progressPercent > 0 && progressPercent < 100) {
html += `
<div class="current-marker update-blink" style="left: ${progressPercent}%">
<div class="current-label">${train.current}</div>
</div>
`;
}
}
html += '</div>';
return html;
}
function openInNewTab(url) {
window.open(url, '_blank').focus();
}
function CarrierCode(carrier) {
const carrierCodes = {
'Železničná spoločnosť Slovensko, a.s.': 'ZSSK',
'Leo Express Slovensko s.r.o.': 'LESK',
'Leo Express s.r.o.': 'LE',
'RegioJet a.s.': 'RJ'
};
return carrierCodes[carrier] || carrier;
}
function updateTrainTable() {
if (!window.routesData) {
$.ajax({
url: 'routes.json',
dataType: 'json',
async: false,
success: function(data) {
window.routesData = data;
},
error: function() {
console.error('Error loading routes.json');
}
});
}
$.ajax({
url: 'get_train_data.php',
type: 'GET',
data: {
search: $('#search').val() || ''
},
success: function(data) {
var trainsContainer = $('#trains');
trainsContainer.empty();
data.sort(function(a, b) {
const priorityA = trainTypePriority[a.type] || 10;
const priorityB = trainTypePriority[b.type] || 10;
return priorityA - priorityB;
});
data.forEach(function(train) {
var trainDiv = createTrainDiv(train);
trainsContainer.append(trainDiv);
trainsContainer.append('<br>');
});
},
error: function() {
console.error('Error fetching train data');
}
});
}
$(document).ready(function() {
Object.values(carrierLogos).forEach(logoPath => {
const img = new Image();
img.src = logoPath;
});
$('#search').on('input', function() {
updateTrainTable();
});
});
setInterval(updateTrainTable, 3000);
updateTrainTable();