2017-01-10 18:02:20 -05:00
|
|
|
/* eslint-disable class-methods-use-this, no-new, func-names, prefer-template, no-unneeded-ternary, object-shorthand, space-before-function-paren, comma-dangle, quote-props, consistent-return, no-else-return, no-param-reassign, max-len */
|
2016-12-13 22:01:05 -05:00
|
|
|
/* global UsersSelect */
|
|
|
|
/* global Turbolinks */
|
|
|
|
|
2016-09-08 11:22:14 -04:00
|
|
|
((global) => {
|
2016-09-08 09:13:15 -04:00
|
|
|
class Todos {
|
2016-09-27 07:40:31 -04:00
|
|
|
constructor({ el } = {}) {
|
2016-09-08 09:13:15 -04:00
|
|
|
this.allDoneClicked = this.allDoneClicked.bind(this);
|
|
|
|
this.doneClicked = this.doneClicked.bind(this);
|
2016-09-09 12:31:26 -04:00
|
|
|
this.el = el || $('.js-todos-options');
|
2016-09-28 10:58:36 -04:00
|
|
|
this.perPage = this.el.data('perPage');
|
2016-07-24 16:45:11 -04:00
|
|
|
this.clearListeners();
|
|
|
|
this.initBtnListeners();
|
2016-08-28 11:24:48 -04:00
|
|
|
this.initFilters();
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
|
|
|
|
2016-09-08 09:13:15 -04:00
|
|
|
clearListeners() {
|
2016-07-24 16:45:11 -04:00
|
|
|
$('.done-todo').off('click');
|
|
|
|
$('.js-todos-mark-all').off('click');
|
|
|
|
return $('.todo').off('click');
|
2016-09-08 09:13:15 -04:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2016-09-08 09:13:15 -04:00
|
|
|
initBtnListeners() {
|
2016-07-24 16:45:11 -04:00
|
|
|
$('.done-todo').on('click', this.doneClicked);
|
|
|
|
$('.js-todos-mark-all').on('click', this.allDoneClicked);
|
|
|
|
return $('.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() {
|
2016-08-28 11:24:48 -04:00
|
|
|
new UsersSelect();
|
2016-08-31 13:33:12 -04:00
|
|
|
this.initFilterDropdown($('.js-project-search'), 'project_id', ['text']);
|
2016-08-29 19:28:44 -04:00
|
|
|
this.initFilterDropdown($('.js-type-search'), 'type');
|
|
|
|
this.initFilterDropdown($('.js-action-search'), 'action_id');
|
2016-08-28 11:24:48 -04:00
|
|
|
|
|
|
|
$('form.filter-form').on('submit', function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
Turbolinks.visit(this.action + '&' + $(this).serialize());
|
|
|
|
});
|
2016-09-08 09:13:15 -04:00
|
|
|
}
|
2016-08-28 11:24:48 -04:00
|
|
|
|
2016-09-08 09:13:15 -04:00
|
|
|
initFilterDropdown($dropdown, fieldName, searchFields) {
|
2016-08-29 19:28:44 -04:00
|
|
|
$dropdown.glDropdown({
|
2016-09-08 09:13:15 -04:00
|
|
|
fieldName,
|
2016-08-28 11:24:48 -04:00
|
|
|
selectable: true,
|
2016-08-31 13:33:12 -04:00
|
|
|
filterable: searchFields ? true : false,
|
|
|
|
search: { fields: searchFields },
|
2016-08-29 19:28:44 -04:00
|
|
|
data: $dropdown.data('data'),
|
2016-08-28 11:24:48 -04:00
|
|
|
clicked: function() {
|
2016-08-29 19:28:44 -04:00
|
|
|
return $dropdown.closest('form.filter-form').submit();
|
2016-08-28 11:24:48 -04:00
|
|
|
}
|
2017-01-10 17:54:56 -05:00
|
|
|
});
|
2016-09-08 09:13:15 -04:00
|
|
|
}
|
2016-08-28 11:24:48 -04:00
|
|
|
|
2016-09-08 09:13:15 -04:00
|
|
|
doneClicked(e) {
|
2016-07-24 16:45:11 -04:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopImmediatePropagation();
|
2016-09-08 09:13:15 -04:00
|
|
|
const $target = $(e.currentTarget);
|
|
|
|
$target.disable();
|
2016-07-24 16:45:11 -04:00
|
|
|
return $.ajax({
|
|
|
|
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-09-09 10:57:13 -04:00
|
|
|
success: (data) => {
|
2016-09-08 09:13:15 -04:00
|
|
|
this.redirectIfNeeded(data.count);
|
|
|
|
this.clearDone($target.closest('li'));
|
|
|
|
return 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();
|
|
|
|
e.stopImmediatePropagation();
|
2016-12-14 00:26:26 -05:00
|
|
|
const $target = $(e.currentTarget);
|
2016-09-08 09:13:15 -04:00
|
|
|
$target.disable();
|
2016-07-24 16:45:11 -04:00
|
|
|
return $.ajax({
|
|
|
|
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-09-09 10:57:13 -04:00
|
|
|
success: (data) => {
|
2016-09-08 09:13:15 -04:00
|
|
|
$target.remove();
|
|
|
|
$('.prepend-top-default').html('<div class="nothing-here-block">You\'re all done!</div>');
|
|
|
|
return 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
|
|
|
clearDone($row) {
|
|
|
|
const $ul = $row.closest('ul');
|
2016-07-24 16:45:11 -04:00
|
|
|
$row.remove();
|
|
|
|
if (!$ul.find('li').length) {
|
|
|
|
return $ul.parents('.panel').remove();
|
|
|
|
}
|
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);
|
2016-07-24 16:45:11 -04:00
|
|
|
return $('.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
|
|
|
getTotalPages() {
|
2016-07-24 16:45:11 -04:00
|
|
|
return this.el.data('totalPages');
|
2016-09-08 09:13:15 -04:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2016-09-08 09:13:15 -04:00
|
|
|
getCurrentPage() {
|
2016-07-24 16:45:11 -04:00
|
|
|
return this.el.data('currentPage');
|
2016-09-08 09:13:15 -04:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2016-09-08 09:13:15 -04:00
|
|
|
getTodosPerPage() {
|
2016-07-24 16:45:11 -04:00
|
|
|
return this.el.data('perPage');
|
2016-09-08 09:13:15 -04:00
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2016-09-08 09:13:15 -04:00
|
|
|
redirectIfNeeded(total) {
|
2016-09-08 09:54:48 -04:00
|
|
|
const currPages = this.getTotalPages();
|
|
|
|
const currPage = this.getCurrentPage();
|
2016-09-08 09:13:15 -04:00
|
|
|
|
2016-07-26 23:32:10 -04:00
|
|
|
// Refresh if no remaining Todos
|
2016-07-24 16:45:11 -04:00
|
|
|
if (!total) {
|
2016-09-08 09:13:15 -04:00
|
|
|
window.location.reload();
|
2016-07-24 16:45:11 -04:00
|
|
|
return;
|
|
|
|
}
|
2016-07-26 23:32:10 -04:00
|
|
|
// Do nothing if no pagination
|
2016-07-24 16:45:11 -04:00
|
|
|
if (!currPages) {
|
|
|
|
return;
|
|
|
|
}
|
2016-09-08 09:13:15 -04:00
|
|
|
|
|
|
|
const newPages = Math.ceil(total / this.getTodosPerPage());
|
|
|
|
let url = location.href;
|
|
|
|
|
2016-07-24 16:45:11 -04:00
|
|
|
if (newPages !== currPages) {
|
2016-07-26 23:32:10 -04:00
|
|
|
// Redirect to previous page if there's one available
|
2016-07-24 16:45:11 -04:00
|
|
|
if (currPages > 1 && currPage === currPages) {
|
2016-09-08 09:13:15 -04:00
|
|
|
const pageParams = {
|
2016-07-24 16:45:11 -04:00
|
|
|
page: currPages - 1
|
|
|
|
};
|
|
|
|
url = gl.utils.mergeUrlParams(pageParams, url);
|
|
|
|
}
|
|
|
|
return Turbolinks.visit(url);
|
|
|
|
}
|
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).data('url');
|
2016-07-24 16:45:11 -04:00
|
|
|
if (!todoLink) {
|
|
|
|
return;
|
|
|
|
}
|
2016-07-26 23:32:10 -04:00
|
|
|
// Allow Meta-Click or Mouse3-click to open in a new tab
|
2016-07-24 16:45:11 -04:00
|
|
|
if (e.metaKey || e.which === 2) {
|
|
|
|
e.preventDefault();
|
|
|
|
return window.open(todoLink, '_blank');
|
|
|
|
} else {
|
|
|
|
return Turbolinks.visit(todoLink);
|
|
|
|
}
|
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 = {}));
|