summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/settings_panels.js
blob: 59ff2a86293c40038f5710c3cbc68afb97309577 (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
function expandSectionParent($section, $content) {
  $section.addClass('expanded');
  $content.off('animationend.expandSectionParent');
}

function expandSection($section) {
  $section.find('.js-settings-toggle').text('Close');

  const $content = $section.find('.settings-content');
  $content.addClass('expanded').off('scroll.expandSection').scrollTop(0);

  if ($content.hasClass('no-animate')) {
    expandSectionParent($section, $content);
  } else {
    $content.on('animationend.expandSectionParent', () => expandSectionParent($section, $content));
  }
}

function closeSection($section) {
  $section.find('.js-settings-toggle').text('Expand');

  const $content = $section.find('.settings-content');
  $content.removeClass('expanded').on('scroll.expandSection', () => expandSection($section));

  $section.removeClass('expanded');
}

function toggleSection($section) {
  const $content = $section.find('.settings-content');
  $content.removeClass('no-animate');
  if ($content.hasClass('expanded')) {
    closeSection($section);
  } else {
    expandSection($section);
  }
}

export default function initSettingsPanels() {
  $('.settings').each((i, elm) => {
    const $section = $(elm);
    $section.on('click.toggleSection', '.js-settings-toggle', () => toggleSection($section));
    $section.find('.settings-content:not(.expanded)').on('scroll.expandSection', () => expandSection($section));
  });
}