web/drupal/misc/collapse.js
branchdrupal
changeset 74 0ff3ba646492
equal deleted inserted replaced
73:fcf75e232c5b 74:0ff3ba646492
       
     1 // $Id: collapse.js,v 1.17 2008/01/29 10:58:25 goba Exp $
       
     2 
       
     3 /**
       
     4  * Toggle the visibility of a fieldset using smooth animations
       
     5  */
       
     6 Drupal.toggleFieldset = function(fieldset) {
       
     7   if ($(fieldset).is('.collapsed')) {
       
     8     // Action div containers are processed separately because of a IE bug
       
     9     // that alters the default submit button behavior.
       
    10     var content = $('> div:not(.action)', fieldset);
       
    11     $(fieldset).removeClass('collapsed');
       
    12     content.hide();
       
    13     content.slideDown( {
       
    14       duration: 'fast',
       
    15       easing: 'linear',
       
    16       complete: function() {
       
    17         Drupal.collapseScrollIntoView(this.parentNode);
       
    18         this.parentNode.animating = false;
       
    19         $('div.action', fieldset).show();
       
    20       },
       
    21       step: function() {
       
    22         // Scroll the fieldset into view
       
    23         Drupal.collapseScrollIntoView(this.parentNode);
       
    24       }
       
    25     });
       
    26   }
       
    27   else {
       
    28     $('div.action', fieldset).hide();
       
    29     var content = $('> div:not(.action)', fieldset).slideUp('fast', function() {
       
    30       $(this.parentNode).addClass('collapsed');
       
    31       this.parentNode.animating = false;
       
    32     });
       
    33   }
       
    34 };
       
    35 
       
    36 /**
       
    37  * Scroll a given fieldset into view as much as possible.
       
    38  */
       
    39 Drupal.collapseScrollIntoView = function (node) {
       
    40   var h = self.innerHeight || document.documentElement.clientHeight || $('body')[0].clientHeight || 0;
       
    41   var offset = self.pageYOffset || document.documentElement.scrollTop || $('body')[0].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     } else {
       
    48       window.scrollTo(0, posY + node.offsetHeight - h + fudge);
       
    49     }
       
    50   }
       
    51 };
       
    52 
       
    53 Drupal.behaviors.collapse = function (context) {
       
    54   $('fieldset.collapsible > legend:not(.collapse-processed)', context).each(function() {
       
    55     var fieldset = $(this.parentNode);
       
    56     // Expand if there are errors inside
       
    57     if ($('input.error, textarea.error, select.error', fieldset).size() > 0) {
       
    58       fieldset.removeClass('collapsed');
       
    59     }
       
    60 
       
    61     // Turn the legend into a clickable link and wrap the contents of the fieldset
       
    62     // in a div for easier animation
       
    63     var text = this.innerHTML;
       
    64       $(this).empty().append($('<a href="#">'+ text +'</a>').click(function() {
       
    65         var fieldset = $(this).parents('fieldset:first')[0];
       
    66         // Don't animate multiple times
       
    67         if (!fieldset.animating) {
       
    68           fieldset.animating = true;
       
    69           Drupal.toggleFieldset(fieldset);
       
    70         }
       
    71         return false;
       
    72       }))
       
    73       .after($('<div class="fieldset-wrapper"></div>')
       
    74       .append(fieldset.children(':not(legend):not(.action)')))
       
    75       .addClass('collapse-processed');
       
    76   });
       
    77 };