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';
|
2021-06-20 23:10:27 -04:00
|
|
|
import createFlash from './flash';
|
2021-02-14 13:09:20 -05:00
|
|
|
import axios from './lib/utils/axios_utils';
|
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);
|
2021-10-20 05:12:43 -04:00
|
|
|
this.onUpdate = options.onUpdate || (() => {});
|
2019-01-26 09:49:49 -05:00
|
|
|
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
|
|
|
|
2021-06-20 23:10:27 -04:00
|
|
|
return createFlash({
|
|
|
|
message: errorMessages || __('Update failed'),
|
|
|
|
});
|
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]`,
|
|
|
|
);
|
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
taskListFields.forEach((taskListField) => {
|
2020-09-25 08:10:00 -04:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
taskListField.value = taskListField.dataset.value;
|
|
|
|
});
|
|
|
|
|
2021-05-14 05:10:24 -04:00
|
|
|
this.enable();
|
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
|
|
|
}
|
|
|
|
|
2021-05-14 05:10:24 -04:00
|
|
|
// Disable any task items that don't have a data-sourcepos attribute, on the
|
|
|
|
// assumption that if it doesn't then it wasn't generated from our markdown parser.
|
|
|
|
// This covers the case of markdown not being able to handle task lists inside
|
|
|
|
// markdown tables. It also includes hand coded HTML lists.
|
|
|
|
disableNonMarkdownTaskListItems(e) {
|
|
|
|
this.getTaskListTarget(e)
|
|
|
|
.find('.task-list-item')
|
|
|
|
.not('[data-sourcepos]')
|
|
|
|
.find('.task-list-item-checkbox')
|
|
|
|
.prop('disabled', true);
|
|
|
|
}
|
|
|
|
|
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');
|
2021-05-14 05:10:24 -04:00
|
|
|
this.disableNonMarkdownTaskListItems(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
enable() {
|
|
|
|
this.enableTaskListItems();
|
|
|
|
$(document).on('tasklist:changed', this.taskListContainerSelector, this.updateHandler);
|
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
|
|
|
|
2021-10-20 05:12:43 -04:00
|
|
|
this.onUpdate();
|
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
|
|
|
}
|
|
|
|
}
|