{# JS for the shared teacher fields partial.
# Param: country_selector = jQuery selector of a country <select> to gate the
# school autocomplete dynamically (optional). When omitted, the initial
# CZ visibility comes from the wrapper's data-cz flag. #}
<script nonce="{{ app_nonce() }}">
$(function () {
const $school = $('#teacher_school');
const $schoolWrapper = $('[data-school-wrapper]');
const $schoolTypeOther = $('[data-school-type-other]');
const $schoolTypeOtherInput = $('#teacher_school_type_other');
const countrySelector = '{{ country_selector|default('')|e('js') }}';
const $country = countrySelector ? $(countrySelector) : $();
function selectedKind () {
return $('input[name="teacher_school_type"]:checked').val() || '';
}
function isOtherSelected () {
return selectedKind() === 'other';
}
function isCz () {
if ($country.length) {
return ($country.val() || '').toUpperCase() === 'CZ';
}
return String($schoolWrapper.data('cz')) === '1';
}
if ($school.length && $.fn.select2) {
$school.select2({
placeholder: '{{ 'register.school.placeholder'|trans|e('js') }}',
allowClear: true,
width: '100%',
minimumInputLength: 2,
ajax: {
url: $school.data('schools-url'),
dataType: 'json',
delay: 200,
cache: true,
data: function (params) {
return {
q: params.term || '',
kind: selectedKind()
};
},
processResults: function (data) {
return { results: data.results || [] };
}
},
templateResult: function (item) {
if (!item.id) { return item.text; }
const name = item.name || item.text;
const addr = item.address || '';
const $row = $('<div class="register-school__item"></div>');
$row.append($('<div class="register-school__name"></div>').text(name));
if (addr) {
$row.append($('<div class="register-school__addr"></div>').text(addr));
}
return $row;
},
templateSelection: function (item) {
return item.name || item.text || '';
},
language: {
inputTooShort: function (args) {
const n = args.minimum - args.input.length;
if ('{{ app.request.locale }}' === 'cs') {
return 'Zadejte ještě ' + n + ' znak' + (n === 1 ? '' : (n < 5 ? 'y' : 'ů'));
}
return 'Please enter ' + n + ' more character' + (n === 1 ? '' : 's');
},
noResults: function () { return '{{ 'register.school.noresults'|trans|e('js') }}'; },
searching: function () { return '{{ 'register.school.searching'|trans|e('js') }}'; },
errorLoading: function () { return '{{ 'register.school.error'|trans|e('js') }}'; }
}
});
}
// Visibility only - safe to call on init without wiping a prefilled school.
function applyVisibility () {
const cz = isCz();
const $czOptions = $('[data-country="cz"]');
const $otherOptions = $('[data-country="other"]');
if (cz) {
$czOptions.show();
$czOptions.find('input').prop('disabled', false);
$otherOptions.hide();
$otherOptions.find('input').prop('disabled', true).prop('checked', false);
} else {
$czOptions.hide();
$czOptions.find('input').prop('disabled', true).prop('checked', false);
$otherOptions.show();
$otherOptions.find('input').prop('disabled', false);
}
const other = isOtherSelected();
$schoolWrapper.prop('hidden', !cz || other);
$schoolTypeOther.prop('hidden', !other);
}
// User-driven change: also reset the previously selected school so the kind
// filter takes effect and clear the "other" text when it no longer applies.
function onUserChange () {
applyVisibility();
if (isOtherSelected()) {
$schoolTypeOtherInput.trigger('focus');
} else {
$schoolTypeOtherInput.val('');
}
if ($school.length) {
$school.val(null).trigger('change.select2');
}
}
if ($country.length) {
$country.on('change', onUserChange);
}
$(document).on('change', 'input[name="teacher_school_type"]', onUserChange);
applyVisibility();
});
</script>