debian-mirror-gitlab/app/assets/javascripts/settings_panels.js

68 lines
1.8 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2018-12-13 13:39:08 +05:30
import { __ } from './locale';
2018-05-09 12:01:36 +05:30
2021-03-11 19:13:27 +05:30
/**
* 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);
2018-12-13 13:39:08 +05:30
$section.find('.js-settings-toggle:not(.js-settings-toggle-trigger-only)').text(__('Collapse'));
2018-03-17 18:26:18 +05:30
$section.addClass('expanded');
if (!$section.hasClass('no-animate')) {
2018-12-13 13:39:08 +05:30
$section
.addClass('animating')
2018-03-17 18:26:18 +05:30
.one('animationend.animateSection', () => $section.removeClass('animating'));
2017-09-10 17:25:29 +05:30
}
}
2021-03-11 19:13:27 +05:30
export function closeSection(sectionArg) {
const $section = $(sectionArg);
2018-12-13 13:39:08 +05:30
$section.find('.js-settings-toggle:not(.js-settings-toggle-trigger-only)').text(__('Expand'));
2017-09-10 17:25:29 +05:30
$section.removeClass('expanded');
2018-03-17 18:26:18 +05:30
if (!$section.hasClass('no-animate')) {
2018-12-13 13:39:08 +05:30
$section
.addClass('animating')
2018-03-17 18:26:18 +05:30
.one('animationend.animateSection', () => $section.removeClass('animating'));
}
2017-09-10 17:25:29 +05:30
}
2021-03-08 18:12:59 +05:30
export function toggleSection($section) {
2018-03-17 18:26:18 +05:30
$section.removeClass('no-animate');
2021-03-11 19:13:27 +05:30
if (isExpanded($section)) {
2017-09-10 17:25:29 +05:30
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));
2018-03-17 18:26:18 +05:30
2022-04-04 11:22:00 +05:30
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');
2018-03-17 18:26:18 +05:30
expandSection($section);
2022-04-04 11:22:00 +05:30
}
2018-03-17 18:26:18 +05:30
}
2017-09-10 17:25:29 +05:30
});
}