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

178 lines
5.3 KiB
JavaScript
Raw Normal View History

2016-04-26 19:59:43 -07:00
define(['appStorage', 'jQuery', 'paper-button'], function (appStorage, $) {
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
$.fn.taskButton = function (options) {
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
function pollTasks(button) {
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
ApiClient.getScheduledTasks({
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
IsEnabled: true
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
}).then(function (tasks) {
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
updateTasks(button, tasks);
});
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
}
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
function updateTasks(button, tasks) {
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
var task = tasks.filter(function (t) {
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
return t.Key == options.taskKey;
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
})[0];
if (options.panel) {
if (task) {
$(options.panel).show();
} else {
$(options.panel).hide();
}
2015-01-20 14:32:48 -07:00
}
2016-03-07 12:13:58 -07:00
if (!task) {
return;
}
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
if (task.State == 'Idle') {
$(button).removeAttr('disabled');
} else {
$(button).attr('disabled', 'disabled');
}
2015-12-14 08:43:03 -07:00
2016-03-07 12:13:58 -07:00
$(button).attr('data-taskid', task.Id);
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
var progress = (task.CurrentProgressPercentage || 0).toFixed(1);
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
if (options.progressElem) {
options.progressElem.value = progress;
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
if (task.State == 'Running') {
options.progressElem.classList.remove('hide');
} else {
options.progressElem.classList.add('hide');
}
2015-01-20 14:32:48 -07:00
}
2016-03-07 12:13:58 -07:00
if (options.lastResultElem) {
var lastResult = task.LastExecutionResult ? task.LastExecutionResult.Status : '';
if (lastResult == "Failed") {
2016-04-09 13:20:19 -07:00
options.lastResultElem.html('<span style="color:#FF0000;">(' + Globalize.translate('LabelFailed') + ')</span>');
2016-03-07 12:13:58 -07:00
}
else if (lastResult == "Cancelled") {
2016-04-09 13:20:19 -07:00
options.lastResultElem.html('<span style="color:#0026FF;">(' + Globalize.translate('LabelCancelled') + ')</span>');
2016-03-07 12:13:58 -07:00
}
else if (lastResult == "Aborted") {
options.lastResultElem.html('<span style="color:#FF0000;">' + Globalize.translate('LabelAbortedByServerShutdown') + '</span>');
} else {
options.lastResultElem.html(lastResult);
}
2015-01-20 14:32:48 -07:00
}
}
2016-03-07 12:13:58 -07:00
function onScheduledTaskMessageConfirmed(instance, id) {
ApiClient.startScheduledTask(id).then(function () {
pollTasks(instance);
});
}
2015-01-22 09:41:34 -07:00
2016-03-07 12:13:58 -07:00
function onButtonClick() {
2015-01-22 09:41:34 -07:00
2016-03-07 12:13:58 -07:00
var button = this;
var id = button.getAttribute('data-taskid');
2015-06-29 11:45:42 -07:00
2016-03-07 12:13:58 -07:00
var key = 'scheduledTaskButton' + options.taskKey;
var expectedValue = new Date().getMonth() + '5';
2015-01-22 09:41:34 -07:00
2016-03-07 12:13:58 -07:00
if (appStorage.getItem(key) == expectedValue) {
onScheduledTaskMessageConfirmed(button, id);
} else {
2015-01-22 09:41:34 -07:00
2016-03-07 12:13:58 -07:00
var msg = Globalize.translate('ConfirmMessageScheduledTaskButton');
msg += '<br/>';
msg += '<div style="margin-top:1em;">';
msg += '<a class="clearLink" href="scheduledtasks.html"><paper-button style="color:#3f51b5!important;margin:0;">' + Globalize.translate('ButtonScheduledTasks') + '</paper-button></a>';
msg += '</div>';
2015-01-22 09:41:34 -07:00
2016-03-07 12:13:58 -07:00
require(['confirm'], function (confirm) {
2015-01-22 09:41:34 -07:00
2016-03-07 12:13:58 -07:00
confirm(msg, Globalize.translate('HeaderConfirmation')).then(function () {
appStorage.setItem(key, expectedValue);
onScheduledTaskMessageConfirmed(button, id);
});
2015-01-22 09:41:34 -07:00
2016-02-22 11:47:56 -07:00
});
2016-03-07 12:13:58 -07:00
}
2015-01-22 09:41:34 -07:00
}
2016-03-07 12:13:58 -07:00
function onSocketOpen() {
startInterval();
}
2015-06-29 11:45:42 -07:00
2016-03-07 12:13:58 -07:00
function onSocketMessage(e, msg) {
if (msg.MessageType == "ScheduledTasksInfo") {
2015-06-29 11:45:42 -07:00
2016-03-07 12:13:58 -07:00
var tasks = msg.Data;
2015-06-29 11:45:42 -07:00
2016-03-07 12:13:58 -07:00
updateTasks(self, tasks);
}
2015-06-29 11:45:42 -07:00
}
2016-03-07 12:13:58 -07:00
var self = this;
var pollInterval;
2015-07-14 09:39:34 -07:00
2016-03-07 12:13:58 -07:00
function onPollIntervalFired() {
2015-07-14 09:39:34 -07:00
2016-03-07 12:13:58 -07:00
if (!ApiClient.isWebSocketOpen()) {
pollTasks(self);
}
2015-07-14 09:39:34 -07:00
}
2016-03-07 12:13:58 -07:00
function startInterval() {
if (ApiClient.isWebSocketOpen()) {
ApiClient.sendWebSocketMessage("ScheduledTasksInfoStart", "1000,1000");
}
if (pollInterval) {
clearInterval(pollInterval);
}
pollInterval = setInterval(onPollIntervalFired, 5000);
2015-07-14 09:39:34 -07:00
}
2016-03-07 12:13:58 -07:00
function stopInterval() {
if (ApiClient.isWebSocketOpen()) {
ApiClient.sendWebSocketMessage("ScheduledTasksInfoStop");
}
if (pollInterval) {
clearInterval(pollInterval);
}
2015-07-14 09:39:34 -07:00
}
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
if (options.panel) {
$(options.panel).hide();
}
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
if (options.mode == 'off') {
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
this.off('click', onButtonClick);
Events.off(ApiClient, 'websocketmessage', onSocketMessage);
Events.off(ApiClient, 'websocketopen', onSocketOpen);
stopInterval();
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
} else if (this.length) {
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
this.on('click', onButtonClick);
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
pollTasks(self);
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
startInterval();
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
Events.on(ApiClient, 'websocketmessage', onSocketMessage);
Events.on(ApiClient, 'websocketopen', onSocketOpen);
}
2015-01-20 14:32:48 -07:00
2016-03-07 12:13:58 -07:00
return this;
};
});