scinote-web/app/assets/javascripts/dashboard/current_tasks.js

266 lines
9 KiB
JavaScript
Raw Normal View History

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() {
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';
function appendTasksList(json, container) {
$.each(json.data, (i, task) => {
var currentTaskItem = task;
$(container).find('.current-tasks-list').append(currentTaskItem);
});
}
function getDefaultStatusValues() {
// Select uncompleted status values
var values = [];
$(statusFilter).find('option').each(function(_, option) {
if ($(option).data('completionConsequence')) {
return false;
}
values.push(option.value);
return this;
});
return values;
}
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_first')
2020-11-06 19:55:46 +08:00
|| dropdownSelector.getValues(statusFilter).sort().toString()
!== getDefaultStatusValues().sort().toString();
}
function markAppliedFilters() {
if (!filtersEnabled()) {
resetMarkAppliedFilters();
2020-10-20 22:45:54 +08:00
} else {
$('.filter-container').addClass('filters-applied');
}
}
function filterStateSave() {
var teamId = $('.current-tasks-filters').data('team-id');
var filterState = {
sort: dropdownSelector.getValues(sortFilter),
statuses: dropdownSelector.getValues(statusFilter),
project_id: dropdownSelector.getData(projectFilter),
experiment_id: dropdownSelector.getData(experimentFilter),
mode: $('.current-tasks-navbar .active').data('mode')
};
2020-11-06 19:55:46 +08:00
markAppliedFilters();
localStorage.setItem('current_tasks_filters_per_team/' + teamId, JSON.stringify(filterState));
}
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;
});
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();
} catch (e) {
dropdownSelector.selectValues(statusFilter, getDefaultStatusValues());
2020-11-11 18:07:43 +08:00
resetMarkAppliedFilters();
}
} else {
dropdownSelector.selectValues(statusFilter, getDefaultStatusValues());
resetMarkAppliedFilters();
}
2020-03-12 22:00:14 +08:00
}
function loadCurrentTasksList(newList) {
var $currentTasksList = $('.current-tasks-list');
var requestParams = {
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(),
mode: $('.current-tasks-navbar .active').data('mode')
};
animateSpinner($currentTasksList, true);
$.get($currentTasksList.data('tasksListUrl'), requestParams, function(result) {
2020-03-12 22:00:14 +08:00
$currentTasksList.empty();
// 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'));
}
}
appendTasksList(result, '.current-tasks-list-wrapper');
PerfectSb().update_all();
InfiniteScroll.init('.current-tasks-list-wrapper', {
url: $currentTasksList.data('tasksListUrl'),
lastPage: !result.next_page,
customResponse: (json, container) => {
appendTasksList(json, container);
},
customParams: (params) => {
return { ...params, ...requestParams };
}
});
animateSpinner($currentTasksList, false);
}).fail(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
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-02-17 17:47:10 +08:00
function initFilters() {
$('.current-tasks-filters .clear-button').click((e) => {
e.stopPropagation();
e.preventDefault();
dropdownSelector.selectValues(sortFilter, 'due_first');
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,
selectAppearance: 'simple',
disableSearch: true
2020-02-14 19:07:40 +08:00
});
2020-08-27 17:30:55 +08:00
dropdownSelector.init(statusFilter, {
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) => {
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 17:47:10 +08:00
dropdownSelector.init(experimentFilter, {
singleSelect: true,
closeOnSelect: true,
emptyOptionAjax: true,
selectAppearance: 'simple',
ajaxParams: (params) => {
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
$('.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
});
$('.current-tasks-filters .apply-filters').click((e) => {
$('.current-tasks-filters').dropdown('toggle');
e.stopPropagation();
e.preventDefault();
});
$('.current-tasks-filters').on('click', '.close-dropdown', (e) => {
$('.current-tasks-filters').dropdown('toggle');
e.stopPropagation();
e.preventDefault();
});
$('.filter-container').on('hide.bs.dropdown', () => {
loadCurrentTasksList(true);
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');
});
}
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');
$(this).addClass('active');
loadCurrentTasksList(true);
filterStateSave();
});
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();
});
}
return {
2020-11-11 18:07:43 +08:00
init: () => {
if ($('.current-tasks-widget').length) {
initNavbar();
initFilters();
initSearch();
filterStateLoad();
loadCurrentTasksList();
}
}
2020-02-14 19:07:40 +08:00
};
}());
$(document).on('turbolinks:load', function() {
DasboardCurrentTasksWidget.init();
});