{% if feature_flag_service.featureEnabledForUser(
constant('App\\Service\\FeatureFlagService::FEATURE_SHOW_SELECT_ZIPCODE_OR_CITY'), app.user) %}
<script>
{{ inlineJsWrapperBegin() }}
var listElement;
var inputZipcodeId;
var inputCityId;
var inputZipcodeField;
const dropdownIcon = jQuery('#select-zipcode-or-city-dropdown-icon')
if (jQuery('#jobseeker_profile_zipcode').length > 0) {
inputZipcodeField = jQuery('#jobseeker_profile_zipcode');
listElement = jQuery('#select-zipcode-or-city2');
bindEvents();
}
inputZipcodeField = jQuery('#recurrent_jobs_search_parameters_filterZipcode');
inputZipcodeField = inputZipcodeField.length === 0 ? jQuery('#wanted_jobs_search_parameters_filterZipcode') : inputZipcodeField;
inputZipcodeField = inputZipcodeField.length === 0 ? jQuery('#jobseeker_profile_availabilityZipcode') : inputZipcodeField;
inputZipcodeField = inputZipcodeField.length === 0 ? jQuery('#jobofferer_profile_zipcode') : inputZipcodeField;
inputZipcodeField = inputZipcodeField.length === 0 ? jQuery('#recurrent_job_zipcode') : inputZipcodeField;
inputZipcodeField = inputZipcodeField.length === 0 ? jQuery('#recurrent-jobs-search-for-one-customer-zipcode') : inputZipcodeField
if (inputZipcodeField.length > 0) {
inputZipcodeId = '#' + inputZipcodeField[0].id;
}
inputCityId = jQuery('#jobofferer_profile_city').length > 0 ? jQuery('#jobofferer_profile_city') : null;
if (inputCityId === null && jQuery('#recurrent_job_city').length > 0) {
inputCityId = jQuery('#recurrent_job_city');
}
listElement = jQuery('#select-zipcode-or-city');
bindEvents();
jQuery('#jobseeker_profile_availabilityZipcode').focus(function () {
if (listElement[0].id === 'select-zipcode-or-city2') {
listElement.hide().empty();
listElement = jQuery('#select-zipcode-or-city');
inputCityId = null;
inputZipcodeField = jQuery(this);
if (inputZipcodeField.length > 0) {
inputZipcodeId = '#' + inputZipcodeField[0].id;
}
}
});
jQuery('#jobseeker_profile_zipcode').focus(function () {
if (listElement[0].id === 'select-zipcode-or-city') {
listElement.hide().empty();
listElement = jQuery('#select-zipcode-or-city2');
inputCityId = jQuery('#jobseeker_profile_city');
inputZipcodeField = jQuery(this);
if (inputZipcodeField.length > 0) {
inputZipcodeId = '#' + inputZipcodeField[0].id;
}
}
});
function checkKeyCodes(event) {
if (event.which === 38) { // ArrowUp
if (listElement.is(':visible')) {
return focusElement(event.which)
}
} else if (event.which === 40) { // ArrowDown
if (listElement.length > 0) {
listElement.show();
return focusElement(event.which)
}
} else if (event.which === 13 || event.which === 27) { // Escape und Return
listElement.hide();
return false;
}
return true;
}
function getZipcodes(filter, joboffererProfileId = null) {
jQuery.ajax({
url: joboffererProfileId !== null ? '{{ path('searchhelper.zipcode_or_city_for_limited_search.api') }}' : '{{ path('searchhelper.zipcode_or_city.api') }}',
method: 'post',
dataType: 'json',
data: joboffererProfileId !== null ? 'joboffererProfileId=' + joboffererProfileId :'filter=' + filter
}).done(function (response) {
listElement.empty();
response.forEach(function (item) {
if (item['city'] !== null && item['city'] !== undefined) {
listElement.append('<li tabindex="-1" zipcode="' + item['zipcode'] + '" city="' + item['city'] + '" class="list-group-item list-group-item-action py-1 tw-font-semibold">' + item['zipcode'] + ' ' + item['city'] + '</li>')
} else {
listElement.append('<li tabindex="-1" zipcode="' + item['zipcode'] + '" class="list-group-item list-group-item-action py-1 tw-font-semibold">' + item['zipcode'] + '</li>')
}
});
listElement.show();
jQuery(listElement.children()[0]).addClass('bg-darkblue');
})
}
function focusElement(direction) {
var element;
var activeElement = jQuery('#' + listElement[0].id + ' li.active');
if (activeElement.length === 0) {
element = listElement.children()[0];
} else {
element = direction === 40 ? activeElement[0].nextElementSibling : activeElement[0].previousElementSibling;
activeElement.removeClass('active');
}
jQuery(element).focus().addClass('active');
if (element !== undefined) {
updateFieldValues(element['attributes']['zipcode']['nodeValue'], element['attributes']['city']['nodeValue']);
}
return false;
}
function updateFieldValues(zipcode, city) {
if (inputCityId && city !== null) {
inputCityId.focus().val(city);
}
inputZipcodeField.val(zipcode).focus();
}
function bindEvents() {
listElement.click(function (event) {
jQuery('#' + listElement[0].id + ' li.active').removeClass('active');
jQuery(event.target).addClass('active');
if ( event.target !== undefined
&& event.target !== null
&& event.target['attributes'] !== undefined
&& event.target['attributes'] !== null
&& event.target['attributes']['zipcode'] !== undefined
&& event.target['attributes']['zipcode'] !== null
&& event.target['attributes']['zipcode']['nodeValue'] !== undefined
&& event.target['attributes']['zipcode']['nodeValue'] !== null
&& event.target['attributes']['city'] !== undefined
&& event.target['attributes']['city'] !== null
&& event.target['attributes']['city']['nodeValue'] !== undefined
&& event.target['attributes']['city']['nodeValue'] !== null
) {
updateFieldValues(event.target['attributes']['zipcode']['nodeValue'], event.target['attributes']['city']['nodeValue']);
} else if(
event.target !== undefined
&& event.target !== null
&& event.target['attributes'] !== undefined
&& event.target['attributes'] !== null
&& event.target['attributes']['zipcode'] !== undefined
&& event.target['attributes']['zipcode'] !== null
&& event.target['attributes']['zipcode']['nodeValue'] !== undefined
&& event.target['attributes']['zipcode']['nodeValue'] !== null
) {
updateFieldValues(event.target['attributes']['zipcode']['nodeValue'], null);
}
jQuery(this).hide();
});
listElement.mouseover(function () {
jQuery(this).css('cursor', 'pointer');
});
const clickHandler = function (){
let joboffererProfileId = null;
if (listElement.data('jobofferer-profile-id')) {
joboffererProfileId = listElement.data('jobofferer-profile-id');
joboffererProfileId = joboffererProfileId !== undefined ? joboffererProfileId : null;
}
listElement.hide().empty();
getZipcodes('', joboffererProfileId);
}
if (inputZipcodeField.attr('readonly')) {
inputZipcodeField.click(clickHandler);
}
dropdownIcon.click(clickHandler);
inputZipcodeField.keydown(function (event) {
var element;
// Key-Codes von 48 - 57 sind die Ziffern von 0-9
if ((event.which >= 48 && event.which <= 57) && jQuery('#restriced-wanted-jobs-search').length > 0) {
jQuery('#zipcode-info-box').modal('toggle');
}
if ((event.which === 9 || event.which === 13) && listElement.is(':visible')) { //Tab and Return
event.preventDefault();
element = jQuery('#' + listElement[0].id + ' li.active');
if (element !== undefined && element[0] !== undefined) {
updateFieldValues(element[0]['attributes']['zipcode']['nodeValue'], element[0]['attributes']['city']['nodeValue']);
}
listElement.hide();
}
});
inputZipcodeField.keyup(function (event) {
var filter = jQuery(this).val();
setTimeout(function () {
if (filter === jQuery(inputZipcodeId).val() && filter.length > 2) {
if (checkKeyCodes(event)) {
getZipcodes(filter);
}
} else if (filter.length < 3) {
listElement.hide();
} else if (listElement.is(':visible')) {
checkKeyCodes(event);
}
}, 150);
});
}
{{ inlineJsWrapperEnd() }}
</script>
{% endif %}