1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/scripts/taskbutton.js

120 lines
4.2 KiB
JavaScript
Raw Normal View History

2020-05-04 12:44:12 +02:00
define(['events', 'userSettings', 'serverNotifications', 'connectionManager', 'globalize', 'emby-button'], function (events, userSettings, serverNotifications, connectionManager, globalize) {
'use strict';
2019-10-08 01:03:49 +03:00
return function (options) {
2018-10-23 01:05:09 +03:00
function pollTasks() {
connectionManager.getApiClient(serverId).getScheduledTasks({
2019-10-08 01:03:49 +03:00
IsEnabled: true
}).then(updateTasks);
2018-10-23 01:05:09 +03:00
}
function updateTasks(tasks) {
2019-10-08 01:03:49 +03:00
var task = tasks.filter(function (t) {
return t.Key == options.taskKey;
2018-10-23 01:05:09 +03:00
})[0];
2019-10-08 01:03:49 +03:00
2019-10-08 19:54:02 +03:00
if (options.panel) {
if (task) {
options.panel.classList.remove('hide');
2019-10-08 01:03:49 +03:00
} else {
2019-10-08 19:54:02 +03:00
options.panel.classList.add('hide');
2019-10-08 01:03:49 +03:00
}
2019-10-08 19:54:02 +03:00
}
if (!task) {
return;
}
if (task.State == 'Idle') {
2020-05-04 12:44:12 +02:00
button.removeAttribute('disabled');
2019-10-08 19:54:02 +03:00
} else {
2020-05-04 12:44:12 +02:00
button.setAttribute('disabled', 'disabled');
2019-10-08 19:54:02 +03:00
}
2020-05-04 12:44:12 +02:00
button.setAttribute('data-taskid', task.Id);
2019-10-08 19:54:02 +03:00
var progress = (task.CurrentProgressPercentage || 0).toFixed(1);
if (options.progressElem) {
options.progressElem.value = progress;
2019-10-08 01:03:49 +03:00
2019-10-08 19:54:02 +03:00
if (task.State == 'Running') {
options.progressElem.classList.remove('hide');
} else {
options.progressElem.classList.add('hide');
}
}
if (options.lastResultElem) {
var lastResult = task.LastExecutionResult ? task.LastExecutionResult.Status : '';
2020-05-04 12:44:12 +02:00
if (lastResult == 'Failed') {
2020-03-26 14:25:16 +01:00
options.lastResultElem.html('<span style="color:#FF0000;">(' + globalize.translate('LabelFailed') + ')</span>');
2020-05-04 12:44:12 +02:00
} else if (lastResult == 'Cancelled') {
2020-03-26 14:25:16 +01:00
options.lastResultElem.html('<span style="color:#0026FF;">(' + globalize.translate('LabelCancelled') + ')</span>');
2020-05-04 12:44:12 +02:00
} else if (lastResult == 'Aborted') {
2020-03-26 14:25:16 +01:00
options.lastResultElem.html('<span style="color:#FF0000;">' + globalize.translate('LabelAbortedByServerShutdown') + '</span>');
2019-10-08 19:54:02 +03:00
} else {
options.lastResultElem.html(lastResult);
2018-10-23 01:05:09 +03:00
}
}
}
function onScheduledTaskMessageConfirmed(id) {
2019-10-08 01:03:49 +03:00
connectionManager.getApiClient(serverId).startScheduledTask(id).then(pollTasks);
2018-10-23 01:05:09 +03:00
}
function onButtonClick() {
2020-05-04 12:44:12 +02:00
onScheduledTaskMessageConfirmed(this.getAttribute('data-taskid'));
2018-10-23 01:05:09 +03:00
}
function onScheduledTasksUpdate(e, apiClient, info) {
2019-10-08 01:03:49 +03:00
if (apiClient.serverId() === serverId) {
updateTasks(info);
}
2018-10-23 01:05:09 +03:00
}
2019-10-08 20:14:43 +03:00
var pollInterval;
var button = options.button;
var serverId = ApiClient.serverId();
2018-10-23 01:05:09 +03:00
function onPollIntervalFired() {
2019-10-08 01:03:49 +03:00
if (!connectionManager.getApiClient(serverId).isMessageChannelOpen()) {
pollTasks();
}
}
2019-10-08 20:14:43 +03:00
function startInterval() {
var apiClient = connectionManager.getApiClient(serverId);
if (pollInterval) {
clearInterval(pollInterval);
}
2020-05-04 12:44:12 +02:00
apiClient.sendMessage('ScheduledTasksInfoStart', '1000,1000');
2019-10-08 20:14:43 +03:00
pollInterval = setInterval(onPollIntervalFired, 5000);
}
function stopInterval() {
2020-05-04 12:44:12 +02:00
connectionManager.getApiClient(serverId).sendMessage('ScheduledTasksInfoStop');
2019-10-08 20:14:43 +03:00
if (pollInterval) {
clearInterval(pollInterval);
}
}
2019-10-08 01:03:49 +03:00
if (options.panel) {
2020-05-04 12:44:12 +02:00
options.panel.classList.add('hide');
2019-10-08 01:03:49 +03:00
}
2019-10-08 19:54:02 +03:00
if (options.mode == 'off') {
2020-05-04 12:44:12 +02:00
button.removeEventListener('click', onButtonClick);
events.off(serverNotifications, 'ScheduledTasksInfo', onScheduledTasksUpdate);
2019-10-08 20:14:43 +03:00
stopInterval();
2019-10-08 01:03:49 +03:00
} else {
2020-05-04 12:44:12 +02:00
button.addEventListener('click', onButtonClick);
2019-10-08 01:03:49 +03:00
pollTasks();
2019-10-08 20:14:43 +03:00
startInterval();
2020-05-04 12:44:12 +02:00
events.on(serverNotifications, 'ScheduledTasksInfo', onScheduledTasksUpdate);
2019-10-08 01:03:49 +03:00
}
};
});