debian-mirror-gitlab/app/assets/javascripts/task_list.js

49 lines
1.4 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2017-09-10 17:25:29 +05:30
import 'deckar01-task_list';
2018-03-17 18:26:18 +05:30
import axios from './lib/utils/axios_utils';
import Flash from './flash';
2017-09-10 17:25:29 +05:30
export default class TaskList {
2017-08-17 22:00:37 +05:30
constructor(options = {}) {
this.selector = options.selector;
this.dataType = options.dataType;
this.fieldName = options.fieldName;
this.onSuccess = options.onSuccess || (() => {});
2018-03-17 18:26:18 +05:30
this.onError = function showFlash(e) {
2017-08-17 22:00:37 +05:30
let errorMessages = '';
2018-03-17 18:26:18 +05:30
if (e.response.data && typeof e.response.data === 'object') {
errorMessages = e.response.data.errors.join(' ');
2017-08-17 22:00:37 +05:30
}
return new Flash(errorMessages || 'Update failed', 'alert');
};
this.init();
}
init() {
// Prevent duplicate event bindings
this.disable();
$(`${this.selector} .js-task-list-container`).taskList('enable');
$(document).on('tasklist:changed', `${this.selector} .js-task-list-container`, this.update.bind(this));
}
disable() {
$(`${this.selector} .js-task-list-container`).taskList('disable');
$(document).off('tasklist:changed', `${this.selector} .js-task-list-container`);
}
update(e) {
const $target = $(e.target);
const patchData = {};
patchData[this.dataType] = {
[this.fieldName]: $target.val(),
};
2018-03-17 18:26:18 +05:30
2018-03-27 19:54:05 +05:30
return axios.patch($target.data('updateUrl') || $('form.js-issuable-update').attr('action'), patchData)
2018-03-17 18:26:18 +05:30
.then(({ data }) => this.onSuccess(data))
.catch(err => this.onError(err));
2017-08-17 22:00:37 +05:30
}
}