collapse.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. (function ($) {
  2. /**
  3. * Toggle the visibility of a fieldset using smooth animations.
  4. */
  5. Drupal.toggleFieldset = function (fieldset) {
  6. var $fieldset = $(fieldset);
  7. if ($fieldset.is('.collapsed')) {
  8. var $content = $('> .fieldset-wrapper', fieldset).hide();
  9. $fieldset
  10. .removeClass('collapsed')
  11. .trigger({ type: 'collapsed', value: false })
  12. .find('> legend span.fieldset-legend-prefix').html(Drupal.t('Hide'));
  13. $content.slideDown({
  14. duration: 'fast',
  15. easing: 'linear',
  16. complete: function () {
  17. Drupal.collapseScrollIntoView(fieldset);
  18. fieldset.animating = false;
  19. },
  20. step: function () {
  21. // Scroll the fieldset into view.
  22. Drupal.collapseScrollIntoView(fieldset);
  23. }
  24. });
  25. }
  26. else {
  27. $fieldset.trigger({ type: 'collapsed', value: true });
  28. $('> .fieldset-wrapper', fieldset).slideUp('fast', function () {
  29. $fieldset
  30. .addClass('collapsed')
  31. .find('> legend span.fieldset-legend-prefix').html(Drupal.t('Show'));
  32. fieldset.animating = false;
  33. });
  34. }
  35. };
  36. /**
  37. * Scroll a given fieldset into view as much as possible.
  38. */
  39. Drupal.collapseScrollIntoView = function (node) {
  40. var h = document.documentElement.clientHeight || document.body.clientHeight || 0;
  41. var offset = document.documentElement.scrollTop || document.body.scrollTop || 0;
  42. var posY = $(node).offset().top;
  43. var fudge = 55;
  44. if (posY + node.offsetHeight + fudge > h + offset) {
  45. if (node.offsetHeight > h) {
  46. window.scrollTo(0, posY);
  47. }
  48. else {
  49. window.scrollTo(0, posY + node.offsetHeight - h + fudge);
  50. }
  51. }
  52. };
  53. Drupal.behaviors.collapse = {
  54. attach: function (context, settings) {
  55. $('fieldset.collapsible', context).once('collapse', function () {
  56. var $fieldset = $(this);
  57. // Expand fieldset if there are errors inside, or if it contains an
  58. // element that is targeted by the URI fragment identifier.
  59. var anchor = location.hash && location.hash != '#' ? ', ' + location.hash : '';
  60. if ($fieldset.find('.error' + anchor).length) {
  61. $fieldset.removeClass('collapsed');
  62. }
  63. var summary = $('<span class="summary"></span>');
  64. $fieldset.
  65. bind('summaryUpdated', function () {
  66. var text = $.trim($fieldset.drupalGetSummary());
  67. summary.html(text ? ' (' + text + ')' : '');
  68. })
  69. .trigger('summaryUpdated');
  70. // Turn the legend into a clickable link, but retain span.fieldset-legend
  71. // for CSS positioning.
  72. var $legend = $('> legend .fieldset-legend', this);
  73. $('<span class="fieldset-legend-prefix element-invisible"></span>')
  74. .append($fieldset.hasClass('collapsed') ? Drupal.t('Show') : Drupal.t('Hide'))
  75. .prependTo($legend)
  76. .after(' ');
  77. // .wrapInner() does not retain bound events.
  78. var $link = $('<a class="fieldset-title" href="#"></a>')
  79. .prepend($legend.contents())
  80. .appendTo($legend)
  81. .click(function () {
  82. var fieldset = $fieldset.get(0);
  83. // Don't animate multiple times.
  84. if (!fieldset.animating) {
  85. fieldset.animating = true;
  86. Drupal.toggleFieldset(fieldset);
  87. }
  88. return false;
  89. });
  90. $legend.append(summary);
  91. });
  92. }
  93. };
  94. })(jQuery);