debian-mirror-gitlab/app/assets/javascripts/sidebar/components/labels/sidebar_labels.vue

102 lines
3.1 KiB
Vue
Raw Normal View History

2020-11-24 15:15:51 +05:30
<script>
import $ from 'jquery';
import { difference, union } from 'lodash';
import flash from '~/flash';
import axios from '~/lib/utils/axios_utils';
import { __ } from '~/locale';
import { DropdownVariant } from '~/vue_shared/components/sidebar/labels_select_vue/constants';
import LabelsSelect from '~/vue_shared/components/sidebar/labels_select_vue/labels_select_root.vue';
export default {
components: {
LabelsSelect,
},
variant: DropdownVariant.Sidebar,
inject: [
'allowLabelCreate',
'allowLabelEdit',
'allowScopedLabels',
'iid',
'initiallySelectedLabels',
'issuableType',
'labelsFetchPath',
'labelsManagePath',
'labelsUpdatePath',
'projectIssuesPath',
'projectPath',
],
2021-01-03 14:25:43 +05:30
data() {
return {
isLabelsSelectInProgress: false,
2020-11-24 15:15:51 +05:30
selectedLabels: this.initiallySelectedLabels,
2021-01-03 14:25:43 +05:30
};
2020-11-24 15:15:51 +05:30
},
methods: {
handleDropdownClose() {
$(this.$el).trigger('hidden.gl.dropdown');
},
2021-01-03 14:25:43 +05:30
handleUpdateSelectedLabels(dropdownLabels) {
2020-11-24 15:15:51 +05:30
const currentLabelIds = this.selectedLabels.map(label => label.id);
2021-01-03 14:25:43 +05:30
const userAddedLabelIds = dropdownLabels.filter(label => label.set).map(label => label.id);
const userRemovedLabelIds = dropdownLabels.filter(label => !label.set).map(label => label.id);
2020-11-24 15:15:51 +05:30
2021-01-03 14:25:43 +05:30
const labelIds = difference(union(currentLabelIds, userAddedLabelIds), userRemovedLabelIds);
2020-11-24 15:15:51 +05:30
2021-01-03 14:25:43 +05:30
this.updateSelectedLabels(labelIds);
},
handleLabelRemove(labelId) {
const currentLabelIds = this.selectedLabels.map(label => label.id);
const labelIds = difference(currentLabelIds, [labelId]);
this.updateSelectedLabels(labelIds);
},
updateSelectedLabels(labelIds) {
this.isLabelsSelectInProgress = true;
2020-11-24 15:15:51 +05:30
axios({
data: {
[this.issuableType]: {
2021-01-03 14:25:43 +05:30
label_ids: labelIds,
2020-11-24 15:15:51 +05:30
},
},
method: 'put',
url: this.labelsUpdatePath,
})
2021-01-03 14:25:43 +05:30
.then(({ data }) => {
this.selectedLabels = data.labels;
})
2020-11-24 15:15:51 +05:30
.catch(() => flash(__('An error occurred while updating labels.')))
.finally(() => {
2021-01-03 14:25:43 +05:30
this.isLabelsSelectInProgress = false;
2020-11-24 15:15:51 +05:30
});
},
},
};
</script>
<template>
<labels-select
class="block labels js-labels-block"
2021-01-03 14:25:43 +05:30
:allow-label-remove="allowLabelEdit"
2020-11-24 15:15:51 +05:30
:allow-label-create="allowLabelCreate"
:allow-label-edit="allowLabelEdit"
:allow-multiselect="true"
:allow-scoped-labels="allowScopedLabels"
:footer-create-label-title="__('Create project label')"
:footer-manage-label-title="__('Manage project labels')"
:labels-create-title="__('Create project label')"
:labels-fetch-path="labelsFetchPath"
:labels-filter-base-path="projectIssuesPath"
:labels-manage-path="labelsManagePath"
2021-01-03 14:25:43 +05:30
:labels-select-in-progress="isLabelsSelectInProgress"
2020-11-24 15:15:51 +05:30
:selected-labels="selectedLabels"
:variant="$options.sidebar"
2021-01-03 14:25:43 +05:30
data-qa-selector="labels_block"
2020-11-24 15:15:51 +05:30
@onDropdownClose="handleDropdownClose"
2021-01-03 14:25:43 +05:30
@onLabelRemove="handleLabelRemove"
2020-11-24 15:15:51 +05:30
@updateSelectedLabels="handleUpdateSelectedLabels"
>
{{ __('None') }}
</labels-select>
</template>