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

taskbutton.js

This commit is contained in:
grafixeyehero 2019-10-08 01:03:49 +03:00
parent 5bed7bbf35
commit fe08645a98

View file

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