collapse.js 3.5 KB

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