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

150 lines
4.2 KiB
JavaScript
Raw Normal View History

2016-09-09 12:57:59 -04:00
((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-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
getElement(selector) {
2016-07-24 16:45:11 -04:00
return this.container.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
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.getElement('.labels-filter .is-indeterminate')
.each((i, el) => labelsToKeep.push($(el).data('labelId')));
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) {
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-09-09 12:57:59 -04:00
}
2016-07-24 16:45:11 -04:00
2016-09-09 12:57:59 -04:00
getLabelsToApply() {
const labelIds = [];
const $labels = this.form.find('.labels-filter input[name="update[label_ids][]"]');
2016-07-24 16:45:11 -04:00
$labels.each(function(k, label) {
if (label) {
return labelIds.push(parseInt($(label).val()));
}
});
return labelIds;
2016-09-09 12:57:59 -04:00
}
2016-07-24 16:45:11 -04:00
/**
* Returns Label IDs that will be removed from issue selection
* @return {Array} Array of labels IDs
*/
2016-09-09 12:57:59 -04:00
getLabelsToRemove() {
const result = [];
const indeterminatedLabels = this.getUnmarkedIndeterminedLabels();
const labelsToApply = this.getLabelsToApply();
2016-07-24 16:45:11 -04:00
indeterminatedLabels.map(function(id) {
// 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-07-24 16:45:11 -04:00
if (labelsToApply.indexOf(id) === -1) {
return result.push(id);
}
});
return result;
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;
2016-07-24 16:45:11 -04:00
2016-09-09 12:57:59 -04:00
})(window.gl || (window.gl = {}));