2017-01-16 18:43:03 +00:00
|
|
|
/* global Flash */
|
2017-02-07 06:42:21 +00:00
|
|
|
require('vendor/task_list');
|
|
|
|
|
|
|
|
class TaskList {
|
|
|
|
constructor(options = {}) {
|
|
|
|
this.selector = options.selector;
|
|
|
|
this.dataType = options.dataType;
|
2017-02-15 05:53:17 +00:00
|
|
|
this.fieldName = options.fieldName;
|
2017-02-08 10:54:33 +00:00
|
|
|
this.onSuccess = options.onSuccess || (() => {});
|
2017-01-16 18:43:03 +00:00
|
|
|
this.onError = function showFlash(response) {
|
|
|
|
let errorMessages = '';
|
|
|
|
|
|
|
|
if (response.responseJSON) {
|
|
|
|
errorMessages = response.responseJSON.errors.join(' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Flash(errorMessages || 'Update failed', 'alert');
|
|
|
|
};
|
|
|
|
|
2017-02-07 06:42:21 +00:00
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
2017-02-08 06:04:00 +00:00
|
|
|
// Prevent duplicate event bindings
|
|
|
|
this.disable();
|
2017-02-08 08:10:04 +00:00
|
|
|
$(`${this.selector} .js-task-list-container`).taskList('enable');
|
2017-02-08 09:23:45 +00:00
|
|
|
$(document).on('tasklist:changed', `${this.selector} .js-task-list-container`, this.update.bind(this));
|
2017-02-07 06:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
disable() {
|
2017-02-08 08:10:04 +00:00
|
|
|
$(`${this.selector} .js-task-list-container`).taskList('disable');
|
2017-02-09 22:14:21 +00:00
|
|
|
$(document).off('tasklist:changed', `${this.selector} .js-task-list-container`);
|
2017-02-07 06:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
update(e) {
|
2017-02-08 09:23:45 +00:00
|
|
|
const $target = $(e.target);
|
2017-02-08 08:10:04 +00:00
|
|
|
const patchData = {};
|
2017-02-07 06:42:21 +00:00
|
|
|
patchData[this.dataType] = {
|
2017-02-15 05:53:17 +00:00
|
|
|
[this.fieldName]: $target.val(),
|
2017-02-07 06:42:21 +00:00
|
|
|
};
|
|
|
|
return $.ajax({
|
|
|
|
type: 'PATCH',
|
2017-02-08 09:23:45 +00:00
|
|
|
url: $target.data('update-url') || $('form.js-issuable-update').attr('action'),
|
2017-02-07 06:42:21 +00:00
|
|
|
data: patchData,
|
2017-02-08 09:23:45 +00:00
|
|
|
success: this.onSuccess,
|
2017-01-16 18:43:03 +00:00
|
|
|
error: this.onError,
|
2017-02-07 06:42:21 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
window.gl = window.gl || {};
|
|
|
|
window.gl.TaskList = TaskList;
|