gitlab-org--gitlab-foss/app/assets/javascripts/todos.js.es6

147 lines
4.2 KiB
JavaScript
Raw Normal View History

/* eslint-disable class-methods-use-this, no-new, func-names, no-unneeded-ternary, object-shorthand, quote-props, no-param-reassign, max-len */
/* global UsersSelect */
((global) => {
2016-09-08 09:13:15 -04:00
class Todos {
constructor() {
this.initFilters();
this.bindEvents();
this.cleanupWrapper = this.cleanup.bind(this);
document.addEventListener('beforeunload', this.cleanupWrapper);
2016-07-24 16:45:11 -04:00
}
cleanup() {
this.unbindEvents();
document.removeEventListener('beforeunload', this.cleanupWrapper);
2016-09-08 09:13:15 -04:00
}
2016-07-24 16:45:11 -04:00
unbindEvents() {
$('.js-done-todo, .js-undo-todo').off('click', this.updateStateClickedWrapper);
$('.js-todos-mark-all').off('click', this.allDoneClickedWrapper);
$('.todo').off('click', this.goToTodoUrl);
}
bindEvents() {
this.updateStateClickedWrapper = this.updateStateClicked.bind(this);
this.allDoneClickedWrapper = this.allDoneClicked.bind(this);
$('.js-done-todo, .js-undo-todo').on('click', this.updateStateClickedWrapper);
$('.js-todos-mark-all').on('click', this.allDoneClickedWrapper);
$('.todo').on('click', this.goToTodoUrl);
2016-09-08 09:13:15 -04:00
}
2016-07-24 16:45:11 -04:00
2016-09-08 09:13:15 -04:00
initFilters() {
new UsersSelect();
2016-08-31 13:33:12 -04:00
this.initFilterDropdown($('.js-project-search'), 'project_id', ['text']);
this.initFilterDropdown($('.js-type-search'), 'type');
this.initFilterDropdown($('.js-action-search'), 'action_id');
$('form.filter-form').on('submit', function (event) {
event.preventDefault();
gl.utils.visitUrl(`${this.action}&${$(this).serialize()}`);
});
2016-09-08 09:13:15 -04:00
}
2016-09-08 09:13:15 -04:00
initFilterDropdown($dropdown, fieldName, searchFields) {
$dropdown.glDropdown({
2016-09-08 09:13:15 -04:00
fieldName,
selectable: true,
2016-08-31 13:33:12 -04:00
filterable: searchFields ? true : false,
search: { fields: searchFields },
data: $dropdown.data('data'),
clicked: function () {
return $dropdown.closest('form.filter-form').submit();
},
});
2016-09-08 09:13:15 -04:00
}
updateStateClicked(e) {
2016-07-24 16:45:11 -04:00
e.preventDefault();
const target = e.target;
target.setAttribute('disabled', '');
target.classList.add('disabled');
$.ajax({
2016-07-24 16:45:11 -04:00
type: 'POST',
url: target.getAttribute('href'),
2016-07-24 16:45:11 -04:00
dataType: 'json',
data: {
'_method': target.getAttribute('data-method'),
2016-07-24 16:45:11 -04:00
},
success: (data) => {
this.updateState(target);
this.updateBadges(data);
},
2016-07-24 16:45:11 -04:00
});
2016-09-08 09:13:15 -04:00
}
2016-07-24 16:45:11 -04:00
2016-09-08 09:13:15 -04:00
allDoneClicked(e) {
2016-07-24 16:45:11 -04:00
e.preventDefault();
const $target = $(e.currentTarget);
2016-09-08 09:13:15 -04:00
$target.disable();
$.ajax({
2016-07-24 16:45:11 -04:00
type: 'POST',
2016-09-08 09:13:15 -04:00
url: $target.attr('href'),
2016-07-24 16:45:11 -04:00
dataType: 'json',
data: {
'_method': 'delete',
2016-07-24 16:45:11 -04:00
},
success: (data) => {
2016-09-08 09:13:15 -04:00
$target.remove();
$('.js-todos-all').html('<div class="nothing-here-block">You\'re all done!</div>');
this.updateBadges(data);
},
2016-07-24 16:45:11 -04:00
});
2016-09-08 09:13:15 -04:00
}
2016-07-24 16:45:11 -04:00
updateState(target) {
const row = target.closest('li');
const restoreBtn = row.querySelector('.js-undo-todo');
const doneBtn = row.querySelector('.js-done-todo');
target.removeAttribute('disabled');
target.classList.remove('disabled');
target.classList.add('hidden');
if (target === doneBtn) {
row.classList.add('done-reversible');
restoreBtn.classList.remove('hidden');
} else {
row.classList.remove('done-reversible');
doneBtn.classList.remove('hidden');
2016-07-24 16:45:11 -04:00
}
2016-09-08 09:13:15 -04:00
}
2016-07-24 16:45:11 -04:00
2016-09-08 09:13:15 -04:00
updateBadges(data) {
2016-08-08 17:19:46 -04:00
$(document).trigger('todo:toggle', data.count);
$('.todos-pending .badge').text(data.count);
$('.todos-done .badge').text(data.done_count);
2016-09-08 09:13:15 -04:00
}
2016-07-24 16:45:11 -04:00
2016-09-08 09:13:15 -04:00
goToTodoUrl(e) {
const todoLink = this.dataset.url;
2017-01-31 04:14:01 -05:00
2016-07-24 16:45:11 -04:00
if (!todoLink) {
return;
}
2017-01-31 04:14:01 -05:00
if (gl.utils.isMetaClick(e)) {
const windowTarget = '_blank';
const selected = e.target;
2016-07-24 16:45:11 -04:00
e.preventDefault();
if (selected.tagName === 'IMG') {
const avatarUrl = selected.parentElement.getAttribute('href');
window.open(avatarUrl, windowTarget);
2017-01-31 04:14:01 -05:00
} else {
window.open(todoLink, windowTarget);
2017-01-31 04:14:01 -05:00
}
2016-07-24 16:45:11 -04:00
} else {
gl.utils.visitUrl(todoLink);
2016-07-24 16:45:11 -04:00
}
2016-09-08 09:13:15 -04:00
}
}
2016-07-24 16:45:11 -04:00
2016-09-08 09:13:15 -04:00
global.Todos = Todos;
})(window.gl || (window.gl = {}));