gitlab-org--gitlab-foss/app/assets/javascripts/right_sidebar.js

213 lines
6.3 KiB
JavaScript
Raw Normal View History

/* eslint-disable func-names, consistent-return, no-param-reassign */
2017-03-11 06:45:34 +00:00
import $ from 'jquery';
2017-03-11 06:45:34 +00:00
import Cookies from 'js-cookie';
import { fixTitle, hide } from '~/tooltips';
import { deprecatedCreateFlash as flash } from './flash';
import axios from './lib/utils/axios_utils';
import { sprintf, s__, __ } from './locale';
function Sidebar() {
2017-12-15 12:57:08 +00:00
this.toggleTodo = this.toggleTodo.bind(this);
this.sidebar = $('aside');
this.removeListeners();
this.addEventListeners();
}
Sidebar.initialize = function () {
2017-12-15 12:57:08 +00:00
if (!this.instance) {
this.instance = new Sidebar();
2017-12-15 12:57:08 +00:00
}
};
Sidebar.prototype.removeListeners = function () {
2017-12-15 12:57:08 +00:00
this.sidebar.off('click', '.sidebar-collapsed-icon');
// eslint-disable-next-line @gitlab/no-global-event-off
2017-12-15 12:57:08 +00:00
this.sidebar.off('hidden.gl.dropdown');
// eslint-disable-next-line @gitlab/no-global-event-off
2017-12-15 12:57:08 +00:00
$('.dropdown').off('loading.gl.dropdown');
// eslint-disable-next-line @gitlab/no-global-event-off
2017-12-15 12:57:08 +00:00
$('.dropdown').off('loaded.gl.dropdown');
$(document).off('click', '.js-sidebar-toggle');
};
Sidebar.prototype.addEventListeners = function () {
2017-12-15 12:57:08 +00:00
const $document = $(document);
this.sidebar.on('click', '.sidebar-collapsed-icon', this, this.sidebarCollapseClicked);
this.sidebar.on('hidden.gl.dropdown', this, this.onSidebarDropdownHidden);
$document.on('click', '.js-sidebar-toggle', this.sidebarToggleClicked);
return $(document)
.off('click', '.js-issuable-todo')
.on('click', '.js-issuable-todo', this.toggleTodo);
2017-12-15 12:57:08 +00:00
};
Sidebar.prototype.sidebarToggleClicked = function (e, triggered) {
const $this = $(this);
const $collapseIcon = $('.js-sidebar-collapse');
const $expandIcon = $('.js-sidebar-expand');
const $toggleContainer = $('.js-sidebar-toggle-container');
const isExpanded = $toggleContainer.data('is-expanded');
const tooltipLabel = isExpanded ? __('Expand sidebar') : __('Collapse sidebar');
2017-12-15 12:57:08 +00:00
e.preventDefault();
if (isExpanded) {
$toggleContainer.data('is-expanded', false);
$collapseIcon.addClass('hidden');
$expandIcon.removeClass('hidden');
$('aside.right-sidebar')
.removeClass('right-sidebar-expanded')
.addClass('right-sidebar-collapsed');
$('.layout-page').removeClass('right-sidebar-expanded').addClass('right-sidebar-collapsed');
2017-12-15 12:57:08 +00:00
} else {
$toggleContainer.data('is-expanded', true);
$expandIcon.addClass('hidden');
$collapseIcon.removeClass('hidden');
$('aside.right-sidebar')
.removeClass('right-sidebar-collapsed')
.addClass('right-sidebar-expanded');
$('.layout-page').removeClass('right-sidebar-collapsed').addClass('right-sidebar-expanded');
2017-12-15 12:57:08 +00:00
}
$this.attr('data-original-title', tooltipLabel);
2017-12-15 12:57:08 +00:00
if (!triggered) {
Cookies.set('collapsed_gutter', $('.right-sidebar').hasClass('right-sidebar-collapsed'));
2017-12-15 12:57:08 +00:00
}
};
Sidebar.prototype.toggleTodo = function (e) {
const $this = $(e.currentTarget);
const ajaxType = $this.data('deletePath') ? 'delete' : 'post';
const url = String($this.data('deletePath') || $this.data('createPath'));
2017-12-15 12:57:08 +00:00
hide($this);
2017-12-15 12:57:08 +00:00
$('.js-issuable-todo').disable().addClass('is-loading');
2018-02-02 14:54:18 +00:00
axios[ajaxType](url, {
2018-02-20 22:20:48 +00:00
issuable_id: $this.data('issuableId'),
issuable_type: $this.data('issuableType'),
})
.then(({ data }) => {
this.todoUpdateDone(data);
})
.catch(() =>
2019-08-05 14:31:40 +00:00
flash(
sprintf(__('There was an error %{message} todo.'), {
message:
ajaxType === 'post' ? s__('RightSidebar|adding a') : s__('RightSidebar|deleting the'),
}),
),
);
2017-12-15 12:57:08 +00:00
};
Sidebar.prototype.todoUpdateDone = function (data) {
2017-12-15 12:57:08 +00:00
const deletePath = data.delete_path ? data.delete_path : null;
const attrPrefix = deletePath ? 'mark' : 'todo';
const $todoBtns = $('.js-issuable-todo');
$(document).trigger('todo:toggle', data.count);
$todoBtns.each((i, el) => {
const $el = $(el);
const $elText = $el.find('.js-issuable-todo-inner');
$el
.removeClass('is-loading')
2017-12-15 12:57:08 +00:00
.enable()
2018-02-20 22:20:48 +00:00
.attr('aria-label', $el.data(`${attrPrefix}Text`))
2018-11-30 04:03:35 +00:00
.attr('title', $el.data(`${attrPrefix}Text`))
.data('deletePath', deletePath);
2017-12-15 12:57:08 +00:00
if ($el.hasClass('has-tooltip')) {
fixTitle(el);
2016-07-24 20:45:11 +00:00
}
2018-11-30 04:03:35 +00:00
if (typeof $el.data('isCollapsed') !== 'undefined') {
2018-02-20 22:20:48 +00:00
$elText.html($el.data(`${attrPrefix}Icon`));
2017-12-15 12:57:08 +00:00
} else {
2018-02-20 22:20:48 +00:00
$elText.text($el.data(`${attrPrefix}Text`));
2017-12-15 12:57:08 +00:00
}
});
};
Sidebar.prototype.sidebarCollapseClicked = function (e) {
2017-12-15 12:57:08 +00:00
if ($(e.currentTarget).hasClass('dont-change-state')) {
return;
}
const sidebar = e.data;
2017-12-15 12:57:08 +00:00
e.preventDefault();
const $block = $(this).closest('.block');
2017-12-15 12:57:08 +00:00
return sidebar.openDropdown($block);
};
Sidebar.prototype.openDropdown = function (blockOrName) {
const $block = typeof blockOrName === 'string' ? this.getBlock(blockOrName) : blockOrName;
2017-12-15 12:57:08 +00:00
if (!this.isOpen()) {
this.setCollapseAfterUpdate($block);
this.toggleSidebar('open');
}
// Wait for the sidebar to trigger('click') open
// so it doesn't cause our dropdown to close preemptively
setTimeout(() => {
$block.find('.js-sidebar-dropdown-toggle').trigger('click');
});
};
Sidebar.prototype.setCollapseAfterUpdate = function ($block) {
2017-12-15 12:57:08 +00:00
$block.addClass('collapse-after-update');
return $('.layout-page').addClass('with-overlay');
};
Sidebar.prototype.onSidebarDropdownHidden = function (e) {
const sidebar = e.data;
2017-12-15 12:57:08 +00:00
e.preventDefault();
const $block = $(e.target).closest('.block');
2017-12-15 12:57:08 +00:00
return sidebar.sidebarDropdownHidden($block);
};
Sidebar.prototype.sidebarDropdownHidden = function ($block) {
2017-12-15 12:57:08 +00:00
if ($block.hasClass('collapse-after-update')) {
$block.removeClass('collapse-after-update');
$('.layout-page').removeClass('with-overlay');
return this.toggleSidebar('hide');
}
};
Sidebar.prototype.triggerOpenSidebar = function () {
2017-12-15 12:57:08 +00:00
return this.sidebar.find('.js-sidebar-toggle').trigger('click');
};
Sidebar.prototype.toggleSidebar = function (action) {
2017-12-15 12:57:08 +00: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 20:45:11 +00:00
Sidebar.prototype.isOpen = function () {
2017-12-15 12:57:08 +00:00
return this.sidebar.is('.right-sidebar-expanded');
};
2016-07-24 20:45:11 +00:00
Sidebar.prototype.getBlock = function (name) {
return this.sidebar.find(`.block.${name}`);
2017-12-15 12:57:08 +00:00
};
2016-07-24 20:45:11 +00:00
2017-12-15 12:57:08 +00:00
export default Sidebar;