4 * Toggle the visibility of a fieldset using smooth animations.
6 Drupal
.toggleFieldset = function (fieldset
) {
7 var $fieldset
= $(fieldset
);
8 if ($fieldset
.is('.collapsed')) {
9 var $content
= $('> .fieldset-wrapper', fieldset
).hide();
11 .removeClass('collapsed')
12 .trigger({ type
: 'collapsed', value
: false })
13 .find('> legend span.fieldset-legend-prefix').html(Drupal
.t('Hide'));
17 complete: function () {
18 Drupal
.collapseScrollIntoView(fieldset
);
19 fieldset
.animating
= false;
22 // Scroll the fieldset into view.
23 Drupal
.collapseScrollIntoView(fieldset
);
28 $fieldset
.trigger({ type
: 'collapsed', value
: true });
29 $('> .fieldset-wrapper', fieldset
).slideUp('fast', function () {
31 .addClass('collapsed')
32 .find('> legend span.fieldset-legend-prefix').html(Drupal
.t('Show'));
33 fieldset
.animating
= false;
39 * Scroll a given fieldset into view as much as possible.
41 Drupal
.collapseScrollIntoView = function (node
) {
42 var h
= document
.documentElement
.clientHeight
|| document
.body
.clientHeight
|| 0;
43 var offset
= document
.documentElement
.scrollTop
|| document
.body
.scrollTop
|| 0;
44 var posY
= $(node
).offset().top
;
46 if (posY
+ node
.offsetHeight
+ fudge
> h
+ offset
) {
47 if (node
.offsetHeight
> h
) {
48 window
.scrollTo(0, posY
);
51 window
.scrollTo(0, posY
+ node
.offsetHeight
- h
+ fudge
);
56 Drupal
.behaviors
.collapse
= {
57 attach: function (context
, settings
) {
58 $('fieldset.collapsible', context
).once('collapse', function () {
59 var $fieldset
= $(this);
60 // Expand fieldset if there are errors inside, or if it contains an
61 // element that is targeted by the uri fragment identifier.
62 var anchor
= location
.hash
&& location
.hash
!= '#' ? ', ' + location
.hash
: '';
63 if ($('.error' + anchor
, $fieldset
).length
) {
64 $fieldset
.removeClass('collapsed');
67 var summary
= $('<span class="summary"></span>');
69 bind('summaryUpdated', function () {
70 var text
= $.trim($fieldset
.drupalGetSummary());
71 summary
.html(text
? ' (' + text
+ ')' : '');
73 .trigger('summaryUpdated');
75 // Turn the legend into a clickable link, but retain span.fieldset-legend
76 // for CSS positioning.
77 var $legend
= $('> legend .fieldset-legend', this);
79 $('<span class="fieldset-legend-prefix element-invisible"></span>')
80 .append($fieldset
.hasClass('collapsed') ? Drupal
.t('Show') : Drupal
.t('Hide'))
84 // .wrapInner() does not retain bound events.
85 var $link
= $('<a class="fieldset-title" href="#"></a>')
86 .prepend($legend
.contents())
89 var fieldset
= $fieldset
.get(0);
90 // Don't animate multiple times.
91 if (!fieldset
.animating
) {
92 fieldset
.animating
= true;
93 Drupal
.toggleFieldset(fieldset
);
98 $legend
.append(summary
);