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

73 lines
2.1 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'));
2021-02-22 17:27:13 +05:30
// eslint-disable-next-line @gitlab/no-global-event-off
2021-03-08 18:12:59 +05:30
$section.find('.settings-content').off('scroll.expandSection').scrollTop(0);
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'));
2018-03-17 18:26:18 +05:30
$section.find('.settings-content').on('scroll.expandSection', () => expandSection($section));
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
2021-03-11 19:13:27 +05:30
if (!isExpanded($section)) {
2018-03-17 18:26:18 +05:30
$section.find('.settings-content').on('scroll.expandSection', () => {
$section.removeClass('no-animate');
expandSection($section);
});
}
2017-09-10 17:25:29 +05:30
});
2018-03-17 18:26:18 +05:30
2018-11-08 19:23:39 +05:30
if (window.location.hash) {
const $target = $(window.location.hash);
2018-03-27 19:54:05 +05:30
if ($target.length && $target.hasClass('settings')) {
2018-03-17 18:26:18 +05:30
expandSection($target);
}
}
2017-09-10 17:25:29 +05:30
}