2020-05-04 12:44:12 +02:00
|
|
|
define(['jQuery', 'loading', 'events', 'globalize', 'serverNotifications', 'date-fns', 'dfnshelper', 'listViewStyle', 'emby-button'], function ($, loading, events, globalize, serverNotifications, datefns, dfnshelper) {
|
|
|
|
'use strict';
|
2018-10-23 01:05:09 +03:00
|
|
|
|
|
|
|
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();
|
2020-04-05 13:48:10 +02:00
|
|
|
});
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function populateList(page, tasks) {
|
|
|
|
tasks = tasks.sort(function(a, b) {
|
2020-05-04 12:44:12 +02:00
|
|
|
a = a.Category + ' ' + a.Name;
|
|
|
|
b = b.Category + ' ' + b.Name;
|
2019-04-22 09:15:28 -07:00
|
|
|
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;
|
2020-05-04 12:44:12 +02:00
|
|
|
var html = '';
|
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];
|
2019-04-22 09:15:28 -07:00
|
|
|
if (task.Category != currentCategory) {
|
|
|
|
currentCategory = task.Category;
|
|
|
|
if (currentCategory) {
|
2020-05-04 12:44:12 +02:00
|
|
|
html += '</div>';
|
|
|
|
html += '</div>';
|
2019-04-22 09:15:28 -07:00
|
|
|
}
|
|
|
|
html += '<div class="verticalSection verticalSection-extrabottompadding">';
|
|
|
|
html += '<div class="sectionTitleContainer" style="margin-bottom:1em;">';
|
|
|
|
html += '<h2 class="sectionTitle">';
|
|
|
|
html += currentCategory;
|
2020-05-04 12:44:12 +02:00
|
|
|
html += '</h2>';
|
2019-04-22 09:15:28 -07:00
|
|
|
if (i === 0) {
|
2020-05-04 12:44:12 +02:00
|
|
|
html += '<a is="emby-linkbutton" class="raised button-alt headerHelpButton" target="_blank" href="https://docs.jellyfin.org/general/server/tasks.html">' + globalize.translate('Help') + '</a>';
|
2019-04-22 09:15:28 -07:00
|
|
|
}
|
2020-05-04 12:44:12 +02:00
|
|
|
html += '</div>';
|
2019-04-22 09:15:28 -07:00
|
|
|
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 + "'>";
|
2020-04-26 02:37:28 +03:00
|
|
|
html += '<span class="material-icons listItemIcon schedule"></span>';
|
2020-05-04 12:44:12 +02:00
|
|
|
html += '</a>';
|
2019-04-22 09:15:28 -07:00
|
|
|
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 + "'>";
|
2020-05-04 12:44:12 +02:00
|
|
|
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') + '"><span class="material-icons stop"></span></button>';
|
|
|
|
} 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') + '"><span class="material-icons play_arrow"></span></button>';
|
2019-04-22 09:15:28 -07:00
|
|
|
}
|
2020-05-04 12:44:12 +02:00
|
|
|
html += '</div>';
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-04-22 09:15:28 -07:00
|
|
|
if (tasks.length) {
|
2020-05-04 12:44:12 +02:00
|
|
|
html += '</div>';
|
|
|
|
html += '</div>';
|
2019-04-22 09:15:28 -07:00
|
|
|
}
|
2020-05-04 12:44:12 +02:00
|
|
|
page.querySelector('.divScheduledTasks').innerHTML = html;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function getTaskProgressHtml(task) {
|
2020-05-04 12:44:12 +02:00
|
|
|
var html = '';
|
|
|
|
if (task.State === 'Idle') {
|
2019-04-22 09:15:28 -07:00
|
|
|
if (task.LastExecutionResult) {
|
2020-04-03 20:11:09 +02:00
|
|
|
var endtime = Date.parse(task.LastExecutionResult.EndTimeUtc);
|
|
|
|
var starttime = Date.parse(task.LastExecutionResult.StartTimeUtc);
|
2020-05-04 12:44:12 +02:00
|
|
|
html += globalize.translate('LabelScheduledTaskLastRan', datefns.formatDistanceToNow(endtime, dfnshelper.localeWithSuffix),
|
2020-04-09 00:25:10 +09:00
|
|
|
datefns.formatDistance(starttime, endtime, { locale: dfnshelper.getLocale() }));
|
2020-05-04 12:44:12 +02:00
|
|
|
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>';
|
2019-04-22 09:15:28 -07:00
|
|
|
}
|
|
|
|
}
|
2020-05-04 12:44:12 +02:00
|
|
|
} 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 + '%;">';
|
2020-05-04 12:44:12 +02:00
|
|
|
html += '</div>';
|
|
|
|
html += '</div>';
|
|
|
|
html += "<span style='color:#00a4dc;margin-left:5px;'>" + progress + '%</span>';
|
|
|
|
html += '</div>';
|
2019-01-21 17:55:19 +01:00
|
|
|
} else {
|
2020-05-04 12:44:12 +02:00
|
|
|
html += "<span style='color:#FF0000;'>" + globalize.translate('LabelStopping') + '</span>';
|
2019-01-21 17:55:19 +01:00
|
|
|
}
|
2019-04-22 09:15:28 -07:00
|
|
|
return html;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2020-03-10 12:32:14 +03:00
|
|
|
function setTaskButtonIcon(button, icon) {
|
2020-05-04 12:44:12 +02:00
|
|
|
var inner = button.querySelector('.material-icons');
|
|
|
|
inner.classList.remove('stop', 'play_arrow');
|
2020-03-10 12:32:14 +03:00
|
|
|
inner.classList.add(icon);
|
|
|
|
}
|
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
function updateTaskButton(elem, state) {
|
2020-05-04 12:44:12 +02:00
|
|
|
if (state === 'Running') {
|
|
|
|
elem.classList.remove('btnStartTask');
|
|
|
|
elem.classList.add('btnStopTask');
|
|
|
|
setTaskButtonIcon(elem, 'stop');
|
|
|
|
elem.title = globalize.translate('ButtonStop');
|
|
|
|
} else if (state === 'Idle') {
|
|
|
|
elem.classList.add('btnStartTask');
|
|
|
|
elem.classList.remove('btnStopTask');
|
|
|
|
setTaskButtonIcon(elem, 'play_arrow');
|
|
|
|
elem.title = globalize.translate('ButtonStart');
|
2019-04-22 09:15:28 -07:00
|
|
|
}
|
2020-05-04 12:44:12 +02:00
|
|
|
$(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];
|
2020-05-04 12:44:12 +02:00
|
|
|
view.querySelector('#taskProgress' + task.Id).innerHTML = getTaskProgressHtml(task);
|
|
|
|
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() {
|
2020-05-04 12:44:12 +02:00
|
|
|
ApiClient.sendMessage('ScheduledTasksInfoStart', '1000,1000');
|
2019-04-22 09:15:28 -07:00
|
|
|
pollInterval && clearInterval(pollInterval);
|
|
|
|
pollInterval = setInterval(onPollIntervalFired, 1e4);
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function stopInterval() {
|
2020-05-04 12:44:12 +02:00
|
|
|
ApiClient.sendMessage('ScheduledTasksInfoStop');
|
2019-04-22 09:15:28 -07:00
|
|
|
pollInterval && clearInterval(pollInterval);
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-04-22 09:15:28 -07:00
|
|
|
|
2019-11-23 00:29:38 +09:00
|
|
|
var pollInterval;
|
|
|
|
var serverId = ApiClient.serverId();
|
2019-04-22 09:15:28 -07:00
|
|
|
|
2020-05-04 12:44:12 +02:00
|
|
|
$('.divScheduledTasks', view).on('click', '.btnStartTask', function() {
|
2019-04-22 09:15:28 -07:00
|
|
|
var button = this;
|
2020-05-04 12:44:12 +02:00
|
|
|
var id = button.getAttribute('data-taskid');
|
2018-10-23 01:05:09 +03:00
|
|
|
ApiClient.startScheduledTask(id).then(function() {
|
2020-05-04 12:44:12 +02:00
|
|
|
updateTaskButton(button, 'Running');
|
2019-04-22 09:15:28 -07:00
|
|
|
reloadList(view);
|
2020-04-05 13:48:10 +02:00
|
|
|
});
|
2019-04-22 09:15:28 -07:00
|
|
|
});
|
|
|
|
|
2020-05-04 12:44:12 +02:00
|
|
|
$('.divScheduledTasks', view).on('click', '.btnStopTask', function() {
|
2019-04-22 09:15:28 -07:00
|
|
|
var button = this;
|
2020-05-04 12:44:12 +02:00
|
|
|
var id = button.getAttribute('data-taskid');
|
2018-10-23 01:05:09 +03:00
|
|
|
ApiClient.stopScheduledTask(id).then(function() {
|
2020-05-04 12:44:12 +02:00
|
|
|
updateTaskButton(button, '');
|
2019-04-22 09:15:28 -07:00
|
|
|
reloadList(view);
|
2020-04-05 13:48:10 +02:00
|
|
|
});
|
2019-04-22 09:15:28 -07:00
|
|
|
});
|
|
|
|
|
2020-05-04 12:44:12 +02:00
|
|
|
view.addEventListener('viewbeforehide', function() {
|
|
|
|
events.off(serverNotifications, 'ScheduledTasksInfo', onScheduledTasksUpdate);
|
2019-04-22 09:15:28 -07:00
|
|
|
stopInterval();
|
|
|
|
});
|
|
|
|
|
2020-05-04 12:44:12 +02:00
|
|
|
view.addEventListener('viewshow', function() {
|
2019-04-22 09:15:28 -07:00
|
|
|
loading.show();
|
|
|
|
startInterval();
|
|
|
|
reloadList(view);
|
2020-05-04 12:44:12 +02:00
|
|
|
events.on(serverNotifications, 'ScheduledTasksInfo', onScheduledTasksUpdate);
|
2019-04-22 09:15:28 -07:00
|
|
|
});
|
2020-04-05 13:48:10 +02:00
|
|
|
};
|
2019-01-21 17:55:19 +01:00
|
|
|
});
|