accordion.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. (function ($) {
  2. 'use strict';
  3. Drupal.FieldGroup = Drupal.FieldGroup || {};
  4. Drupal.FieldGroup.Effects = Drupal.FieldGroup.Effects || {};
  5. /**
  6. * Implements Drupal.FieldGroup.processHook().
  7. */
  8. Drupal.FieldGroup.Effects.processAccordion = {
  9. execute: function (context, settings, group_info) {
  10. $('div.field-group-accordion-wrapper', context).once('fieldgroup-effects').each(function () {
  11. var wrapper = $(this);
  12. // Get the index to set active.
  13. var active_index = false;
  14. wrapper.find('.accordion-item').each(function (i) {
  15. if ($(this).hasClass('field-group-accordion-active')) {
  16. active_index = i;
  17. }
  18. });
  19. wrapper.accordion({
  20. heightStyle: 'content',
  21. active: active_index,
  22. collapsible: true,
  23. changestart: function (event, ui) {
  24. if ($(this).hasClass('effect-none')) {
  25. ui.options.animated = false;
  26. }
  27. else {
  28. ui.options.animated = 'slide';
  29. }
  30. }
  31. });
  32. if (group_info.context === 'form') {
  33. var $firstErrorItem = false;
  34. // Add required fields mark to any element containing required fields
  35. wrapper.find('div.field-group-accordion-item').each(function (i) {
  36. var $this = $(this);
  37. if ($this.is('.required-fields') && ($this.find('[required]').length > 0 || $this.find('.form-required').length > 0)) {
  38. $('h3.ui-accordion-header a').eq(i).addClass('form-required');
  39. }
  40. if ($('.error', $this).length) {
  41. // Save first error item, for focussing it.
  42. if (!$firstErrorItem) {
  43. $firstErrorItem = $this.parent().accordion('activate', i);
  44. }
  45. $('h3.ui-accordion-header').eq(i).addClass('error');
  46. }
  47. });
  48. // Save first error item, for focussing it.
  49. if (!$firstErrorItem) {
  50. $('.ui-accordion-content-active', $firstErrorItem).css({height: 'auto', width: 'auto', display: 'block'});
  51. }
  52. }
  53. });
  54. }
  55. };
  56. })(jQuery);