This repository has been archived by the owner on May 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
countries.js
executable file
·382 lines (308 loc) · 10.4 KB
/
countries.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// init
var params;
function init() {
var url = new URLSearchParams(window.location.search);
params = {};
if (url.has('order')) { params.order = url.get('order'); }
if (url.has('text')) { params.text = url.get('text'); document.getElementById('search').value = params.text; }
if (url.has('date')) { params.date = url.get('date'); document.getElementById('date').value = params.date; }
if (url.has('page')) { params.page = +url.get('page'); }
if (url.get('vw')) vw(); // for fun
updateSortIndex();
updatePagesView();
updateCountries();
}
init();
// HTTP
function formatParams(params, prefix = '', filter = true) {
var combined = [];
for (var key in params) {
// filter out vw (vw is for fun)
if ((filter && key !== 'vw') || !filter) {
if (params[key]) {
combined.push(encodeURIComponent(prefix + key) + '=' + encodeURIComponent(params[key]));
}
}
}
return combined.join('&');
}
function request(type, url, params, callback) {
var http = new XMLHttpRequest();
const _url = params ? url + '?' + formatParams(params) : url;
http.open(type, _url);
http.responseType = 'json';
http.onload = function() {
// console.log({type, url: _url, status: http.status, response: http.response});
callback(http.status, http.response);
};
http.send();
};
// Countries
function updateCountries() {
request(
'GET',
'https://akademija.teltonika.lt/api2/countries',
params,
(status, response) => {
if (status !== 200) {
pushNotification('Nepavyko gauti šalių');
// console.log(response);
return;
}
var countries = response.countires; // count i res ??
updatePageForwardView(response.count);
updateCountriesTable(countries);
}
);
}
function updateCountriesTable(countries) {
var table = document.getElementById('countriesTable');
while (table.rows.length > 1) {
table.deleteRow(1);
}
// var placeholder = document.getElementById('noElements');
// placeholder.hidden = countries.length > 0;
if (countries.length == 0) {
var row = table.insertRow();
var cell = row.insertCell(0);
// cell.classList.add('noElements');
cell.innerText = 'Šalių nėra.';
cell.colSpan = 5;
}
for (var i in countries) {
var row = table.insertRow();
var name = row.insertCell(0)
name.classList.add('nameCell');
name.innerHTML =
'<a class="city-link" href="cities.html?id=' + countries[i].id + '&name=' + countries[i].name + '&' + formatParams(params, 'country_', false) + '">' +
countries[i].name +
'</a>';
row.insertCell(1).innerText = countries[i].area + ' km²';
row.insertCell(2).innerText = countries[i].population;
row.insertCell(3).innerText = countries[i].calling_code;
var actions = row.insertCell(4);
actions.innerHTML = `
<button class="icon-button" onclick='editCountryDialog(` + JSON.stringify(countries[i]) + `)'>
<img src="assets/edit.svg">
</button>
<button class="icon-button" onclick='deleteCountryDialog(` + JSON.stringify(countries[i]) + `)'>
<img src="assets/delete.svg">
</button>
`;
}
}
// Sorting
function sortChanged() {
// Tri-state order
params.order = (
!params.order ?
'asc' :
(params.order == 'asc' ? 'desc' : null)
);
updateSortIndex();
updateCountries();
}
function updateSortIndex() {
document.getElementById('sortingIndex').innerText = (params.order ? (params.order === 'asc' ? 'A-Z' : 'Z-A') : '');
}
// Filter
function searchChanged() {
var search = document.getElementById('search');
params.text = search ? search.value : '';
if (search && !params.vw && search.value.toLowerCase() === 'vaporwave')
{
vw();
pushNotification('(ノ◕ヮ◕)ノ*:・゚✧');
}
filterChanged();
}
function dateChanged() {
var date = document.getElementById('date');
params.date = date ? date.value : '';
filterChanged();
}
function filterChanged() {
params.page = 1;
updatePagesView();
updateCountries();
}
// Paginator
function changePage(addPages) {
params.page = (
params.page ?
params.page + addPages :
(
addPages > 0 ? 2 : 1
)
);
updatePagesView();
updateCountries();
}
function updatePagesView() {
if (!params.page) params.page = 1;
document.getElementById('pageBack').disabled = params.page <= 1;
document.getElementById('page').innerText = params.page;
}
function updatePageForwardView(elementsCount) {
var pageForward = document.getElementById('pageForward');
if (elementsCount < 10) {
pageForward.disabled = true;
return;
}
var temp = {...params};
temp.page = temp.page ? temp.page+1 : 2;
request(
'GET',
'https://akademija.teltonika.lt/api2/countries',
temp,
(status, response) => {
if (status !== 200) {
// console.log('Error');
return;
}
pageForward.disabled = response.count <= 0;
}
);
}
// Notification
function pushNotification(message) {
var area = document.getElementById('notifications-area');
var notification = document.createElement('div');
var fadeout = function() {
notification.style.opacity = 0;
setTimeout(() => { notification.remove(); }, 333);
};
notification.classList.add('notification');
notification.innerText = message;
notification.onclick = fadeout;
area.appendChild(notification);
setTimeout(fadeout, 3000);
}
// Dialog
function newCountryDialog() { showDialog(); }
function editCountryDialog(country) { showDialog(true, country); }
function deleteCountryDialog(country) {
var area = document.getElementById('dialog-area');
if (area.classList.contains('active')) { return; }
area.classList.add('active');
area.classList.add('delete');
var window = document.getElementById('dialog-window');
window.innerHTML = `
<div class="bar">
<span id="title">Ar tikrai norite ištrinti?</span>
</div>
<form onsubmit="dialogSubmit(); return false">
<div class="dialog-buttons">
<button type="button" onclick="hideDialog()">Uždaryti</button>
<button type="submit" class="delete" id="submit">Ištrinti</button>
</div>
</form>
`;
document.getElementById('title').innerText = 'Ar tikrai norite ištrinti "' + country.name +'" ?';
dialogSubmit = function() {
request(
'DELETE',
'https://akademija.teltonika.lt/api2/countries/' + country.id,
null,
function (status, response) {
hideDialog();
if (status !== 200) {
pushNotification('Nepavyko ištrinti šalies');
return;
}
pushNotification('Šalis sėkmingai ištrinta');
updateCountries();
}
);
};
}
var dialogSubmit;
function showDialog(isEdit = false, country = null) {
var area = document.getElementById('dialog-area');
if (area.classList.contains('active')) { return; }
area.classList.add('active');
area.classList.remove('delete');
var window = document.getElementById('dialog-window');
window.innerHTML = `
<div class="bar">
<span id="title">Nauja šalis</span>
</div>
<form onsubmit="dialogSubmit(); return false">
<div>
<span class="title">Pavadinimas</span>
<input required minlength="3" maxlength="50" pattern="[A-Za-zĄČĄĘĖĮŠŲŪŽąččęėęįšųūž ]+" id="name" type="text" placeholder="Lietuva">
</div>
<div>
<span class="title">Užimamas plotas, km²</span>
<input required min="0" step="0.001" id="area" type="number" placeholder="65300">
</div>
<div>
<span class="title">Gyventojų skaičius</span>
<input required min="1" id="population" type="number" placeholder="2795000">
</div>
<div>
<span class="title">Šalies tel. kodas</span>
<input required min="0" max="9999" id="callingCode" type="number" placeholder="370">
</div>
<div class="dialog-buttons">
<button type="button" onclick="hideDialog()">Uždaryti</button>
<button type="submit" id="submit">Sukurti</button>
</div>
</form>
`;
if (isEdit) {
document.getElementById('title').innerText = 'Redaguoti šalį';
document.getElementById('submit').innerText = 'Išsaugoti';
document.getElementById('name').value = country.name;
document.getElementById('area').value = country.area;
document.getElementById('population').value = country.population;
document.getElementById('callingCode').value = +country.calling_code;
}
dialogSubmit = function() {
// console.log('');
var name = document.getElementById('name');
var area = document.getElementById('area');
var population = document.getElementById('population');
var callingCode = document.getElementById('callingCode');
request(
(isEdit) ? 'PUT' : 'POST',
'https://akademija.teltonika.lt/api2/countries' + (isEdit ? '/' + country.id : ''),
{
name: name.value,
area: area.value,
population: population.value,
calling_code: '+' + (+callingCode.value).toString()
},
function (status, response) {
hideDialog();
if (status !== 200) {
pushNotification(isEdit ? 'Nepavyko išsaugoti šalies' : 'Nepavyko sukurti šalies');
return;
}
pushNotification(isEdit ? 'Šalis sėkmingai išsaugota' : 'Šalis sėkmingai sukurta');
updateCountries();
}
);
};
}
function hideDialog() {
var area = document.getElementById('dialog-area');
area.classList.remove('active');
}
// purely for fun
function vw() {
params.vw = true;
var addVW = function(elements) {
for (var i = 0; i < elements.length; i++) {
elements[i].classList.add('vw');
}
};
// addVW(document.getElementsByTagName('body'));
addVW(document.getElementsByTagName('tbody'));
addVW(document.getElementsByClassName('bar'));
addVW(document.getElementsByClassName('sub-bar'));
addVW(document.getElementsByClassName('icon-button'));
addVW(document.getElementsByClassName('bigger-icon-button'));
addVW([document.getElementById('dialog-window')]);
addVW([document.getElementById('notifications-area')]);
}