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

50 lines
1.3 KiB
JavaScript
Raw Normal View History

import 'deckar01-task_list';
import Flash from './flash';
export default class TaskList {
constructor(options = {}) {
this.selector = options.selector;
this.dataType = options.dataType;
this.fieldName = options.fieldName;
2017-02-08 05:54:33 -05:00
this.onSuccess = options.onSuccess || (() => {});
this.onError = function showFlash(response) {
let errorMessages = '';
if (response.responseJSON) {
errorMessages = response.responseJSON.errors.join(' ');
}
return new Flash(errorMessages || 'Update failed', 'alert');
};
this.init();
}
init() {
// Prevent duplicate event bindings
this.disable();
2017-02-08 03:10:04 -05:00
$(`${this.selector} .js-task-list-container`).taskList('enable');
$(document).on('tasklist:changed', `${this.selector} .js-task-list-container`, this.update.bind(this));
}
disable() {
2017-02-08 03:10:04 -05:00
$(`${this.selector} .js-task-list-container`).taskList('disable');
2017-02-09 17:14:21 -05:00
$(document).off('tasklist:changed', `${this.selector} .js-task-list-container`);
}
update(e) {
const $target = $(e.target);
2017-02-08 03:10:04 -05:00
const patchData = {};
patchData[this.dataType] = {
[this.fieldName]: $target.val(),
};
return $.ajax({
type: 'PATCH',
url: $target.data('update-url') || $('form.js-issuable-update').attr('action'),
data: patchData,
success: this.onSuccess,
error: this.onError,
});
}
}