2017-08-17 22:00:37 +05:30
|
|
|
/* eslint-disable class-methods-use-this, no-unneeded-ternary, quote-props */
|
2018-05-09 12:01:36 +05:30
|
|
|
|
|
|
|
import $ from 'jquery';
|
2018-03-17 18:26:18 +05:30
|
|
|
import { visitUrl } from '~/lib/utils/url_utility';
|
|
|
|
import UsersSelect from '~/users_select';
|
|
|
|
import { isMetaClick } from '~/lib/utils/common_utils';
|
2018-03-27 19:54:05 +05:30
|
|
|
import { __ } from '~/locale';
|
|
|
|
import flash from '~/flash';
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
export default class Todos {
|
2017-08-17 22:00:37 +05:30
|
|
|
constructor() {
|
|
|
|
this.initFilters();
|
|
|
|
this.bindEvents();
|
|
|
|
this.todo_ids = [];
|
|
|
|
|
|
|
|
this.cleanupWrapper = this.cleanup.bind(this);
|
|
|
|
document.addEventListener('beforeunload', this.cleanupWrapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup() {
|
|
|
|
this.unbindEvents();
|
|
|
|
document.removeEventListener('beforeunload', this.cleanupWrapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
unbindEvents() {
|
|
|
|
$('.js-done-todo, .js-undo-todo, .js-add-todo').off('click', this.updateRowStateClickedWrapper);
|
|
|
|
$('.js-todos-mark-all', '.js-todos-undo-all').off('click', this.updateallStateClickedWrapper);
|
|
|
|
$('.todo').off('click', this.goToTodoUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
bindEvents() {
|
|
|
|
this.updateRowStateClickedWrapper = this.updateRowStateClicked.bind(this);
|
|
|
|
this.updateAllStateClickedWrapper = this.updateAllStateClicked.bind(this);
|
|
|
|
|
|
|
|
$('.js-done-todo, .js-undo-todo, .js-add-todo').on('click', this.updateRowStateClickedWrapper);
|
|
|
|
$('.js-todos-mark-all, .js-todos-undo-all').on('click', this.updateAllStateClickedWrapper);
|
|
|
|
$('.todo').on('click', this.goToTodoUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
initFilters() {
|
|
|
|
this.initFilterDropdown($('.js-project-search'), 'project_id', ['text']);
|
|
|
|
this.initFilterDropdown($('.js-type-search'), 'type');
|
|
|
|
this.initFilterDropdown($('.js-action-search'), 'action_id');
|
|
|
|
|
|
|
|
return new UsersSelect();
|
|
|
|
}
|
|
|
|
|
|
|
|
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(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
updateRowStateClicked(e) {
|
2017-09-10 17:25:29 +05:30
|
|
|
e.stopPropagation();
|
2017-08-17 22:00:37 +05:30
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
const target = e.target;
|
|
|
|
target.setAttribute('disabled', true);
|
|
|
|
target.classList.add('disabled');
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
axios[target.dataset.method](target.dataset.href)
|
|
|
|
.then(({ data }) => {
|
2017-08-17 22:00:37 +05:30
|
|
|
this.updateRowState(target);
|
2018-03-17 18:26:18 +05:30
|
|
|
this.updateBadges(data);
|
|
|
|
}).catch(() => flash(__('Error updating todo status.')));
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updateAllStateClicked(e) {
|
2017-09-10 17:25:29 +05:30
|
|
|
e.stopPropagation();
|
2017-08-17 22:00:37 +05:30
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
const target = e.currentTarget;
|
|
|
|
target.setAttribute('disabled', true);
|
|
|
|
target.classList.add('disabled');
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
axios[target.dataset.method](target.dataset.href, {
|
|
|
|
ids: this.todo_ids,
|
|
|
|
}).then(({ data }) => {
|
|
|
|
this.updateAllState(target, data);
|
|
|
|
this.updateBadges(data);
|
|
|
|
}).catch(() => flash(__('Error updating status for all todos.')));
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
goToTodoUrl(e) {
|
|
|
|
const todoLink = this.dataset.url;
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
if (!todoLink || e.target.tagName === 'A' || e.target.tagName === 'IMG') {
|
2017-08-17 22:00:37 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
if (isMetaClick(e)) {
|
2017-08-17 22:00:37 +05:30
|
|
|
const windowTarget = '_blank';
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
window.open(todoLink, windowTarget);
|
2017-08-17 22:00:37 +05:30
|
|
|
} else {
|
2018-03-17 18:26:18 +05:30
|
|
|
visitUrl(todoLink);
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|