2017-03-10 07:31:30 -05:00
|
|
|
/* eslint-disable class-methods-use-this, no-unneeded-ternary, quote-props */
|
2017-05-12 04:15:28 -04:00
|
|
|
|
|
|
|
import UsersSelect from './users_select';
|
2017-09-06 12:14:34 -04:00
|
|
|
import { isMetaClick } from './lib/utils/common_utils';
|
2016-12-13 22:01:05 -05:00
|
|
|
|
2017-07-06 13:57:01 -04:00
|
|
|
export default class Todos {
|
2017-03-10 07:31:30 -05:00
|
|
|
constructor() {
|
|
|
|
this.initFilters();
|
|
|
|
this.bindEvents();
|
2017-01-29 04:44:30 -05:00
|
|
|
this.todo_ids = [];
|
2017-01-16 08:11:08 -05:00
|
|
|
|
2017-03-10 07:31:30 -05:00
|
|
|
this.cleanupWrapper = this.cleanup.bind(this);
|
|
|
|
document.addEventListener('beforeunload', this.cleanupWrapper);
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-10 07:31:30 -05:00
|
|
|
cleanup() {
|
|
|
|
this.unbindEvents();
|
|
|
|
document.removeEventListener('beforeunload', this.cleanupWrapper);
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-10 07:31:30 -05:00
|
|
|
unbindEvents() {
|
|
|
|
$('.js-done-todo, .js-undo-todo, .js-add-todo').off('click', this.updateRowStateClickedWrapper);
|
2017-01-29 04:44:30 -05:00
|
|
|
$('.js-todos-mark-all', '.js-todos-undo-all').off('click', this.updateallStateClickedWrapper);
|
2017-03-10 07:31:30 -05:00
|
|
|
$('.todo').off('click', this.goToTodoUrl);
|
|
|
|
}
|
2017-01-16 08:11:08 -05:00
|
|
|
|
2017-03-10 07:31:30 -05:00
|
|
|
bindEvents() {
|
|
|
|
this.updateRowStateClickedWrapper = this.updateRowStateClicked.bind(this);
|
2017-01-29 04:44:30 -05:00
|
|
|
this.updateAllStateClickedWrapper = this.updateAllStateClicked.bind(this);
|
2017-01-16 08:11:08 -05:00
|
|
|
|
2017-03-10 07:31:30 -05:00
|
|
|
$('.js-done-todo, .js-undo-todo, .js-add-todo').on('click', this.updateRowStateClickedWrapper);
|
2017-01-29 04:44:30 -05:00
|
|
|
$('.js-todos-mark-all, .js-todos-undo-all').on('click', this.updateAllStateClickedWrapper);
|
2017-03-10 07:31:30 -05:00
|
|
|
$('.todo').on('click', this.goToTodoUrl);
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-10 07:31:30 -05:00
|
|
|
initFilters() {
|
|
|
|
this.initFilterDropdown($('.js-project-search'), 'project_id', ['text']);
|
|
|
|
this.initFilterDropdown($('.js-type-search'), 'type');
|
|
|
|
this.initFilterDropdown($('.js-action-search'), 'action_id');
|
2016-08-28 11:24:48 -04:00
|
|
|
|
2017-03-10 07:31:30 -05:00
|
|
|
return new UsersSelect();
|
|
|
|
}
|
2016-08-28 11:24:48 -04:00
|
|
|
|
2017-03-10 07:31:30 -05:00
|
|
|
initFilterDropdown($dropdown, fieldName, searchFields) {
|
|
|
|
$dropdown.glDropdown({
|
|
|
|
fieldName,
|
|
|
|
selectable: true,
|
|
|
|
filterable: searchFields ? true : false,
|
|
|
|
search: { fields: searchFields },
|
|
|
|
data: $dropdown.data('data'),
|
|
|
|
clicked: () => $dropdown.closest('form.filter-form').submit(),
|
|
|
|
});
|
|
|
|
}
|
2016-08-28 11:24:48 -04:00
|
|
|
|
2017-03-10 07:31:30 -05:00
|
|
|
updateRowStateClicked(e) {
|
2017-08-03 16:31:53 -04:00
|
|
|
e.stopPropagation();
|
2017-03-10 07:31:30 -05:00
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
const target = e.target;
|
2017-01-29 04:44:30 -05:00
|
|
|
target.setAttribute('disabled', true);
|
2017-03-10 07:31:30 -05:00
|
|
|
target.classList.add('disabled');
|
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
2017-01-29 04:44:30 -05:00
|
|
|
url: target.dataset.href,
|
2017-03-10 07:31:30 -05:00
|
|
|
dataType: 'json',
|
|
|
|
data: {
|
2017-01-29 04:44:30 -05:00
|
|
|
'_method': target.dataset.method,
|
2017-03-10 07:31:30 -05:00
|
|
|
},
|
|
|
|
success: (data) => {
|
|
|
|
this.updateRowState(target);
|
|
|
|
return this.updateBadges(data);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-10 07:31:30 -05:00
|
|
|
updateRowState(target) {
|
|
|
|
const row = target.closest('li');
|
|
|
|
const restoreBtn = row.querySelector('.js-undo-todo');
|
|
|
|
const doneBtn = row.querySelector('.js-done-todo');
|
|
|
|
|
|
|
|
target.classList.add('hidden');
|
|
|
|
target.removeAttribute('disabled');
|
|
|
|
target.classList.remove('disabled');
|
|
|
|
|
|
|
|
if (target === doneBtn) {
|
|
|
|
row.classList.add('done-reversible');
|
|
|
|
restoreBtn.classList.remove('hidden');
|
|
|
|
} else if (target === restoreBtn) {
|
|
|
|
row.classList.remove('done-reversible');
|
|
|
|
doneBtn.classList.remove('hidden');
|
|
|
|
} else {
|
|
|
|
row.parentNode.removeChild(row);
|
2016-09-08 09:13:15 -04:00
|
|
|
}
|
2017-03-10 07:31:30 -05:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-01-29 04:44:30 -05:00
|
|
|
updateAllStateClicked(e) {
|
2017-08-03 16:31:53 -04:00
|
|
|
e.stopPropagation();
|
2017-01-29 04:44:30 -05:00
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
const target = e.currentTarget;
|
|
|
|
const requestData = { '_method': target.dataset.method, ids: this.todo_ids };
|
|
|
|
target.setAttribute('disabled', true);
|
|
|
|
target.classList.add('disabled');
|
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: target.dataset.href,
|
|
|
|
dataType: 'json',
|
|
|
|
data: requestData,
|
|
|
|
success: (data) => {
|
|
|
|
this.updateAllState(target, data);
|
|
|
|
return this.updateBadges(data);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
updateAllState(target, data) {
|
|
|
|
const markAllDoneBtn = document.querySelector('.js-todos-mark-all');
|
|
|
|
const undoAllBtn = document.querySelector('.js-todos-undo-all');
|
|
|
|
const todoListContainer = document.querySelector('.js-todos-list-container');
|
|
|
|
const nothingHereContainer = document.querySelector('.js-nothing-here-container');
|
|
|
|
|
|
|
|
target.removeAttribute('disabled');
|
|
|
|
target.classList.remove('disabled');
|
|
|
|
|
|
|
|
this.todo_ids = (target === markAllDoneBtn) ? data.updated_ids : [];
|
|
|
|
undoAllBtn.classList.toggle('hidden');
|
|
|
|
markAllDoneBtn.classList.toggle('hidden');
|
|
|
|
todoListContainer.classList.toggle('hidden');
|
|
|
|
nothingHereContainer.classList.toggle('hidden');
|
|
|
|
}
|
|
|
|
|
2017-03-10 07:31:30 -05:00
|
|
|
updateBadges(data) {
|
|
|
|
$(document).trigger('todo:toggle', data.count);
|
|
|
|
document.querySelector('.todos-pending .badge').innerHTML = data.count;
|
|
|
|
document.querySelector('.todos-done .badge').innerHTML = data.done_count;
|
|
|
|
}
|
2017-01-16 08:11:08 -05:00
|
|
|
|
2017-03-10 07:31:30 -05:00
|
|
|
goToTodoUrl(e) {
|
|
|
|
const todoLink = this.dataset.url;
|
2017-01-16 08:11:08 -05:00
|
|
|
|
2017-09-12 10:38:42 -04:00
|
|
|
if (!todoLink || e.target.tagName === 'A' || e.target.tagName === 'IMG') {
|
2017-03-10 07:31:30 -05:00
|
|
|
return;
|
2016-09-08 09:13:15 -04:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-09-12 10:38:42 -04:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
|
2017-09-06 12:14:34 -04:00
|
|
|
if (isMetaClick(e)) {
|
2017-03-10 07:31:30 -05:00
|
|
|
const windowTarget = '_blank';
|
2017-09-12 10:38:42 -04:00
|
|
|
|
|
|
|
window.open(todoLink, windowTarget);
|
2017-03-10 07:31:30 -05:00
|
|
|
} else {
|
|
|
|
gl.utils.visitUrl(todoLink);
|
2016-09-08 09:13:15 -04:00
|
|
|
}
|
|
|
|
}
|
2017-03-10 07:31:30 -05:00
|
|
|
}
|