jellyfish-web/src/controllers/dashboard/scheduledtasks/scheduledtasks.js

193 lines
8.6 KiB
JavaScript
Raw Normal View History

2020-08-14 08:46:34 +02:00
import 'jquery';
import loading from '../../../components/loading/loading';
2020-09-08 02:05:02 -04:00
import { Events } from 'jellyfin-apiclient';
2020-08-14 08:46:34 +02:00
import globalize from '../../../scripts/globalize';
import serverNotifications from '../../../scripts/serverNotifications';
import { formatDistance, formatDistanceToNow } from 'date-fns';
import { getLocale, localeWithSuffix } from '../../../scripts/dfnshelper';
import '../../../components/listview/listview.css';
import '../../../elements/emby-button/emby-button';
/* eslint-disable indent */
2018-10-23 01:05:09 +03:00
function reloadList(page) {
ApiClient.getScheduledTasks({
2019-04-22 09:15:28 -07:00
isHidden: false
2018-10-23 01:05:09 +03:00
}).then(function(tasks) {
2019-04-22 09:15:28 -07:00
populateList(page, tasks);
loading.hide();
});
2018-10-23 01:05:09 +03:00
}
function populateList(page, tasks) {
tasks = tasks.sort(function(a, b) {
2020-05-04 12:44:12 +02:00
a = a.Category + ' ' + a.Name;
b = b.Category + ' ' + b.Name;
2019-04-22 09:15:28 -07:00
return a == b ? 0 : a < b ? -1 : 1;
2018-10-23 01:05:09 +03:00
});
2019-04-22 09:15:28 -07:00
2020-07-18 21:51:48 +01:00
let currentCategory;
let html = '';
for (let i = 0; i < tasks.length; i++) {
const task = tasks[i];
2019-04-22 09:15:28 -07:00
if (task.Category != currentCategory) {
currentCategory = task.Category;
if (currentCategory) {
2020-05-04 12:44:12 +02:00
html += '</div>';
html += '</div>';
2019-04-22 09:15:28 -07:00
}
html += '<div class="verticalSection verticalSection-extrabottompadding">';
html += '<div class="sectionTitleContainer" style="margin-bottom:1em;">';
html += '<h2 class="sectionTitle">';
html += currentCategory;
2020-05-04 12:44:12 +02:00
html += '</h2>';
2019-04-22 09:15:28 -07:00
if (i === 0) {
2020-05-04 12:44:12 +02:00
html += '<a is="emby-linkbutton" class="raised button-alt headerHelpButton" target="_blank" href="https://docs.jellyfin.org/general/server/tasks.html">' + globalize.translate('Help') + '</a>';
2019-04-22 09:15:28 -07:00
}
2020-05-04 12:44:12 +02:00
html += '</div>';
2019-04-22 09:15:28 -07:00
html += '<div class="paperList">';
}
html += '<div class="listItem listItem-border scheduledTaskPaperIconItem" data-status="' + task.State + '">';
html += "<a is='emby-linkbutton' style='margin:0;padding:0;' class='clearLink listItemIconContainer' href='scheduledtask.html?id=" + task.Id + "'>";
2020-04-26 02:37:28 +03:00
html += '<span class="material-icons listItemIcon schedule"></span>';
2020-05-04 12:44:12 +02:00
html += '</a>';
2019-04-22 09:15:28 -07:00
html += '<div class="listItemBody two-line">';
html += "<a class='clearLink' style='margin:0;padding:0;display:block;text-align:left;' is='emby-linkbutton' href='scheduledtask.html?id=" + task.Id + "'>";
2020-05-04 12:44:12 +02:00
html += "<h3 class='listItemBodyText'>" + task.Name + '</h3>';
html += "<div class='secondary listItemBodyText' id='taskProgress" + task.Id + "'>" + getTaskProgressHtml(task) + '</div>';
html += '</a>';
html += '</div>';
if (task.State === 'Running') {
html += '<button type="button" is="paper-icon-button-light" id="btnTask' + task.Id + '" class="btnStopTask" data-taskid="' + task.Id + '" title="' + globalize.translate('ButtonStop') + '"><span class="material-icons stop"></span></button>';
} else if (task.State === 'Idle') {
html += '<button type="button" is="paper-icon-button-light" id="btnTask' + task.Id + '" class="btnStartTask" data-taskid="' + task.Id + '" title="' + globalize.translate('ButtonStart') + '"><span class="material-icons play_arrow"></span></button>';
2019-04-22 09:15:28 -07:00
}
2020-05-04 12:44:12 +02:00
html += '</div>';
2018-10-23 01:05:09 +03:00
}
2019-04-22 09:15:28 -07:00
if (tasks.length) {
2020-05-04 12:44:12 +02:00
html += '</div>';
html += '</div>';
2019-04-22 09:15:28 -07:00
}
2020-05-04 12:44:12 +02:00
page.querySelector('.divScheduledTasks').innerHTML = html;
2018-10-23 01:05:09 +03:00
}
function getTaskProgressHtml(task) {
2020-07-18 21:51:48 +01:00
let html = '';
2020-05-04 12:44:12 +02:00
if (task.State === 'Idle') {
2019-04-22 09:15:28 -07:00
if (task.LastExecutionResult) {
2020-07-18 21:51:48 +01:00
const endtime = Date.parse(task.LastExecutionResult.EndTimeUtc);
const starttime = Date.parse(task.LastExecutionResult.StartTimeUtc);
2020-08-14 08:46:34 +02:00
html += globalize.translate('LabelScheduledTaskLastRan', formatDistanceToNow(endtime, localeWithSuffix),
formatDistance(starttime, endtime, { locale: getLocale() }));
2020-05-04 12:44:12 +02:00
if (task.LastExecutionResult.Status === 'Failed') {
html += " <span style='color:#FF0000;'>(" + globalize.translate('LabelFailed') + ')</span>';
} else if (task.LastExecutionResult.Status === 'Cancelled') {
html += " <span style='color:#0026FF;'>(" + globalize.translate('LabelCancelled') + ')</span>';
} else if (task.LastExecutionResult.Status === 'Aborted') {
html += " <span style='color:#FF0000;'>" + globalize.translate('LabelAbortedByServerShutdown') + '</span>';
2019-04-22 09:15:28 -07:00
}
}
2020-05-04 12:44:12 +02:00
} else if (task.State === 'Running') {
2020-07-18 21:51:48 +01:00
const progress = (task.CurrentProgressPercentage || 0).toFixed(1);
2019-01-21 17:55:19 +01:00
html += '<div style="display:flex;align-items:center;">';
html += '<div class="taskProgressOuter" title="' + progress + '%" style="flex-grow:1;">';
html += '<div class="taskProgressInner" style="width:' + progress + '%;">';
2020-05-04 12:44:12 +02:00
html += '</div>';
html += '</div>';
html += "<span style='color:#00a4dc;margin-left:5px;'>" + progress + '%</span>';
html += '</div>';
2019-01-21 17:55:19 +01:00
} else {
2020-05-04 12:44:12 +02:00
html += "<span style='color:#FF0000;'>" + globalize.translate('LabelStopping') + '</span>';
2019-01-21 17:55:19 +01:00
}
2019-04-22 09:15:28 -07:00
return html;
2018-10-23 01:05:09 +03:00
}
2020-03-10 12:32:14 +03:00
function setTaskButtonIcon(button, icon) {
2020-07-19 17:38:42 +02:00
const inner = button.querySelector('.material-icons');
2020-05-04 12:44:12 +02:00
inner.classList.remove('stop', 'play_arrow');
2020-03-10 12:32:14 +03:00
inner.classList.add(icon);
}
2018-10-23 01:05:09 +03:00
function updateTaskButton(elem, state) {
2020-05-04 12:44:12 +02:00
if (state === 'Running') {
elem.classList.remove('btnStartTask');
elem.classList.add('btnStopTask');
setTaskButtonIcon(elem, 'stop');
elem.title = globalize.translate('ButtonStop');
} else if (state === 'Idle') {
elem.classList.add('btnStartTask');
elem.classList.remove('btnStopTask');
setTaskButtonIcon(elem, 'play_arrow');
elem.title = globalize.translate('ButtonStart');
2019-04-22 09:15:28 -07:00
}
2020-05-04 12:44:12 +02:00
$(elem).parents('.listItem')[0].setAttribute('data-status', state);
2018-10-23 01:05:09 +03:00
}
2019-04-22 09:15:28 -07:00
export default function(view, params) {
2018-10-23 01:05:09 +03:00
function updateTasks(tasks) {
2020-07-18 21:51:48 +01:00
for (let i = 0; i < tasks.length; i++) {
const task = tasks[i];
2020-05-04 12:44:12 +02:00
view.querySelector('#taskProgress' + task.Id).innerHTML = getTaskProgressHtml(task);
updateTaskButton(view.querySelector('#btnTask' + task.Id), task.State);
2018-10-23 01:05:09 +03:00
}
}
function onPollIntervalFired() {
2019-04-22 09:15:28 -07:00
if (!ApiClient.isMessageChannelOpen()) {
reloadList(view);
}
2018-10-23 01:05:09 +03:00
}
function onScheduledTasksUpdate(e, apiClient, info) {
2019-04-22 09:15:28 -07:00
if (apiClient.serverId() === serverId) {
updateTasks(info);
}
2018-10-23 01:05:09 +03:00
}
function startInterval() {
2020-05-04 12:44:12 +02:00
ApiClient.sendMessage('ScheduledTasksInfoStart', '1000,1000');
2019-04-22 09:15:28 -07:00
pollInterval && clearInterval(pollInterval);
pollInterval = setInterval(onPollIntervalFired, 1e4);
2018-10-23 01:05:09 +03:00
}
function stopInterval() {
2020-05-04 12:44:12 +02:00
ApiClient.sendMessage('ScheduledTasksInfoStop');
2019-04-22 09:15:28 -07:00
pollInterval && clearInterval(pollInterval);
2018-10-23 01:05:09 +03:00
}
2019-04-22 09:15:28 -07:00
2020-07-18 21:51:48 +01:00
let pollInterval;
const serverId = ApiClient.serverId();
2019-04-22 09:15:28 -07:00
2020-05-04 12:44:12 +02:00
$('.divScheduledTasks', view).on('click', '.btnStartTask', function() {
2020-07-18 21:51:48 +01:00
const button = this;
2020-07-19 17:38:42 +02:00
const id = button.getAttribute('data-taskid');
2018-10-23 01:05:09 +03:00
ApiClient.startScheduledTask(id).then(function() {
2020-05-04 12:44:12 +02:00
updateTaskButton(button, 'Running');
2019-04-22 09:15:28 -07:00
reloadList(view);
});
2019-04-22 09:15:28 -07:00
});
2020-05-04 12:44:12 +02:00
$('.divScheduledTasks', view).on('click', '.btnStopTask', function() {
2020-07-18 21:51:48 +01:00
const button = this;
2020-07-19 17:38:42 +02:00
const id = button.getAttribute('data-taskid');
2018-10-23 01:05:09 +03:00
ApiClient.stopScheduledTask(id).then(function() {
2020-05-04 12:44:12 +02:00
updateTaskButton(button, '');
2019-04-22 09:15:28 -07:00
reloadList(view);
});
2019-04-22 09:15:28 -07:00
});
2020-05-04 12:44:12 +02:00
view.addEventListener('viewbeforehide', function() {
2020-09-08 02:05:02 -04:00
Events.off(serverNotifications, 'ScheduledTasksInfo', onScheduledTasksUpdate);
2019-04-22 09:15:28 -07:00
stopInterval();
});
2020-05-04 12:44:12 +02:00
view.addEventListener('viewshow', function() {
2019-04-22 09:15:28 -07:00
loading.show();
startInterval();
reloadList(view);
2020-09-08 02:05:02 -04:00
Events.on(serverNotifications, 'ScheduledTasksInfo', onScheduledTasksUpdate);
2019-04-22 09:15:28 -07:00
});
}
/* eslint-enable indent */