import 'jquery'; import loading from '../../../components/loading/loading'; import globalize from '../../../lib/globalize'; import serverNotifications from '../../../scripts/serverNotifications'; import { formatDistance, formatDistanceToNow } from 'date-fns'; import { getLocale, getLocaleWithSuffix } from '../../../utils/dateFnsLocale.ts'; import Events from '../../../utils/events.ts'; import '../../../components/listview/listview.scss'; import '../../../elements/emby-button/emby-button'; import dom from 'scripts/dom'; function reloadList(page) { ApiClient.getScheduledTasks({ isHidden: false }).then(function(tasks) { populateList(page, tasks); loading.hide(); }); } function populateList(page, tasks) { tasks = tasks.sort(function(a, b) { a = a.Category + ' ' + a.Name; b = b.Category + ' ' + b.Name; if (a > b) { return 1; } else if (a < b) { return -1; } else { return 0; } }); let currentCategory; let html = ''; for (const task of tasks) { if (task.Category != currentCategory) { currentCategory = task.Category; if (currentCategory) { html += ''; html += ''; } html += '
'; html += '
'; html += '

'; html += currentCategory; html += '

'; html += '
'; html += '
'; } html += '
'; html += ""; html += ''; html += ''; html += '
'; const textAlignStyle = globalize.getIsRTL() ? 'right' : 'left'; html += ""; html += "

" + task.Name + '

'; html += "
" + getTaskProgressHtml(task) + '
'; html += '
'; html += '
'; if (task.State === 'Running') { html += ''; } else if (task.State === 'Idle') { html += ''; } html += '
'; } if (tasks.length) { html += '
'; html += '
'; } page.querySelector('.divScheduledTasks').innerHTML = html; } function getTaskProgressHtml(task) { let html = ''; if (task.State === 'Idle') { if (task.LastExecutionResult) { const endtime = Date.parse(task.LastExecutionResult.EndTimeUtc); const starttime = Date.parse(task.LastExecutionResult.StartTimeUtc); html += globalize.translate('LabelScheduledTaskLastRan', formatDistanceToNow(endtime, getLocaleWithSuffix()), formatDistance(starttime, endtime, { locale: getLocale() })); if (task.LastExecutionResult.Status === 'Failed') { html += " (" + globalize.translate('LabelFailed') + ')'; } else if (task.LastExecutionResult.Status === 'Cancelled') { html += " (" + globalize.translate('LabelCancelled') + ')'; } else if (task.LastExecutionResult.Status === 'Aborted') { html += " " + globalize.translate('LabelAbortedByServerShutdown') + ''; } } } else if (task.State === 'Running') { const progress = (task.CurrentProgressPercentage || 0).toFixed(1); html += '
'; html += '
'; html += '
'; html += '
'; html += '
'; html += "" + progress + '%'; html += '
'; } else { html += "" + globalize.translate('LabelStopping') + ''; } return html; } function setTaskButtonIcon(button, icon) { const inner = button.querySelector('.material-icons'); inner.classList.remove('stop', 'play_arrow'); inner.classList.add(icon); } function updateTaskButton(elem, state) { 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'); } dom.parentWithClass(elem, 'listItem').setAttribute('data-status', state); } export default function(view) { function updateTasks(tasks) { for (const task of tasks) { const taskProgress = view.querySelector(`#taskProgress${task.Id}`); if (taskProgress) taskProgress.innerHTML = getTaskProgressHtml(task); const taskButton = view.querySelector(`#btnTask${task.Id}`); if (taskButton) updateTaskButton(taskButton, task.State); } } function onPollIntervalFired() { if (!ApiClient.isMessageChannelOpen()) { reloadList(view); } } function onScheduledTasksUpdate(e, apiClient, info) { if (apiClient.serverId() === serverId) { updateTasks(info); } } function startInterval() { ApiClient.sendMessage('ScheduledTasksInfoStart', '1000,1000'); pollInterval && clearInterval(pollInterval); pollInterval = setInterval(onPollIntervalFired, 1e4); } function stopInterval() { ApiClient.sendMessage('ScheduledTasksInfoStop'); pollInterval && clearInterval(pollInterval); } let pollInterval; const serverId = ApiClient.serverId(); $('.divScheduledTasks', view).on('click', '.btnStartTask', function() { const button = this; const id = button.getAttribute('data-taskid'); ApiClient.startScheduledTask(id).then(function() { updateTaskButton(button, 'Running'); reloadList(view); }); }); $('.divScheduledTasks', view).on('click', '.btnStopTask', function() { const button = this; const id = button.getAttribute('data-taskid'); ApiClient.stopScheduledTask(id).then(function() { updateTaskButton(button, ''); reloadList(view); }); }); view.addEventListener('viewbeforehide', function() { Events.off(serverNotifications, 'ScheduledTasksInfo', onScheduledTasksUpdate); stopInterval(); }); view.addEventListener('viewshow', function() { loading.show(); startInterval(); reloadList(view); Events.on(serverNotifications, 'ScheduledTasksInfo', onScheduledTasksUpdate); }); }