Projet

Général

Profil

Paste
Télécharger (2,84 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / themes / bootstrap / js / misc / _collapse.js @ 7547bb19

1
(function ($) {
2

    
3
/**
4
 * Toggle the visibility of a fieldset using smooth animations.
5
 */
6
Drupal.toggleFieldset = function (fieldset) {
7
  var $toggle = $($(fieldset).find('[data-toggle=collapse]').data('target'));
8
  if ($toggle.length) {
9
    $toggle.collapse('toggle');
10
  }
11
};
12

    
13
/**
14
 * Scroll a given fieldset into view as much as possible.
15
 */
16
Drupal.collapseScrollIntoView = function (node) {
17
  var h = document.documentElement.clientHeight || document.body.clientHeight || 0;
18
  var offset = document.documentElement.scrollTop || document.body.scrollTop || 0;
19
  var posY = $(node).offset().top;
20
  var fudge = 55;
21
  if (posY + node.offsetHeight + fudge > h + offset) {
22
    if (node.offsetHeight > h) {
23
      window.scrollTo(0, posY);
24
    }
25
    else {
26
      window.scrollTo(0, posY + node.offsetHeight - h + fudge);
27
    }
28
  }
29
};
30

    
31
Drupal.behaviors.collapse = {
32
  attach: function (context, settings) {
33
    $('fieldset.collapsible', context).once('collapse', function () {
34
      var $fieldset = $(this);
35
      // Expand fieldset if there are errors inside, or if it contains an
36
      // element that is targeted by the URI fragment identifier.
37
      var anchor = location.hash && location.hash != '#' ? ', ' + location.hash : '';
38
      if ($fieldset.find('.error' + anchor).length) {
39
        $fieldset.removeClass('collapsed');
40
      }
41

    
42
      var summary = $('<span class="summary"></span>');
43
      $fieldset.
44
        bind('summaryUpdated', function () {
45
          var text = $.trim($fieldset.drupalGetSummary());
46
          summary.html(text ? ' (' + text + ')' : '');
47
        })
48
        .trigger('summaryUpdated');
49

    
50
      // Turn the legend into a clickable link, but retain span.fieldset-legend
51
      // for CSS positioning.
52
      var $legend = $('> legend .fieldset-legend', this);
53

    
54
      $('<span class="fieldset-legend-prefix element-invisible"></span>')
55
        .append($fieldset.hasClass('collapsed') ? Drupal.t('Show') : Drupal.t('Hide'))
56
        .prependTo($legend);
57

    
58
      $fieldset.find('[data-toggle=collapse]').on('click', function (e) {
59
        e.preventDefault();
60
      });
61

    
62
      // Bind Bootstrap events with Drupal core events.
63
      $fieldset
64
        .append(summary)
65
        .on('show.bs.collapse', function () {
66
          $fieldset
67
            .removeClass('collapsed')
68
            .find('> legend span.fieldset-legend-prefix').html(Drupal.t('Hide'));
69
        })
70
        .on('shown.bs.collapse', function () {
71
          $fieldset.trigger({ type: 'collapsed', value: false });
72
          Drupal.collapseScrollIntoView($fieldset.get(0));
73
        })
74
        .on('hide.bs.collapse', function () {
75
          $fieldset
76
            .addClass('collapsed')
77
            .find('> legend span.fieldset-legend-prefix').html(Drupal.t('Show'));
78
        })
79
        .on('hidden.bs.collapse', function () {
80
          $fieldset.trigger({ type: 'collapsed', value: true });
81
        });
82
    });
83
  }
84
};
85

    
86
})(jQuery);