Registration and fuzzy search in progress

This commit is contained in:
Claude Brisson
2023-12-15 13:45:23 +01:00
parent 519eca8af3
commit ea44f6068e
26 changed files with 708 additions and 102 deletions

View File

@@ -105,7 +105,8 @@ Element.prototype.modal = function(show) {
/* DOM helpers */
HTMLFormElement.prototype.val = function(name) {
HTMLFormElement.prototype.val = function(name, value) {
let hasValue = typeof(value) !== 'undefined';
let ctl = this.find(`[name="${name}"]`)[0];
if (!ctl) {
console.error(`unknown input name: ${name}`)
@@ -113,15 +114,30 @@ HTMLFormElement.prototype.val = function(name) {
let tag = ctl.tagName;
let type = tag === 'INPUT' ? ctl.attr('type') : undefined;
if (
(tag === 'INPUT' && ['text', 'number'].includes(ctl.attr('type'))) ||
(tag === 'INPUT' && ['text', 'number', 'hidden'].includes(ctl.attr('type'))) ||
tag === 'SELECT'
) {
return ctl.value;
if (hasValue) {
ctl.value = value;
return;
}
else return ctl.value;
} else if (tag === 'INPUT' && ctl.attr('type') === 'radio') {
ctl = $(`input[name="${name}"]:checked`)[0];
if (ctl) return ctl.value;
if (hasValue) {
ctl = $(`input[name="${name}"][value="${value}"]`);
if (ctl) ctl.checked = true;
return;
} else {
ctl = $(`input[name="${name}"]:checked`);
if (ctl) return ctl[0].value;
else return null;
}
} else if (tag === 'INPUT' && ctl.attr('type') === 'checkbox') {
return ctl.checked;
if (hasValue) {
ctl.checked = value !== 'false' && Boolean(value);
return;
}
else return ctl.checked;
}
console.error(`unhandled input tag or type for input ${name} (tag: ${tag}, type:${type}`);
return null;
@@ -142,6 +158,11 @@ function modal(id) {
$(`#${id}.popup`).addClass('shown');
}
function close_modal() {
$('body').removeClass('dimmed');
$(`.popup`).removeClass('shown');
}
onLoad(() => {
$('button.close').on('click', e => {
let modal = e.target.closest('.popup');

View File

@@ -5,8 +5,23 @@ onLoad(() => {
min: 0,
max: 4000
});
new Tablesort($('#players')[0]);
$('#add').on('click', e => {
let form = $('#player-form')[0];
form.addClass('add');
// $('#player-form input.participation').forEach(chk => chk.checked = true);
form.reset();
modal('player');
});
$('#cancel-register').on('click', e => {
e.preventDefault();
close_modal();
return false;
});
$('#register').on('click', e => {
let form = e.target.closest('form');
let valid = true;
let required = ['name', 'firstname', 'country', 'club', 'rank', 'rating'];
for (let name of required) {
let ctl = form.find(`[name=${name}]`)[0];
@@ -29,15 +44,65 @@ onLoad(() => {
rating: form.val('rating'),
rank: form.val('rank'),
country: form.val('country'),
club: form.val('club')
club: form.val('club'),
skip: form.find('input.participation').map((input,i) => [i+1, input.checked]).filter(arr => !arr[1]).map(arr => arr[0])
}
console.log(player);
api.postJson(`tour/${tour_id}/part`, player)
if (form.hasClass('add')) {
api.postJson(`tour/${tour_id}/part`, player)
.then(player => {
console.log(player)
if (player !== 'error') {
window.location.reload();
}
});
} else {
let id = form.val('id');
player['id'] = id;
api.putJson(`tour/${tour_id}/part/${id}`, player)
.then(player => {
console.log(player)
if (player !== 'error') {
window.location.reload();
}
});
}
});
$('#players > tbody > tr').on('click', e => {
let id = e.target.closest('tr').attr('data-id');
api.getJson(`tour/${tour_id}/part/${id}`)
.then(player => {
console.log(player)
if (player !== 'error') {
window.location.reload();
let form = $('#player-form')[0];
form.val('id', player.id);
form.val('name', player.name);
form.val('firstname', player.firstname);
form.val('rating', player.rating);
form.val('rank', player.rank);
form.val('country', player.country);
form.val('club', player.club);
for (r = 1; r <= tour_rounds; ++r) {
form.val(`r${r}`, !(player.skip && player.skip.includes(r)));
}
form.removeClass('add');
modal('player');
}
});
});
$('#needle').on('input', e => {
let needle = $('#needle')[0].value;
if (needle && needle.length > 2) {
let form = $('#player-form')[0];
let search = {
needle: needle,
aga: form.val('aga'),
egf: form.val('egf'),
ffg: form.val('ffg')
}
api.postJson('search', search)
.then(result => {
console.log(result);
})
} else $('#search-result').addClass('hidden');
});
});