debian-mirror-gitlab/app/assets/javascripts/issues_bulk_assignment.js.es6

150 lines
4.2 KiB
JavaScript
Raw Normal View History

2016-11-03 12:29:30 +05:30
((global) => {
class IssuableBulkActions {
constructor({ container, form, issues } = {}) {
this.container = container || $('.content'),
this.form = form || this.getElement('.bulk-update');
this.issues = issues || this.getElement('.issues-list .issue');
2016-09-13 17:45:13 +05:30
this.form.data('bulkActions', this);
this.willUpdateLabels = false;
this.bindEvents();
2016-09-29 09:46:39 +05:30
// Fixes bulk-assign not working when navigating through pages
2016-09-13 17:45:13 +05:30
Issuable.initChecks();
}
2016-11-03 12:29:30 +05:30
getElement(selector) {
2016-09-13 17:45:13 +05:30
return this.container.find(selector);
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
bindEvents() {
2016-09-13 17:45:13 +05:30
return this.form.off('submit').on('submit', this.onFormSubmit.bind(this));
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
onFormSubmit(e) {
2016-09-13 17:45:13 +05:30
e.preventDefault();
return this.submit();
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
submit() {
const _this = this;
const xhr = $.ajax({
2016-09-13 17:45:13 +05:30
url: this.form.attr('action'),
method: this.form.attr('method'),
dataType: 'JSON',
data: this.getFormDataAsObject()
});
2016-11-03 12:29:30 +05:30
xhr.done(() => window.location.reload());
xhr.fail(() => new Flash("Issue update failed"));
2016-09-13 17:45:13 +05:30
return xhr.always(this.onFormSubmitAlways.bind(this));
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
onFormSubmitAlways() {
2016-09-13 17:45:13 +05:30
return this.form.find('[type="submit"]').enable();
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
getSelectedIssues() {
2016-09-13 17:45:13 +05:30
return this.issues.has('.selected_issue:checked');
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
getLabelsFromSelection() {
const labels = [];
2016-09-13 17:45:13 +05:30
this.getSelectedIssues().map(function() {
2016-11-03 12:29:30 +05:30
const labelsData = $(this).data('labels');
if (labelsData) {
return labelsData.map(function(labelId) {
2016-09-13 17:45:13 +05:30
if (labels.indexOf(labelId) === -1) {
return labels.push(labelId);
}
});
}
});
return labels;
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
/**
* Will return only labels that were marked previously and the user has unmarked
* @return {Array} Label IDs
*/
2016-11-03 12:29:30 +05:30
getUnmarkedIndeterminedLabels() {
const result = [];
const labelsToKeep = [];
this.getElement('.labels-filter .is-indeterminate')
.each((i, el) => labelsToKeep.push($(el).data('labelId')));
this.getLabelsFromSelection().forEach((id) => {
2016-09-13 17:45:13 +05:30
if (labelsToKeep.indexOf(id) === -1) {
result.push(id);
}
2016-11-03 12:29:30 +05:30
});
2016-09-13 17:45:13 +05:30
return result;
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
/**
* Simple form serialization, it will return just what we need
* Returns key/value pairs from form data
*/
2016-11-03 12:29:30 +05:30
getFormDataAsObject() {
const formData = {
2016-09-13 17:45:13 +05:30
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(),
2016-09-29 09:46:39 +05:30
issuable_ids: this.form.find('input[name="update[issuable_ids]"]').val(),
2016-09-13 17:45:13 +05:30
subscription_event: this.form.find('input[name="update[subscription_event]"]').val(),
add_label_ids: [],
remove_label_ids: []
}
};
if (this.willUpdateLabels) {
this.getLabelsToApply().map(function(id) {
return formData.update.add_label_ids.push(id);
});
this.getLabelsToRemove().map(function(id) {
return formData.update.remove_label_ids.push(id);
});
}
return formData;
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
getLabelsToApply() {
const labelIds = [];
const $labels = this.form.find('.labels-filter input[name="update[label_ids][]"]');
2016-09-13 17:45:13 +05:30
$labels.each(function(k, label) {
if (label) {
return labelIds.push(parseInt($(label).val()));
}
});
return labelIds;
2016-11-03 12:29:30 +05:30
}
2016-09-13 17:45:13 +05:30
/**
* Returns Label IDs that will be removed from issue selection
* @return {Array} Array of labels IDs
*/
2016-11-03 12:29:30 +05:30
getLabelsToRemove() {
const result = [];
const indeterminatedLabels = this.getUnmarkedIndeterminedLabels();
const labelsToApply = this.getLabelsToApply();
2016-09-13 17:45:13 +05:30
indeterminatedLabels.map(function(id) {
2016-09-29 09:46:39 +05:30
// We need to exclude label IDs that will be applied
// By not doing this will cause issues from selection to not add labels at all
2016-09-13 17:45:13 +05:30
if (labelsToApply.indexOf(id) === -1) {
return result.push(id);
}
});
return result;
2016-11-03 12:29:30 +05:30
}
}
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
global.IssuableBulkActions = IssuableBulkActions;
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
})(window.gl || (window.gl = {}));