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

118 lines
3.8 KiB
JavaScript
Raw Normal View History

2017-08-17 22:00:37 +05:30
/* eslint-disable comma-dangle, class-methods-use-this, no-underscore-dangle, no-param-reassign, no-unused-vars, consistent-return, func-names, space-before-function-paren, max-len */
2018-03-17 18:26:18 +05:30
import Sortable from 'vendor/Sortable';
2016-11-03 12:29:30 +05:30
2018-03-17 18:26:18 +05:30
import flash from './flash';
import axios from './lib/utils/axios_utils';
2016-11-03 12:29:30 +05:30
2018-03-17 18:26:18 +05:30
export default class LabelManager {
constructor({ togglePriorityButton, prioritizedLabels, otherLabels } = {}) {
this.togglePriorityButton = togglePriorityButton || $('.js-toggle-priority');
this.prioritizedLabels = prioritizedLabels || $('.js-prioritized-labels');
this.otherLabels = otherLabels || $('.js-other-labels');
this.errorMessage = 'Unable to update label prioritization at this time';
this.emptyState = document.querySelector('#js-priority-labels-empty-state');
this.sortable = Sortable.create(this.prioritizedLabels.get(0), {
filter: '.empty-message',
forceFallback: true,
fallbackClass: 'is-dragging',
dataIdAttr: 'data-id',
onUpdate: this.onPrioritySortUpdate.bind(this),
});
this.bindEvents();
}
2016-11-03 12:29:30 +05:30
2018-03-17 18:26:18 +05:30
bindEvents() {
this.prioritizedLabels.find('.btn-action').on('mousedown', this, this.onButtonActionClick);
return this.togglePriorityButton.on('click', this, this.onTogglePriorityClick);
}
onTogglePriorityClick(e) {
e.preventDefault();
const _this = e.data;
const $btn = $(e.currentTarget);
const $label = $(`#${$btn.data('domId')}`);
const action = $btn.parents('.js-prioritized-labels').length ? 'remove' : 'add';
const $tooltip = $(`#${$btn.find('.has-tooltip:visible').attr('aria-describedby')}`);
$tooltip.tooltip('destroy');
_this.toggleLabelPriority($label, action);
_this.toggleEmptyState($label, $btn, action);
}
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
onButtonActionClick(e) {
e.stopPropagation();
$(e.currentTarget).tooltip('hide');
}
toggleEmptyState($label, $btn, action) {
this.emptyState.classList.toggle('hidden', !!this.prioritizedLabels[0].querySelector(':scope > li'));
}
toggleLabelPriority($label, action, persistState) {
if (persistState == null) {
persistState = true;
2017-09-10 17:25:29 +05:30
}
2018-03-17 18:26:18 +05:30
const _this = this;
const url = $label.find('.js-toggle-priority').data('url');
let $target = this.prioritizedLabels;
let $from = this.otherLabels;
const rollbackLabelPosition = this.rollbackLabelPosition.bind(this, $label, action);
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
if (action === 'remove') {
$target = this.otherLabels;
$from = this.prioritizedLabels;
}
$label.detach().appendTo($target);
if ($from.find('li').length) {
$from.find('.empty-message').removeClass('hidden');
2016-11-03 12:29:30 +05:30
}
2018-03-17 18:26:18 +05:30
if ($target.find('> li:not(.empty-message)').length) {
$target.find('.empty-message').addClass('hidden');
}
// Return if we are not persisting state
if (!persistState) {
return;
}
if (action === 'remove') {
axios.delete(url)
.catch(rollbackLabelPosition);
2016-11-03 12:29:30 +05:30
2018-03-17 18:26:18 +05:30
// Restore empty message
if (!$from.find('li').length) {
2016-11-03 12:29:30 +05:30
$from.find('.empty-message').removeClass('hidden');
}
2018-03-17 18:26:18 +05:30
} else {
this.savePrioritySort($label, action)
.catch(rollbackLabelPosition);
2016-11-03 12:29:30 +05:30
}
2018-03-17 18:26:18 +05:30
}
2016-11-03 12:29:30 +05:30
2018-03-17 18:26:18 +05:30
onPrioritySortUpdate() {
this.savePrioritySort()
.catch(() => flash(this.errorMessage));
}
2016-11-03 12:29:30 +05:30
2018-03-17 18:26:18 +05:30
savePrioritySort() {
return axios.post(this.prioritizedLabels.data('url'), {
label_ids: this.getSortedLabelsIds(),
});
}
2016-11-03 12:29:30 +05:30
2018-03-17 18:26:18 +05:30
rollbackLabelPosition($label, originalAction) {
const action = originalAction === 'remove' ? 'add' : 'remove';
this.toggleLabelPriority($label, action, false);
flash(this.errorMessage);
}
2016-11-03 12:29:30 +05:30
2018-03-17 18:26:18 +05:30
getSortedLabelsIds() {
const sortedIds = [];
this.prioritizedLabels.find('> li').each(function() {
const id = $(this).data('id');
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
if (id) {
sortedIds.push(id);
}
});
return sortedIds;
2016-11-03 12:29:30 +05:30
}
2018-03-17 18:26:18 +05:30
}