commit | author | age
|
68aeb4
|
1 |
// Special directions |
JK |
2 |
var special_directions = { |
c57bed
|
3 |
'Zajezdnia Nowa Huta' : 'ZH', |
JK |
4 |
'Zajezdnia Podgórze' : 'ZP', |
|
5 |
'Zjazd do zajezdni' : 'Z', |
|
6 |
'Wyjazd na linię' : 'W', |
|
7 |
'Przejazd techniczny' : 'PT', |
68aeb4
|
8 |
}; |
JK |
9 |
|
572390
|
10 |
var script_version; |
JK |
11 |
var script_version_xhr; |
|
12 |
|
b16328
|
13 |
var vehicles_info = {}; |
f0bae0
|
14 |
|
572390
|
15 |
// Check for website updates |
JK |
16 |
function checkVersion() { |
|
17 |
if(script_version_xhr) script_version_xhr.abort(); |
|
18 |
|
|
19 |
script_version_xhr = $.get( |
|
20 |
'version.php' |
|
21 |
).done(function(data) { |
|
22 |
if(!script_version) { |
|
23 |
script_version = data; |
|
24 |
return; |
|
25 |
} |
|
26 |
|
|
27 |
if(script_version != data) { |
|
28 |
fail(lang.error_new_version); |
|
29 |
location.reload(true); |
|
30 |
} |
|
31 |
}); |
|
32 |
} |
|
33 |
|
|
34 |
function checkVersionInit() { |
|
35 |
checkVersion(); |
|
36 |
setInterval(checkVersion, 3600000); |
|
37 |
} |
|
38 |
|
8b6250
|
39 |
/* Parsing of received JSON parts */ |
JK |
40 |
function parseStatus(status) { |
|
41 |
switch(status.status) { |
|
42 |
case 'STOPPING': |
|
43 |
case 'PREDICTED': |
|
44 |
if(status.actualRelativeTime <= 0) |
|
45 |
return lang.boarding_sign; |
|
46 |
if(status.actualRelativeTime >= 60) |
|
47 |
return lang.time_minutes_prefix + Math.floor(status.actualRelativeTime / 60) + lang.time_minutes_suffix; |
|
48 |
return lang.time_seconds_prefix + status.actualRelativeTime + lang.time_seconds_suffix; |
|
49 |
case 'DEPARTED': |
|
50 |
return lang.time_minutes_ago_prefix + Math.floor(-status.actualRelativeTime / 60) + lang.time_minutes_ago_suffix; |
|
51 |
default: |
|
52 |
return status.mixedTime; |
|
53 |
} |
|
54 |
} |
|
55 |
|
|
56 |
function parseTime(date, time) { |
|
57 |
var result = new Date(date.getFullYear(), date.getMonth(), date.getDay()); |
|
58 |
var time_split = time.split(':'); |
|
59 |
result.setHours(time_split[0]); |
|
60 |
result.setMinutes(time_split[1]); |
|
61 |
|
|
62 |
if(result.getTime() - date.getTime() > 72000000) { |
|
63 |
result.setTime(result.getTime() - 86400000); |
|
64 |
} |
|
65 |
|
|
66 |
if(date.getTime() - result.getTime() > 72000000) { |
|
67 |
result.setTime(result.getTime() + 86400000); |
|
68 |
} |
|
69 |
|
|
70 |
return result; |
|
71 |
} |
|
72 |
|
|
73 |
function parseDelay(status) { |
|
74 |
if(!status.actualTime) return lang.unknown_sign; |
|
75 |
if(!status.plannedTime) return lang.unknown_sign; |
|
76 |
|
|
77 |
var now = new Date(); |
|
78 |
var actual = parseTime(now, status.actualTime); |
|
79 |
var planned = parseTime(now, status.plannedTime); |
|
80 |
|
|
81 |
return lang.time_minutes_prefix + ((actual.getTime() - planned.getTime()) / 1000 / 60) + lang.time_minutes_suffix; |
|
82 |
} |
|
83 |
|
68aeb4
|
84 |
// Webservice-related functions |
JK |
85 |
function parseVehicle(vehicleId) { |
765604
|
86 |
if(!vehicleId) return false; |
f0bae0
|
87 |
if(!vehicles_info || !vehicles_info[vehicleId]) { |
765604
|
88 |
return false; |
68aeb4
|
89 |
} else { |
f0bae0
|
90 |
var vehicle = vehicles_info[vehicleId]; |
JK |
91 |
return { |
|
92 |
vehicleId: vehicleId, |
|
93 |
prefix: vehicle['num'].substr(0, 2), |
|
94 |
id: vehicle['num'].substr(2, 3), |
|
95 |
num: vehicle['num'], |
|
96 |
type: vehicle['type'], |
|
97 |
low: vehicle['low'] |
|
98 |
}; |
68aeb4
|
99 |
} |
f0bae0
|
100 |
} |
JK |
101 |
|
|
102 |
function updateVehicleInfo() { |
|
103 |
return $.get( |
|
104 |
'https://mpk.jacekk.net/vehicles/' |
|
105 |
).done(function(data) { |
|
106 |
vehicles_info = data; |
|
107 |
}); |
68aeb4
|
108 |
} |
JK |
109 |
|
7ca6a1
|
110 |
function tramIdToVehicleId(tramId) { |
b16328
|
111 |
for(var prop in vehicles_info) { |
JK |
112 |
if(vehicles_info[prop]['num'].substr(2) == tramId) { |
|
113 |
return prop; |
|
114 |
} |
7ca6a1
|
115 |
} |
JK |
116 |
} |
|
117 |
|
07c714
|
118 |
function displayVehicle(vehicleInfo) { |
JK |
119 |
if(!vehicleInfo) return document.createTextNode(''); |
|
120 |
|
|
121 |
var span = document.createElement('span'); |
|
122 |
span.className = 'vehicleInfo'; |
|
123 |
|
|
124 |
var floor_type = ''; |
f0bae0
|
125 |
if(vehicleInfo.low == '0') { |
07c714
|
126 |
setText(span, lang.high_floor_sign); |
JK |
127 |
floor_type = lang.high_floor; |
f0bae0
|
128 |
} else if(vehicleInfo.low == '1') { |
07c714
|
129 |
setText(span, lang.partially_low_floor_sign); |
JK |
130 |
floor_type = lang.partially_low_floor; |
f0bae0
|
131 |
} else if(vehicleInfo.low == '2') { |
07c714
|
132 |
setText(span, lang.low_floor_sign); |
JK |
133 |
floor_type = lang.low_floor; |
|
134 |
} |
|
135 |
|
|
136 |
span.title = lang.tram_type_pattern |
|
137 |
.replace('$num', vehicleInfo.num) |
|
138 |
.replace('$type', vehicleInfo.type) |
|
139 |
.replace('$floor', floor_type); |
|
140 |
|
|
141 |
return span; |
|
142 |
} |
|
143 |
|
68aeb4
|
144 |
// Element mangling |
JK |
145 |
function deleteChildren(element) { |
|
146 |
while(element.lastChild) element.removeChild(element.lastChild); |
|
147 |
} |
|
148 |
|
|
149 |
function addElementWithText(parent, element, text) { |
|
150 |
var elem = document.createElement(element); |
|
151 |
elem.appendChild(document.createTextNode(text)); |
|
152 |
parent.appendChild(elem); |
|
153 |
return elem; |
|
154 |
} |
|
155 |
|
|
156 |
function addCellWithText(parent, text) { |
|
157 |
return addElementWithText(parent, 'td', text); |
|
158 |
} |
|
159 |
|
|
160 |
function addParaWithText(parent, text) { |
|
161 |
return addElementWithText(parent, 'p', text); |
|
162 |
} |
|
163 |
|
|
164 |
function setText(element, text) { |
|
165 |
deleteChildren(element); |
|
166 |
element.appendChild(document.createTextNode(text)); |
|
167 |
} |