2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2017-05-23 10:21:33 -04:00
|
|
|
import 'deckar01-task_list';
|
2018-02-02 06:26:08 -05:00
|
|
|
import axios from './lib/utils/axios_utils';
|
2017-10-02 08:32:53 -04:00
|
|
|
import 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;
|
2017-02-08 05:54:33 -05:00
|
|
|
this.onSuccess = options.onSuccess || (() => {});
|
2018-02-02 07:36:51 -05:00
|
|
|
this.onError = function showFlash(e) {
|
2017-01-16 13:43:03 -05:00
|
|
|
let errorMessages = '';
|
|
|
|
|
2018-02-02 07:36:51 -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
|
|
|
}
|
|
|
|
|
|
|
|
return new Flash(errorMessages || 'Update failed', 'alert');
|
|
|
|
};
|
|
|
|
|
2017-02-07 01:42:21 -05:00
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
2017-02-08 01:04:00 -05:00
|
|
|
// Prevent duplicate event bindings
|
|
|
|
this.disable();
|
2017-02-08 03:10:04 -05:00
|
|
|
$(`${this.selector} .js-task-list-container`).taskList('enable');
|
2017-02-08 04:23:45 -05:00
|
|
|
$(document).on('tasklist:changed', `${this.selector} .js-task-list-container`, this.update.bind(this));
|
2017-02-07 01:42:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
disable() {
|
2017-02-08 03:10:04 -05:00
|
|
|
$(`${this.selector} .js-task-list-container`).taskList('disable');
|
2017-02-09 17:14:21 -05:00
|
|
|
$(document).off('tasklist:changed', `${this.selector} .js-task-list-container`);
|
2017-02-07 01:42:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
update(e) {
|
2017-02-08 04:23:45 -05:00
|
|
|
const $target = $(e.target);
|
2017-02-08 03:10:04 -05:00
|
|
|
const patchData = {};
|
2017-02-07 01:42:21 -05:00
|
|
|
patchData[this.dataType] = {
|
2017-02-15 00:53:17 -05:00
|
|
|
[this.fieldName]: $target.val(),
|
2017-02-07 01:42:21 -05:00
|
|
|
};
|
2018-02-02 06:26:08 -05:00
|
|
|
|
2018-02-20 17:20:48 -05:00
|
|
|
return axios.patch($target.data('updateUrl') || $('form.js-issuable-update').attr('action'), patchData)
|
2018-02-02 06:26:08 -05:00
|
|
|
.then(({ data }) => this.onSuccess(data))
|
2018-02-02 07:36:51 -05:00
|
|
|
.catch(err => this.onError(err));
|
2017-02-07 01:42:21 -05:00
|
|
|
}
|
|
|
|
}
|