location_autocomplete.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. /**
  2. * Twiddle the province autocomplete whenever the user changes the country.
  3. */
  4. (function ($) {
  5. Drupal.behaviors.location = {
  6. attach: function(context) {
  7. $('select.location_auto_country:not(.location-processed)', context).change(function(e) {
  8. var obj = this;
  9. var input = null;
  10. var result = this.className.match(/(location_auto_join_[^ ]*)/);
  11. if (result) {
  12. input = $('.location_auto_province.' + result)
  13. }
  14. else {
  15. // No joining class found, fallback to searching the immediate area.
  16. input = $('.location_auto_province', $(this).parents('fieldset:first, .views-exposed-form:first'))
  17. }
  18. if (input && input.length) {
  19. //Unbind events on province field and empty its value
  20. input.unbind().val('');
  21. input.each(function(i) {
  22. //Get the (hidden) *-autocomplete input element
  23. var input_autocomplete = $('#' + this.id + '-autocomplete');
  24. // Update autocomplete url
  25. input_autocomplete.val(input_autocomplete.val().substr(0, input_autocomplete.val().lastIndexOf('/') + 1) + $(obj).val());
  26. // Mark as not processed.
  27. input_autocomplete.removeClass('autocomplete-processed');
  28. });
  29. // Reprocess.
  30. Drupal.behaviors.autocomplete.attach(document);
  31. }
  32. }).addClass('location-processed');
  33. }
  34. };
  35. })(jQuery);