Newer
Older
Steven Wittens
committed
/**
* Toggle the visibility of a fieldset using smooth animations
*/
Drupal.toggleFieldset = function(fieldset) {
if ($(fieldset).is('.collapsed')) {
var content = $('> div', fieldset);
Steven Wittens
committed
$(fieldset).removeClass('collapsed');
content.hide();
Gábor Hojtsy
committed
content.slideDown( {
duration: 'fast',
easing: 'linear',
Gábor Hojtsy
committed
complete: function() {
Steven Wittens
committed
Drupal.collapseScrollIntoView(this.parentNode);
this.parentNode.animating = false;
Gábor Hojtsy
committed
},
step: function() {
// Scroll the fieldset into view
Drupal.collapseScrollIntoView(this.parentNode);
}
Steven Wittens
committed
});
}
else {
var content = $('> div', fieldset).slideUp('fast', function() {
Steven Wittens
committed
$(this.parentNode).addClass('collapsed');
this.parentNode.animating = false;
});
}
/**
* Scroll a given fieldset into view as much as possible.
*/
Drupal.collapseScrollIntoView = function (node) {
var h = self.innerHeight || document.documentElement.clientHeight || $('body')[0].clientHeight || 0;
var offset = self.pageYOffset || document.documentElement.scrollTop || $('body')[0].scrollTop || 0;
var posY = $(node).offset().top;
if (posY + node.offsetHeight + fudge > h + offset) {
if (node.offsetHeight > h) {
window.scrollTo(0, posY);
window.scrollTo(0, posY + node.offsetHeight - h + fudge);
Dries Buytaert
committed
Drupal.behaviors.collapse = function (context) {
$('fieldset.collapsible > legend:not(.collapse-processed)', context).each(function() {
var fieldset = $(this.parentNode);
// Expand if there are errors inside
if ($('input.error, textarea.error, select.error', fieldset).size() > 0) {
fieldset.removeClass('collapsed');
}
Steven Wittens
committed
Dries Buytaert
committed
// Turn the legend into a clickable link and wrap the contents of the fieldset
// in a div for easier animation
var text = this.innerHTML;
Steven Wittens
committed
$(this).empty().append($('<a href="#">'+ text +'</a>').click(function() {
var fieldset = $(this).parents('fieldset:first')[0];
// Don't animate multiple times
if (!fieldset.animating) {
fieldset.animating = true;
Drupal.toggleFieldset(fieldset);
}
return false;
Dries Buytaert
committed
}))
.after($('<div class="fieldset-wrapper"></div>')
.append(fieldset.children(':not(legend)')))
.addClass('collapse-processed');
Steven Wittens
committed
});