jellyfin-web/dashboard-ui/scripts/taskbutton.js

149 lines
4.2 KiB
JavaScript
Raw Normal View History

2016-10-01 00:06:00 -07:00
define(['userSettings', 'emby-button'], function (userSettings) {
2016-10-22 22:11:46 -07:00
'use strict';
2015-01-20 14:32:48 -07:00
2016-08-17 13:32:39 -07:00
return function (options) {
2015-01-20 14:32:48 -07:00
2016-08-17 13:32:39 -07:00
var button = options.button;
function pollTasks() {
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
ApiClient.getScheduledTasks({
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
IsEnabled: true
2015-01-20 14:32:48 -07:00
2016-08-17 13:32:39 -07:00
}).then(updateTasks);
2016-03-07 12:13:58 -07:00
}
2015-01-20 14:32:48 -07:00
2016-08-17 13:32:39 -07:00
function updateTasks(tasks) {
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
var task = tasks.filter(function (t) {
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
return t.Key == options.taskKey;
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
})[0];
if (options.panel) {
if (task) {
2016-06-07 22:24:25 -07:00
options.panel.classList.remove('hide');
2016-03-07 12:13:58 -07:00
} else {
2016-06-07 22:24:25 -07:00
options.panel.classList.add('hide');
2016-03-07 12:13:58 -07:00
}
2015-01-20 14:32:48 -07:00
}
2016-03-07 12:13:58 -07:00
if (!task) {
return;
}
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
if (task.State == 'Idle') {
2016-08-17 13:32:39 -07:00
button.removeAttribute('disabled');
2016-03-07 12:13:58 -07:00
} else {
2016-08-17 13:32:39 -07:00
button.setAttribute('disabled', 'disabled');
2016-03-07 12:13:58 -07:00
}
2015-12-14 08:43:03 -07:00
2016-08-17 13:32:39 -07:00
button.setAttribute('data-taskid', task.Id);
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
var progress = (task.CurrentProgressPercentage || 0).toFixed(1);
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
if (options.progressElem) {
options.progressElem.value = progress;
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
if (task.State == 'Running') {
options.progressElem.classList.remove('hide');
} else {
options.progressElem.classList.add('hide');
}
2015-01-20 14:32:48 -07:00
}
2016-03-07 12:13:58 -07:00
if (options.lastResultElem) {
var lastResult = task.LastExecutionResult ? task.LastExecutionResult.Status : '';
if (lastResult == "Failed") {
2016-04-09 13:20:19 -07:00
options.lastResultElem.html('<span style="color:#FF0000;">(' + Globalize.translate('LabelFailed') + ')</span>');
2016-03-07 12:13:58 -07:00
}
else if (lastResult == "Cancelled") {
2016-04-09 13:20:19 -07:00
options.lastResultElem.html('<span style="color:#0026FF;">(' + Globalize.translate('LabelCancelled') + ')</span>');
2016-03-07 12:13:58 -07:00
}
else if (lastResult == "Aborted") {
options.lastResultElem.html('<span style="color:#FF0000;">' + Globalize.translate('LabelAbortedByServerShutdown') + '</span>');
} else {
options.lastResultElem.html(lastResult);
}
2015-01-20 14:32:48 -07:00
}
}
2016-08-17 13:32:39 -07:00
function onScheduledTaskMessageConfirmed(id) {
ApiClient.startScheduledTask(id).then(pollTasks);
2016-03-07 12:13:58 -07:00
}
2015-01-22 09:41:34 -07:00
2016-03-07 12:13:58 -07:00
function onButtonClick() {
2015-01-22 09:41:34 -07:00
2016-03-07 12:13:58 -07:00
var button = this;
2016-10-01 00:06:00 -07:00
var taskId = button.getAttribute('data-taskid');
2016-10-14 09:22:04 -07:00
onScheduledTaskMessageConfirmed(taskId);
2015-01-22 09:41:34 -07:00
}
2016-03-07 12:13:58 -07:00
function onSocketOpen() {
startInterval();
}
2015-06-29 11:45:42 -07:00
2016-03-07 12:13:58 -07:00
function onSocketMessage(e, msg) {
if (msg.MessageType == "ScheduledTasksInfo") {
2015-06-29 11:45:42 -07:00
2016-03-07 12:13:58 -07:00
var tasks = msg.Data;
2015-06-29 11:45:42 -07:00
2016-08-17 13:32:39 -07:00
updateTasks(tasks);
2016-03-07 12:13:58 -07:00
}
2015-06-29 11:45:42 -07:00
}
2016-03-07 12:13:58 -07:00
var pollInterval;
2015-07-14 09:39:34 -07:00
2016-03-07 12:13:58 -07:00
function onPollIntervalFired() {
2015-07-14 09:39:34 -07:00
2016-03-07 12:13:58 -07:00
if (!ApiClient.isWebSocketOpen()) {
2016-08-17 13:32:39 -07:00
pollTasks();
2016-03-07 12:13:58 -07:00
}
2015-07-14 09:39:34 -07:00
}
2016-03-07 12:13:58 -07:00
function startInterval() {
if (ApiClient.isWebSocketOpen()) {
ApiClient.sendWebSocketMessage("ScheduledTasksInfoStart", "1000,1000");
}
if (pollInterval) {
clearInterval(pollInterval);
}
pollInterval = setInterval(onPollIntervalFired, 5000);
2015-07-14 09:39:34 -07:00
}
2016-03-07 12:13:58 -07:00
function stopInterval() {
if (ApiClient.isWebSocketOpen()) {
ApiClient.sendWebSocketMessage("ScheduledTasksInfoStop");
}
if (pollInterval) {
clearInterval(pollInterval);
}
2015-07-14 09:39:34 -07:00
}
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
if (options.panel) {
2016-06-07 22:24:25 -07:00
options.panel.classList.add('hide');
2016-03-07 12:13:58 -07:00
}
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
if (options.mode == 'off') {
2015-01-20 14:32:48 -07:00
2016-08-17 13:32:39 -07:00
button.removeEventListener('click', onButtonClick);
2016-03-07 12:13:58 -07:00
Events.off(ApiClient, 'websocketmessage', onSocketMessage);
Events.off(ApiClient, 'websocketopen', onSocketOpen);
stopInterval();
2015-01-20 14:32:48 -07:00
2016-10-01 00:06:00 -07:00
} else {
2015-01-20 14:32:48 -07:00
2016-08-17 13:32:39 -07:00
button.addEventListener('click', onButtonClick);
2015-01-20 14:32:48 -07:00
2016-08-17 13:32:39 -07:00
pollTasks();
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
startInterval();
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
Events.on(ApiClient, 'websocketmessage', onSocketMessage);
Events.on(ApiClient, 'websocketopen', onSocketOpen);
}
};
});