2016-07-24 20:45:11 +00:00
|
|
|
(function() {
|
|
|
|
var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
|
|
|
|
|
|
|
|
this.Todos = (function() {
|
|
|
|
function Todos(opts) {
|
|
|
|
var ref;
|
|
|
|
if (opts == null) {
|
|
|
|
opts = {};
|
|
|
|
}
|
|
|
|
this.allDoneClicked = bind(this.allDoneClicked, this);
|
|
|
|
this.doneClicked = bind(this.doneClicked, this);
|
|
|
|
this.el = (ref = opts.el) != null ? ref : $('.js-todos-options');
|
|
|
|
this.perPage = this.el.data('perPage');
|
|
|
|
this.clearListeners();
|
|
|
|
this.initBtnListeners();
|
2016-08-28 15:24:48 +00:00
|
|
|
this.initFilters();
|
2016-07-24 20:45:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Todos.prototype.clearListeners = function() {
|
|
|
|
$('.done-todo').off('click');
|
|
|
|
$('.js-todos-mark-all').off('click');
|
|
|
|
return $('.todo').off('click');
|
|
|
|
};
|
|
|
|
|
|
|
|
Todos.prototype.initBtnListeners = function() {
|
|
|
|
$('.done-todo').on('click', this.doneClicked);
|
|
|
|
$('.js-todos-mark-all').on('click', this.allDoneClicked);
|
|
|
|
return $('.todo').on('click', this.goToTodoUrl);
|
|
|
|
};
|
|
|
|
|
2016-08-28 15:24:48 +00:00
|
|
|
Todos.prototype.initFilters = function() {
|
|
|
|
new UsersSelect();
|
2016-08-31 17:33:12 +00:00
|
|
|
this.initFilterDropdown($('.js-project-search'), 'project_id', ['text']);
|
2016-08-29 23:28:44 +00:00
|
|
|
this.initFilterDropdown($('.js-type-search'), 'type');
|
|
|
|
this.initFilterDropdown($('.js-action-search'), 'action_id');
|
2016-08-28 15:24:48 +00:00
|
|
|
|
|
|
|
$('form.filter-form').on('submit', function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
Turbolinks.visit(this.action + '&' + $(this).serialize());
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-08-31 17:33:12 +00:00
|
|
|
Todos.prototype.initFilterDropdown = function($dropdown, fieldName, searchFields) {
|
2016-08-29 23:28:44 +00:00
|
|
|
$dropdown.glDropdown({
|
2016-08-28 15:24:48 +00:00
|
|
|
selectable: true,
|
2016-08-31 17:33:12 +00:00
|
|
|
filterable: searchFields ? true : false,
|
2016-08-29 23:28:44 +00:00
|
|
|
fieldName: fieldName,
|
2016-08-31 17:33:12 +00:00
|
|
|
search: { fields: searchFields },
|
2016-08-29 23:28:44 +00:00
|
|
|
data: $dropdown.data('data'),
|
2016-08-28 15:24:48 +00:00
|
|
|
clicked: function() {
|
2016-08-29 23:28:44 +00:00
|
|
|
return $dropdown.closest('form.filter-form').submit();
|
2016-08-28 15:24:48 +00:00
|
|
|
}
|
2016-08-29 23:28:44 +00:00
|
|
|
})
|
2016-08-28 15:24:48 +00:00
|
|
|
};
|
|
|
|
|
2016-07-24 20:45:11 +00:00
|
|
|
Todos.prototype.doneClicked = function(e) {
|
|
|
|
var $this;
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopImmediatePropagation();
|
|
|
|
$this = $(e.currentTarget);
|
|
|
|
$this.disable();
|
|
|
|
return $.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: $this.attr('href'),
|
|
|
|
dataType: 'json',
|
|
|
|
data: {
|
|
|
|
'_method': 'delete'
|
|
|
|
},
|
|
|
|
success: (function(_this) {
|
|
|
|
return function(data) {
|
|
|
|
_this.redirectIfNeeded(data.count);
|
|
|
|
_this.clearDone($this.closest('li'));
|
|
|
|
return _this.updateBadges(data);
|
|
|
|
};
|
|
|
|
})(this)
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Todos.prototype.allDoneClicked = function(e) {
|
|
|
|
var $this;
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopImmediatePropagation();
|
|
|
|
$this = $(e.currentTarget);
|
|
|
|
$this.disable();
|
|
|
|
return $.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: $this.attr('href'),
|
|
|
|
dataType: 'json',
|
|
|
|
data: {
|
|
|
|
'_method': 'delete'
|
|
|
|
},
|
|
|
|
success: (function(_this) {
|
|
|
|
return function(data) {
|
|
|
|
$this.remove();
|
2016-08-30 19:20:31 +00:00
|
|
|
$('.prepend-top-default').html('<div class="nothing-here-block">You\'re all done!</div>');
|
2016-07-24 20:45:11 +00:00
|
|
|
return _this.updateBadges(data);
|
|
|
|
};
|
|
|
|
})(this)
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Todos.prototype.clearDone = function($row) {
|
|
|
|
var $ul;
|
|
|
|
$ul = $row.closest('ul');
|
|
|
|
$row.remove();
|
|
|
|
if (!$ul.find('li').length) {
|
|
|
|
return $ul.parents('.panel').remove();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Todos.prototype.updateBadges = function(data) {
|
|
|
|
$('.todos-pending .badge, .todos-pending-count').text(data.count);
|
|
|
|
return $('.todos-done .badge').text(data.done_count);
|
|
|
|
};
|
|
|
|
|
|
|
|
Todos.prototype.getTotalPages = function() {
|
|
|
|
return this.el.data('totalPages');
|
|
|
|
};
|
|
|
|
|
|
|
|
Todos.prototype.getCurrentPage = function() {
|
|
|
|
return this.el.data('currentPage');
|
|
|
|
};
|
|
|
|
|
|
|
|
Todos.prototype.getTodosPerPage = function() {
|
|
|
|
return this.el.data('perPage');
|
|
|
|
};
|
|
|
|
|
|
|
|
Todos.prototype.redirectIfNeeded = function(total) {
|
|
|
|
var currPage, currPages, newPages, pageParams, url;
|
|
|
|
currPages = this.getTotalPages();
|
|
|
|
currPage = this.getCurrentPage();
|
2016-07-27 03:32:10 +00:00
|
|
|
// Refresh if no remaining Todos
|
2016-07-24 20:45:11 +00:00
|
|
|
if (!total) {
|
|
|
|
location.reload();
|
|
|
|
return;
|
|
|
|
}
|
2016-07-27 03:32:10 +00:00
|
|
|
// Do nothing if no pagination
|
2016-07-24 20:45:11 +00:00
|
|
|
if (!currPages) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
newPages = Math.ceil(total / this.getTodosPerPage());
|
2016-07-27 03:32:10 +00:00
|
|
|
// Includes query strings
|
2016-07-24 20:45:11 +00:00
|
|
|
url = location.href;
|
2016-07-27 03:32:10 +00:00
|
|
|
// If new total of pages is different than we have now
|
2016-07-24 20:45:11 +00:00
|
|
|
if (newPages !== currPages) {
|
2016-07-27 03:32:10 +00:00
|
|
|
// Redirect to previous page if there's one available
|
2016-07-24 20:45:11 +00:00
|
|
|
if (currPages > 1 && currPage === currPages) {
|
|
|
|
pageParams = {
|
|
|
|
page: currPages - 1
|
|
|
|
};
|
|
|
|
url = gl.utils.mergeUrlParams(pageParams, url);
|
|
|
|
}
|
|
|
|
return Turbolinks.visit(url);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Todos.prototype.goToTodoUrl = function(e) {
|
|
|
|
var todoLink;
|
|
|
|
todoLink = $(this).data('url');
|
|
|
|
if (!todoLink) {
|
|
|
|
return;
|
|
|
|
}
|
2016-07-27 03:32:10 +00:00
|
|
|
// Allow Meta-Click or Mouse3-click to open in a new tab
|
2016-07-24 20:45:11 +00:00
|
|
|
if (e.metaKey || e.which === 2) {
|
|
|
|
e.preventDefault();
|
|
|
|
return window.open(todoLink, '_blank');
|
|
|
|
} else {
|
|
|
|
return Turbolinks.visit(todoLink);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return Todos;
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
}).call(this);
|