2018-03-09 20:18:59 +00:00
|
|
|
import $ from 'jquery';
|
2017-05-23 14:21:33 +00:00
|
|
|
import 'deckar01-task_list';
|
2018-02-02 11:26:08 +00:00
|
|
|
import axios from './lib/utils/axios_utils';
|
2017-10-02 12:32:53 +00:00
|
|
|
import Flash from './flash';
|
2017-02-07 06:42:21 +00:00
|
|
|
|
2017-07-06 18:05:58 +00:00
|
|
|
export default class TaskList {
|
2017-02-07 06:42:21 +00:00
|
|
|
constructor(options = {}) {
|
|
|
|
this.selector = options.selector;
|
|
|
|
this.dataType = options.dataType;
|
2017-02-15 05:53:17 +00:00
|
|
|
this.fieldName = options.fieldName;
|
2019-01-10 21:22:28 +00:00
|
|
|
this.lockVersion = options.lockVersion;
|
2017-02-08 10:54:33 +00:00
|
|
|
this.onSuccess = options.onSuccess || (() => {});
|
2019-01-10 21:22:28 +00:00
|
|
|
this.onError = options.onError || function showFlash(e) {
|
2017-01-16 18:43:03 +00:00
|
|
|
let errorMessages = '';
|
|
|
|
|
2018-02-02 12:36:51 +00:00
|
|
|
if (e.response.data && typeof e.response.data === 'object') {
|
|
|
|
errorMessages = e.response.data.errors.join(' ');
|
2017-01-16 18:43:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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');
|
2018-10-10 07:13:34 +00:00
|
|
|
$(document).on(
|
|
|
|
'tasklist:changed',
|
|
|
|
`${this.selector} .js-task-list-container`,
|
|
|
|
this.update.bind(this),
|
|
|
|
);
|
2017-02-07 06:42:21 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 00:34:30 +00:00
|
|
|
disableTaskListItems() {
|
2017-02-08 08:10:04 +00:00
|
|
|
$(`${this.selector} .js-task-list-container`).taskList('disable');
|
2019-01-16 00:34:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enableTaskListItems() {
|
|
|
|
$(`${this.selector} .js-task-list-container`).taskList('enable');
|
|
|
|
}
|
|
|
|
|
|
|
|
disable() {
|
|
|
|
this.disableTaskListItems();
|
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);
|
2019-01-18 20:56:10 +00:00
|
|
|
const { lineNumber, lineSource, checked } = e.detail;
|
2017-02-08 08:10:04 +00:00
|
|
|
const patchData = {};
|
2019-01-18 00:52:22 +00:00
|
|
|
|
2017-02-07 06:42:21 +00:00
|
|
|
patchData[this.dataType] = {
|
2017-02-15 05:53:17 +00:00
|
|
|
[this.fieldName]: $target.val(),
|
2019-01-11 00:07:09 +00:00
|
|
|
lock_version: this.lockVersion,
|
2019-01-18 00:52:22 +00:00
|
|
|
update_task: {
|
|
|
|
line_number: lineNumber,
|
|
|
|
line_source: lineSource,
|
2019-01-18 20:56:10 +00:00
|
|
|
checked: checked,
|
2019-01-18 00:52:22 +00:00
|
|
|
},
|
2017-02-07 06:42:21 +00:00
|
|
|
};
|
2018-02-02 11:26:08 +00:00
|
|
|
|
2019-01-16 00:34:30 +00:00
|
|
|
this.disableTaskListItems();
|
|
|
|
|
2018-10-10 07:13:34 +00:00
|
|
|
return axios
|
|
|
|
.patch($target.data('updateUrl') || $('form.js-issuable-update').attr('action'), patchData)
|
2019-01-16 00:33:58 +00:00
|
|
|
.then(({ data }) => {
|
|
|
|
this.lockVersion = data.lock_version;
|
2019-01-16 00:34:30 +00:00
|
|
|
this.enableTaskListItems();
|
|
|
|
|
2019-01-16 00:33:58 +00:00
|
|
|
return this.onSuccess(data);
|
|
|
|
})
|
2019-01-16 00:34:30 +00:00
|
|
|
.catch(({ response }) => {
|
|
|
|
this.enableTaskListItems();
|
|
|
|
|
|
|
|
return this.onError(response.data);
|
|
|
|
});
|
2017-02-07 06:42:21 +00:00
|
|
|
}
|
|
|
|
}
|