Select Git revision
-
Martin Mareš authored
Účastník ve stavech registered a refused má omezená práva
Martin Mareš authoredÚčastník ve stavech registered a refused má omezená práva
osmo.js 3.88 KiB
/*
* JavaScriptové funkce pro OSMO
*/
'use strict';
let osmo_api_root = undefined;
/*** Výběr škol ***/
class OSMOSchool {
constructor() {
this.prefill_town_query = undefined;
this.prefill_town_list = undefined;
this.prefill_school = undefined;
}
find_town_error(msg) {
document.getElementById('town_query-group').classList.add('has-error');
document.getElementById('town_query-help').innerText = msg;
}
async do_find_town(query) {
let resp = undefined;
try {
resp = await fetch(osmo_api_root + 'find-town?q=' + encodeURIComponent(query));
} catch (err) {
console.log('OSMO: Search failed: ' + err);
this.find_town_error('Spojení se serverem selhalo.');
return;
}
if (resp.status !== 200) {
console.log('OSMO: Search status: ' + resp.status);
this.find_town_error('Spojení se serverem selhalo.');
return;
}
const ans = await resp.json();
if (ans.error !== undefined) {
this.find_town_error(ans.error);
return;
}
const list = document.getElementById('town_list');
const opts = list.options;
opts.length = 0;
opts.add(new Option('Vyberte obec ze seznamu', ""));
for (const t of ans.found) {
opts.add(new Option(t[1], t[0]));
}
document.getElementById('town_list-div').style.display = 'block';
document.getElementById('town_query-help').innerText = ans.msg
if (this.prefill_town_list !== undefined) {
list.value = this.prefill_town_list;
this.prefill_town_list = undefined;
this.town_picked();
} else if (ans.found.length == 1) {
list.selectedIndex = 1;
this.town_picked();
}
}
find_town(during_init) {
const query = document.getElementById('town_query').value;
document.getElementById('town_list-div').style.display = 'none';
document.getElementById('school-div').style.display = 'none';
document.getElementById('town_query-help').innerText = 'Hledám...';
if (!during_init) {
document.getElementById('town_query-group').classList.remove('has-error');
}
this.do_find_town(query);
}
town_keydown(event) {
if (event.key === 'Enter') {
event.preventDefault();
this.find_town(false);
}
}
async do_get_schools(town_id) {
let resp = undefined;
try {
resp = await fetch(osmo_api_root + 'get-schools?town=' + encodeURIComponent(town_id));
} catch (err) {
console.log('OSMO: Search failed: ' + err);
return;
}
if (resp.status !== 200) {
console.log('OSMO: Search status: ' + resp.status);
return;
}
const ans = await resp.json();
const list = document.getElementById('school');
const opts = list.options;
list.replaceChildren();
if (ans.zs.length > 0 || ans.ss.length > 0) {
opts.add(new Option('Vyberte školu ze seznamu', ""));
if (ans.zs.length > 0) {
const g = document.createElement('optgroup');
g.label = 'Základní školy'
for (const s of ans.zs) {
g.append(new Option(s.name, '#' + s.id));
}
opts.add(g);
}
if (ans.ss.length > 0) {
const g = document.createElement('optgroup');
g.label = 'Střední školy'
for (const s of ans.ss) {
g.append(new Option(s.name, '#' + s.id));
}
opts.add(g);
}
} else {
opts.add(new Option('V této obci nejsou žádné školy.', ""));
}
document.getElementById('school-div').style.display = 'block';
if (this.prefill_school !== undefined) {
list.value = this.prefill_school;
this.prefill_school = undefined;
}
}
town_picked() {
const town_id = document.getElementById('town_list').value;
document.getElementById('school-div').style.display = 'none';
if (town_id != "") {
this.do_get_schools(town_id);
}
}
init() {
console.log('OSMO: Init schools');
var tq = document.getElementById('town_query');
tq.addEventListener('keydown', (event) => { this.town_keydown(event); });
if (this.prefill_town_query !== undefined) {
tq.value = this.prefill_town_query;
this.prefill_town_query = undefined;
this.find_town(true);
}
}
}