summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/behaviors/toggler_behavior.js
blob: 0726c6c96362ffde31a25883fd6f8a7545227666 (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
/* eslint-disable wrap-iife, func-names, space-before-function-paren, prefer-arrow-callback, vars-on-top, no-var, max-len */
(function(w) {
  $(function() {
    var toggleContainer = function(container, /* optional */toggleState) {
      var $container = $(container);

      $container
        .find('.js-toggle-button .fa')
        .toggleClass('fa-chevron-up', toggleState)
        .toggleClass('fa-chevron-down', toggleState !== undefined ? !toggleState : undefined);

      $container
        .find('.js-toggle-content')
        .toggle(toggleState);
    };

    // Toggle button. Show/hide content inside parent container.
    // Button does not change visibility. If button has icon - it changes chevron style.
    //
    // %div.js-toggle-container
    //   %a.js-toggle-button
    //   %div.js-toggle-content
    //
    $('body').on('click', '.js-toggle-button', function() {
      toggleContainer($(this).closest('.js-toggle-container'));
    });

    // If we're accessing a permalink, ensure it is not inside a
    // closed js-toggle-container!
    var hash = w.gl.utils.getLocationHash();
    var anchor = hash && document.getElementById(hash);
    var container = anchor && $(anchor).closest('.js-toggle-container');

    if (container) {
      toggleContainer(container, true);
      anchor.scrollIntoView();
    }
  });
})(window);