jellyfin-web/src/scripts/taskbutton.js

122 lines
3.8 KiB
JavaScript
Raw Normal View History

2020-07-26 04:10:32 -07:00
2020-08-16 11:24:45 -07:00
import { ConnectionManager, events } from 'jellyfin-apiclient';
2020-08-13 23:46:34 -07:00
import serverNotifications from '../scripts/serverNotifications';
import globalize from '../scripts/globalize';
import '../elements/emby-button/emby-button';
export default function (options) {
function pollTasks() {
2020-08-16 11:24:45 -07:00
ConnectionManager.getApiClient(serverId).getScheduledTasks({
IsEnabled: true
}).then(updateTasks);
}
function updateTasks(tasks) {
const task = tasks.filter(function (t) {
2020-10-11 20:29:57 -07:00
return t.Key == options.taskKey;
})[0];
2019-10-08 09:54:02 -07:00
if (options.panel) {
if (task) {
options.panel.classList.remove('hide');
2019-10-08 09:54:02 -07:00
} else {
options.panel.classList.add('hide');
2018-10-22 15:05:09 -07:00
}
}
if (!task) {
return;
2018-10-22 15:05:09 -07:00
}
if (task.State == 'Idle') {
button.removeAttribute('disabled');
} else {
button.setAttribute('disabled', 'disabled');
2018-10-22 15:05:09 -07:00
}
button.setAttribute('data-taskid', task.Id);
const progress = (task.CurrentProgressPercentage || 0).toFixed(1);
if (options.progressElem) {
options.progressElem.value = progress;
if (task.State == 'Running') {
options.progressElem.classList.remove('hide');
} else {
options.progressElem.classList.add('hide');
2019-10-07 15:03:49 -07:00
}
2018-10-22 15:05:09 -07:00
}
if (options.lastResultElem) {
const lastResult = task.LastExecutionResult ? task.LastExecutionResult.Status : '';
2019-10-08 10:14:43 -07:00
if (lastResult == 'Failed') {
options.lastResultElem.html('<span style="color:#FF0000;">(' + globalize.translate('LabelFailed') + ')</span>');
} else if (lastResult == 'Cancelled') {
options.lastResultElem.html('<span style="color:#0026FF;">(' + globalize.translate('LabelCancelled') + ')</span>');
} else if (lastResult == 'Aborted') {
options.lastResultElem.html('<span style="color:#FF0000;">' + globalize.translate('LabelAbortedByServerShutdown') + '</span>');
} else {
options.lastResultElem.html(lastResult);
2019-10-07 15:03:49 -07:00
}
}
}
2019-10-07 15:03:49 -07:00
function onScheduledTaskMessageConfirmed(id) {
2020-08-16 11:24:45 -07:00
ConnectionManager.getApiClient(serverId).startScheduledTask(id).then(pollTasks);
}
2019-10-08 10:14:43 -07:00
function onButtonClick() {
onScheduledTaskMessageConfirmed(this.getAttribute('data-taskid'));
}
function onScheduledTasksUpdate(e, apiClient, info) {
if (apiClient.serverId() === serverId) {
updateTasks(info);
2019-10-08 10:14:43 -07:00
}
}
2019-10-08 10:14:43 -07:00
let pollInterval;
const button = options.button;
const serverId = ApiClient.serverId();
2019-10-08 10:14:43 -07:00
function onPollIntervalFired() {
2020-08-16 11:24:45 -07:00
if (!ConnectionManager.getApiClient(serverId).isMessageChannelOpen()) {
pollTasks();
2019-10-08 10:14:43 -07:00
}
}
2019-10-07 15:03:49 -07:00
function startInterval() {
2020-08-16 11:24:45 -07:00
const apiClient = ConnectionManager.getApiClient(serverId);
if (pollInterval) {
clearInterval(pollInterval);
2019-10-07 15:03:49 -07:00
}
apiClient.sendMessage('ScheduledTasksInfoStart', '1000,1000');
pollInterval = setInterval(onPollIntervalFired, 5000);
}
2019-10-07 15:03:49 -07:00
function stopInterval() {
2020-08-16 11:24:45 -07:00
ConnectionManager.getApiClient(serverId).sendMessage('ScheduledTasksInfoStop');
if (pollInterval) {
clearInterval(pollInterval);
2019-10-07 15:03:49 -07:00
}
}
if (options.panel) {
options.panel.classList.add('hide');
}
if (options.mode == 'off') {
button.removeEventListener('click', onButtonClick);
events.off(serverNotifications, 'ScheduledTasksInfo', onScheduledTasksUpdate);
stopInterval();
} else {
button.addEventListener('click', onButtonClick);
pollTasks();
startInterval();
events.on(serverNotifications, 'ScheduledTasksInfo', onScheduledTasksUpdate);
}
}