2020-02-21 04:09:01 -05:00
|
|
|
/* eslint-disable func-names, consistent-return, no-param-reassign */
|
2017-03-11 01:45:34 -05:00
|
|
|
|
2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2022-02-01 16:12:02 -05:00
|
|
|
import { setCookie } from '~/lib/utils/common_utils';
|
2021-08-20 20:11:06 -04:00
|
|
|
import { hide, fixTitle } from '~/tooltips';
|
2021-06-20 23:10:27 -04:00
|
|
|
import createFlash from './flash';
|
2018-02-02 06:16:57 -05:00
|
|
|
import axios from './lib/utils/axios_utils';
|
2019-05-07 02:24:27 -04:00
|
|
|
import { sprintf, s__, __ } from './locale';
|
2016-12-14 00:26:26 -05:00
|
|
|
|
2019-09-16 08:06:26 -04:00
|
|
|
function Sidebar() {
|
2017-12-15 07:57:08 -05:00
|
|
|
this.toggleTodo = this.toggleTodo.bind(this);
|
|
|
|
this.sidebar = $('aside');
|
|
|
|
|
|
|
|
this.removeListeners();
|
|
|
|
this.addEventListeners();
|
|
|
|
}
|
|
|
|
|
2020-12-23 07:10:26 -05:00
|
|
|
Sidebar.initialize = function () {
|
2017-12-15 07:57:08 -05:00
|
|
|
if (!this.instance) {
|
2019-09-16 08:06:26 -04:00
|
|
|
this.instance = new Sidebar();
|
2017-12-15 07:57:08 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-23 07:10:26 -05:00
|
|
|
Sidebar.prototype.removeListeners = function () {
|
2017-12-15 07:57:08 -05:00
|
|
|
this.sidebar.off('click', '.sidebar-collapsed-icon');
|
2020-12-08 16:10:06 -05:00
|
|
|
// eslint-disable-next-line @gitlab/no-global-event-off
|
2017-12-15 07:57:08 -05:00
|
|
|
this.sidebar.off('hidden.gl.dropdown');
|
2020-12-08 16:10:06 -05:00
|
|
|
// eslint-disable-next-line @gitlab/no-global-event-off
|
2021-02-25 07:11:01 -05:00
|
|
|
this.sidebar.off('hiddenGlDropdown');
|
|
|
|
// eslint-disable-next-line @gitlab/no-global-event-off
|
2017-12-15 07:57:08 -05:00
|
|
|
$('.dropdown').off('loading.gl.dropdown');
|
2020-12-08 16:10:06 -05:00
|
|
|
// eslint-disable-next-line @gitlab/no-global-event-off
|
2017-12-15 07:57:08 -05:00
|
|
|
$('.dropdown').off('loaded.gl.dropdown');
|
|
|
|
$(document).off('click', '.js-sidebar-toggle');
|
|
|
|
};
|
|
|
|
|
2020-12-23 07:10:26 -05:00
|
|
|
Sidebar.prototype.addEventListeners = function () {
|
2017-12-15 07:57:08 -05:00
|
|
|
const $document = $(document);
|
|
|
|
|
|
|
|
this.sidebar.on('click', '.sidebar-collapsed-icon', this, this.sidebarCollapseClicked);
|
|
|
|
this.sidebar.on('hidden.gl.dropdown', this, this.onSidebarDropdownHidden);
|
2021-02-25 07:11:01 -05:00
|
|
|
this.sidebar.on('hiddenGlDropdown', this, this.onSidebarDropdownHidden);
|
2017-12-15 07:57:08 -05:00
|
|
|
|
|
|
|
$document.on('click', '.js-sidebar-toggle', this.sidebarToggleClicked);
|
2018-10-30 16:28:31 -04:00
|
|
|
return $(document)
|
|
|
|
.off('click', '.js-issuable-todo')
|
|
|
|
.on('click', '.js-issuable-todo', this.toggleTodo);
|
2017-12-15 07:57:08 -05:00
|
|
|
};
|
|
|
|
|
2020-12-23 07:10:26 -05:00
|
|
|
Sidebar.prototype.sidebarToggleClicked = function (e, triggered) {
|
2019-11-16 01:06:30 -05:00
|
|
|
const $this = $(this);
|
2020-10-02 23:08:48 -04:00
|
|
|
const $collapseIcon = $('.js-sidebar-collapse');
|
|
|
|
const $expandIcon = $('.js-sidebar-expand');
|
|
|
|
const $toggleContainer = $('.js-sidebar-toggle-container');
|
|
|
|
const isExpanded = $toggleContainer.data('is-expanded');
|
2019-11-16 01:06:30 -05:00
|
|
|
const tooltipLabel = isExpanded ? __('Expand sidebar') : __('Collapse sidebar');
|
2017-12-15 07:57:08 -05:00
|
|
|
e.preventDefault();
|
2018-04-19 10:43:20 -04:00
|
|
|
|
|
|
|
if (isExpanded) {
|
2020-10-02 23:08:48 -04:00
|
|
|
$toggleContainer.data('is-expanded', false);
|
|
|
|
$collapseIcon.addClass('hidden');
|
|
|
|
$expandIcon.removeClass('hidden');
|
2018-10-30 16:28:31 -04:00
|
|
|
$('aside.right-sidebar')
|
|
|
|
.removeClass('right-sidebar-expanded')
|
|
|
|
.addClass('right-sidebar-collapsed');
|
2020-12-23 07:10:26 -05:00
|
|
|
$('.layout-page').removeClass('right-sidebar-expanded').addClass('right-sidebar-collapsed');
|
2017-12-15 07:57:08 -05:00
|
|
|
} else {
|
2020-10-02 23:08:48 -04:00
|
|
|
$toggleContainer.data('is-expanded', true);
|
|
|
|
$expandIcon.addClass('hidden');
|
|
|
|
$collapseIcon.removeClass('hidden');
|
2018-10-30 16:28:31 -04:00
|
|
|
$('aside.right-sidebar')
|
|
|
|
.removeClass('right-sidebar-collapsed')
|
|
|
|
.addClass('right-sidebar-expanded');
|
2020-12-23 07:10:26 -05:00
|
|
|
$('.layout-page').removeClass('right-sidebar-collapsed').addClass('right-sidebar-expanded');
|
2017-12-15 07:57:08 -05:00
|
|
|
}
|
2018-04-19 10:43:20 -04:00
|
|
|
|
|
|
|
$this.attr('data-original-title', tooltipLabel);
|
2021-08-20 20:11:06 -04:00
|
|
|
$this.attr('title', tooltipLabel);
|
|
|
|
fixTitle($this);
|
|
|
|
hide($this);
|
2018-04-19 10:43:20 -04:00
|
|
|
|
2017-12-15 07:57:08 -05:00
|
|
|
if (!triggered) {
|
2022-02-01 16:12:02 -05:00
|
|
|
setCookie('collapsed_gutter', $('.right-sidebar').hasClass('right-sidebar-collapsed'));
|
2017-12-15 07:57:08 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-23 07:10:26 -05:00
|
|
|
Sidebar.prototype.toggleTodo = function (e) {
|
2019-11-16 01:06:30 -05:00
|
|
|
const $this = $(e.currentTarget);
|
|
|
|
const ajaxType = $this.data('deletePath') ? 'delete' : 'post';
|
|
|
|
const url = String($this.data('deletePath') || $this.data('createPath'));
|
2017-12-15 07:57:08 -05:00
|
|
|
|
2020-10-02 14:08:56 -04:00
|
|
|
hide($this);
|
2017-12-15 07:57:08 -05:00
|
|
|
|
2020-12-23 07:10:26 -05:00
|
|
|
$('.js-issuable-todo').disable().addClass('is-loading');
|
2018-02-02 06:16:57 -05:00
|
|
|
|
2018-02-02 09:54:18 -05:00
|
|
|
axios[ajaxType](url, {
|
2018-02-20 17:20:48 -05:00
|
|
|
issuable_id: $this.data('issuableId'),
|
|
|
|
issuable_type: $this.data('issuableType'),
|
2018-10-30 16:28:31 -04:00
|
|
|
})
|
|
|
|
.then(({ data }) => {
|
|
|
|
this.todoUpdateDone(data);
|
|
|
|
})
|
|
|
|
.catch(() =>
|
2021-06-20 23:10:27 -04:00
|
|
|
createFlash({
|
2021-09-13 02:11:29 -04:00
|
|
|
message: sprintf(__('There was an error %{message} to-do item.'), {
|
2019-08-05 10:31:40 -04:00
|
|
|
message:
|
|
|
|
ajaxType === 'post' ? s__('RightSidebar|adding a') : s__('RightSidebar|deleting the'),
|
|
|
|
}),
|
2021-06-20 23:10:27 -04:00
|
|
|
}),
|
2018-10-30 16:28:31 -04:00
|
|
|
);
|
2017-12-15 07:57:08 -05:00
|
|
|
};
|
|
|
|
|
2020-12-23 07:10:26 -05:00
|
|
|
Sidebar.prototype.sidebarCollapseClicked = function (e) {
|
2021-10-08 08:11:10 -04:00
|
|
|
if ($(e.currentTarget).hasClass('js-dont-change-state')) {
|
2017-12-15 07:57:08 -05:00
|
|
|
return;
|
|
|
|
}
|
2019-11-16 01:06:30 -05:00
|
|
|
const sidebar = e.data;
|
2017-12-15 07:57:08 -05:00
|
|
|
e.preventDefault();
|
2019-11-16 01:06:30 -05:00
|
|
|
const $block = $(this).closest('.block');
|
2017-12-15 07:57:08 -05:00
|
|
|
return sidebar.openDropdown($block);
|
|
|
|
};
|
|
|
|
|
2020-12-23 07:10:26 -05:00
|
|
|
Sidebar.prototype.openDropdown = function (blockOrName) {
|
2020-04-21 11:21:10 -04:00
|
|
|
const $block = typeof blockOrName === 'string' ? this.getBlock(blockOrName) : blockOrName;
|
2017-12-15 07:57:08 -05:00
|
|
|
if (!this.isOpen()) {
|
|
|
|
this.setCollapseAfterUpdate($block);
|
|
|
|
this.toggleSidebar('open');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-23 07:10:26 -05:00
|
|
|
Sidebar.prototype.setCollapseAfterUpdate = function ($block) {
|
2017-12-15 07:57:08 -05:00
|
|
|
$block.addClass('collapse-after-update');
|
|
|
|
return $('.layout-page').addClass('with-overlay');
|
|
|
|
};
|
|
|
|
|
2020-12-23 07:10:26 -05:00
|
|
|
Sidebar.prototype.onSidebarDropdownHidden = function (e) {
|
2019-11-16 01:06:30 -05:00
|
|
|
const sidebar = e.data;
|
2017-12-15 07:57:08 -05:00
|
|
|
e.preventDefault();
|
2019-11-16 01:06:30 -05:00
|
|
|
const $block = $(e.target).closest('.block');
|
2017-12-15 07:57:08 -05:00
|
|
|
return sidebar.sidebarDropdownHidden($block);
|
|
|
|
};
|
|
|
|
|
2020-12-23 07:10:26 -05:00
|
|
|
Sidebar.prototype.sidebarDropdownHidden = function ($block) {
|
2017-12-15 07:57:08 -05:00
|
|
|
if ($block.hasClass('collapse-after-update')) {
|
|
|
|
$block.removeClass('collapse-after-update');
|
|
|
|
$('.layout-page').removeClass('with-overlay');
|
|
|
|
return this.toggleSidebar('hide');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-23 07:10:26 -05:00
|
|
|
Sidebar.prototype.triggerOpenSidebar = function () {
|
2017-12-15 07:57:08 -05:00
|
|
|
return this.sidebar.find('.js-sidebar-toggle').trigger('click');
|
|
|
|
};
|
|
|
|
|
2020-12-23 07:10:26 -05:00
|
|
|
Sidebar.prototype.toggleSidebar = function (action) {
|
2017-12-15 07:57:08 -05:00
|
|
|
if (action == null) {
|
|
|
|
action = 'toggle';
|
|
|
|
}
|
|
|
|
if (action === 'toggle') {
|
|
|
|
this.triggerOpenSidebar();
|
|
|
|
}
|
|
|
|
if (action === 'open') {
|
|
|
|
if (!this.isOpen()) {
|
|
|
|
this.triggerOpenSidebar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (action === 'hide') {
|
|
|
|
if (this.isOpen()) {
|
|
|
|
return this.triggerOpenSidebar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2020-12-23 07:10:26 -05:00
|
|
|
Sidebar.prototype.isOpen = function () {
|
2017-12-15 07:57:08 -05:00
|
|
|
return this.sidebar.is('.right-sidebar-expanded');
|
|
|
|
};
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2020-12-23 07:10:26 -05:00
|
|
|
Sidebar.prototype.getBlock = function (name) {
|
2019-09-24 08:06:20 -04:00
|
|
|
return this.sidebar.find(`.block.${name}`);
|
2017-12-15 07:57:08 -05:00
|
|
|
};
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-12-15 07:57:08 -05:00
|
|
|
export default Sidebar;
|