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

301 lines
9.9 KiB
JavaScript
Raw Normal View History

define(['jQuery', 'humanedate', 'listViewStyle'], function ($) {
2016-10-22 22:11:46 -07:00
'use strict';
2013-02-20 18:33:05 -07:00
2014-05-10 10:28:03 -07:00
function reloadList(page) {
2013-02-20 18:33:05 -07:00
2015-12-14 08:43:03 -07:00
ApiClient.getScheduledTasks({ isHidden: false }).then(function (tasks) {
2014-05-10 10:28:03 -07:00
populateList(page, tasks);
2013-04-16 19:56:16 -07:00
2013-02-20 18:33:05 -07:00
Dashboard.hideLoadingMsg();
});
}
2013-02-20 18:33:05 -07:00
function populateList(page, tasks) {
2013-02-20 18:33:05 -07:00
tasks = tasks.sort(function (a, b) {
a = a.Category + " " + a.Name;
b = b.Category + " " + b.Name;
if (a == b) {
return 0;
}
if (a < b) {
return -1;
}
return 1;
});
var html = "";
var currentCategory;
for (var i = 0, length = tasks.length; i < length; i++) {
var task = tasks[i];
if (task.Category != currentCategory) {
currentCategory = task.Category;
2015-10-12 12:09:56 -07:00
if (currentCategory) {
html += '</div>';
html += '</div>';
}
html += '<div style="margin-bottom:2em;">';
html += '<h1>';
html += currentCategory;
html += '</h1>';
html += '<div class="paperList">';
2013-02-20 18:33:05 -07:00
}
2016-08-05 12:34:10 -07:00
html += '<div class="listItem scheduledTaskPaperIconItem" data-status="' + task.State + '">';
2015-10-12 12:09:56 -07:00
2016-08-05 12:34:10 -07:00
html += "<a item-icon class='clearLink listItemIconContainer' href='scheduledtask.html?id=" + task.Id + "'>";
html += '<i class="md-icon listItemIcon">schedule</i>';
2015-10-12 12:09:56 -07:00
html += "</a>";
2013-02-20 18:33:05 -07:00
2016-08-05 12:34:10 -07:00
html += '<div class="listItemBody two-line">';
2015-10-12 12:09:56 -07:00
html += "<a class='clearLink' href='scheduledtask.html?id=" + task.Id + "'>";
2013-02-20 18:33:05 -07:00
2016-08-05 12:34:10 -07:00
html += "<h3 class='listItemBodyText'>" + task.Name + "</h3>";
2015-10-12 12:09:56 -07:00
//html += "<div secondary>" + task.Description + "</div>";
2013-02-20 18:33:05 -07:00
2016-08-05 12:34:10 -07:00
html += "<div class='secondary listItemBodyText' id='taskProgress" + task.Id + "'>" + getTaskProgressHtml(task) + "</div>";
2015-10-12 12:09:56 -07:00
html += "</a>";
2016-08-05 12:34:10 -07:00
html += '</div>';
2013-02-20 18:33:05 -07:00
if (task.State == "Idle") {
2016-08-05 12:34:10 -07:00
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>';
}
else if (task.State == "Running") {
2013-02-20 18:33:05 -07:00
2016-08-05 12:34:10 -07:00
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>';
2013-02-20 18:33:05 -07:00
} else {
2013-02-20 18:33:05 -07:00
2016-08-05 12:34:10 -07:00
html += '<button type="button" is="paper-icon-button-light" id="btnTask' + task.Id + '" class="btnStartTask hide" data-taskid="' + task.Id + '" title="' + Globalize.translate('ButtonStart') + '"><i class="md-icon">play_arrow</i></button>';
}
2016-08-05 12:34:10 -07:00
html += '</div>';
}
2013-02-20 18:33:05 -07:00
2015-10-12 12:09:56 -07:00
if (tasks.length) {
html += '</div>';
html += '</div>';
}
2015-10-12 12:09:56 -07:00
var divScheduledTasks = page.querySelector('.divScheduledTasks');
divScheduledTasks.innerHTML = html;
}
2015-12-14 08:43:03 -07:00
function humane_elapsed(firstDateStr, secondDateStr) {
var dt1 = new Date(firstDateStr);
var dt2 = new Date(secondDateStr);
var seconds = (dt2.getTime() - dt1.getTime()) / 1000;
var numdays = Math.floor((seconds % 31536000) / 86400);
var numhours = Math.floor(((seconds % 31536000) % 86400) / 3600);
var numminutes = Math.floor((((seconds % 31536000) % 86400) % 3600) / 60);
var numseconds = Math.round((((seconds % 31536000) % 86400) % 3600) % 60);
var elapsedStr = '';
elapsedStr += numdays == 1 ? numdays + ' day ' : '';
elapsedStr += numdays > 1 ? numdays + ' days ' : '';
elapsedStr += numhours == 1 ? numhours + ' hour ' : '';
elapsedStr += numhours > 1 ? numhours + ' hours ' : '';
elapsedStr += numminutes == 1 ? numminutes + ' minute ' : '';
elapsedStr += numminutes > 1 ? numminutes + ' minutes ' : '';
elapsedStr += elapsedStr.length > 0 ? 'and ' : '';
elapsedStr += numseconds == 1 ? numseconds + ' second' : '';
elapsedStr += numseconds == 0 || numseconds > 1 ? numseconds + ' seconds' : '';
return elapsedStr;
}
function getTaskProgressHtml(task) {
var html = '';
if (task.State == "Idle") {
if (task.LastExecutionResult) {
2014-05-30 12:23:56 -07:00
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") {
2016-04-09 13:20:19 -07:00
html += " <span style='color:#FF0000;'>(" + Globalize.translate('LabelFailed') + ")</span>";
}
else if (task.LastExecutionResult.Status == "Cancelled") {
2016-04-09 13:20:19 -07:00
html += " <span style='color:#0026FF;'>(" + Globalize.translate('LabelCancelled') + ")</span>";
}
else if (task.LastExecutionResult.Status == "Aborted") {
2014-05-30 12:23:56 -07:00
html += " <span style='color:#FF0000;'>" + Globalize.translate('LabelAbortedByServerShutdown') + "</span>";
}
2013-02-20 18:33:05 -07:00
}
}
else if (task.State == "Running") {
var progress = (task.CurrentProgressPercentage || 0).toFixed(1);
2016-08-21 10:58:33 -07: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:#009F00;margin-left:5px;'>" + progress + "%</span>";
2016-08-21 10:58:33 -07:00
html += '</div>';
2013-02-20 18:33:05 -07:00
} else {
2014-05-30 12:23:56 -07:00
html += "<span style='color:#FF0000;'>" + Globalize.translate('LabelStopping') + "</span>";
}
return html;
}
function onWebSocketMessage(e, msg) {
if (msg.MessageType == "ScheduledTasksInfo") {
var tasks = msg.Data;
2015-10-12 12:09:56 -07:00
var page = $($.mobile.activePage)[0];
2013-04-16 19:56:16 -07:00
updateTasks(page, tasks);
}
}
2013-04-16 19:56:16 -07:00
function updateTasks(page, tasks) {
for (var i = 0, length = tasks.length; i < length; i++) {
2013-04-16 19:56:16 -07:00
var task = tasks[i];
2015-10-12 12:09:56 -07:00
page.querySelector('#taskProgress' + task.Id).innerHTML = getTaskProgressHtml(task);
2013-04-16 19:56:16 -07:00
2015-10-12 12:09:56 -07:00
var btnTask = page.querySelector('#btnTask' + task.Id);
2013-04-16 19:56:16 -07:00
updateTaskButton(btnTask, task.State);
}
}
2013-04-16 19:56:16 -07:00
2015-10-12 12:09:56 -07:00
function updateTaskButton(elem, state) {
2013-04-16 19:56:16 -07:00
if (state == "Idle") {
2013-02-20 18:33:05 -07:00
2015-10-12 12:09:56 -07:00
elem.classList.add('btnStartTask');
elem.classList.remove('btnStopTask');
elem.classList.remove('hide');
2016-08-05 12:34:10 -07:00
elem.querySelector('i').innerHTML = 'play_arrow';
2015-10-12 12:09:56 -07:00
elem.title = Globalize.translate('ButtonStart');
}
else if (state == "Running") {
2013-02-20 18:33:05 -07:00
2015-10-12 12:09:56 -07:00
elem.classList.remove('btnStartTask');
elem.classList.add('btnStopTask');
elem.classList.remove('hide');
2016-08-05 12:34:10 -07:00
elem.querySelector('i').innerHTML = 'stop';
2015-10-12 12:09:56 -07:00
elem.title = Globalize.translate('ButtonStop');
2013-02-20 18:33:05 -07:00
} else {
2013-02-20 18:33:05 -07:00
2015-10-12 12:09:56 -07:00
elem.classList.add('btnStartTask');
elem.classList.remove('btnStopTask');
elem.classList.add('hide');
2016-08-05 12:34:10 -07:00
elem.querySelector('i').innerHTML = 'play_arrow';
2015-10-12 12:09:56 -07:00
elem.title = Globalize.translate('ButtonStart');
}
2015-10-12 12:09:56 -07:00
2016-08-05 12:34:10 -07:00
var item = $(elem).parents('.listItem')[0];
2015-10-12 12:09:56 -07:00
item.setAttribute('data-status', state);
}
2013-02-20 18:33:05 -07:00
2014-05-10 10:28:03 -07:00
function onWebSocketConnectionOpen() {
2014-05-30 12:23:56 -07:00
2015-10-12 12:09:56 -07:00
var page = $($.mobile.activePage)[0];
2014-05-10 10:28:03 -07:00
startInterval();
2015-10-12 12:09:56 -07:00
reloadList(page);
}
2013-02-20 18:33:05 -07:00
2015-07-14 09:39:34 -07:00
var pollInterval;
function onPollIntervalFired() {
2015-10-12 12:09:56 -07:00
var page = $($.mobile.activePage)[0];
2015-07-14 09:39:34 -07:00
if (!ApiClient.isWebSocketOpen()) {
2015-10-12 12:09:56 -07:00
reloadList(page);
2015-07-14 09:39:34 -07:00
}
}
function startInterval() {
if (ApiClient.isWebSocketOpen()) {
2014-05-10 10:28:03 -07:00
ApiClient.sendWebSocketMessage("ScheduledTasksInfoStart", "1000,1000");
2013-02-20 18:33:05 -07:00
}
2015-07-14 09:39:34 -07:00
if (pollInterval) {
clearInterval(pollInterval);
}
2015-11-12 11:05:51 -07:00
pollInterval = setInterval(onPollIntervalFired, 5000);
}
2013-02-20 18:33:05 -07:00
function stopInterval() {
if (ApiClient.isWebSocketOpen()) {
ApiClient.sendWebSocketMessage("ScheduledTasksInfoStop");
}
2015-07-14 09:39:34 -07:00
if (pollInterval) {
clearInterval(pollInterval);
}
}
2015-09-01 07:01:59 -07:00
$(document).on('pageinit', "#scheduledTasksPage", function () {
2013-02-20 18:33:05 -07:00
var page = this;
2013-02-20 18:33:05 -07:00
2015-06-23 15:13:06 -07:00
$('.divScheduledTasks', page).on('click', '.btnStartTask', function () {
var button = this;
var id = button.getAttribute('data-taskid');
2015-12-14 08:43:03 -07:00
ApiClient.startScheduledTask(id).then(function () {
2013-02-20 18:33:05 -07:00
2015-10-12 12:09:56 -07:00
updateTaskButton(button, "Running");
reloadList(page);
});
2013-02-20 18:33:05 -07:00
}).on('click', '.btnStopTask', function () {
2013-02-20 18:33:05 -07:00
var button = this;
var id = button.getAttribute('data-taskid');
2015-12-14 08:43:03 -07:00
ApiClient.stopScheduledTask(id).then(function () {
2013-02-20 18:33:05 -07:00
2015-10-12 12:09:56 -07:00
updateTaskButton(button, "");
reloadList(page);
});
2013-02-20 18:33:05 -07:00
});
2015-09-24 10:08:10 -07:00
}).on('pageshow', "#scheduledTasksPage", function () {
2015-06-07 18:23:56 -07:00
var page = this;
Dashboard.showLoadingMsg();
startInterval();
2015-12-14 08:43:03 -07:00
2016-08-05 12:34:10 -07:00
reloadList(page);
2015-06-07 18:23:56 -07:00
2015-12-23 10:46:01 -07:00
Events.on(ApiClient, "websocketmessage", onWebSocketMessage);
Events.on(ApiClient, "websocketopen", onWebSocketConnectionOpen);
2015-06-07 18:23:56 -07:00
2015-06-20 17:49:42 -07:00
}).on('pagebeforehide', "#scheduledTasksPage", function () {
var page = this;
2015-12-23 10:46:01 -07:00
Events.off(ApiClient, "websocketmessage", onWebSocketMessage);
Events.off(ApiClient, "websocketopen", onWebSocketConnectionOpen);
stopInterval();
});
});