2017-01-10 18:02:20 -05:00
|
|
|
/* eslint-disable no-useless-return, func-names, space-before-function-paren, wrap-iife, no-var, no-underscore-dangle, prefer-arrow-callback, max-len, one-var, no-unused-vars, one-var-declaration-per-line, prefer-template, no-new, consistent-return, object-shorthand, comma-dangle, no-shadow, no-param-reassign, brace-style, vars-on-top, quotes, no-lonely-if, no-else-return, dot-notation, no-empty, no-return-assign, camelcase, prefer-spread */
|
2016-12-14 00:26:26 -05:00
|
|
|
/* global Issuable */
|
|
|
|
/* global ListLabel */
|
2018-03-09 15:18:59 -05:00
|
|
|
|
|
|
|
import $ from 'jquery';
|
2017-08-03 16:31:53 -04:00
|
|
|
import _ from 'underscore';
|
2018-01-30 11:40:47 -05:00
|
|
|
import { __ } from './locale';
|
|
|
|
import axios from './lib/utils/axios_utils';
|
2017-05-15 18:12:43 -04:00
|
|
|
import IssuableBulkUpdateActions from './issuable_bulk_update_actions';
|
2017-08-02 05:27:24 -04:00
|
|
|
import DropdownUtils from './filtered_search/dropdown_utils';
|
2017-10-09 05:04:13 -04:00
|
|
|
import CreateLabelDropdown from './create_label';
|
2018-01-30 11:40:47 -05:00
|
|
|
import flash from './flash';
|
2018-04-06 08:12:21 -04:00
|
|
|
import ModalStore from './boards/stores/modal_store';
|
2017-05-15 18:12:43 -04:00
|
|
|
|
2017-11-01 08:32:15 -04:00
|
|
|
export default class LabelsSelect {
|
2017-11-01 17:20:59 -04:00
|
|
|
constructor(els, options = {}) {
|
2017-11-01 08:32:15 -04:00
|
|
|
var _this, $els;
|
|
|
|
_this = this;
|
2017-02-01 12:02:17 -05:00
|
|
|
|
2017-11-01 08:32:15 -04:00
|
|
|
$els = $(els);
|
2017-02-01 12:02:17 -05:00
|
|
|
|
2017-11-01 08:32:15 -04:00
|
|
|
if (!els) {
|
|
|
|
$els = $('.js-label-select');
|
|
|
|
}
|
2017-02-01 12:02:17 -05:00
|
|
|
|
2017-11-01 08:32:15 -04:00
|
|
|
$els.each(function(i, dropdown) {
|
2018-02-27 04:34:51 -05:00
|
|
|
var $block, $colorPreview, $dropdown, $form, $loading, $selectbox, $sidebarCollapsedValue, $value, abilityName, defaultLabel, enableLabelCreateButton, issueURLSplit, issueUpdateURL, labelUrl, namespacePath, projectPath, saveLabelData, selectedLabel, showAny, showNo, $sidebarLabelTooltip, initialSelected, $toggleText, fieldName, useId, propertyName, showMenuAbove, $container, $dropdownContainer;
|
2017-11-01 08:32:15 -04:00
|
|
|
$dropdown = $(dropdown);
|
|
|
|
$dropdownContainer = $dropdown.closest('.labels-filter');
|
|
|
|
$toggleText = $dropdown.find('.dropdown-toggle-text');
|
2018-02-20 17:20:48 -05:00
|
|
|
namespacePath = $dropdown.data('namespacePath');
|
|
|
|
projectPath = $dropdown.data('projectPath');
|
2017-11-01 08:32:15 -04:00
|
|
|
labelUrl = $dropdown.data('labels');
|
|
|
|
issueUpdateURL = $dropdown.data('issueUpdate');
|
|
|
|
selectedLabel = $dropdown.data('selected');
|
|
|
|
if ((selectedLabel != null) && !$dropdown.hasClass('js-multiselect')) {
|
|
|
|
selectedLabel = selectedLabel.split(',');
|
|
|
|
}
|
2018-02-20 17:20:48 -05:00
|
|
|
showNo = $dropdown.data('showNo');
|
|
|
|
showAny = $dropdown.data('showAny');
|
2017-11-01 08:32:15 -04:00
|
|
|
showMenuAbove = $dropdown.data('showMenuAbove');
|
2018-02-20 17:20:48 -05:00
|
|
|
defaultLabel = $dropdown.data('defaultLabel');
|
|
|
|
abilityName = $dropdown.data('abilityName');
|
2017-11-01 08:32:15 -04:00
|
|
|
$selectbox = $dropdown.closest('.selectbox');
|
|
|
|
$block = $selectbox.closest('.block');
|
|
|
|
$form = $dropdown.closest('form, .js-issuable-update');
|
|
|
|
$sidebarCollapsedValue = $block.find('.sidebar-collapsed-icon span');
|
|
|
|
$sidebarLabelTooltip = $block.find('.js-sidebar-labels-tooltip');
|
|
|
|
$value = $block.find('.value');
|
|
|
|
$loading = $block.find('.block-loading').fadeOut();
|
2018-02-20 17:20:48 -05:00
|
|
|
fieldName = $dropdown.data('fieldName');
|
2017-11-01 08:32:15 -04:00
|
|
|
useId = $dropdown.is('.js-issuable-form-dropdown, .js-filter-bulk-update, .js-label-sidebar-dropdown');
|
|
|
|
propertyName = useId ? 'id' : 'title';
|
|
|
|
initialSelected = $selectbox
|
2018-02-20 17:20:48 -05:00
|
|
|
.find('input[name="' + $dropdown.data('fieldName') + '"]')
|
2017-11-01 08:32:15 -04:00
|
|
|
.map(function () {
|
|
|
|
return this.value;
|
|
|
|
}).get();
|
2017-11-01 17:20:59 -04:00
|
|
|
const handleClick = options.handleClick;
|
2016-08-08 10:43:07 -04:00
|
|
|
|
2017-11-01 08:32:15 -04:00
|
|
|
$sidebarLabelTooltip.tooltip();
|
2016-08-18 06:38:12 -04:00
|
|
|
|
2017-11-01 08:32:15 -04:00
|
|
|
if ($dropdown.closest('.dropdown').find('.dropdown-new-label').length) {
|
|
|
|
new CreateLabelDropdown($dropdown.closest('.dropdown').find('.dropdown-new-label'), namespacePath, projectPath);
|
|
|
|
}
|
2016-08-08 10:43:07 -04:00
|
|
|
|
2017-11-01 08:32:15 -04:00
|
|
|
saveLabelData = function() {
|
|
|
|
var data, selected;
|
|
|
|
selected = $dropdown.closest('.selectbox').find("input[name='" + fieldName + "']").map(function() {
|
|
|
|
return this.value;
|
|
|
|
}).get();
|
2016-09-27 11:23:15 -04:00
|
|
|
|
2017-11-01 08:32:15 -04:00
|
|
|
if (_.isEqual(initialSelected, selected)) return;
|
|
|
|
initialSelected = selected;
|
2016-09-27 11:23:15 -04:00
|
|
|
|
2017-11-01 08:32:15 -04:00
|
|
|
data = {};
|
|
|
|
data[abilityName] = {};
|
|
|
|
data[abilityName].label_ids = selected;
|
|
|
|
if (!selected.length) {
|
|
|
|
data[abilityName].label_ids = [''];
|
|
|
|
}
|
|
|
|
$loading.removeClass('hidden').fadeIn();
|
|
|
|
$dropdown.trigger('loading.gl.dropdown');
|
2018-01-30 11:40:47 -05:00
|
|
|
axios.put(issueUpdateURL, data)
|
|
|
|
.then(({ data }) => {
|
|
|
|
var labelCount, template, labelTooltipTitle, labelTitles;
|
|
|
|
$loading.fadeOut();
|
|
|
|
$dropdown.trigger('loaded.gl.dropdown');
|
|
|
|
$selectbox.hide();
|
2018-02-27 04:34:51 -05:00
|
|
|
data.issueUpdateURL = issueUpdateURL;
|
2018-01-30 11:40:47 -05:00
|
|
|
labelCount = 0;
|
2018-02-27 04:34:51 -05:00
|
|
|
if (data.labels.length && issueUpdateURL) {
|
|
|
|
template = LabelsSelect.getLabelTemplate({
|
|
|
|
labels: data.labels,
|
|
|
|
issueUpdateURL,
|
|
|
|
});
|
2018-01-30 11:40:47 -05:00
|
|
|
labelCount = data.labels.length;
|
2016-07-13 06:53:27 -04:00
|
|
|
}
|
2018-01-30 11:40:47 -05:00
|
|
|
else {
|
2018-02-27 04:34:51 -05:00
|
|
|
template = '<span class="no-value">None</span>';
|
2018-01-30 11:40:47 -05:00
|
|
|
}
|
|
|
|
$value.removeAttr('style').html(template);
|
|
|
|
$sidebarCollapsedValue.text(labelCount);
|
2018-01-31 04:27:30 -05:00
|
|
|
|
2018-01-30 11:40:47 -05:00
|
|
|
if (data.labels.length) {
|
|
|
|
labelTitles = data.labels.map(function(label) {
|
|
|
|
return label.title;
|
|
|
|
});
|
2018-01-31 04:27:30 -05:00
|
|
|
|
2018-01-30 11:40:47 -05:00
|
|
|
if (labelTitles.length > 5) {
|
|
|
|
labelTitles = labelTitles.slice(0, 5);
|
|
|
|
labelTitles.push('and ' + (data.labels.length - 5) + ' more');
|
|
|
|
}
|
2018-01-31 04:27:30 -05:00
|
|
|
|
2018-01-30 11:40:47 -05:00
|
|
|
labelTooltipTitle = labelTitles.join(', ');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
labelTooltipTitle = '';
|
|
|
|
$sidebarLabelTooltip.tooltip('destroy');
|
|
|
|
}
|
2018-01-31 04:27:30 -05:00
|
|
|
|
2018-01-30 11:40:47 -05:00
|
|
|
$sidebarLabelTooltip
|
|
|
|
.attr('title', labelTooltipTitle)
|
|
|
|
.tooltip('fixTitle');
|
2018-01-31 04:27:30 -05:00
|
|
|
|
2018-01-30 11:40:47 -05:00
|
|
|
$('.has-tooltip', $value).tooltip({
|
|
|
|
container: 'body'
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(() => flash(__('Error saving label update.')));
|
2017-11-01 08:32:15 -04:00
|
|
|
};
|
|
|
|
$dropdown.glDropdown({
|
|
|
|
showMenuAbove: showMenuAbove,
|
|
|
|
data: function(term, callback) {
|
2018-01-30 11:40:47 -05:00
|
|
|
axios.get(labelUrl)
|
|
|
|
.then((res) => {
|
|
|
|
let data = _.chain(res.data).groupBy(function(label) {
|
|
|
|
return label.title;
|
|
|
|
}).map(function(label) {
|
|
|
|
var color;
|
|
|
|
color = _.map(label, function(dup) {
|
|
|
|
return dup.color;
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
2018-01-30 11:40:47 -05:00
|
|
|
return {
|
|
|
|
id: label[0].id,
|
|
|
|
title: label[0].title,
|
|
|
|
color: color,
|
|
|
|
duplicate: color.length > 1
|
|
|
|
};
|
|
|
|
}).value();
|
|
|
|
if ($dropdown.hasClass('js-extra-options')) {
|
|
|
|
var extraData = [];
|
|
|
|
if (showNo) {
|
|
|
|
extraData.unshift({
|
|
|
|
id: 0,
|
|
|
|
title: 'No Label'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (showAny) {
|
|
|
|
extraData.unshift({
|
|
|
|
isAny: true,
|
|
|
|
title: 'Any Label'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (extraData.length) {
|
|
|
|
extraData.push('divider');
|
|
|
|
data = extraData.concat(data);
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2018-01-31 04:27:30 -05:00
|
|
|
|
2018-01-30 11:40:47 -05:00
|
|
|
callback(data);
|
|
|
|
if (showMenuAbove) {
|
|
|
|
$dropdown.data('glDropdown').positionMenuAbove();
|
2016-11-29 02:47:02 -05:00
|
|
|
}
|
2018-01-30 11:40:47 -05:00
|
|
|
})
|
|
|
|
.catch(() => flash(__('Error fetching labels.')));
|
2017-11-01 08:32:15 -04:00
|
|
|
},
|
|
|
|
renderRow: function(label, instance) {
|
|
|
|
var $a, $li, color, colorEl, indeterminate, removesAll, selectedClass, spacing, i, marked, dropdownName, dropdownValue;
|
|
|
|
$li = $('<li>');
|
|
|
|
$a = $('<a href="#">');
|
|
|
|
selectedClass = [];
|
|
|
|
removesAll = label.id <= 0 || (label.id == null);
|
|
|
|
if ($dropdown.hasClass('js-filter-bulk-update')) {
|
|
|
|
indeterminate = $dropdown.data('indeterminate') || [];
|
|
|
|
marked = $dropdown.data('marked') || [];
|
|
|
|
|
|
|
|
if (indeterminate.indexOf(label.id) !== -1) {
|
|
|
|
selectedClass.push('is-indeterminate');
|
2016-10-04 10:52:35 -04:00
|
|
|
}
|
2017-11-01 08:32:15 -04:00
|
|
|
|
|
|
|
if (marked.indexOf(label.id) !== -1) {
|
|
|
|
// Remove is-indeterminate class if the item will be marked as active
|
|
|
|
i = selectedClass.indexOf('is-indeterminate');
|
|
|
|
if (i !== -1) {
|
|
|
|
selectedClass.splice(i, 1);
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2017-11-01 08:32:15 -04:00
|
|
|
selectedClass.push('is-active');
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2017-11-01 08:32:15 -04:00
|
|
|
} else {
|
|
|
|
if (this.id(label)) {
|
|
|
|
dropdownName = $dropdown.data('fieldName');
|
|
|
|
dropdownValue = this.id(label).toString().replace(/'/g, '\\\'');
|
|
|
|
|
|
|
|
if ($form.find("input[type='hidden'][name='" + dropdownName + "'][value='" + dropdownValue + "']").length) {
|
|
|
|
selectedClass.push('is-active');
|
|
|
|
}
|
2016-10-04 10:52:35 -04:00
|
|
|
}
|
2017-11-01 08:32:15 -04:00
|
|
|
|
|
|
|
if ($dropdown.hasClass('js-multiselect') && removesAll) {
|
|
|
|
selectedClass.push('dropdown-clear-active');
|
2016-10-04 10:52:35 -04:00
|
|
|
}
|
2017-11-01 08:32:15 -04:00
|
|
|
}
|
|
|
|
if (label.duplicate) {
|
2018-02-26 02:12:55 -05:00
|
|
|
color = DropdownUtils.duplicateLabelColor(label.color);
|
2017-11-01 08:32:15 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (label.color != null) {
|
|
|
|
color = label.color[0];
|
2016-08-30 07:10:05 -04:00
|
|
|
}
|
2017-11-01 08:32:15 -04:00
|
|
|
}
|
|
|
|
if (color) {
|
|
|
|
colorEl = "<span class='dropdown-label-box' style='background: " + color + "'></span>";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
colorEl = '';
|
|
|
|
}
|
|
|
|
// We need to identify which items are actually labels
|
|
|
|
if (label.id) {
|
|
|
|
selectedClass.push('label-item');
|
|
|
|
$a.attr('data-label-id', label.id);
|
|
|
|
}
|
2017-12-15 15:29:53 -05:00
|
|
|
$a.addClass(selectedClass.join(' ')).html(`${colorEl} ${_.escape(label.title)}`);
|
2017-11-01 08:32:15 -04:00
|
|
|
// Return generated html
|
|
|
|
return $li.html($a).prop('outerHTML');
|
|
|
|
},
|
|
|
|
search: {
|
|
|
|
fields: ['title']
|
|
|
|
},
|
|
|
|
selectable: true,
|
|
|
|
filterable: true,
|
|
|
|
selected: $dropdown.data('selected') || [],
|
|
|
|
toggleLabel: function(selected, el) {
|
2018-02-27 06:43:04 -05:00
|
|
|
var $dropdownParent = $dropdown.parent();
|
|
|
|
var $dropdownInputField = $dropdownParent.find('.dropdown-input-field');
|
2017-11-01 08:32:15 -04:00
|
|
|
var isSelected = el !== null ? el.hasClass('is-active') : false;
|
|
|
|
var title = selected.title;
|
|
|
|
var selectedLabels = this.selected;
|
|
|
|
|
2018-02-27 06:43:04 -05:00
|
|
|
if ($dropdownInputField.length && $dropdownInputField.val().length) {
|
|
|
|
$dropdownParent.find('.dropdown-input-clear').trigger('click');
|
|
|
|
}
|
|
|
|
|
2017-11-01 08:32:15 -04:00
|
|
|
if (selected.id === 0) {
|
|
|
|
this.selected = [];
|
|
|
|
return 'No Label';
|
|
|
|
}
|
|
|
|
else if (isSelected) {
|
|
|
|
this.selected.push(title);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var index = this.selected.indexOf(title);
|
|
|
|
this.selected.splice(index, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (selectedLabels.length === 1) {
|
|
|
|
return selectedLabels;
|
|
|
|
}
|
|
|
|
else if (selectedLabels.length) {
|
|
|
|
return selectedLabels[0] + " +" + (selectedLabels.length - 1) + " more";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return defaultLabel;
|
|
|
|
}
|
|
|
|
},
|
2018-02-20 17:20:48 -05:00
|
|
|
fieldName: $dropdown.data('fieldName'),
|
2017-11-01 08:32:15 -04:00
|
|
|
id: function(label) {
|
|
|
|
if (label.id <= 0) return label.title;
|
|
|
|
|
|
|
|
if ($dropdown.hasClass('js-issuable-form-dropdown')) {
|
|
|
|
return label.id;
|
|
|
|
}
|
2016-08-30 07:10:05 -04:00
|
|
|
|
2017-11-01 08:32:15 -04:00
|
|
|
if ($dropdown.hasClass("js-filter-submit") && (label.isAny == null)) {
|
|
|
|
return label.title;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return label.id;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
hidden: function() {
|
|
|
|
var isIssueIndex, isMRIndex, page, selectedLabels;
|
|
|
|
page = $('body').attr('data-page');
|
|
|
|
isIssueIndex = page === 'projects:issues:index';
|
|
|
|
isMRIndex = page === 'projects:merge_requests:index';
|
|
|
|
$selectbox.hide();
|
|
|
|
// display:block overrides the hide-collapse rule
|
|
|
|
$value.removeAttr('style');
|
|
|
|
|
|
|
|
if ($dropdown.hasClass('js-issuable-form-dropdown')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($('html').hasClass('issue-boards-page')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($dropdown.hasClass('js-multiselect')) {
|
|
|
|
if ($dropdown.hasClass('js-filter-submit') && (isIssueIndex || isMRIndex)) {
|
|
|
|
selectedLabels = $dropdown.closest('form').find("input:hidden[name='" + ($dropdown.data('fieldName')) + "']");
|
|
|
|
Issuable.filterResults($dropdown.closest('form'));
|
2016-10-04 10:52:35 -04:00
|
|
|
}
|
2017-11-01 08:32:15 -04:00
|
|
|
else if ($dropdown.hasClass('js-filter-submit')) {
|
|
|
|
$dropdown.closest('form').submit();
|
2016-10-04 10:52:35 -04:00
|
|
|
}
|
|
|
|
else {
|
2017-11-01 08:32:15 -04:00
|
|
|
if (!$dropdown.hasClass('js-filter-bulk-update')) {
|
|
|
|
saveLabelData();
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2017-11-01 08:32:15 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
multiSelect: $dropdown.hasClass('js-multiselect'),
|
|
|
|
vue: $dropdown.hasClass('js-issue-board-sidebar'),
|
2018-02-21 10:17:03 -05:00
|
|
|
clicked: function (clickEvent) {
|
|
|
|
const { $el, e, isMarking } = clickEvent;
|
|
|
|
const label = clickEvent.selectedObj;
|
2017-11-01 08:32:15 -04:00
|
|
|
|
|
|
|
var isIssueIndex, isMRIndex, page, boardsModel;
|
|
|
|
var fadeOutLoader = () => {
|
|
|
|
$loading.fadeOut();
|
|
|
|
};
|
2016-08-30 06:21:29 -04:00
|
|
|
|
2017-11-01 08:32:15 -04:00
|
|
|
page = $('body').attr('data-page');
|
|
|
|
isIssueIndex = page === 'projects:issues:index';
|
|
|
|
isMRIndex = page === 'projects:merge_requests:index';
|
2016-08-30 06:21:29 -04:00
|
|
|
|
2017-11-01 08:32:15 -04:00
|
|
|
if ($dropdown.parent().find('.is-active:not(.dropdown-clear-active)').length) {
|
|
|
|
$dropdown.parent()
|
|
|
|
.find('.dropdown-clear-active')
|
|
|
|
.removeClass('is-active');
|
|
|
|
}
|
2016-08-30 06:21:29 -04:00
|
|
|
|
2017-11-01 08:32:15 -04:00
|
|
|
if ($dropdown.hasClass('js-issuable-form-dropdown')) {
|
|
|
|
return;
|
|
|
|
}
|
2016-08-30 06:21:29 -04:00
|
|
|
|
2017-11-01 08:32:15 -04:00
|
|
|
if ($dropdown.hasClass('js-filter-bulk-update')) {
|
|
|
|
_this.enableBulkLabelDropdown();
|
|
|
|
_this.setDropdownData($dropdown, isMarking, label.id);
|
|
|
|
return;
|
|
|
|
}
|
2017-01-16 16:56:53 -05:00
|
|
|
|
2017-11-01 08:32:15 -04:00
|
|
|
if ($dropdown.closest('.add-issues-modal').length) {
|
2018-04-06 08:12:21 -04:00
|
|
|
boardsModel = ModalStore.store.filter;
|
2017-11-01 08:32:15 -04:00
|
|
|
}
|
2016-08-30 06:21:29 -04:00
|
|
|
|
2017-11-01 08:32:15 -04:00
|
|
|
if (boardsModel) {
|
|
|
|
if (label.isAny) {
|
|
|
|
boardsModel['label_name'] = [];
|
|
|
|
} else if ($el.hasClass('is-active')) {
|
|
|
|
boardsModel['label_name'].push(label.title);
|
2017-02-01 12:02:17 -05:00
|
|
|
}
|
|
|
|
|
2017-11-01 08:32:15 -04:00
|
|
|
e.preventDefault();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if ($dropdown.hasClass('js-filter-submit') && (isIssueIndex || isMRIndex)) {
|
|
|
|
if (!$dropdown.hasClass('js-multiselect')) {
|
|
|
|
selectedLabel = label.title;
|
|
|
|
return Issuable.filterResults($dropdown.closest('form'));
|
2016-10-04 10:52:35 -04:00
|
|
|
}
|
2017-11-01 08:32:15 -04:00
|
|
|
}
|
|
|
|
else if ($dropdown.hasClass('js-filter-submit')) {
|
|
|
|
return $dropdown.closest('form').submit();
|
|
|
|
}
|
|
|
|
else if ($dropdown.hasClass('js-issue-board-sidebar')) {
|
|
|
|
if ($el.hasClass('is-active')) {
|
|
|
|
gl.issueBoards.BoardsStore.detail.issue.labels.push(new ListLabel({
|
|
|
|
id: label.id,
|
|
|
|
title: label.title,
|
|
|
|
color: label.color[0],
|
|
|
|
textColor: '#fff'
|
|
|
|
}));
|
2016-10-04 10:52:35 -04:00
|
|
|
}
|
2017-11-01 08:32:15 -04:00
|
|
|
else {
|
|
|
|
var labels = gl.issueBoards.BoardsStore.detail.issue.labels;
|
|
|
|
labels = labels.filter(function (selectedLabel) {
|
|
|
|
return selectedLabel.id !== label.id;
|
|
|
|
});
|
|
|
|
gl.issueBoards.BoardsStore.detail.issue.labels = labels;
|
2016-10-04 10:52:35 -04:00
|
|
|
}
|
2016-10-05 07:20:59 -04:00
|
|
|
|
2017-11-01 08:32:15 -04:00
|
|
|
$loading.fadeIn();
|
|
|
|
|
|
|
|
gl.issueBoards.BoardsStore.detail.issue.update($dropdown.attr('data-issue-update'))
|
|
|
|
.then(fadeOutLoader)
|
|
|
|
.catch(fadeOutLoader);
|
|
|
|
}
|
2017-11-02 06:09:20 -04:00
|
|
|
else if (handleClick) {
|
|
|
|
e.preventDefault();
|
|
|
|
handleClick(label);
|
|
|
|
}
|
2017-11-01 08:32:15 -04:00
|
|
|
else {
|
|
|
|
if ($dropdown.hasClass('js-multiselect')) {
|
2016-10-05 07:20:59 -04:00
|
|
|
|
2017-10-24 09:48:41 -04:00
|
|
|
}
|
2016-10-04 10:52:35 -04:00
|
|
|
else {
|
2017-11-01 08:32:15 -04:00
|
|
|
return saveLabelData();
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2017-11-01 08:32:15 -04:00
|
|
|
}
|
|
|
|
},
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
|
|
|
|
2017-11-01 08:32:15 -04:00
|
|
|
// Set dropdown data
|
|
|
|
_this.setOriginalDropdownData($dropdownContainer, $dropdown);
|
|
|
|
});
|
|
|
|
this.bindEvents();
|
|
|
|
}
|
|
|
|
|
2018-02-27 04:34:51 -05:00
|
|
|
static getLabelTemplate(tplData) {
|
|
|
|
// We could use ES6 template string here
|
|
|
|
// and properly indent markup for readability
|
|
|
|
// but that also introduces unintended white-space
|
|
|
|
// so best approach is to use traditional way of
|
|
|
|
// concatenation
|
|
|
|
// see: http://2ality.com/2016/05/template-literal-whitespace.html#joining-arrays
|
|
|
|
const tpl = _.template([
|
|
|
|
'<% _.each(labels, function(label){ %>',
|
|
|
|
'<a href="<%- issueUpdateURL.slice(0, issueUpdateURL.lastIndexOf("/")) %>?label_name[]=<%- encodeURIComponent(label.title) %>">',
|
|
|
|
'<span class="label has-tooltip color-label" title="<%- label.description %>" style="background-color: <%- label.color %>; color: <%- label.text_color %>;">',
|
|
|
|
'<%- label.title %>',
|
|
|
|
'</span>',
|
|
|
|
'</a>',
|
|
|
|
'<% }); %>',
|
|
|
|
].join(''));
|
|
|
|
|
|
|
|
return tpl(tplData);
|
|
|
|
}
|
|
|
|
|
2017-11-01 08:32:15 -04:00
|
|
|
bindEvents() {
|
|
|
|
return $('body').on('change', '.selected_issue', this.onSelectCheckboxIssue);
|
|
|
|
}
|
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
onSelectCheckboxIssue() {
|
|
|
|
if ($('.selected_issue:checked').length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return $('.issues-bulk-update .labels-filter .dropdown-toggle-text').text('Label');
|
|
|
|
}
|
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
enableBulkLabelDropdown() {
|
|
|
|
IssuableBulkUpdateActions.willUpdateLabels = true;
|
|
|
|
}
|
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
setDropdownData($dropdown, isMarking, value) {
|
|
|
|
var i, markedIds, unmarkedIds, indeterminateIds;
|
|
|
|
|
|
|
|
markedIds = $dropdown.data('marked') || [];
|
|
|
|
unmarkedIds = $dropdown.data('unmarked') || [];
|
|
|
|
indeterminateIds = $dropdown.data('indeterminate') || [];
|
|
|
|
|
|
|
|
if (isMarking) {
|
|
|
|
markedIds.push(value);
|
|
|
|
|
|
|
|
i = indeterminateIds.indexOf(value);
|
|
|
|
if (i > -1) {
|
|
|
|
indeterminateIds.splice(i, 1);
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
|
|
|
|
2017-11-01 08:32:15 -04:00
|
|
|
i = unmarkedIds.indexOf(value);
|
|
|
|
if (i > -1) {
|
|
|
|
unmarkedIds.splice(i, 1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If marked item (not common) is unmarked
|
|
|
|
i = markedIds.indexOf(value);
|
|
|
|
if (i > -1) {
|
|
|
|
markedIds.splice(i, 1);
|
2016-11-29 02:47:02 -05:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-11-01 08:32:15 -04:00
|
|
|
// If an indeterminate item is being unmarked
|
|
|
|
if (IssuableBulkUpdateActions.getOriginalIndeterminateIds().indexOf(value) > -1) {
|
|
|
|
unmarkedIds.push(value);
|
|
|
|
}
|
2016-11-29 02:47:02 -05:00
|
|
|
|
2017-11-01 08:32:15 -04:00
|
|
|
// If a marked item is being unmarked
|
|
|
|
// (a marked item could also be a label that is present in all selection)
|
|
|
|
if (IssuableBulkUpdateActions.getOriginalCommonIds().indexOf(value) > -1) {
|
|
|
|
unmarkedIds.push(value);
|
|
|
|
}
|
|
|
|
}
|
2016-11-29 02:47:02 -05:00
|
|
|
|
2017-11-01 08:32:15 -04:00
|
|
|
$dropdown.data('marked', markedIds);
|
|
|
|
$dropdown.data('unmarked', unmarkedIds);
|
|
|
|
$dropdown.data('indeterminate', indeterminateIds);
|
|
|
|
}
|
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
setOriginalDropdownData($container, $dropdown) {
|
|
|
|
const labels = [];
|
|
|
|
$container.find('[name="label_name[]"]').map(function() {
|
|
|
|
return labels.push(this.value);
|
|
|
|
});
|
|
|
|
$dropdown.data('marked', labels);
|
|
|
|
}
|
|
|
|
}
|