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

185 lines
8.9 KiB
JavaScript
Raw Normal View History

define(["jQuery", "loading", "events", "globalize", "serverNotifications", "humanedate", "listViewStyle", "emby-button"], function($, loading, events, globalize, serverNotifications) {
2018-10-23 01:05:09 +03:00
"use strict";
function reloadList(page) {
ApiClient.getScheduledTasks({
2019-04-22 09:15:28 -07:00
isHidden: false
2018-10-23 01:05:09 +03:00
}).then(function(tasks) {
2019-04-22 09:15:28 -07:00
populateList(page, tasks);
loading.hide();
2018-10-23 01:05:09 +03:00
})
}
function populateList(page, tasks) {
tasks = tasks.sort(function(a, b) {
2019-04-22 09:15:28 -07:00
a = a.Category + " " + a.Name;
b = b.Category + " " + b.Name;
return a == b ? 0 : a < b ? -1 : 1;
2018-10-23 01:05:09 +03:00
});
2019-04-22 09:15:28 -07:00
var currentCategory;
var html = "";
for (var i = 0; i < tasks.length; i++) {
2018-10-23 01:05:09 +03:00
var task = tasks[i];
2019-04-22 09:15:28 -07:00
if (task.Category != currentCategory) {
currentCategory = task.Category;
if (currentCategory) {
html += "</div>";
html += "</div>";
}
html += '<div class="verticalSection verticalSection-extrabottompadding">';
html += '<div class="sectionTitleContainer" style="margin-bottom:1em;">';
html += '<h2 class="sectionTitle">';
html += currentCategory;
html += "</h2>";
if (i === 0) {
html += '<a is="emby-linkbutton" class="raised button-alt headerHelpButton" target="_blank" href="https://jellyfin.readthedocs.io/en/latest/server/tasks/">' + globalize.translate("Help") + "</a>";
2019-04-22 09:15:28 -07:00
}
html += "</div>";
html += '<div class="paperList">';
}
html += '<div class="listItem listItem-border scheduledTaskPaperIconItem" data-status="' + task.State + '">';
html += "<a is='emby-linkbutton' style='margin:0;padding:0;' class='clearLink listItemIconContainer' href='scheduledtask.html?id=" + task.Id + "'>";
html += '<i class="md-icon listItemIcon">schedule</i>';
html += "</a>";
html += '<div class="listItemBody two-line">';
html += "<a class='clearLink' style='margin:0;padding:0;display:block;text-align:left;' is='emby-linkbutton' href='scheduledtask.html?id=" + task.Id + "'>";
html += "<h3 class='listItemBodyText'>" + task.Name + "</h3>";
html += "<div class='secondary listItemBodyText' id='taskProgress" + task.Id + "'>" + getTaskProgressHtml(task) + "</div>";
html += "</a>";
html += "</div>";
if (task.State === "Running") {
html += '<button type="button" is="paper-icon-button-light" id="btnTask' + task.Id + '" class="btnStopTask" data-taskid="' + task.Id + '" title="' + globalize.translate("ButtonStop") + '"><i class="md-icon">stop</i></button>';
2019-05-08 14:14:32 -07:00
} else if (task.State === "Idle") {
html += '<button type="button" is="paper-icon-button-light" id="btnTask' + task.Id + '" class="btnStartTask" data-taskid="' + task.Id + '" title="' + globalize.translate("ButtonStart") + '"><i class="md-icon">play_arrow</i></button>';
2019-04-22 09:15:28 -07:00
}
html += "</div>";
2018-10-23 01:05:09 +03:00
}
2019-04-22 09:15:28 -07:00
if (tasks.length) {
html += "</div>";
html += "</div>";
}
page.querySelector(".divScheduledTasks").innerHTML = html;
2018-10-23 01:05:09 +03:00
}
function humane_elapsed(firstDateStr, secondDateStr) {
var dt1 = new Date(firstDateStr),
dt2 = new Date(secondDateStr),
seconds = (dt2.getTime() - dt1.getTime()) / 1e3,
numdays = Math.floor(seconds % 31536e3 / 86400),
numhours = Math.floor(seconds % 31536e3 % 86400 / 3600),
numminutes = Math.floor(seconds % 31536e3 % 86400 % 3600 / 60),
numseconds = Math.round(seconds % 31536e3 % 86400 % 3600 % 60),
elapsedStr = "";
return elapsedStr += 1 == numdays ? numdays + " day " : "", elapsedStr += numdays > 1 ? numdays + " days " : "", elapsedStr += 1 == numhours ? numhours + " hour " : "", elapsedStr += numhours > 1 ? numhours + " hours " : "", elapsedStr += 1 == numminutes ? numminutes + " minute " : "", elapsedStr += numminutes > 1 ? numminutes + " minutes " : "", elapsedStr += elapsedStr.length > 0 ? "and " : "", elapsedStr += 1 == numseconds ? numseconds + " second" : "", elapsedStr += 0 == numseconds || numseconds > 1 ? numseconds + " seconds" : ""
}
function getTaskProgressHtml(task) {
var html = "";
2019-04-22 09:15:28 -07:00
if (task.State === "Idle") {
if (task.LastExecutionResult) {
html += globalize.translate("LabelScheduledTaskLastRan").replace("{0}", humane_date(task.LastExecutionResult.EndTimeUtc)).replace("{1}", humane_elapsed(task.LastExecutionResult.StartTimeUtc, task.LastExecutionResult.EndTimeUtc));
if (task.LastExecutionResult.Status === "Failed") {
html += " <span style='color:#FF0000;'>(" + globalize.translate("LabelFailed") + ")</span>";
} else if (task.LastExecutionResult.Status === "Cancelled") {
html += " <span style='color:#0026FF;'>(" + globalize.translate("LabelCancelled") + ")</span>";
} else if (task.LastExecutionResult.Status === "Aborted") {
html += " <span style='color:#FF0000;'>" + globalize.translate("LabelAbortedByServerShutdown") + "</span>";
}
}
} else if (task.State === "Running") {
2018-10-23 01:05:09 +03:00
var progress = (task.CurrentProgressPercentage || 0).toFixed(1);
2019-01-21 17:55:19 +01:00
html += '<div style="display:flex;align-items:center;">';
html += '<div class="taskProgressOuter" title="' + progress + '%" style="flex-grow:1;">';
html += '<div class="taskProgressInner" style="width:' + progress + '%;">';
html += "</div>";
html += "</div>";
html += "<span style='color:#00a4dc;margin-left:5px;'>" + progress + "%</span>";
html += "</div>";
} else {
html += "<span style='color:#FF0000;'>" + globalize.translate("LabelStopping") + "</span>";
}
2019-04-22 09:15:28 -07:00
return html;
2018-10-23 01:05:09 +03:00
}
function updateTaskButton(elem, state) {
2019-04-22 09:15:28 -07:00
if (state === "Running") {
elem.classList.remove("btnStartTask");
elem.classList.add("btnStopTask");
elem.querySelector("i").innerHTML = "stop";
elem.title = globalize.translate("ButtonStop");
2019-05-08 14:14:32 -07:00
} else if (state === "Idle") {
2019-04-22 09:15:28 -07:00
elem.classList.add("btnStartTask");
elem.classList.remove("btnStopTask");
elem.querySelector("i").innerHTML = "play_arrow";
elem.title = globalize.translate("ButtonStart");
}
$(elem).parents(".listItem")[0].setAttribute("data-status", state);
2018-10-23 01:05:09 +03:00
}
2019-04-22 09:15:28 -07:00
2018-10-23 01:05:09 +03:00
return function(view, params) {
function updateTasks(tasks) {
2019-04-22 09:15:28 -07:00
for (var i = 0; i < tasks.length; i++) {
2018-10-23 01:05:09 +03:00
var task = tasks[i];
view.querySelector("#taskProgress" + task.Id).innerHTML = getTaskProgressHtml(task);
2019-04-22 09:15:28 -07:00
updateTaskButton(view.querySelector("#btnTask" + task.Id), task.State);
2018-10-23 01:05:09 +03:00
}
}
function onPollIntervalFired() {
2019-04-22 09:15:28 -07:00
if (!ApiClient.isMessageChannelOpen()) {
reloadList(view);
}
2018-10-23 01:05:09 +03:00
}
function onScheduledTasksUpdate(e, apiClient, info) {
2019-04-22 09:15:28 -07:00
if (apiClient.serverId() === serverId) {
updateTasks(info);
}
2018-10-23 01:05:09 +03:00
}
function startInterval() {
2019-04-22 09:15:28 -07:00
ApiClient.sendMessage("ScheduledTasksInfoStart", "1000,1000");
pollInterval && clearInterval(pollInterval);
pollInterval = setInterval(onPollIntervalFired, 1e4);
2018-10-23 01:05:09 +03:00
}
function stopInterval() {
2019-04-22 09:15:28 -07:00
ApiClient.sendMessage("ScheduledTasksInfoStop");
pollInterval && clearInterval(pollInterval);
2018-10-23 01:05:09 +03:00
}
2019-04-22 09:15:28 -07:00
2018-10-23 01:05:09 +03:00
var pollInterval, serverId = ApiClient.serverId();
2019-04-22 09:15:28 -07:00
2018-10-23 01:05:09 +03:00
$(".divScheduledTasks", view).on("click", ".btnStartTask", function() {
2019-04-22 09:15:28 -07:00
var button = this;
var id = button.getAttribute("data-taskid");
2018-10-23 01:05:09 +03:00
ApiClient.startScheduledTask(id).then(function() {
2019-04-22 09:15:28 -07:00
updateTaskButton(button, "Running");
reloadList(view);
2018-10-23 01:05:09 +03:00
})
2019-04-22 09:15:28 -07:00
});
$(".divScheduledTasks", view).on("click", ".btnStopTask", function() {
var button = this;
var id = button.getAttribute("data-taskid");
2018-10-23 01:05:09 +03:00
ApiClient.stopScheduledTask(id).then(function() {
2019-04-22 09:15:28 -07:00
updateTaskButton(button, "");
reloadList(view);
2018-10-23 01:05:09 +03:00
})
2019-04-22 09:15:28 -07:00
});
view.addEventListener("viewbeforehide", function() {
events.off(serverNotifications, "ScheduledTasksInfo", onScheduledTasksUpdate);
stopInterval();
});
view.addEventListener("viewshow", function() {
loading.show();
startInterval();
reloadList(view);
events.on(serverNotifications, "ScheduledTasksInfo", onScheduledTasksUpdate);
});
2018-10-23 01:05:09 +03:00
}
2019-01-21 17:55:19 +01:00
});