1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/dashboard-ui/scripts/taskbutton.js

198 lines
6 KiB
JavaScript
Raw Normal View History

2016-10-01 03:06:00 -04:00
define(['userSettings', 'emby-button'], function (userSettings) {
2015-01-20 16:32:48 -05:00
2016-08-17 16:32:39 -04:00
return function (options) {
2015-01-20 16:32:48 -05:00
2016-08-17 16:32:39 -04:00
var button = options.button;
function pollTasks() {
2015-01-20 16:32:48 -05:00
2016-03-07 14:13:58 -05:00
ApiClient.getScheduledTasks({
2015-01-20 16:32:48 -05:00
2016-03-07 14:13:58 -05:00
IsEnabled: true
2015-01-20 16:32:48 -05:00
2016-08-17 16:32:39 -04:00
}).then(updateTasks);
2016-03-07 14:13:58 -05:00
}
2015-01-20 16:32:48 -05:00
2016-08-17 16:32:39 -04:00
function updateTasks(tasks) {
2015-01-20 16:32:48 -05:00
2016-03-07 14:13:58 -05:00
var task = tasks.filter(function (t) {
2015-01-20 16:32:48 -05:00
2016-03-07 14:13:58 -05:00
return t.Key == options.taskKey;
2015-01-20 16:32:48 -05:00
2016-03-07 14:13:58 -05:00
})[0];
if (options.panel) {
if (task) {
2016-06-08 01:24:25 -04:00
options.panel.classList.remove('hide');
2016-03-07 14:13:58 -05:00
} else {
2016-06-08 01:24:25 -04:00
options.panel.classList.add('hide');
2016-03-07 14:13:58 -05:00
}
2015-01-20 16:32:48 -05:00
}
2016-03-07 14:13:58 -05:00
if (!task) {
return;
}
2015-01-20 16:32:48 -05:00
2016-03-07 14:13:58 -05:00
if (task.State == 'Idle') {
2016-08-17 16:32:39 -04:00
button.removeAttribute('disabled');
2016-03-07 14:13:58 -05:00
} else {
2016-08-17 16:32:39 -04:00
button.setAttribute('disabled', 'disabled');
2016-03-07 14:13:58 -05:00
}
2015-12-14 10:43:03 -05:00
2016-08-17 16:32:39 -04:00
button.setAttribute('data-taskid', task.Id);
2015-01-20 16:32:48 -05:00
2016-03-07 14:13:58 -05:00
var progress = (task.CurrentProgressPercentage || 0).toFixed(1);
2015-01-20 16:32:48 -05:00
2016-03-07 14:13:58 -05:00
if (options.progressElem) {
options.progressElem.value = progress;
2015-01-20 16:32:48 -05:00
2016-03-07 14:13:58 -05:00
if (task.State == 'Running') {
options.progressElem.classList.remove('hide');
} else {
options.progressElem.classList.add('hide');
}
2015-01-20 16:32:48 -05:00
}
2016-03-07 14:13:58 -05:00
if (options.lastResultElem) {
var lastResult = task.LastExecutionResult ? task.LastExecutionResult.Status : '';
if (lastResult == "Failed") {
2016-04-09 16:20:19 -04:00
options.lastResultElem.html('<span style="color:#FF0000;">(' + Globalize.translate('LabelFailed') + ')</span>');
2016-03-07 14:13:58 -05:00
}
else if (lastResult == "Cancelled") {
2016-04-09 16:20:19 -04:00
options.lastResultElem.html('<span style="color:#0026FF;">(' + Globalize.translate('LabelCancelled') + ')</span>');
2016-03-07 14:13:58 -05:00
}
else if (lastResult == "Aborted") {
options.lastResultElem.html('<span style="color:#FF0000;">' + Globalize.translate('LabelAbortedByServerShutdown') + '</span>');
} else {
options.lastResultElem.html(lastResult);
}
2015-01-20 16:32:48 -05:00
}
}
2016-08-17 16:32:39 -04:00
function onScheduledTaskMessageConfirmed(id) {
ApiClient.startScheduledTask(id).then(pollTasks);
2016-03-07 14:13:58 -05:00
}
2015-01-22 11:41:34 -05:00
2016-03-07 14:13:58 -05:00
function onButtonClick() {
2015-01-22 11:41:34 -05:00
2016-03-07 14:13:58 -05:00
var button = this;
2016-10-01 03:06:00 -04:00
var buttonTextElement = this.querySelector('span') || this;
var text = buttonTextElement.textContent || buttonTextElement.innerText;
var taskId = button.getAttribute('data-taskid');
2015-06-29 14:45:42 -04:00
2016-03-07 14:13:58 -05:00
var key = 'scheduledTaskButton' + options.taskKey;
2016-05-14 01:40:01 -04:00
var expectedValue = new Date().getMonth() + '6';
2015-01-22 11:41:34 -05:00
2016-10-01 03:06:00 -04:00
if (userSettings.get(key) == expectedValue) {
onScheduledTaskMessageConfirmed(taskId);
2016-03-07 14:13:58 -05:00
} else {
2015-01-22 11:41:34 -05:00
2016-10-01 03:06:00 -04:00
require(['dialog'], function (dialog) {
2015-01-22 11:41:34 -05:00
2016-10-01 03:06:00 -04:00
var msg = Globalize.translate('ConfirmMessageScheduledTaskButton');
2015-01-22 11:41:34 -05:00
2016-10-01 03:06:00 -04:00
var menuItems = [];
2016-05-14 01:40:01 -04:00
2016-10-01 03:06:00 -04:00
menuItems.push({
name: text,
id: 'task'
});
menuItems.push({
name: Globalize.translate('ButtonScheduledTasks'),
id: 'tasks'
});
menuItems.push({
name: Globalize.translate('ButtonCancel'),
id: 'cancel'
});
2016-05-14 01:40:01 -04:00
2016-10-01 03:06:00 -04:00
dialog({
buttons: menuItems,
text: msg
}).then(function (id) {
switch (id) {
case 'task':
userSettings.set(key, expectedValue);
onScheduledTaskMessageConfirmed(taskId);
break;
case 'tasks':
userSettings.set(key, expectedValue);
Dashboard.navigate('scheduledtasks.html');
break;
default:
break;
}
2016-03-07 14:13:58 -05:00
});
2015-01-22 11:41:34 -05:00
2016-02-22 13:47:56 -05:00
});
2016-03-07 14:13:58 -05:00
}
2015-01-22 11:41:34 -05:00
}
2016-03-07 14:13:58 -05:00
function onSocketOpen() {
startInterval();
}
2015-06-29 14:45:42 -04:00
2016-03-07 14:13:58 -05:00
function onSocketMessage(e, msg) {
if (msg.MessageType == "ScheduledTasksInfo") {
2015-06-29 14:45:42 -04:00
2016-03-07 14:13:58 -05:00
var tasks = msg.Data;
2015-06-29 14:45:42 -04:00
2016-08-17 16:32:39 -04:00
updateTasks(tasks);
2016-03-07 14:13:58 -05:00
}
2015-06-29 14:45:42 -04:00
}
2016-03-07 14:13:58 -05:00
var pollInterval;
2015-07-14 12:39:34 -04:00
2016-03-07 14:13:58 -05:00
function onPollIntervalFired() {
2015-07-14 12:39:34 -04:00
2016-03-07 14:13:58 -05:00
if (!ApiClient.isWebSocketOpen()) {
2016-08-17 16:32:39 -04:00
pollTasks();
2016-03-07 14:13:58 -05:00
}
2015-07-14 12:39:34 -04:00
}
2016-03-07 14:13:58 -05:00
function startInterval() {
if (ApiClient.isWebSocketOpen()) {
ApiClient.sendWebSocketMessage("ScheduledTasksInfoStart", "1000,1000");
}
if (pollInterval) {
clearInterval(pollInterval);
}
pollInterval = setInterval(onPollIntervalFired, 5000);
2015-07-14 12:39:34 -04:00
}
2016-03-07 14:13:58 -05:00
function stopInterval() {
if (ApiClient.isWebSocketOpen()) {
ApiClient.sendWebSocketMessage("ScheduledTasksInfoStop");
}
if (pollInterval) {
clearInterval(pollInterval);
}
2015-07-14 12:39:34 -04:00
}
2015-01-20 16:32:48 -05:00
2016-03-07 14:13:58 -05:00
if (options.panel) {
2016-06-08 01:24:25 -04:00
options.panel.classList.add('hide');
2016-03-07 14:13:58 -05:00
}
2015-01-20 16:32:48 -05:00
2016-03-07 14:13:58 -05:00
if (options.mode == 'off') {
2015-01-20 16:32:48 -05:00
2016-08-17 16:32:39 -04:00
button.removeEventListener('click', onButtonClick);
2016-03-07 14:13:58 -05:00
Events.off(ApiClient, 'websocketmessage', onSocketMessage);
Events.off(ApiClient, 'websocketopen', onSocketOpen);
stopInterval();
2015-01-20 16:32:48 -05:00
2016-10-01 03:06:00 -04:00
} else {
2015-01-20 16:32:48 -05:00
2016-08-17 16:32:39 -04:00
button.addEventListener('click', onButtonClick);
2015-01-20 16:32:48 -05:00
2016-08-17 16:32:39 -04:00
pollTasks();
2015-01-20 16:32:48 -05:00
2016-03-07 14:13:58 -05:00
startInterval();
2015-01-20 16:32:48 -05:00
2016-03-07 14:13:58 -05:00
Events.on(ApiClient, 'websocketmessage', onSocketMessage);
Events.on(ApiClient, 'websocketopen', onSocketOpen);
}
};
});