jellyfin-web/src/scripts/taskbutton.js

104 lines
4.0 KiB
JavaScript
Raw Normal View History

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