2020-08-10 15:10:56 +08:00
|
|
|
/* global dropdownSelector animateSpinner PerfectSb InfiniteScroll */
|
2020-02-17 17:47:10 +08:00
|
|
|
/* eslint-disable no-param-reassign */
|
2020-02-14 19:07:40 +08:00
|
|
|
|
|
|
|
var DasboardCurrentTasksWidget = (function() {
|
2020-09-15 00:15:57 +08:00
|
|
|
var sortFilter = '.current-tasks-filters .sort-filter';
|
|
|
|
var statusFilter = '.current-tasks-filters .view-filter';
|
|
|
|
var projectFilter = '.current-tasks-filters .project-filter';
|
|
|
|
var experimentFilter = '.current-tasks-filters .experiment-filter';
|
2020-02-21 17:41:42 +08:00
|
|
|
|
2020-10-22 14:02:15 +08:00
|
|
|
function appendTasksList(json, container) {
|
2020-03-02 23:11:14 +08:00
|
|
|
$.each(json.data, (i, task) => {
|
2020-10-22 14:02:15 +08:00
|
|
|
var currentTaskItem = task;
|
2021-02-18 22:31:31 +08:00
|
|
|
$(container).find('.current-tasks-list').append(currentTaskItem);
|
2020-03-02 23:11:14 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-15 00:15:57 +08:00
|
|
|
function getDefaultStatusValues() {
|
2020-09-16 15:48:06 +08:00
|
|
|
// Select uncompleted status values
|
|
|
|
var values = [];
|
|
|
|
$(statusFilter).find('option').each(function(_, option) {
|
|
|
|
if ($(option).data('completionConsequence')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
values.push(option.value);
|
|
|
|
return this;
|
2020-09-15 00:15:57 +08:00
|
|
|
});
|
2020-09-16 15:48:06 +08:00
|
|
|
return values;
|
2020-09-15 00:15:57 +08:00
|
|
|
}
|
|
|
|
|
2020-11-11 18:07:43 +08:00
|
|
|
function resetMarkAppliedFilters() {
|
|
|
|
$('.filter-container').removeClass('filters-applied');
|
|
|
|
}
|
|
|
|
|
2020-11-06 19:55:46 +08:00
|
|
|
function filtersEnabled() {
|
|
|
|
return dropdownSelector.getData(experimentFilter).length > 0
|
|
|
|
|| dropdownSelector.getData(projectFilter).length > 0
|
|
|
|
|| (dropdownSelector.getValues(sortFilter) !== 'due_date')
|
|
|
|
|| dropdownSelector.getValues(statusFilter).sort().toString()
|
|
|
|
!== getDefaultStatusValues().sort().toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
function markAppliedFilters() {
|
|
|
|
if (!filtersEnabled()) {
|
2020-11-06 17:39:04 +08:00
|
|
|
resetMarkAppliedFilters();
|
2020-10-20 22:45:54 +08:00
|
|
|
} else {
|
|
|
|
$('.filter-container').addClass('filters-applied');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 23:11:14 +08:00
|
|
|
function initInfiniteScroll() {
|
2021-02-18 22:31:31 +08:00
|
|
|
InfiniteScroll.init('.current-tasks-list-wrapper', {
|
2020-03-02 23:11:14 +08:00
|
|
|
url: $('.current-tasks-list').data('tasksListUrl'),
|
|
|
|
customResponse: (json, container) => {
|
2020-10-22 14:02:15 +08:00
|
|
|
appendTasksList(json, container);
|
2020-03-02 23:11:14 +08:00
|
|
|
},
|
|
|
|
customParams: (params) => {
|
|
|
|
params.project_id = dropdownSelector.getValues(projectFilter);
|
|
|
|
params.experiment_id = dropdownSelector.getValues(experimentFilter);
|
|
|
|
params.sort = dropdownSelector.getValues(sortFilter);
|
2020-08-27 17:30:55 +08:00
|
|
|
params.statuses = dropdownSelector.getValues(statusFilter);
|
2020-03-02 23:11:14 +08:00
|
|
|
params.query = $('.current-tasks-widget .task-search-field').val();
|
|
|
|
params.mode = $('.current-tasks-navbar .active').data('mode');
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-15 00:15:57 +08:00
|
|
|
function filterStateSave() {
|
|
|
|
var teamId = $('.current-tasks-filters').data('team-id');
|
|
|
|
var filterState = {
|
|
|
|
sort: dropdownSelector.getValues(sortFilter),
|
|
|
|
statuses: dropdownSelector.getValues(statusFilter),
|
2020-09-16 15:48:06 +08:00
|
|
|
project_id: dropdownSelector.getData(projectFilter),
|
|
|
|
experiment_id: dropdownSelector.getData(experimentFilter),
|
2020-09-15 00:15:57 +08:00
|
|
|
mode: $('.current-tasks-navbar .active').data('mode')
|
|
|
|
};
|
2020-11-06 19:55:46 +08:00
|
|
|
markAppliedFilters();
|
2020-11-06 17:39:04 +08:00
|
|
|
localStorage.setItem('current_tasks_filters_per_team/' + teamId, JSON.stringify(filterState));
|
2020-09-15 00:15:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function filterStateLoad() {
|
|
|
|
var teamId = $('.current-tasks-filters').data('team-id');
|
|
|
|
var filterState = localStorage.getItem('current_tasks_filters_per_team/' + teamId);
|
|
|
|
var parsedFilterState;
|
|
|
|
var allStatusValues = $.map($(statusFilter).find('option'), function(option) {
|
|
|
|
return option.value;
|
|
|
|
});
|
|
|
|
|
2020-09-16 15:48:06 +08:00
|
|
|
if (filterState) {
|
|
|
|
try {
|
|
|
|
parsedFilterState = JSON.parse(filterState);
|
|
|
|
dropdownSelector.selectValues(sortFilter, parsedFilterState.sort);
|
|
|
|
// Check if saved statuses are valid
|
|
|
|
if (parsedFilterState.statuses.every(status => allStatusValues.includes(status))) {
|
|
|
|
dropdownSelector.selectValues(statusFilter, parsedFilterState.statuses);
|
|
|
|
} else {
|
|
|
|
dropdownSelector.selectValues(statusFilter, getDefaultStatusValues());
|
|
|
|
}
|
|
|
|
dropdownSelector.setData(projectFilter, parsedFilterState.project_id);
|
|
|
|
dropdownSelector.setData(experimentFilter, parsedFilterState.experiment_id);
|
|
|
|
// Select saved navbar state
|
|
|
|
$('.current-tasks-navbar .navbar-link').removeClass('active');
|
|
|
|
$('.current-tasks-navbar').find(`[data-mode='${parsedFilterState.mode}']`).addClass('active');
|
2020-11-06 19:55:46 +08:00
|
|
|
markAppliedFilters();
|
2020-09-16 15:48:06 +08:00
|
|
|
} catch (e) {
|
2020-09-15 00:15:57 +08:00
|
|
|
dropdownSelector.selectValues(statusFilter, getDefaultStatusValues());
|
2020-11-11 18:07:43 +08:00
|
|
|
resetMarkAppliedFilters();
|
2020-09-15 00:15:57 +08:00
|
|
|
}
|
2020-09-16 15:48:06 +08:00
|
|
|
} else {
|
|
|
|
dropdownSelector.selectValues(statusFilter, getDefaultStatusValues());
|
2020-11-06 17:39:04 +08:00
|
|
|
resetMarkAppliedFilters();
|
2020-09-15 00:15:57 +08:00
|
|
|
}
|
2020-03-12 22:00:14 +08:00
|
|
|
}
|
|
|
|
|
2020-03-02 23:11:14 +08:00
|
|
|
function loadCurrentTasksList(newList) {
|
2020-02-21 17:41:42 +08:00
|
|
|
var $currentTasksList = $('.current-tasks-list');
|
|
|
|
var params = {
|
|
|
|
project_id: dropdownSelector.getValues(projectFilter),
|
|
|
|
experiment_id: dropdownSelector.getValues(experimentFilter),
|
|
|
|
sort: dropdownSelector.getValues(sortFilter),
|
2020-08-27 17:30:55 +08:00
|
|
|
statuses: dropdownSelector.getValues(statusFilter),
|
2020-03-03 16:50:44 +08:00
|
|
|
query: $('.current-tasks-widget .task-search-field').val(),
|
2020-02-21 17:41:42 +08:00
|
|
|
mode: $('.current-tasks-navbar .active').data('mode')
|
|
|
|
};
|
|
|
|
animateSpinner($currentTasksList, true);
|
2020-03-03 20:52:36 +08:00
|
|
|
$.get($currentTasksList.data('tasksListUrl'), params, function(result) {
|
2020-03-12 22:00:14 +08:00
|
|
|
$currentTasksList.empty();
|
2020-02-26 18:07:17 +08:00
|
|
|
// Toggle empty state
|
2020-03-03 20:52:36 +08:00
|
|
|
if (result.data.length === 0) {
|
2020-11-06 19:55:46 +08:00
|
|
|
if (filtersEnabled() || $('.current-tasks-widget .task-search-field').val().length > 0) {
|
2020-03-12 22:00:14 +08:00
|
|
|
$currentTasksList.append($('#dashboard-current-task-no-search-results').html());
|
|
|
|
} else {
|
|
|
|
$currentTasksList.append($('#dashboard-current-task-no-tasks').html());
|
|
|
|
$currentTasksList.find('.widget-placeholder').addClass($('.current-tasks-navbar .active').data('mode'));
|
|
|
|
}
|
2020-02-26 18:07:17 +08:00
|
|
|
}
|
2021-03-12 20:33:37 +08:00
|
|
|
appendTasksList(result, '.current-tasks-list-wrapper');
|
2020-02-26 18:07:17 +08:00
|
|
|
PerfectSb().update_all();
|
2021-02-18 22:31:31 +08:00
|
|
|
if (newList) InfiniteScroll.resetScroll('.current-tasks-list-wrapper');
|
2020-02-21 17:41:42 +08:00
|
|
|
animateSpinner($currentTasksList, false);
|
2020-11-06 17:39:04 +08:00
|
|
|
}).error(function(error) {
|
|
|
|
// If error is 403, it is possible that the user was removed from project/experiment,
|
2020-11-11 18:07:43 +08:00
|
|
|
// so clear local storage and filter state
|
2020-11-06 17:39:04 +08:00
|
|
|
if (error.status === 403) {
|
|
|
|
localStorage.removeItem('current_tasks_filters_per_team/' + $('.current-tasks-filters').data('team-id'));
|
2020-11-11 18:07:43 +08:00
|
|
|
$('.current-tasks-filters .clear-button').trigger('click');
|
|
|
|
resetMarkAppliedFilters();
|
|
|
|
loadCurrentTasksList();
|
2020-11-06 17:39:04 +08:00
|
|
|
}
|
2020-02-21 17:41:42 +08:00
|
|
|
});
|
|
|
|
}
|
2020-02-17 17:47:10 +08:00
|
|
|
|
2020-02-21 17:41:42 +08:00
|
|
|
function initFilters() {
|
2020-09-15 00:15:57 +08:00
|
|
|
$('.current-tasks-filters .clear-button').click((e) => {
|
2020-02-21 17:41:42 +08:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
2020-09-15 00:15:57 +08:00
|
|
|
|
|
|
|
dropdownSelector.selectValues(sortFilter, 'due_date');
|
|
|
|
dropdownSelector.selectValues(statusFilter, getDefaultStatusValues());
|
2020-02-17 17:47:10 +08:00
|
|
|
dropdownSelector.clearData(projectFilter);
|
|
|
|
dropdownSelector.clearData(experimentFilter);
|
|
|
|
});
|
|
|
|
|
|
|
|
dropdownSelector.init(sortFilter, {
|
2020-02-14 19:07:40 +08:00
|
|
|
noEmptyOption: true,
|
|
|
|
singleSelect: true,
|
|
|
|
closeOnSelect: true,
|
2020-02-17 22:32:43 +08:00
|
|
|
selectAppearance: 'simple',
|
|
|
|
disableSearch: true
|
2020-02-14 19:07:40 +08:00
|
|
|
});
|
|
|
|
|
2020-08-27 17:30:55 +08:00
|
|
|
dropdownSelector.init(statusFilter, {
|
2020-02-17 22:32:43 +08:00
|
|
|
selectAppearance: 'simple',
|
2020-08-27 17:30:55 +08:00
|
|
|
optionClass: 'checkbox-icon'
|
2020-02-14 19:07:40 +08:00
|
|
|
});
|
|
|
|
|
2020-02-17 17:47:10 +08:00
|
|
|
dropdownSelector.init(projectFilter, {
|
|
|
|
singleSelect: true,
|
|
|
|
closeOnSelect: true,
|
|
|
|
emptyOptionAjax: true,
|
|
|
|
selectAppearance: 'simple',
|
|
|
|
ajaxParams: (params) => {
|
2020-02-21 17:41:42 +08:00
|
|
|
params.mode = $('.current-tasks-navbar .active').data('mode');
|
2020-02-17 17:47:10 +08:00
|
|
|
return params;
|
|
|
|
},
|
|
|
|
onChange: () => {
|
|
|
|
var selectedValue = dropdownSelector.getValues(projectFilter);
|
|
|
|
if (selectedValue > 0) {
|
|
|
|
dropdownSelector.enableSelector(experimentFilter);
|
|
|
|
} else {
|
|
|
|
dropdownSelector.disableSelector(experimentFilter);
|
|
|
|
}
|
|
|
|
dropdownSelector.clearData(experimentFilter);
|
|
|
|
}
|
|
|
|
});
|
2020-02-17 22:32:43 +08:00
|
|
|
|
2020-02-17 17:47:10 +08:00
|
|
|
dropdownSelector.init(experimentFilter, {
|
|
|
|
singleSelect: true,
|
|
|
|
closeOnSelect: true,
|
|
|
|
emptyOptionAjax: true,
|
|
|
|
selectAppearance: 'simple',
|
|
|
|
ajaxParams: (params) => {
|
2020-02-21 17:41:42 +08:00
|
|
|
params.mode = $('.current-tasks-navbar .active').data('mode');
|
2020-02-17 17:47:10 +08:00
|
|
|
params.project_id = dropdownSelector.getValues(projectFilter);
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
});
|
2020-02-14 19:07:40 +08:00
|
|
|
|
2020-09-15 00:15:57 +08:00
|
|
|
$('.current-tasks-filters').click((e) => {
|
2020-02-17 17:47:10 +08:00
|
|
|
// Prevent filter window close
|
2020-02-14 19:07:40 +08:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
2020-02-17 17:47:10 +08:00
|
|
|
dropdownSelector.closeDropdown(sortFilter);
|
2020-08-27 17:30:55 +08:00
|
|
|
dropdownSelector.closeDropdown(statusFilter);
|
2020-02-17 17:47:10 +08:00
|
|
|
dropdownSelector.closeDropdown(projectFilter);
|
|
|
|
dropdownSelector.closeDropdown(experimentFilter);
|
2020-02-14 19:07:40 +08:00
|
|
|
});
|
2020-02-21 17:41:42 +08:00
|
|
|
|
2020-09-15 00:15:57 +08:00
|
|
|
$('.current-tasks-filters .apply-filters').click((e) => {
|
|
|
|
$('.current-tasks-filters').dropdown('toggle');
|
2020-02-21 17:41:42 +08:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
2020-03-04 20:21:52 +08:00
|
|
|
|
|
|
|
$('.filter-container').on('hide.bs.dropdown', () => {
|
|
|
|
loadCurrentTasksList(true);
|
2020-09-15 00:15:57 +08:00
|
|
|
filterStateSave();
|
2020-03-12 22:05:38 +08:00
|
|
|
$('.current-tasks-list').removeClass('disabled');
|
2020-03-12 22:00:14 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
$('.filter-container').on('shown.bs.dropdown', () => {
|
2020-03-12 22:05:38 +08:00
|
|
|
$('.current-tasks-list').addClass('disabled');
|
2020-03-04 20:21:52 +08:00
|
|
|
});
|
2020-02-21 17:41:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function initNavbar() {
|
2020-03-06 20:51:18 +08:00
|
|
|
$('.current-tasks-navbar .navbar-link').on('click', function() {
|
|
|
|
$(this).parent().find('.navbar-link').removeClass('active');
|
2020-02-21 17:41:42 +08:00
|
|
|
$(this).addClass('active');
|
2020-03-02 23:11:14 +08:00
|
|
|
loadCurrentTasksList(true);
|
2020-09-15 00:15:57 +08:00
|
|
|
filterStateSave();
|
2020-02-21 17:41:42 +08:00
|
|
|
});
|
2020-02-14 19:07:40 +08:00
|
|
|
}
|
|
|
|
|
2020-03-03 16:50:44 +08:00
|
|
|
function initSearch() {
|
|
|
|
$('.current-tasks-widget').on('change', '.task-search-field', () => {
|
|
|
|
loadCurrentTasksList();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-06 17:39:04 +08:00
|
|
|
return {
|
2020-11-11 18:07:43 +08:00
|
|
|
init: () => {
|
|
|
|
if ($('.current-tasks-widget').length) {
|
|
|
|
initNavbar();
|
|
|
|
initFilters();
|
|
|
|
initSearch();
|
|
|
|
filterStateLoad();
|
|
|
|
loadCurrentTasksList();
|
|
|
|
initInfiniteScroll();
|
|
|
|
}
|
|
|
|
}
|
2020-02-14 19:07:40 +08:00
|
|
|
};
|
|
|
|
}());
|
|
|
|
|
|
|
|
$(document).on('turbolinks:load', function() {
|
|
|
|
DasboardCurrentTasksWidget.init();
|
|
|
|
});
|