commit | author | age
|
7e7221
|
1 |
'use strict'; |
f4a54f
|
2 |
|
4bfa36
|
3 |
var ttss_urls = { |
JK |
4 |
t: 'proxy_tram.php', |
|
5 |
// t: 'http://www.ttss.krakow.pl/internetservice', |
|
6 |
b: 'proxy_bus.php', |
bb78f4
|
7 |
// b: 'http://ttss.mpk.krakow.pl/internetservice', |
4bfa36
|
8 |
}; |
JK |
9 |
var ttss_types = ['t', 'b']; |
cbce8b
|
10 |
|
68aeb4
|
11 |
var special_directions = { |
c57bed
|
12 |
'Zajezdnia Nowa Huta' : 'ZH', |
JK |
13 |
'Zajezdnia Podgórze' : 'ZP', |
|
14 |
'Zjazd do zajezdni' : 'Z', |
|
15 |
'Wyjazd na linię' : 'W', |
|
16 |
'Przejazd techniczny' : 'PT', |
68aeb4
|
17 |
}; |
JK |
18 |
|
2b6454
|
19 |
|
JK |
20 |
/******** |
|
21 |
* AJAX * |
|
22 |
********/ |
|
23 |
|
|
24 |
function Deferred(promise, request) { |
|
25 |
return { |
|
26 |
promise: promise, |
|
27 |
request: request, |
|
28 |
abort: function() { |
7e7221
|
29 |
request.abort.bind(request); |
2b6454
|
30 |
return Deferred(promise, request); |
JK |
31 |
}, |
|
32 |
done: function(func) { |
|
33 |
return Deferred(promise.then(func), request); |
|
34 |
}, |
|
35 |
fail: function(func) { |
|
36 |
return Deferred(promise.catch(func), request); |
|
37 |
}, |
|
38 |
always: function(func) { |
|
39 |
return Deferred(promise.finally(func), request); |
|
40 |
}, |
|
41 |
}; |
|
42 |
} |
|
43 |
|
|
44 |
Deferred.all = function(iterable) { |
|
45 |
return Deferred( |
|
46 |
Promise.all( |
|
47 |
iterable.map(x => x.promise) |
|
48 |
) |
|
49 |
); |
|
50 |
}; |
|
51 |
|
|
52 |
var $ = { |
|
53 |
timeout: 10000, |
|
54 |
dataType: 'json', |
|
55 |
get: function(url) { |
|
56 |
var self = this; |
|
57 |
var request = new XMLHttpRequest(); |
|
58 |
var promise = new Promise(function(resolve, reject) { |
|
59 |
request.timeout = self.timeout; |
|
60 |
request.onreadystatechange = function() { |
|
61 |
if(this.readyState == 4) { |
|
62 |
if(this.status == 200) { |
|
63 |
if(self.dataType == 'json') { |
|
64 |
resolve(JSON.parse(this.responseText)); |
|
65 |
} else { |
|
66 |
resolve(this.responseText); |
|
67 |
} |
|
68 |
} else { |
|
69 |
reject(request); |
|
70 |
} |
|
71 |
} |
|
72 |
}; |
7e7221
|
73 |
request.open('GET', url, true); |
2b6454
|
74 |
request.send(); |
JK |
75 |
}); |
|
76 |
return Deferred(promise, request); |
|
77 |
}, |
|
78 |
}; |
|
79 |
|
|
80 |
|
|
81 |
/*********** |
|
82 |
* VERSION * |
|
83 |
***********/ |
|
84 |
|
572390
|
85 |
var script_version; |
JK |
86 |
var script_version_xhr; |
|
87 |
|
b16328
|
88 |
var vehicles_info = {}; |
f0bae0
|
89 |
|
572390
|
90 |
function checkVersion() { |
JK |
91 |
if(script_version_xhr) script_version_xhr.abort(); |
|
92 |
|
|
93 |
script_version_xhr = $.get( |
|
94 |
'version.php' |
|
95 |
).done(function(data) { |
|
96 |
if(!script_version) { |
|
97 |
script_version = data; |
|
98 |
return; |
|
99 |
} |
|
100 |
|
|
101 |
if(script_version != data) { |
|
102 |
fail(lang.error_new_version); |
|
103 |
location.reload(true); |
|
104 |
} |
|
105 |
}); |
|
106 |
} |
|
107 |
|
|
108 |
function checkVersionInit() { |
|
109 |
checkVersion(); |
|
110 |
setInterval(checkVersion, 3600000); |
7e7221
|
111 |
} |
JK |
112 |
|
|
113 |
|
|
114 |
/******* |
|
115 |
* DOM * |
|
116 |
*******/ |
|
117 |
|
|
118 |
function deleteChildren(element) { |
|
119 |
while(element.lastChild) element.removeChild(element.lastChild); |
|
120 |
} |
|
121 |
|
|
122 |
function addElementWithText(parent, element, text) { |
|
123 |
var elem = document.createElement(element); |
|
124 |
elem.appendChild(document.createTextNode(text)); |
|
125 |
parent.appendChild(elem); |
|
126 |
return elem; |
|
127 |
} |
|
128 |
|
|
129 |
function addCellWithText(parent, text) { |
|
130 |
return addElementWithText(parent, 'td', text); |
|
131 |
} |
|
132 |
|
|
133 |
function addParaWithText(parent, text) { |
|
134 |
return addElementWithText(parent, 'p', text); |
|
135 |
} |
|
136 |
|
|
137 |
function setText(element, text) { |
|
138 |
deleteChildren(element); |
|
139 |
element.appendChild(document.createTextNode(text)); |
572390
|
140 |
} |
JK |
141 |
|
2b6454
|
142 |
|
JK |
143 |
/*********** |
|
144 |
* PARSING * |
|
145 |
***********/ |
|
146 |
|
ca42d3
|
147 |
function normalizeName(string) { |
JK |
148 |
return string.replace('.', '. ').replace(' ', ' '); |
|
149 |
} |
|
150 |
|
8b6250
|
151 |
function parseStatus(status) { |
JK |
152 |
switch(status.status) { |
|
153 |
case 'STOPPING': |
|
154 |
case 'PREDICTED': |
|
155 |
if(status.actualRelativeTime <= 0) |
|
156 |
return lang.boarding_sign; |
|
157 |
if(status.actualRelativeTime >= 60) |
|
158 |
return lang.time_minutes_prefix + Math.floor(status.actualRelativeTime / 60) + lang.time_minutes_suffix; |
|
159 |
return lang.time_seconds_prefix + status.actualRelativeTime + lang.time_seconds_suffix; |
|
160 |
case 'DEPARTED': |
|
161 |
return lang.time_minutes_ago_prefix + Math.floor(-status.actualRelativeTime / 60) + lang.time_minutes_ago_suffix; |
|
162 |
default: |
|
163 |
return status.mixedTime; |
|
164 |
} |
|
165 |
} |
|
166 |
|
|
167 |
function parseTime(date, time) { |
|
168 |
var result = new Date(date.getFullYear(), date.getMonth(), date.getDay()); |
|
169 |
var time_split = time.split(':'); |
|
170 |
result.setHours(time_split[0]); |
|
171 |
result.setMinutes(time_split[1]); |
|
172 |
|
|
173 |
if(result.getTime() - date.getTime() > 72000000) { |
|
174 |
result.setTime(result.getTime() - 86400000); |
|
175 |
} |
|
176 |
|
|
177 |
if(date.getTime() - result.getTime() > 72000000) { |
|
178 |
result.setTime(result.getTime() + 86400000); |
|
179 |
} |
|
180 |
|
|
181 |
return result; |
|
182 |
} |
|
183 |
|
|
184 |
function parseDelay(status) { |
|
185 |
if(!status.actualTime) return lang.unknown_sign; |
|
186 |
if(!status.plannedTime) return lang.unknown_sign; |
|
187 |
|
|
188 |
var now = new Date(); |
|
189 |
var actual = parseTime(now, status.actualTime); |
|
190 |
var planned = parseTime(now, status.plannedTime); |
|
191 |
|
|
192 |
return lang.time_minutes_prefix + ((actual.getTime() - planned.getTime()) / 1000 / 60) + lang.time_minutes_suffix; |
|
193 |
} |
|
194 |
|
68aeb4
|
195 |
function parseVehicle(vehicleId) { |
765604
|
196 |
if(!vehicleId) return false; |
f0bae0
|
197 |
if(!vehicles_info || !vehicles_info[vehicleId]) { |
765604
|
198 |
return false; |
68aeb4
|
199 |
} else { |
f0bae0
|
200 |
var vehicle = vehicles_info[vehicleId]; |
JK |
201 |
return { |
|
202 |
vehicleId: vehicleId, |
|
203 |
prefix: vehicle['num'].substr(0, 2), |
|
204 |
id: vehicle['num'].substr(2, 3), |
|
205 |
num: vehicle['num'], |
|
206 |
type: vehicle['type'], |
|
207 |
low: vehicle['low'] |
|
208 |
}; |
68aeb4
|
209 |
} |
f0bae0
|
210 |
} |
JK |
211 |
|
|
212 |
function updateVehicleInfo() { |
|
213 |
return $.get( |
|
214 |
'https://mpk.jacekk.net/vehicles/' |
|
215 |
).done(function(data) { |
|
216 |
vehicles_info = data; |
|
217 |
}); |
68aeb4
|
218 |
} |
JK |
219 |
|
f4a54f
|
220 |
function depotIdToVehicleId(depotId, typeHelper) { |
7e7221
|
221 |
var prop; |
f4a54f
|
222 |
if(typeHelper) { |
7e7221
|
223 |
for(prop in vehicles_info) { |
d06aef
|
224 |
if(prop.substr(0,1) == typeHelper && vehicles_info[prop]['num'].substr(2) == depotId) { |
f4a54f
|
225 |
return prop; |
JK |
226 |
} |
|
227 |
} |
|
228 |
} else { |
7e7221
|
229 |
for(prop in vehicles_info) { |
f4a54f
|
230 |
if(vehicles_info[prop]['num'] == depotId) { |
JK |
231 |
return prop; |
|
232 |
} |
b16328
|
233 |
} |
7ca6a1
|
234 |
} |
JK |
235 |
} |
|
236 |
|
07c714
|
237 |
function displayVehicle(vehicleInfo) { |
JK |
238 |
if(!vehicleInfo) return document.createTextNode(''); |
|
239 |
|
|
240 |
var span = document.createElement('span'); |
|
241 |
span.className = 'vehicleInfo'; |
|
242 |
|
|
243 |
var floor_type = ''; |
125626
|
244 |
if(vehicleInfo.low == 0) { |
07c714
|
245 |
setText(span, lang.high_floor_sign); |
JK |
246 |
floor_type = lang.high_floor; |
125626
|
247 |
} else if(vehicleInfo.low == 1) { |
07c714
|
248 |
setText(span, lang.partially_low_floor_sign); |
JK |
249 |
floor_type = lang.partially_low_floor; |
125626
|
250 |
} else if(vehicleInfo.low == 2) { |
07c714
|
251 |
setText(span, lang.low_floor_sign); |
JK |
252 |
floor_type = lang.low_floor; |
|
253 |
} |
|
254 |
|
|
255 |
span.title = lang.tram_type_pattern |
|
256 |
.replace('$num', vehicleInfo.num) |
|
257 |
.replace('$type', vehicleInfo.type) |
|
258 |
.replace('$floor', floor_type); |
|
259 |
|
|
260 |
return span; |
68aeb4
|
261 |
} |