geocoder.admin.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. (function ($) {
  2. Drupal.behaviors.geocoderWidgetSettings = {
  3. attach: function (context, settings) {
  4. $(':input[name="instance[widget][settings][geocoder_field]"]', context).once('geofield_widget_settings', function () {
  5. // When the geocoder_field field is changed, update the geocoder_handler options.
  6. $(this).change(function() {
  7. var field = $(this).val();
  8. var field_type = settings.geocoder_widget_settings.types[field];
  9. var valid_handlers = settings.geocoder_widget_settings.handlers[field_type];
  10. $(':input[name="instance[widget][settings][geocoder_handler]"] option', context).each(function () {
  11. var handler_type = $(this).val();
  12. if (geocoder_admin_handler_in_array(handler_type, valid_handlers)) {
  13. $(this).removeAttr('disabled').show();
  14. }
  15. else {
  16. $(this).attr('disabled', 'disabled').hide();
  17. }
  18. });
  19. // If the currently selected handler is not valid, set it to the first valid handler.
  20. // Trigger a change event so that depdendent fields will update.
  21. if (!geocoder_admin_handler_in_array($(':input[name="instance[widget][settings][geocoder_handler]"]').val(), valid_handlers)) {
  22. $('#:input[name="instance[widget][settings][geocoder_handler]"]').val(valid_handlers[0]).trigger('change');
  23. }
  24. });
  25. function geocoder_admin_handler_in_array(needle, haystack) {
  26. var length = haystack.length;
  27. for(var i = 0; i < length; i++) {
  28. if(haystack[i] == needle) return true;
  29. }
  30. return false;
  31. }
  32. });
  33. // Trigger an initial change event to initialize the dependent field.
  34. $(this).trigger('change');
  35. }
  36. };
  37. })(jQuery);