gitlab-org--gitlab-foss/app/assets/javascripts/issues_bulk_assignment.js

164 lines
5.4 KiB
JavaScript
Raw Normal View History

/* eslint-disable comma-dangle, quotes, consistent-return, func-names, array-callback-return, space-before-function-paren, prefer-arrow-callback, max-len, no-unused-expressions, no-sequences, no-underscore-dangle, no-unused-vars, no-param-reassign */
/* global Issuable */
/* global Flash */
2016-09-09 12:57:59 -04:00
((global) => {
class IssuableBulkActions {
2016-12-13 16:38:21 -05:00
constructor({ container, form, issues, prefixId } = {}) {
2016-12-15 18:02:45 -05:00
this.prefixId = prefixId || 'issue_';
2016-09-09 12:57:59 -04:00
this.form = form || this.getElement('.bulk-update');
this.$labelDropdown = this.form.find('.js-label-select');
2016-09-09 12:57:59 -04:00
this.issues = issues || this.getElement('.issues-list .issue');
2016-07-24 16:45:11 -04:00
this.form.data('bulkActions', this);
this.willUpdateLabels = false;
this.bindEvents();
// Fixes bulk-assign not working when navigating through pages
2016-07-24 16:45:11 -04:00
Issuable.initChecks();
}
2016-09-09 12:57:59 -04:00
bindEvents() {
2016-07-24 16:45:11 -04:00
return this.form.off('submit').on('submit', this.onFormSubmit.bind(this));
2016-09-09 12:57:59 -04:00
}
2016-07-24 16:45:11 -04:00
2016-09-09 12:57:59 -04:00
onFormSubmit(e) {
2016-07-24 16:45:11 -04:00
e.preventDefault();
return this.submit();
2016-09-09 12:57:59 -04:00
}
2016-07-24 16:45:11 -04:00
2016-09-09 12:57:59 -04:00
submit() {
const _this = this;
const xhr = $.ajax({
2016-07-24 16:45:11 -04:00
url: this.form.attr('action'),
method: this.form.attr('method'),
dataType: 'JSON',
data: this.getFormDataAsObject()
});
2016-09-09 12:57:59 -04:00
xhr.done(() => window.location.reload());
xhr.fail(() => new Flash("Issue update failed"));
2016-07-24 16:45:11 -04:00
return xhr.always(this.onFormSubmitAlways.bind(this));
2016-09-09 12:57:59 -04:00
}
2016-07-24 16:45:11 -04:00
2016-09-09 12:57:59 -04:00
onFormSubmitAlways() {
2016-07-24 16:45:11 -04:00
return this.form.find('[type="submit"]').enable();
2016-09-09 12:57:59 -04:00
}
2016-07-24 16:45:11 -04:00
2016-09-09 12:57:59 -04:00
getSelectedIssues() {
2016-07-24 16:45:11 -04:00
return this.issues.has('.selected_issue:checked');
2016-09-09 12:57:59 -04:00
}
2016-07-24 16:45:11 -04:00
2016-09-09 12:57:59 -04:00
getLabelsFromSelection() {
const labels = [];
2016-07-24 16:45:11 -04:00
this.getSelectedIssues().map(function() {
2016-09-09 12:57:59 -04:00
const labelsData = $(this).data('labels');
if (labelsData) {
return labelsData.map(function(labelId) {
2016-07-24 16:45:11 -04:00
if (labels.indexOf(labelId) === -1) {
return labels.push(labelId);
}
});
}
});
return labels;
2016-09-09 12:57:59 -04:00
}
2016-07-24 16:45:11 -04:00
/**
* Will return only labels that were marked previously and the user has unmarked
* @return {Array} Label IDs
*/
2016-09-09 12:57:59 -04:00
getUnmarkedIndeterminedLabels() {
const result = [];
const labelsToKeep = this.$labelDropdown.data('indeterminate');
this.getLabelsFromSelection().forEach((id) => {
if (labelsToKeep.indexOf(id) === -1) {
result.push(id);
}
});
2016-07-24 16:45:11 -04:00
return result;
2016-09-09 12:57:59 -04:00
}
2016-07-24 16:45:11 -04:00
/**
* Simple form serialization, it will return just what we need
* Returns key/value pairs from form data
*/
2016-09-09 12:57:59 -04:00
getFormDataAsObject() {
const formData = {
2016-07-24 16:45:11 -04:00
update: {
state_event: this.form.find('input[name="update[state_event]"]').val(),
assignee_id: this.form.find('input[name="update[assignee_id]"]').val(),
milestone_id: this.form.find('input[name="update[milestone_id]"]').val(),
issuable_ids: this.form.find('input[name="update[issuable_ids]"]').val(),
2016-07-24 16:45:11 -04:00
subscription_event: this.form.find('input[name="update[subscription_event]"]').val(),
add_label_ids: [],
remove_label_ids: []
}
};
if (this.willUpdateLabels) {
formData.update.add_label_ids = this.$labelDropdown.data('marked');
formData.update.remove_label_ids = this.$labelDropdown.data('unmarked');
2016-07-24 16:45:11 -04:00
}
return formData;
2016-09-09 12:57:59 -04:00
}
2016-07-24 16:45:11 -04:00
setOriginalDropdownData() {
2016-12-15 18:02:45 -05:00
const $labelSelect = $('.bulk-update .js-label-select');
2016-12-13 16:38:21 -05:00
$labelSelect.data('common', this.getOriginalCommonIds());
$labelSelect.data('marked', this.getOriginalMarkedIds());
$labelSelect.data('indeterminate', this.getOriginalIndeterminateIds());
2016-09-09 12:57:59 -04:00
}
2016-07-24 16:45:11 -04:00
// From issuable's initial bulk selection
getOriginalCommonIds() {
2016-12-15 18:02:45 -05:00
const labelIds = [];
2016-07-24 16:45:11 -04:00
this.getElement('.selected_issue:checked').each((i, el) => {
labelIds.push(this.getElement(`#${this.prefixId}${el.dataset.id}`).data('labels'));
});
return _.intersection.apply(this, labelIds);
}
2016-07-24 16:45:11 -04:00
// From issuable's initial bulk selection
getOriginalMarkedIds() {
2016-12-15 18:02:45 -05:00
const labelIds = [];
this.getElement('.selected_issue:checked').each((i, el) => {
labelIds.push(this.getElement(`#${this.prefixId}${el.dataset.id}`).data('labels'));
2016-07-24 16:45:11 -04:00
});
2016-12-15 18:02:45 -05:00
return _.intersection.apply(this, labelIds);
}
// From issuable's initial bulk selection
getOriginalIndeterminateIds() {
2016-12-15 18:02:45 -05:00
const uniqueIds = [];
const labelIds = [];
let issuableLabels = [];
// Collect unique label IDs for all checked issues
this.getElement('.selected_issue:checked').each((i, el) => {
issuableLabels = this.getElement(`#${this.prefixId}${el.dataset.id}`).data('labels');
issuableLabels.forEach((labelId) => {
// 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
return _.difference(uniqueIds, _.intersection.apply(this, labelIds));
}
getElement(selector) {
2016-12-13 16:38:21 -05:00
this.scopeEl = this.scopeEl || $('.content');
return this.scopeEl.find(selector);
2016-09-09 12:57:59 -04:00
}
}
2016-07-24 16:45:11 -04:00
2016-09-09 12:57:59 -04:00
global.IssuableBulkActions = IssuableBulkActions;
})(window.gl || (window.gl = {}));