commit | author | age
|
f4a54f
|
1 |
"use strict"; |
JK |
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', |
|
7 |
// b: 'http://91.223.13.70/internetservice', |
|
8 |
}; |
|
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() { |
|
29 |
request.abort.bind(request) |
|
30 |
return Deferred(promise, request); |
|
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 |
}; |
|
73 |
request.open("GET", url, true); |
|
74 |
request.send(); |
|
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); |
|
111 |
} |
|
112 |
|
2b6454
|
113 |
|
JK |
114 |
/*********** |
|
115 |
* PARSING * |
|
116 |
***********/ |
|
117 |
|
ca42d3
|
118 |
function normalizeName(string) { |
JK |
119 |
return string.replace('.', '. ').replace(' ', ' '); |
|
120 |
} |
|
121 |
|
8b6250
|
122 |
function parseStatus(status) { |
JK |
123 |
switch(status.status) { |
|
124 |
case 'STOPPING': |
|
125 |
case 'PREDICTED': |
|
126 |
if(status.actualRelativeTime <= 0) |
|
127 |
return lang.boarding_sign; |
|
128 |
if(status.actualRelativeTime >= 60) |
|
129 |
return lang.time_minutes_prefix + Math.floor(status.actualRelativeTime / 60) + lang.time_minutes_suffix; |
|
130 |
return lang.time_seconds_prefix + status.actualRelativeTime + lang.time_seconds_suffix; |
|
131 |
case 'DEPARTED': |
|
132 |
return lang.time_minutes_ago_prefix + Math.floor(-status.actualRelativeTime / 60) + lang.time_minutes_ago_suffix; |
|
133 |
default: |
|
134 |
return status.mixedTime; |
|
135 |
} |
|
136 |
} |
|
137 |
|
|
138 |
function parseTime(date, time) { |
|
139 |
var result = new Date(date.getFullYear(), date.getMonth(), date.getDay()); |
|
140 |
var time_split = time.split(':'); |
|
141 |
result.setHours(time_split[0]); |
|
142 |
result.setMinutes(time_split[1]); |
|
143 |
|
|
144 |
if(result.getTime() - date.getTime() > 72000000) { |
|
145 |
result.setTime(result.getTime() - 86400000); |
|
146 |
} |
|
147 |
|
|
148 |
if(date.getTime() - result.getTime() > 72000000) { |
|
149 |
result.setTime(result.getTime() + 86400000); |
|
150 |
} |
|
151 |
|
|
152 |
return result; |
|
153 |
} |
|
154 |
|
|
155 |
function parseDelay(status) { |
|
156 |
if(!status.actualTime) return lang.unknown_sign; |
|
157 |
if(!status.plannedTime) return lang.unknown_sign; |
|
158 |
|
|
159 |
var now = new Date(); |
|
160 |
var actual = parseTime(now, status.actualTime); |
|
161 |
var planned = parseTime(now, status.plannedTime); |
|
162 |
|
|
163 |
return lang.time_minutes_prefix + ((actual.getTime() - planned.getTime()) / 1000 / 60) + lang.time_minutes_suffix; |
|
164 |
} |
|
165 |
|
68aeb4
|
166 |
function parseVehicle(vehicleId) { |
765604
|
167 |
if(!vehicleId) return false; |
f0bae0
|
168 |
if(!vehicles_info || !vehicles_info[vehicleId]) { |
765604
|
169 |
return false; |
68aeb4
|
170 |
} else { |
f0bae0
|
171 |
var vehicle = vehicles_info[vehicleId]; |
JK |
172 |
return { |
|
173 |
vehicleId: vehicleId, |
|
174 |
prefix: vehicle['num'].substr(0, 2), |
|
175 |
id: vehicle['num'].substr(2, 3), |
|
176 |
num: vehicle['num'], |
|
177 |
type: vehicle['type'], |
|
178 |
low: vehicle['low'] |
|
179 |
}; |
68aeb4
|
180 |
} |
f0bae0
|
181 |
} |
JK |
182 |
|
|
183 |
function updateVehicleInfo() { |
|
184 |
return $.get( |
|
185 |
'https://mpk.jacekk.net/vehicles/' |
|
186 |
).done(function(data) { |
|
187 |
vehicles_info = data; |
|
188 |
}); |
68aeb4
|
189 |
} |
JK |
190 |
|
f4a54f
|
191 |
function depotIdToVehicleId(depotId, typeHelper) { |
JK |
192 |
if(typeHelper) { |
|
193 |
for(var prop in vehicles_info) { |
d06aef
|
194 |
if(prop.substr(0,1) == typeHelper && vehicles_info[prop]['num'].substr(2) == depotId) { |
f4a54f
|
195 |
return prop; |
JK |
196 |
} |
|
197 |
} |
|
198 |
} else { |
|
199 |
for(var prop in vehicles_info) { |
|
200 |
if(vehicles_info[prop]['num'] == depotId) { |
|
201 |
return prop; |
|
202 |
} |
b16328
|
203 |
} |
7ca6a1
|
204 |
} |
JK |
205 |
} |
|
206 |
|
07c714
|
207 |
function displayVehicle(vehicleInfo) { |
JK |
208 |
if(!vehicleInfo) return document.createTextNode(''); |
|
209 |
|
|
210 |
var span = document.createElement('span'); |
|
211 |
span.className = 'vehicleInfo'; |
|
212 |
|
|
213 |
var floor_type = ''; |
125626
|
214 |
if(vehicleInfo.low == 0) { |
07c714
|
215 |
setText(span, lang.high_floor_sign); |
JK |
216 |
floor_type = lang.high_floor; |
125626
|
217 |
} else if(vehicleInfo.low == 1) { |
07c714
|
218 |
setText(span, lang.partially_low_floor_sign); |
JK |
219 |
floor_type = lang.partially_low_floor; |
125626
|
220 |
} else if(vehicleInfo.low == 2) { |
07c714
|
221 |
setText(span, lang.low_floor_sign); |
JK |
222 |
floor_type = lang.low_floor; |
|
223 |
} |
|
224 |
|
|
225 |
span.title = lang.tram_type_pattern |
|
226 |
.replace('$num', vehicleInfo.num) |
|
227 |
.replace('$type', vehicleInfo.type) |
|
228 |
.replace('$floor', floor_type); |
|
229 |
|
|
230 |
return span; |
|
231 |
} |
|
232 |
|
2b6454
|
233 |
|
JK |
234 |
/******* |
|
235 |
* DOM * |
|
236 |
*******/ |
|
237 |
|
68aeb4
|
238 |
function deleteChildren(element) { |
JK |
239 |
while(element.lastChild) element.removeChild(element.lastChild); |
|
240 |
} |
|
241 |
|
|
242 |
function addElementWithText(parent, element, text) { |
|
243 |
var elem = document.createElement(element); |
|
244 |
elem.appendChild(document.createTextNode(text)); |
|
245 |
parent.appendChild(elem); |
|
246 |
return elem; |
|
247 |
} |
|
248 |
|
|
249 |
function addCellWithText(parent, text) { |
|
250 |
return addElementWithText(parent, 'td', text); |
|
251 |
} |
|
252 |
|
|
253 |
function addParaWithText(parent, text) { |
|
254 |
return addElementWithText(parent, 'p', text); |
|
255 |
} |
|
256 |
|
|
257 |
function setText(element, text) { |
|
258 |
deleteChildren(element); |
|
259 |
element.appendChild(document.createTextNode(text)); |
|
260 |
} |