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

88 lines
2.3 KiB
JavaScript
Raw Normal View History

import $ from 'jquery';
import 'deckar01-task_list';
import { __ } from '~/locale';
2018-02-02 06:26:08 -05:00
import axios from './lib/utils/axios_utils';
import Flash from './flash';
export default class TaskList {
constructor(options = {}) {
this.selector = options.selector;
this.dataType = options.dataType;
this.fieldName = options.fieldName;
this.lockVersion = options.lockVersion;
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 = '';
2019-01-22 15:53:08 -05:00
if (e.response.data && typeof e.response.data === 'object') {
errorMessages = e.response.data.errors.join(' ');
}
return new Flash(errorMessages || __('Update failed'), 'alert');
2019-01-22 15:53:08 -05:00
};
this.init();
}
init() {
2019-01-26 09:49:49 -05:00
this.disable(); // Prevent duplicate event bindings
$(this.taskListContainerSelector).taskList('enable');
2019-01-26 09:49:49 -05:00
$(document).on('tasklist:changed', this.taskListContainerSelector, this.updateHandler);
}
getTaskListTarget(e) {
return e && e.currentTarget ? $(e.currentTarget) : $(this.taskListContainerSelector);
}
disableTaskListItems(e) {
this.getTaskListTarget(e).taskList('disable');
}
enableTaskListItems(e) {
this.getTaskListTarget(e).taskList('enable');
}
disable() {
this.disableTaskListItems();
$(document).off('tasklist:changed', this.taskListContainerSelector);
}
update(e) {
const $target = $(e.target);
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
patchData[this.dataType] = {
[this.fieldName]: $target.val(),
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,
},
};
2018-02-02 06:26:08 -05:00
this.disableTaskListItems(e);
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;
this.enableTaskListItems(e);
2019-01-15 19:33:58 -05:00
return this.onSuccess(data);
})
.catch(({ response }) => {
this.enableTaskListItems(e);
return this.onError(response.data);
});
}
}