summaryrefslogtreecommitdiff
path: root/misc/collapse.js
blob: 548c2b24ceedc726b0494c02f83e086d9c780731 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// $Id$

Drupal.collapseAutoAttach = function () {
  $('fieldset.collapsible legend').each(function () {
    // Turn the legend into clickable link
    var a = document.createElement('a');
    a.href = '#';
    $(a)
      .click(function() {
        var fieldset = this.parentNode.parentNode;

        // Prevent double animations
        if (fieldset.animating) {
          return false;
        }
        fieldset.animating = true;

        if ($(fieldset).is('.collapsed')) {
          // Open fieldset with animation
          $(fieldset.contentWrapper).hide();
          $(fieldset).removeClass('collapsed');
          $(fieldset.contentWrapper).slideDown(300,
            {
              // Make sure we open to height auto
              complete: function() {
                $(fieldset.contentWrapper).css('height', 'auto');
                Drupal.collapseScrollIntoView(fieldset);
                fieldset.animating = false;
              },
              // Scroll the fieldset into view
              step: function() {
                Drupal.collapseScrollIntoView(fieldset);
              }
            }
          );
          if (typeof Drupal.textareaAttach != 'undefined') {
            // Initialize resizable textareas that are now revealed
            Drupal.textareaAttach(null, fieldset);
          }
        }
        else {
          // Collapse fieldset with animation (reverse of opening)
          $(fieldset.contentWrapper)
            .slideUp('medium', function () { $(fieldset).addClass('collapsed'); fieldset.animating = false; } )
            .show();
        }
        this.blur();
        return false;
      })
      .html(this.innerHTML);
    $(this)
      .empty()
      .append(a);

    // Wrap fieldsets contents (except for the legend) into wrapper divs for animating.
    // div1 is used to avoid margin problems inside fieldsets,
    // div2 is the one that is actually animated.
    var div1 = document.createElement('div');
    var div2 = document.createElement('div');
    this.parentNode.contentWrapper = div2;
    $(this).after(div1);
    $(div1).append(div2);
    var el = div1.nextSibling;
    while (el != null) {
      var next = el.nextSibling;
      $(el).remove();
      $(div2).append(el);
      el = next;
    }
    // Avoid jumping around due to margins collapsing into fieldset border
    $(div1).css('overflow', 'hidden');

    // Expand if there are errors inside
    if ($('input.error, textarea.error, select.error', this.parentNode).size() > 0) {
      $(this.parentNode).removeClass('collapsed');
    }
  });
}

/**
 * Scroll a given fieldset into view as much as possible.
 */
Drupal.collapseScrollIntoView = function (node) {
  var h = self.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || 0;
  var offset = self.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
  var pos = Drupal.absolutePosition(node);
  var fudge = 55;
  if (pos.y + node.offsetHeight + fudge > h + offset) {
    if (node.offsetHeight > h) {
      window.scrollTo(0, pos.y);
    } else {
      window.scrollTo(0, pos.y + node.offsetHeight - h + fudge);
    }
  }
}

// Global Killswitch
if (Drupal.jsEnabled) {
  $(document).ready(Drupal.collapseAutoAttach);
}