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