-
Notifications
You must be signed in to change notification settings - Fork 399
/
Copy pathfunctions.js
250 lines (227 loc) · 5.76 KB
/
functions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// These can't live in openflights.js, because then we also have to load OpenLayers.min.js...
// Which is potentially unnecessary!
/**
* Validate 24-hr time ([0]0:00-23:59)
* @type {RegExp}
*/
const RE_TIME = /(^0?[0-9]|1[0-9]|2[0-3]):?([0-5][0-9])$/;
var gt;
var eliteicons;
window.onload = function init() {
gt = new Gettext({ domain: "messages" });
eliteicons = {
S: [gt.gettext("Silver Elite"), "/img/silver-star.png"],
G: [gt.gettext("Gold Elite"), "/img/gold-star.png"],
P: [gt.gettext("Platinum Elite"), "/img/platinum-star.png"],
X: [
gt.gettext("Thank you for using OpenFlights — please donate!"),
"/img/icon-warning.png",
],
};
};
/**
* User has changed locale, reload this page with new lang attribute
* (preserve any other attributes, but nuke anchors and overwrite existing lang if any)
*/
function changeLocale() {
var locale = "lang=" + document.getElementById("locale").value,
re_lang = /lang=...../,
url = location.origin + location.pathname + location.search; // omit #anchor
if (re_lang.test(url)) {
url = url.replace(re_lang, locale);
} else if (url.indexOf("?") == -1) {
url += "?" + locale;
} else {
url += "&" + locale;
}
location.href = url;
}
/**
* Check if DST is active
* @param type
* @param date
* @param year
* @returns {boolean}
*/
function checkDST(type, date, year) {
switch (type) {
case "E":
// Europe: Last Sunday in Mar to last Sunday in Oct
if (date >= getLastDay(year, 3, 0) && date < getLastDay(year, 10, 0)) {
return true;
}
break;
case "A":
// US/Canada: 2nd Sunday in Mar to 1st Sunday in Nov
if (
date >= getNthDay(year, 3, 2, 0) &&
date < getNthDay(year, 11, 1, 0)
) {
return true;
}
break;
case "S":
// South America: Until 3rd Sunday in Mar or after 3nd Sunday in Oct
if (
date < getNthDay(year, 3, 3, 0) ||
date >= getNthDay(year, 10, 3, 0)
) {
return true;
}
break;
case "O":
// Australia: Until 1st Sunday in April or after 1st Sunday in Oct
if (
date < getNthDay(year, 4, 1, 0) ||
date >= getNthDay(year, 10, 1, 0)
) {
return true;
}
break;
case "Z":
// New Zealand: Until 1st Sunday in April or after last Sunday in Sep
if (date < getNthDay(year, 4, 1, 0) || date >= getLastDay(year, 9, 0)) {
return true;
}
break;
default:
// cases U, N -- do nothing
}
return false;
}
/**
* Get Nth day of type X in a given month (e.g., third Sunday in March 2009)
* @param year
* @param month
* @param nth
* @param type 0 for Sun, 1 for Mon, etc
* @returns {Date}
*/
function getNthDay(year, month, nth, type) {
var date = new Date();
date.setFullYear(year, month - 1, 1); // Date object months start from 0
var day = date.getDay();
if (type >= day) {
nth--;
}
date.setDate(date.getDate() + (7 - (day - type)) + (nth - 1) * 7);
return date;
}
/**
* Get the last day of type X in a given month (e.g., last Sunday in March 2009)
* @param year
* @param month
* @param type
* @returns {Date}
*/
function getLastDay(year, month, type) {
var date = new Date();
date.setFullYear(year, month, 1); // Date object months start from 0, so this is +1
date.setDate(date.getDate() - 1); // last day of the previous month
date.setDate(date.getDate() - (date.getDay() - type));
return date;
}
/**
* Parse a time string into a float
* @param time_str string
* @returns {number}
*/
function parseTimeString(time_str) {
var chunks = time_str.match(RE_TIME);
return parseFloat(chunks[1]) + parseFloat(chunks[2] / 60);
}
/**
* @param element code:apid:x:y:tz:dst
* @returns {*}
*/
function getApid(element) {
return $(element + "id").value.split(":")[1];
}
/**
* @param element code:apid:x:y:tz:dst
* @returns {*}
*/
function getX(element) {
return $(element + "id").value.split(":")[2];
}
/**
* @param element code:apid:x:y:tz:dst
* @returns {*}
*/
function getY(element) {
return $(element + "id").value.split(":")[3];
}
/**
* @param element code:apid:x:y:tz:dst
* @returns {*}
*/
function getTZ(element) {
var tz = $(element + "id").value.split(":")[4];
return !tz || tz == "" ? 0 : parseFloat(tz);
}
/**
* @param element code:apid:x:y:tz:dst
* @returns {*}
*/
function getDST(element) {
var dst = $(element + "id").value.split(":")[5];
return !dst || dst == "" ? "N" : dst;
}
/**
* Return HTML string representing user's elite status icon.
* If validity is not null, also return text description and validity period.
* @param e {string}
* @param validity {string|null}
* @returns {string}
*/
function getEliteIcon(e, validity = "") {
if (
!e ||
e === "" ||
eliteicons === undefined ||
eliteicons[e] === undefined
) {
return "";
}
const icon = eliteicons[e];
if (validity) {
return (
// TODO: Add alt tags
"<center><img src='" +
icon[1] +
"' title='" +
icon[0] +
"' height=34 width=34 /><br><b>" +
icon[0] +
"</b><br><small>" +
gt.gettext("Valid until") +
"<br>" +
validity +
"</small></center>"
);
}
return (
// TODO: Add alt tags
"<span style='float: right'><a href='/donate' target='_blank'><img src='" +
icon[1] +
"' title='" +
icon[0] +
"' height=34 width=34></a></span>"
);
}
/**
* Given element "select"; select option matching "value" or #0 if not found
* @param select
* @param value
*/
function selectInSelect(select, value) {
if (!select) {
return;
}
select.selectedIndex = 0; // default element to not be selected
for (var index = 0; index < select.length; index++) {
if (select[index].value == value) {
select.selectedIndex = index;
}
}
}