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

270 lines
8.3 KiB
JavaScript
Raw Normal View History

(function ($, document, window) {
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
2014-05-30 12:23:56 -07:00
ApiClient.getScheduledTasks({ isHidden: false }).done(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
}
2015-10-12 12:09:56 -07:00
html += '<paper-icon-item class="scheduledTaskPaperIconItem" data-status="' + task.State + '">';
html += "<a item-icon class='clearLink' href='scheduledtask.html?id=" + task.Id + "'>";
2015-10-26 11:55:46 -07:00
html += '<paper-fab mini icon="schedule"></paper-fab>';
2015-10-12 12:09:56 -07:00
html += "</a>";
2013-02-20 18:33:05 -07:00
2015-10-12 12:09:56 -07:00
html += '<paper-item-body two-line>';
html += "<a class='clearLink' href='scheduledtask.html?id=" + task.Id + "'>";
2013-02-20 18:33:05 -07:00
2015-10-12 12:09:56 -07:00
html += "<div>" + task.Name + "</div>";
//html += "<div secondary>" + task.Description + "</div>";
2013-02-20 18:33:05 -07:00
2015-10-12 12:09:56 -07:00
html += "<div secondary id='taskProgress" + task.Id + "'>" + getTaskProgressHtml(task) + "</div>";
html += "</a>";
html += '</paper-item-body>';
2013-02-20 18:33:05 -07:00
if (task.State == "Idle") {
2015-10-12 12:09:56 -07:00
html += '<paper-icon-button id="btnTask' + task.Id + '" icon="play-arrow" class="btnStartTask" data-taskid="' + task.Id + '" title="' + Globalize.translate('ButtonStart') + '"></paper-icon-button>';
}
else if (task.State == "Running") {
2013-02-20 18:33:05 -07:00
2015-10-12 12:09:56 -07:00
html += '<paper-icon-button id="btnTask' + task.Id + '" icon="stop" class="btnStopTask" data-taskid="' + task.Id + '" title="' + Globalize.translate('ButtonStop') + '"></paper-icon-button>';
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
html += '<paper-icon-button id="btnTask' + task.Id + '" icon="play-arrow" class="btnStartTask hide" data-taskid="' + task.Id + '" title="' + Globalize.translate('ButtonStart') + '"></paper-icon-button>';
}
2015-10-12 12:09:56 -07:00
html += '</paper-icon-item>';
}
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;
}
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") {
2014-05-30 12:23:56 -07:00
html += " <span style='color:#FF0000;'>" + Globalize.translate('LabelFailed') + "</span>";
}
else if (task.LastExecutionResult.Status == "Cancelled") {
2014-05-30 12:23:56 -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);
2015-10-12 12:09:56 -07:00
html += '<paper-progress max="100" value="' + progress + '" title="' + progress + '%">';
html += '' + progress + '%';
2015-10-12 12:09:56 -07:00
html += '</paper-progress>';
html += "<span style='color:#009F00;margin-left:5px;'>" + progress + "%</span>";
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');
elem.icon = 'play-arrow';
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');
elem.icon = 'stop';
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');
elem.icon = 'play-arrow';
elem.title = Globalize.translate('ButtonStart');
}
2015-10-12 12:09:56 -07:00
var item = $(elem).parents('paper-icon-item')[0];
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');
ApiClient.startScheduledTask(id).done(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');
ApiClient.stopScheduledTask(id).done(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();
reloadList(page);
$(ApiClient).on("websocketmessage", onWebSocketMessage).on("websocketopen", onWebSocketConnectionOpen);
2015-06-20 17:49:42 -07:00
}).on('pagebeforehide', "#scheduledTasksPage", function () {
var page = this;
2014-05-10 10:28:03 -07:00
$(ApiClient).off("websocketmessage", onWebSocketMessage).off("websocketopen", onWebSocketConnectionOpen);
stopInterval();
});
})(jQuery, document, window);