2017-05-23 10:21:33 -04:00
|
|
|
import 'deckar01-task_list';
|
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 || (() => {});
|
2017-01-16 13:43:03 -05:00
|
|
|
this.onError = function showFlash(response) {
|
|
|
|
let errorMessages = '';
|
|
|
|
|
|
|
|
if (response.responseJSON) {
|
|
|
|
errorMessages = response.responseJSON.errors.join(' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
return $.ajax({
|
|
|
|
type: 'PATCH',
|
2017-02-08 04:23:45 -05:00
|
|
|
url: $target.data('update-url') || $('form.js-issuable-update').attr('action'),
|
2017-02-07 01:42:21 -05:00
|
|
|
data: patchData,
|
2017-02-08 04:23:45 -05:00
|
|
|
success: this.onSuccess,
|
2017-01-16 13:43:03 -05:00
|
|
|
error: this.onError,
|
2017-02-07 01:42:21 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|