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

125 lines
4.3 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2020-07-28 23:09:34 +05:30
import { difference, intersection, union } from 'lodash';
2020-10-24 23:57:45 +05:30
import { deprecatedCreateFlash as Flash } from './flash';
2021-03-11 19:13:27 +05:30
import axios from './lib/utils/axios_utils';
2019-09-04 21:01:54 +05:30
import { __ } from './locale';
2017-09-10 17:25:29 +05:30
export default {
2019-09-30 21:07:59 +05:30
init({ form, issues, prefixId } = {}) {
2017-09-10 17:25:29 +05:30
this.prefixId = prefixId || 'issue_';
this.form = form || this.getElement('.bulk-update');
this.$labelDropdown = this.form.find('.js-label-select');
this.issues = issues || this.getElement('.issues-list .issue');
this.willUpdateLabels = false;
this.bindEvents();
},
bindEvents() {
2021-02-22 17:27:13 +05:30
// eslint-disable-next-line @gitlab/no-global-event-off
2017-09-10 17:25:29 +05:30
return this.form.off('submit').on('submit', this.onFormSubmit.bind(this));
},
onFormSubmit(e) {
e.preventDefault();
return this.submit();
},
submit() {
2018-03-17 18:26:18 +05:30
axios[this.form.attr('method')](this.form.attr('action'), this.getFormDataAsObject())
.then(() => window.location.reload())
.catch(() => this.onFormSubmitFailure());
2017-09-10 17:25:29 +05:30
},
onFormSubmitFailure() {
this.form.find('[type="submit"]').enable();
2019-09-04 21:01:54 +05:30
return new Flash(__('Issue update failed'));
2017-09-10 17:25:29 +05:30
},
/**
* Simple form serialization, it will return just what we need
* Returns key/value pairs from form data
*/
getFormDataAsObject() {
const formData = {
update: {
state_event: this.form.find('input[name="update[state_event]"]').val(),
assignee_ids: [this.form.find('input[name="update[assignee_ids][]"]').val()],
milestone_id: this.form.find('input[name="update[milestone_id]"]').val(),
issuable_ids: this.form.find('input[name="update[issuable_ids]"]').val(),
subscription_event: this.form.find('input[name="update[subscription_event]"]').val(),
2020-07-28 23:09:34 +05:30
health_status: this.form.find('input[name="update[health_status]"]').val(),
epic_id: this.form.find('input[name="update[epic_id]"]').val(),
2021-03-11 19:13:27 +05:30
sprint_id: this.form.find('input[name="update[iteration_id]"]').val(),
2017-09-10 17:25:29 +05:30
add_label_ids: [],
2018-12-13 13:39:08 +05:30
remove_label_ids: [],
},
2017-09-10 17:25:29 +05:30
};
if (this.willUpdateLabels) {
2020-07-28 23:09:34 +05:30
formData.update.add_label_ids = this.$labelDropdown.data('user-checked');
formData.update.remove_label_ids = this.$labelDropdown.data('user-unchecked');
2017-09-10 17:25:29 +05:30
}
return formData;
},
setOriginalDropdownData() {
const $labelSelect = $('.bulk-update .js-label-select');
2020-07-28 23:09:34 +05:30
const userCheckedIds = $labelSelect.data('user-checked') || [];
const userUncheckedIds = $labelSelect.data('user-unchecked') || [];
// Common labels plus user checked labels minus user unchecked labels
const checkedIdsToShow = difference(
union(this.getOriginalCommonIds(), userCheckedIds),
userUncheckedIds,
);
// Indeterminate labels minus user checked labels minus user unchecked labels
const indeterminateIdsToShow = difference(
this.getOriginalIndeterminateIds(),
userCheckedIds,
userUncheckedIds,
);
$labelSelect.data('marked', checkedIdsToShow);
$labelSelect.data('indeterminate', indeterminateIdsToShow);
2017-09-10 17:25:29 +05:30
},
// From issuable's initial bulk selection
getOriginalCommonIds() {
const labelIds = [];
2021-04-29 21:17:54 +05:30
this.getElement('.issuable-list input[type="checkbox"]:checked').each((i, el) => {
2017-09-10 17:25:29 +05:30
labelIds.push(this.getElement(`#${this.prefixId}${el.dataset.id}`).data('labels'));
});
2020-05-24 23:13:21 +05:30
return intersection.apply(this, labelIds);
2017-09-10 17:25:29 +05:30
},
// From issuable's initial bulk selection
getOriginalIndeterminateIds() {
const uniqueIds = [];
const labelIds = [];
let issuableLabels = [];
// Collect unique label IDs for all checked issues
2021-04-29 21:17:54 +05:30
this.getElement('.issuable-list input[type="checkbox"]:checked').each((i, el) => {
2017-09-10 17:25:29 +05:30
issuableLabels = this.getElement(`#${this.prefixId}${el.dataset.id}`).data('labels');
2021-03-08 18:12:59 +05:30
issuableLabels.forEach((labelId) => {
2017-09-10 17:25:29 +05:30
// Store unique IDs
if (uniqueIds.indexOf(labelId) === -1) {
uniqueIds.push(labelId);
}
});
// Store array of IDs per issuable
labelIds.push(issuableLabels);
});
// Add uniqueIds to add it as argument for _.intersection
labelIds.unshift(uniqueIds);
// Return IDs that are present but not in all selected issueables
2021-03-08 18:12:59 +05:30
return uniqueIds.filter((x) => !intersection.apply(this, labelIds).includes(x));
2017-09-10 17:25:29 +05:30
},
getElement(selector) {
this.scopeEl = this.scopeEl || $('.content');
return this.scopeEl.find(selector);
},
};