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

199 lines
7.1 KiB
JavaScript
Raw Normal View History

/* global dropdownSelector I18n 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 = '.curent-tasks-filters .sort-filter';
var viewFilter = '.curent-tasks-filters .view-filter';
var projectFilter = '.curent-tasks-filters .project-filter';
var experimentFilter = '.curent-tasks-filters .experiment-filter';
function generateTasksListHtml(json, container) {
$.each(json.data, (i, task) => {
2020-03-05 21:40:46 +08:00
var currentTaskItem = ` <a class="current-task-item" href="${task.link}">
<div class="current-task-breadcrumbs">${task.project}<span class="slash">/</span>${task.experiment}</div>
<div class="item-row">
<div class="task-name">${task.name}</div>
<div class="task-due-date ${task.state.class} ${task.due_date ? '' : 'hidden'}">
<i class="fas fa-calendar-day"></i> ${I18n.t('dashboard.current_tasks.due_date', { date: task.due_date })}
</div>
<div class="task-progress-container ${task.state.class}">
2020-03-09 16:54:02 +08:00
<div class="task-progress" style="padding-left: ${task.steps_precentage}%"></div>
2020-03-05 21:40:46 +08:00
<div class="task-progress-label">${task.state.text}</div>
</div>
</div>
2020-03-05 21:40:46 +08:00
</a>`;
$(container).append(currentTaskItem);
});
}
function initInfiniteScroll() {
InfiniteScroll.init('.current-tasks-list', {
url: $('.current-tasks-list').data('tasksListUrl'),
customResponse: (json, container) => {
generateTasksListHtml(json, container);
},
customParams: (params) => {
params.project_id = dropdownSelector.getValues(projectFilter);
params.experiment_id = dropdownSelector.getValues(experimentFilter);
params.sort = dropdownSelector.getValues(sortFilter);
params.view = dropdownSelector.getValues(viewFilter);
params.query = $('.current-tasks-widget .task-search-field').val();
params.mode = $('.current-tasks-navbar .active').data('mode');
return params;
}
});
}
2020-03-12 22:00:14 +08:00
function filtersEnabled() {
2020-03-12 22:05:38 +08:00
return dropdownSelector.getValues(experimentFilter)
|| dropdownSelector.getValues(projectFilter)
|| $('.current-tasks-widget .task-search-field').val().length > 0
2020-03-12 22:05:38 +08:00
|| dropdownSelector.getValues(viewFilter) !== 'uncompleted';
2020-03-12 22:00:14 +08:00
}
function loadCurrentTasksList(newList) {
var $currentTasksList = $('.current-tasks-list');
var params = {
project_id: dropdownSelector.getValues(projectFilter),
experiment_id: dropdownSelector.getValues(experimentFilter),
sort: dropdownSelector.getValues(sortFilter),
view: dropdownSelector.getValues(viewFilter),
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);
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();
// Toggle empty state
2020-03-03 20:52:36 +08:00
if (result.data.length === 0) {
2020-03-12 22:00:14 +08:00
if (filtersEnabled()) {
$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-03-03 20:52:36 +08:00
generateTasksListHtml(result, $currentTasksList);
PerfectSb().update_all();
if (newList) InfiniteScroll.resetScroll('.current-tasks-list');
animateSpinner($currentTasksList, false);
});
}
2020-02-17 17:47:10 +08:00
function initFilters() {
$('.curent-tasks-filters .clear-button').click((e) => {
e.stopPropagation();
e.preventDefault();
dropdownSelector.selectValue(sortFilter, 'date_asc');
dropdownSelector.selectValue(viewFilter, 'uncompleted');
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-02-17 17:47:10 +08:00
dropdownSelector.init(viewFilter, {
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-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
$('.curent-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);
dropdownSelector.closeDropdown(viewFilter);
dropdownSelector.closeDropdown(projectFilter);
dropdownSelector.closeDropdown(experimentFilter);
2020-02-14 19:07:40 +08:00
});
$('.curent-tasks-filters .apply-filters').click((e) => {
$('.curent-tasks-filters').dropdown('toggle');
e.stopPropagation();
e.preventDefault();
loadCurrentTasksList(true);
});
$('.filter-container').on('hide.bs.dropdown', () => {
loadCurrentTasksList(true);
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);
});
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-02-14 19:07:40 +08:00
return {
init: () => {
if ($('.current-tasks-widget').length) {
initNavbar();
2020-02-14 19:07:40 +08:00
initFilters();
2020-03-03 16:50:44 +08:00
initSearch();
loadCurrentTasksList();
initInfiniteScroll();
2020-02-14 19:07:40 +08:00
}
}
};
}());
$(document).on('turbolinks:load', function() {
DasboardCurrentTasksWidget.init();
});