templates/_includes/selectZipcodeOrCity.html.twig line 1

Open in your IDE?
  1. {% if feature_flag_service.featureEnabledForUser(
  2. constant('App\\Service\\FeatureFlagService::FEATURE_SHOW_SELECT_ZIPCODE_OR_CITY'), app.user) %}
  3. <script>
  4. {{ inlineJsWrapperBegin() }}
  5. var listElement;
  6. var inputZipcodeId;
  7. var inputCityId;
  8. var inputZipcodeField;
  9. const dropdownIcon = jQuery('#select-zipcode-or-city-dropdown-icon')
  10. if (jQuery('#jobseeker_profile_zipcode').length > 0) {
  11. inputZipcodeField = jQuery('#jobseeker_profile_zipcode');
  12. listElement = jQuery('#select-zipcode-or-city2');
  13. bindEvents();
  14. }
  15. inputZipcodeField = jQuery('#recurrent_jobs_search_parameters_filterZipcode');
  16. inputZipcodeField = inputZipcodeField.length === 0 ? jQuery('#wanted_jobs_search_parameters_filterZipcode') : inputZipcodeField;
  17. inputZipcodeField = inputZipcodeField.length === 0 ? jQuery('#jobseeker_profile_availabilityZipcode') : inputZipcodeField;
  18. inputZipcodeField = inputZipcodeField.length === 0 ? jQuery('#jobofferer_profile_zipcode') : inputZipcodeField;
  19. inputZipcodeField = inputZipcodeField.length === 0 ? jQuery('#recurrent_job_zipcode') : inputZipcodeField;
  20. inputZipcodeField = inputZipcodeField.length === 0 ? jQuery('#recurrent-jobs-search-for-one-customer-zipcode') : inputZipcodeField
  21. if (inputZipcodeField.length > 0) {
  22. inputZipcodeId = '#' + inputZipcodeField[0].id;
  23. }
  24. inputCityId = jQuery('#jobofferer_profile_city').length > 0 ? jQuery('#jobofferer_profile_city') : null;
  25. if (inputCityId === null && jQuery('#recurrent_job_city').length > 0) {
  26. inputCityId = jQuery('#recurrent_job_city');
  27. }
  28. listElement = jQuery('#select-zipcode-or-city');
  29. bindEvents();
  30. jQuery('#jobseeker_profile_availabilityZipcode').focus(function () {
  31. if (listElement[0].id === 'select-zipcode-or-city2') {
  32. listElement.hide().empty();
  33. listElement = jQuery('#select-zipcode-or-city');
  34. inputCityId = null;
  35. inputZipcodeField = jQuery(this);
  36. if (inputZipcodeField.length > 0) {
  37. inputZipcodeId = '#' + inputZipcodeField[0].id;
  38. }
  39. }
  40. });
  41. jQuery('#jobseeker_profile_zipcode').focus(function () {
  42. if (listElement[0].id === 'select-zipcode-or-city') {
  43. listElement.hide().empty();
  44. listElement = jQuery('#select-zipcode-or-city2');
  45. inputCityId = jQuery('#jobseeker_profile_city');
  46. inputZipcodeField = jQuery(this);
  47. if (inputZipcodeField.length > 0) {
  48. inputZipcodeId = '#' + inputZipcodeField[0].id;
  49. }
  50. }
  51. });
  52. function checkKeyCodes(event) {
  53. if (event.which === 38) { // ArrowUp
  54. if (listElement.is(':visible')) {
  55. return focusElement(event.which)
  56. }
  57. } else if (event.which === 40) { // ArrowDown
  58. if (listElement.length > 0) {
  59. listElement.show();
  60. return focusElement(event.which)
  61. }
  62. } else if (event.which === 13 || event.which === 27) { // Escape und Return
  63. listElement.hide();
  64. return false;
  65. }
  66. return true;
  67. }
  68. function getZipcodes(filter, joboffererProfileId = null) {
  69. jQuery.ajax({
  70. url: joboffererProfileId !== null ? '{{ path('searchhelper.zipcode_or_city_for_limited_search.api') }}' : '{{ path('searchhelper.zipcode_or_city.api') }}',
  71. method: 'post',
  72. dataType: 'json',
  73. data: joboffererProfileId !== null ? 'joboffererProfileId=' + joboffererProfileId :'filter=' + filter
  74. }).done(function (response) {
  75. listElement.empty();
  76. response.forEach(function (item) {
  77. if (item['city'] !== null && item['city'] !== undefined) {
  78. 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>')
  79. } else {
  80. listElement.append('<li tabindex="-1" zipcode="' + item['zipcode'] + '" class="list-group-item list-group-item-action py-1 tw-font-semibold">' + item['zipcode'] + '</li>')
  81. }
  82. });
  83. listElement.show();
  84. jQuery(listElement.children()[0]).addClass('bg-darkblue');
  85. })
  86. }
  87. function focusElement(direction) {
  88. var element;
  89. var activeElement = jQuery('#' + listElement[0].id + ' li.active');
  90. if (activeElement.length === 0) {
  91. element = listElement.children()[0];
  92. } else {
  93. element = direction === 40 ? activeElement[0].nextElementSibling : activeElement[0].previousElementSibling;
  94. activeElement.removeClass('active');
  95. }
  96. jQuery(element).focus().addClass('active');
  97. if (element !== undefined) {
  98. updateFieldValues(element['attributes']['zipcode']['nodeValue'], element['attributes']['city']['nodeValue']);
  99. }
  100. return false;
  101. }
  102. function updateFieldValues(zipcode, city) {
  103. if (inputCityId && city !== null) {
  104. inputCityId.focus().val(city);
  105. }
  106. inputZipcodeField.val(zipcode).focus();
  107. }
  108. function bindEvents() {
  109. listElement.click(function (event) {
  110. jQuery('#' + listElement[0].id + ' li.active').removeClass('active');
  111. jQuery(event.target).addClass('active');
  112. if ( event.target !== undefined
  113. && event.target !== null
  114. && event.target['attributes'] !== undefined
  115. && event.target['attributes'] !== null
  116. && event.target['attributes']['zipcode'] !== undefined
  117. && event.target['attributes']['zipcode'] !== null
  118. && event.target['attributes']['zipcode']['nodeValue'] !== undefined
  119. && event.target['attributes']['zipcode']['nodeValue'] !== null
  120. && event.target['attributes']['city'] !== undefined
  121. && event.target['attributes']['city'] !== null
  122. && event.target['attributes']['city']['nodeValue'] !== undefined
  123. && event.target['attributes']['city']['nodeValue'] !== null
  124. ) {
  125. updateFieldValues(event.target['attributes']['zipcode']['nodeValue'], event.target['attributes']['city']['nodeValue']);
  126. } else if(
  127. event.target !== undefined
  128. && event.target !== null
  129. && event.target['attributes'] !== undefined
  130. && event.target['attributes'] !== null
  131. && event.target['attributes']['zipcode'] !== undefined
  132. && event.target['attributes']['zipcode'] !== null
  133. && event.target['attributes']['zipcode']['nodeValue'] !== undefined
  134. && event.target['attributes']['zipcode']['nodeValue'] !== null
  135. ) {
  136. updateFieldValues(event.target['attributes']['zipcode']['nodeValue'], null);
  137. }
  138. jQuery(this).hide();
  139. });
  140. listElement.mouseover(function () {
  141. jQuery(this).css('cursor', 'pointer');
  142. });
  143. const clickHandler = function (){
  144. let joboffererProfileId = null;
  145. if (listElement.data('jobofferer-profile-id')) {
  146. joboffererProfileId = listElement.data('jobofferer-profile-id');
  147. joboffererProfileId = joboffererProfileId !== undefined ? joboffererProfileId : null;
  148. }
  149. listElement.hide().empty();
  150. getZipcodes('', joboffererProfileId);
  151. }
  152. if (inputZipcodeField.attr('readonly')) {
  153. inputZipcodeField.click(clickHandler);
  154. }
  155. dropdownIcon.click(clickHandler);
  156. inputZipcodeField.keydown(function (event) {
  157. var element;
  158. // Key-Codes von 48 - 57 sind die Ziffern von 0-9
  159. if ((event.which >= 48 && event.which <= 57) && jQuery('#restriced-wanted-jobs-search').length > 0) {
  160. jQuery('#zipcode-info-box').modal('toggle');
  161. }
  162. if ((event.which === 9 || event.which === 13) && listElement.is(':visible')) { //Tab and Return
  163. event.preventDefault();
  164. element = jQuery('#' + listElement[0].id + ' li.active');
  165. if (element !== undefined && element[0] !== undefined) {
  166. updateFieldValues(element[0]['attributes']['zipcode']['nodeValue'], element[0]['attributes']['city']['nodeValue']);
  167. }
  168. listElement.hide();
  169. }
  170. });
  171. inputZipcodeField.keyup(function (event) {
  172. var filter = jQuery(this).val();
  173. setTimeout(function () {
  174. if (filter === jQuery(inputZipcodeId).val() && filter.length > 2) {
  175. if (checkKeyCodes(event)) {
  176. getZipcodes(filter);
  177. }
  178. } else if (filter.length < 3) {
  179. listElement.hide();
  180. } else if (listElement.is(':visible')) {
  181. checkKeyCodes(event);
  182. }
  183. }, 150);
  184. });
  185. }
  186. {{ inlineJsWrapperEnd() }}
  187. </script>
  188. {% endif %}