All files / app/assets/javascripts settings_panels.js

84.84% Statements 28/33
77.77% Branches 14/18
72.72% Functions 8/11
84.37% Lines 27/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                        57x   57x       22x   22x 22x 22x 20x         22x           22x   22x 22x 22x 22x             1x 1x 1x             21x 21x               21x 29x 29x   29x 8x 8x         2x 2x         21x    
import $ from 'jquery';
import { InternalEvents } from '~/tracking';
import { __ } from './locale';
 
/**
 * Returns true if the given section is expanded or not
 *
 * For legacy consistency, it supports both jQuery and DOM elements
 *
 * @param {jQuery | Element} section
 */
export function isExpanded(sectionArg) {
  const section = sectionArg instanceof $ ? sectionArg[0] : sectionArg;
 
  return section.classList.contains('expanded');
}
 
export function expandSection(sectionArg) {
  const $section = $(sectionArg);
 
  $section.find('.js-settings-toggle:not(.js-settings-toggle-trigger-only)').text(__('Collapse'));
  $section.addClass('expanded');
  if (!$section.hasClass('no-animate')) {
    $section
      .addClass('animating')
      .one('animationend.animateSection', () => $section.removeClass('animating'));
  }
 
  InternalEvents.trackEvent('click_expand_panel_on_settings', {
    label: $section.find('.settings-title').text(),
  });
}
 
export function closeSection(sectionArg) {
  const $section = $(sectionArg);
 
  $section.find('.js-settings-toggle:not(.js-settings-toggle-trigger-only)').text(__('Expand'));
  $section.removeClass('expanded');
  Eif (!$section.hasClass('no-animate')) {
    $section
      .addClass('animating')
      .one('animationend.animateSection', () => $section.removeClass('animating'));
  }
}
 
export function toggleSection($section) {
  $section.removeClass('no-animate');
  if (isExpanded($section)) {
    closeSection($section);
  } else E{
    expandSection($section);
  }
}
 
export function initTrackProductAnalyticsExpanded() {
  const $analyticsSection = $('#js-product-analytics-settings');
  $analyticsSection.on('click.toggleSection', '.js-settings-toggle', () => {
    if (isExpanded($analyticsSection)) {
      InternalEvents.trackEvent('user_viewed_cluster_configuration');
    }
  });
}
 
export default function initSettingsPanels() {
  $('.settings').each((i, elm) => {
    const $section = $(elm);
    $section.on('click.toggleSection', '.js-settings-toggle', () => toggleSection($section));
 
    if (window.location.hash) {
      const $target = $(window.location.hash);
      if (
        $target.length &&
        !isExpanded($section) &&
        ($section.is($target) || $section.find($target).length)
      ) {
        $section.addClass('no-animate');
        expandSection($section);
      }
    }
  });
 
  initTrackProductAnalyticsExpanded();
}