2016-11-03 12:29:30 +05:30
|
|
|
((global) => {
|
|
|
|
|
|
|
|
class Todos {
|
|
|
|
constructor({ el } = {}) {
|
|
|
|
this.allDoneClicked = this.allDoneClicked.bind(this);
|
|
|
|
this.doneClicked = this.doneClicked.bind(this);
|
|
|
|
this.el = el || $('.js-todos-options');
|
2016-09-13 17:45:13 +05:30
|
|
|
this.perPage = this.el.data('perPage');
|
|
|
|
this.clearListeners();
|
|
|
|
this.initBtnListeners();
|
2016-09-29 09:46:39 +05:30
|
|
|
this.initFilters();
|
2016-09-13 17:45:13 +05:30
|
|
|
}
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
clearListeners() {
|
2016-09-13 17:45:13 +05:30
|
|
|
$('.done-todo').off('click');
|
|
|
|
$('.js-todos-mark-all').off('click');
|
|
|
|
return $('.todo').off('click');
|
2016-11-03 12:29:30 +05:30
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
initBtnListeners() {
|
2016-09-13 17:45:13 +05:30
|
|
|
$('.done-todo').on('click', this.doneClicked);
|
|
|
|
$('.js-todos-mark-all').on('click', this.allDoneClicked);
|
|
|
|
return $('.todo').on('click', this.goToTodoUrl);
|
2016-11-03 12:29:30 +05:30
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
initFilters() {
|
2016-09-29 09:46:39 +05:30
|
|
|
new UsersSelect();
|
|
|
|
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();
|
|
|
|
Turbolinks.visit(this.action + '&' + $(this).serialize());
|
|
|
|
});
|
2016-11-03 12:29:30 +05:30
|
|
|
}
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
initFilterDropdown($dropdown, fieldName, searchFields) {
|
2016-09-29 09:46:39 +05:30
|
|
|
$dropdown.glDropdown({
|
2016-11-03 12:29:30 +05:30
|
|
|
fieldName,
|
2016-09-29 09:46:39 +05:30
|
|
|
selectable: true,
|
|
|
|
filterable: searchFields ? true : false,
|
|
|
|
search: { fields: searchFields },
|
|
|
|
data: $dropdown.data('data'),
|
|
|
|
clicked: function() {
|
|
|
|
return $dropdown.closest('form.filter-form').submit();
|
|
|
|
}
|
|
|
|
})
|
2016-11-03 12:29:30 +05:30
|
|
|
}
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
doneClicked(e) {
|
2016-09-13 17:45:13 +05:30
|
|
|
e.preventDefault();
|
|
|
|
e.stopImmediatePropagation();
|
2016-11-03 12:29:30 +05:30
|
|
|
const $target = $(e.currentTarget);
|
|
|
|
$target.disable();
|
2016-09-13 17:45:13 +05:30
|
|
|
return $.ajax({
|
|
|
|
type: 'POST',
|
2016-11-03 12:29:30 +05:30
|
|
|
url: $target.attr('href'),
|
2016-09-13 17:45:13 +05:30
|
|
|
dataType: 'json',
|
|
|
|
data: {
|
|
|
|
'_method': 'delete'
|
|
|
|
},
|
2016-11-03 12:29:30 +05:30
|
|
|
success: (data) => {
|
|
|
|
this.redirectIfNeeded(data.count);
|
|
|
|
this.clearDone($target.closest('li'));
|
|
|
|
return this.updateBadges(data);
|
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
});
|
2016-11-03 12:29:30 +05:30
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
allDoneClicked(e) {
|
2016-09-13 17:45:13 +05:30
|
|
|
e.preventDefault();
|
|
|
|
e.stopImmediatePropagation();
|
2016-11-03 12:29:30 +05:30
|
|
|
$target = $(e.currentTarget);
|
|
|
|
$target.disable();
|
2016-09-13 17:45:13 +05:30
|
|
|
return $.ajax({
|
|
|
|
type: 'POST',
|
2016-11-03 12:29:30 +05:30
|
|
|
url: $target.attr('href'),
|
2016-09-13 17:45:13 +05:30
|
|
|
dataType: 'json',
|
|
|
|
data: {
|
|
|
|
'_method': 'delete'
|
|
|
|
},
|
2016-11-03 12:29:30 +05:30
|
|
|
success: (data) => {
|
|
|
|
$target.remove();
|
|
|
|
$('.prepend-top-default').html('<div class="nothing-here-block">You\'re all done!</div>');
|
|
|
|
return this.updateBadges(data);
|
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
});
|
2016-11-03 12:29:30 +05:30
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
clearDone($row) {
|
|
|
|
const $ul = $row.closest('ul');
|
2016-09-13 17:45:13 +05:30
|
|
|
$row.remove();
|
|
|
|
if (!$ul.find('li').length) {
|
|
|
|
return $ul.parents('.panel').remove();
|
|
|
|
}
|
2016-11-03 12:29:30 +05:30
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
updateBadges(data) {
|
2016-09-13 17:45:13 +05:30
|
|
|
$('.todos-pending .badge, .todos-pending-count').text(data.count);
|
|
|
|
return $('.todos-done .badge').text(data.done_count);
|
2016-11-03 12:29:30 +05:30
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
getTotalPages() {
|
2016-09-13 17:45:13 +05:30
|
|
|
return this.el.data('totalPages');
|
2016-11-03 12:29:30 +05:30
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
getCurrentPage() {
|
2016-09-13 17:45:13 +05:30
|
|
|
return this.el.data('currentPage');
|
2016-11-03 12:29:30 +05:30
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
getTodosPerPage() {
|
2016-09-13 17:45:13 +05:30
|
|
|
return this.el.data('perPage');
|
2016-11-03 12:29:30 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
redirectIfNeeded(total) {
|
|
|
|
const currPages = this.getTotalPages();
|
|
|
|
const currPage = this.getCurrentPage();
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
// Refresh if no remaining Todos
|
2016-09-13 17:45:13 +05:30
|
|
|
if (!total) {
|
2016-11-03 12:29:30 +05:30
|
|
|
window.location.reload();
|
2016-09-13 17:45:13 +05:30
|
|
|
return;
|
|
|
|
}
|
2016-09-29 09:46:39 +05:30
|
|
|
// Do nothing if no pagination
|
2016-09-13 17:45:13 +05:30
|
|
|
if (!currPages) {
|
|
|
|
return;
|
|
|
|
}
|
2016-11-03 12:29:30 +05:30
|
|
|
|
|
|
|
const newPages = Math.ceil(total / this.getTodosPerPage());
|
|
|
|
let url = location.href;
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
if (newPages !== currPages) {
|
2016-09-29 09:46:39 +05:30
|
|
|
// Redirect to previous page if there's one available
|
2016-09-13 17:45:13 +05:30
|
|
|
if (currPages > 1 && currPage === currPages) {
|
2016-11-03 12:29:30 +05:30
|
|
|
const pageParams = {
|
2016-09-13 17:45:13 +05:30
|
|
|
page: currPages - 1
|
|
|
|
};
|
|
|
|
url = gl.utils.mergeUrlParams(pageParams, url);
|
|
|
|
}
|
|
|
|
return Turbolinks.visit(url);
|
|
|
|
}
|
2016-11-03 12:29:30 +05:30
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
goToTodoUrl(e) {
|
|
|
|
const todoLink = $(this).data('url');
|
2016-09-13 17:45:13 +05:30
|
|
|
if (!todoLink) {
|
|
|
|
return;
|
|
|
|
}
|
2016-09-29 09:46:39 +05:30
|
|
|
// Allow Meta-Click or Mouse3-click to open in a new tab
|
2016-09-13 17:45:13 +05:30
|
|
|
if (e.metaKey || e.which === 2) {
|
|
|
|
e.preventDefault();
|
|
|
|
return window.open(todoLink, '_blank');
|
|
|
|
} else {
|
|
|
|
return Turbolinks.visit(todoLink);
|
|
|
|
}
|
2016-11-03 12:29:30 +05:30
|
|
|
}
|
|
|
|
}
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
global.Todos = Todos;
|
|
|
|
})(window.gl || (window.gl = {}));
|