2019-09-16 08:06:26 -04:00
|
|
|
/* eslint-disable class-methods-use-this, no-underscore-dangle, no-param-reassign, func-names */
|
2018-03-09 15:18:59 -05:00
|
|
|
|
|
|
|
import $ from 'jquery';
|
2018-05-31 08:10:41 -04:00
|
|
|
import Sortable from 'sortablejs';
|
2021-02-11 07:08:52 -05:00
|
|
|
import { dispose } from '~/tooltips';
|
2021-12-07 10:15:03 -05:00
|
|
|
import createFlash from '~/flash';
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
|
|
|
import { __ } from '~/locale';
|
2017-10-02 08:32:53 -04:00
|
|
|
|
2017-10-10 13:22:34 -04:00
|
|
|
export default class LabelManager {
|
|
|
|
constructor({ togglePriorityButton, prioritizedLabels, otherLabels } = {}) {
|
|
|
|
this.togglePriorityButton = togglePriorityButton || $('.js-toggle-priority');
|
2018-05-16 22:20:21 -04:00
|
|
|
this.prioritizedLabels = prioritizedLabels || $('.js-prioritized-labels');
|
2017-10-10 13:22:34 -04:00
|
|
|
this.otherLabels = otherLabels || $('.js-other-labels');
|
2019-05-03 08:36:40 -04:00
|
|
|
this.errorMessage = __('Unable to update label prioritization at this time');
|
2017-10-10 13:22:34 -04:00
|
|
|
this.emptyState = document.querySelector('#js-priority-labels-empty-state');
|
2018-06-05 05:47:43 -04:00
|
|
|
this.$badgeItemTemplate = $('#js-badge-item-template');
|
2019-04-12 06:54:23 -04:00
|
|
|
|
|
|
|
if ('sortable' in this.prioritizedLabels.data()) {
|
|
|
|
Sortable.create(this.prioritizedLabels.get(0), {
|
|
|
|
filter: '.empty-message',
|
|
|
|
forceFallback: true,
|
|
|
|
fallbackClass: 'is-dragging',
|
|
|
|
dataIdAttr: 'data-id',
|
|
|
|
onUpdate: this.onPrioritySortUpdate.bind(this),
|
|
|
|
});
|
|
|
|
}
|
2017-10-10 13:22:34 -04:00
|
|
|
this.bindEvents();
|
|
|
|
}
|
2016-09-12 13:05:47 -04:00
|
|
|
|
2017-10-10 13:22:34 -04:00
|
|
|
bindEvents() {
|
|
|
|
return this.togglePriorityButton.on('click', this, this.onTogglePriorityClick);
|
|
|
|
}
|
2016-09-12 13:05:47 -04:00
|
|
|
|
2017-10-10 13:22:34 -04:00
|
|
|
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')}`);
|
2020-10-06 11:08:33 -04:00
|
|
|
dispose($tooltip);
|
2017-10-10 13:22:34 -04:00
|
|
|
_this.toggleLabelPriority($label, action);
|
|
|
|
_this.toggleEmptyState($label, $btn, action);
|
|
|
|
}
|
2016-11-12 18:51:47 -05:00
|
|
|
|
2019-09-16 08:06:26 -04:00
|
|
|
toggleEmptyState() {
|
2018-10-10 03:13:34 -04:00
|
|
|
this.emptyState.classList.toggle(
|
|
|
|
'hidden',
|
2019-06-03 18:51:02 -04:00
|
|
|
Boolean(this.prioritizedLabels[0].querySelector(':scope > li')),
|
2018-10-10 03:13:34 -04:00
|
|
|
);
|
2017-10-10 13:22:34 -04:00
|
|
|
}
|
2016-09-12 13:05:47 -04:00
|
|
|
|
2017-10-10 13:22:34 -04:00
|
|
|
toggleLabelPriority($label, action, persistState) {
|
|
|
|
if (persistState == null) {
|
|
|
|
persistState = true;
|
2016-09-12 13:05:47 -04:00
|
|
|
}
|
2017-10-10 13:22:34 -04:00
|
|
|
const url = $label.find('.js-toggle-priority').data('url');
|
|
|
|
let $target = this.prioritizedLabels;
|
|
|
|
let $from = this.otherLabels;
|
2018-01-24 22:03:21 -05:00
|
|
|
const rollbackLabelPosition = this.rollbackLabelPosition.bind(this, $label, action);
|
|
|
|
|
2017-10-10 13:22:34 -04:00
|
|
|
if (action === 'remove') {
|
|
|
|
$target = this.otherLabels;
|
|
|
|
$from = this.prioritizedLabels;
|
2016-09-12 13:05:47 -04:00
|
|
|
}
|
2018-04-18 22:19:55 -04:00
|
|
|
|
|
|
|
const $detachedLabel = $label.detach();
|
|
|
|
this.toggleLabelPriorityBadge($detachedLabel, action);
|
2019-01-31 00:38:55 -05:00
|
|
|
|
|
|
|
const $labelEls = $target.find('li.label-list-item');
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If there is a label element in the target, we'd want to
|
|
|
|
* append the new label just right next to it.
|
|
|
|
*/
|
|
|
|
if ($labelEls.length) {
|
|
|
|
$labelEls.last().after($detachedLabel);
|
|
|
|
} else {
|
|
|
|
$detachedLabel.appendTo($target);
|
|
|
|
}
|
2018-04-18 22:19:55 -04:00
|
|
|
|
2017-10-10 13:22:34 -04:00
|
|
|
if ($from.find('li').length) {
|
|
|
|
$from.find('.empty-message').removeClass('hidden');
|
|
|
|
}
|
|
|
|
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') {
|
2018-10-10 03:13:34 -04:00
|
|
|
axios.delete(url).catch(rollbackLabelPosition);
|
2018-01-24 22:03:21 -05:00
|
|
|
|
2017-10-10 13:22:34 -04:00
|
|
|
// Restore empty message
|
|
|
|
if (!$from.find('li').length) {
|
|
|
|
$from.find('.empty-message').removeClass('hidden');
|
|
|
|
}
|
|
|
|
} else {
|
2018-10-10 03:13:34 -04:00
|
|
|
this.savePrioritySort($label, action).catch(rollbackLabelPosition);
|
2016-09-12 13:05:47 -04:00
|
|
|
}
|
2017-10-10 13:22:34 -04:00
|
|
|
}
|
2016-09-12 13:05:47 -04:00
|
|
|
|
2018-04-18 22:19:55 -04:00
|
|
|
toggleLabelPriorityBadge($label, action) {
|
|
|
|
if (action === 'remove') {
|
|
|
|
$('.js-priority-badge', $label).remove();
|
|
|
|
} else {
|
2018-06-05 05:47:43 -04:00
|
|
|
$('.label-links', $label).append(this.$badgeItemTemplate.clone().html());
|
2018-04-18 22:19:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-10 13:22:34 -04:00
|
|
|
onPrioritySortUpdate() {
|
2021-06-18 02:10:20 -04:00
|
|
|
this.savePrioritySort().catch(() =>
|
|
|
|
createFlash({
|
|
|
|
message: this.errorMessage,
|
|
|
|
}),
|
|
|
|
);
|
2017-10-10 13:22:34 -04:00
|
|
|
}
|
2016-09-12 13:05:47 -04:00
|
|
|
|
2017-10-10 13:22:34 -04:00
|
|
|
savePrioritySort() {
|
2018-01-24 22:03:21 -05:00
|
|
|
return axios.post(this.prioritizedLabels.data('url'), {
|
|
|
|
label_ids: this.getSortedLabelsIds(),
|
2017-10-10 13:22:34 -04:00
|
|
|
});
|
|
|
|
}
|
2017-01-06 11:52:18 -05:00
|
|
|
|
2017-10-10 13:22:34 -04:00
|
|
|
rollbackLabelPosition($label, originalAction) {
|
|
|
|
const action = originalAction === 'remove' ? 'add' : 'remove';
|
|
|
|
this.toggleLabelPriority($label, action, false);
|
2021-06-18 02:10:20 -04:00
|
|
|
createFlash({
|
|
|
|
message: this.errorMessage,
|
|
|
|
});
|
2016-09-12 13:05:47 -04:00
|
|
|
}
|
|
|
|
|
2017-10-10 13:22:34 -04:00
|
|
|
getSortedLabelsIds() {
|
|
|
|
const sortedIds = [];
|
2020-12-23 07:10:26 -05:00
|
|
|
this.prioritizedLabels.find('> li').each(function () {
|
2017-10-10 13:22:34 -04:00
|
|
|
const id = $(this).data('id');
|
|
|
|
|
|
|
|
if (id) {
|
|
|
|
sortedIds.push(id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return sortedIds;
|
|
|
|
}
|
|
|
|
}
|