2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2017-05-23 10:21:33 -04:00
|
|
|
import 'deckar01-task_list';
|
2019-01-29 08:50:22 -05:00
|
|
|
import { __ } from '~/locale';
|
2018-02-02 06:26:08 -05:00
|
|
|
import axios from './lib/utils/axios_utils';
|
2020-08-20 05:09:55 -04:00
|
|
|
import { deprecatedCreateFlash as Flash } from './flash';
|
2017-02-07 01:42:21 -05:00
|
|
|
|
2017-07-06 14:05:58 -04:00
|
|
|
export default class TaskList {
|
2017-02-07 01:42:21 -05:00
|
|
|
constructor(options = {}) {
|
|
|
|
this.selector = options.selector;
|
|
|
|
this.dataType = options.dataType;
|
2017-02-15 00:53:17 -05:00
|
|
|
this.fieldName = options.fieldName;
|
2019-01-10 16:22:28 -05:00
|
|
|
this.lockVersion = options.lockVersion;
|
2019-01-23 18:44:15 -05:00
|
|
|
this.taskListContainerSelector = `${this.selector} .js-task-list-container`;
|
2019-01-26 09:49:49 -05:00
|
|
|
this.updateHandler = this.update.bind(this);
|
|
|
|
this.onSuccess = options.onSuccess || (() => {});
|
2019-01-22 15:53:08 -05:00
|
|
|
this.onError =
|
|
|
|
options.onError ||
|
|
|
|
function showFlash(e) {
|
|
|
|
let errorMessages = '';
|
2017-01-16 13:43:03 -05:00
|
|
|
|
2019-01-22 15:53:08 -05:00
|
|
|
if (e.response.data && typeof e.response.data === 'object') {
|
|
|
|
errorMessages = e.response.data.errors.join(' ');
|
|
|
|
}
|
2017-01-16 13:43:03 -05:00
|
|
|
|
2019-01-29 08:50:22 -05:00
|
|
|
return new Flash(errorMessages || __('Update failed'), 'alert');
|
2019-01-22 15:53:08 -05:00
|
|
|
};
|
2017-01-16 13:43:03 -05:00
|
|
|
|
2017-02-07 01:42:21 -05:00
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
2019-01-26 09:49:49 -05:00
|
|
|
this.disable(); // Prevent duplicate event bindings
|
|
|
|
|
2020-09-25 08:10:00 -04:00
|
|
|
const taskListFields = document.querySelectorAll(
|
|
|
|
`${this.taskListContainerSelector} .js-task-list-field[data-value]`,
|
|
|
|
);
|
|
|
|
|
|
|
|
taskListFields.forEach(taskListField => {
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
taskListField.value = taskListField.dataset.value;
|
|
|
|
});
|
|
|
|
|
2019-01-23 18:44:15 -05:00
|
|
|
$(this.taskListContainerSelector).taskList('enable');
|
2019-01-26 09:49:49 -05:00
|
|
|
$(document).on('tasklist:changed', this.taskListContainerSelector, this.updateHandler);
|
2017-02-07 01:42:21 -05:00
|
|
|
}
|
|
|
|
|
2019-01-29 08:50:22 -05:00
|
|
|
getTaskListTarget(e) {
|
|
|
|
return e && e.currentTarget ? $(e.currentTarget) : $(this.taskListContainerSelector);
|
2019-01-23 18:44:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
disableTaskListItems(e) {
|
|
|
|
this.getTaskListTarget(e).taskList('disable');
|
2019-01-15 19:34:30 -05:00
|
|
|
}
|
|
|
|
|
2019-01-23 18:44:15 -05:00
|
|
|
enableTaskListItems(e) {
|
|
|
|
this.getTaskListTarget(e).taskList('enable');
|
2019-01-15 19:34:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
disable() {
|
|
|
|
this.disableTaskListItems();
|
2019-01-23 18:44:15 -05:00
|
|
|
$(document).off('tasklist:changed', this.taskListContainerSelector);
|
2017-02-07 01:42:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
update(e) {
|
2017-02-08 04:23:45 -05:00
|
|
|
const $target = $(e.target);
|
2019-01-21 16:16:37 -05:00
|
|
|
const { index, checked, lineNumber, lineSource } = e.detail;
|
2017-02-08 03:10:04 -05:00
|
|
|
const patchData = {};
|
2019-01-17 19:52:22 -05:00
|
|
|
|
2017-02-07 01:42:21 -05:00
|
|
|
patchData[this.dataType] = {
|
2017-02-15 00:53:17 -05:00
|
|
|
[this.fieldName]: $target.val(),
|
2019-01-10 19:07:09 -05:00
|
|
|
lock_version: this.lockVersion,
|
2019-01-17 19:52:22 -05:00
|
|
|
update_task: {
|
2019-01-22 15:53:08 -05:00
|
|
|
index,
|
|
|
|
checked,
|
2019-01-17 19:52:22 -05:00
|
|
|
line_number: lineNumber,
|
|
|
|
line_source: lineSource,
|
|
|
|
},
|
2017-02-07 01:42:21 -05:00
|
|
|
};
|
2018-02-02 06:26:08 -05:00
|
|
|
|
2019-01-23 18:44:15 -05:00
|
|
|
this.disableTaskListItems(e);
|
2019-01-15 19:34:30 -05:00
|
|
|
|
2018-10-10 03:13:34 -04:00
|
|
|
return axios
|
|
|
|
.patch($target.data('updateUrl') || $('form.js-issuable-update').attr('action'), patchData)
|
2019-01-15 19:33:58 -05:00
|
|
|
.then(({ data }) => {
|
|
|
|
this.lockVersion = data.lock_version;
|
2019-01-23 18:44:15 -05:00
|
|
|
this.enableTaskListItems(e);
|
2019-01-15 19:34:30 -05:00
|
|
|
|
2019-01-15 19:33:58 -05:00
|
|
|
return this.onSuccess(data);
|
|
|
|
})
|
2019-01-15 19:34:30 -05:00
|
|
|
.catch(({ response }) => {
|
2019-01-23 18:44:15 -05:00
|
|
|
this.enableTaskListItems(e);
|
2019-01-15 19:34:30 -05:00
|
|
|
|
|
|
|
return this.onError(response.data);
|
|
|
|
});
|
2017-02-07 01:42:21 -05:00
|
|
|
}
|
|
|
|
}
|