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