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