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