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

169 lines
4.5 KiB
JavaScript
Raw Normal View History

2015-01-20 14:32:48 -07:00

$.fn.taskButton = function (options) {
function pollTasks(button) {
ApiClient.getScheduledTasks({
IsEnabled: true
}).done(function (tasks) {
updateTasks(button, tasks);
});
}
function updateTasks(button, tasks) {
var task = tasks.filter(function (t) {
return t.Key == options.taskKey;
})[0];
if (options.panel) {
if (task) {
$(options.panel).show();
} else {
$(options.panel).hide();
}
}
if (!task) {
return;
}
2015-07-14 09:39:34 -07:00
$(button).buttonEnabled(task.State == 'Idle').attr('data-taskid', task.Id);
2015-01-20 14:32:48 -07:00
var progress = (task.CurrentProgressPercentage || 0).toFixed(1);
if (options.progressElem) {
2015-09-12 09:39:24 -07:00
options.progressElem.value = progress;
2015-01-20 14:32:48 -07:00
if (task.State == 'Running') {
2015-09-12 09:39:24 -07:00
options.progressElem.classList.remove('hide');
2015-01-20 14:32:48 -07:00
} else {
2015-09-12 09:39:24 -07:00
options.progressElem.classList.add('hide');
2015-01-20 14:32:48 -07:00
}
}
if (options.lastResultElem) {
var lastResult = task.LastExecutionResult ? task.LastExecutionResult.Status : '';
if (lastResult == "Failed") {
options.lastResultElem.html('<span style="color:#FF0000;">' + Globalize.translate('LabelFailed') + '</span>');
}
else if (lastResult == "Cancelled") {
options.lastResultElem.html('<span style="color:#0026FF;">' + Globalize.translate('LabelCancelled') + '</span>');
}
else if (lastResult == "Aborted") {
options.lastResultElem.html('<span style="color:#FF0000;">' + Globalize.translate('LabelAbortedByServerShutdown') + '</span>');
} else {
options.lastResultElem.html(lastResult);
}
}
}
2015-01-22 09:41:34 -07:00
function onScheduledTaskMessageConfirmed(instance, id) {
ApiClient.startScheduledTask(id).done(function () {
pollTasks(instance);
});
}
2015-06-29 11:45:42 -07:00
function onButtonClick() {
var button = this;
var id = button.getAttribute('data-taskid');
2015-01-22 09:41:34 -07:00
var key = 'scheduledTaskButton' + options.taskKey;
2015-09-19 19:06:56 -07:00
var expectedValue = new Date().getMonth() + '5';
2015-01-22 09:41:34 -07:00
2015-05-20 10:29:26 -07:00
if (appStorage.getItem(key) == expectedValue) {
2015-06-29 11:45:42 -07:00
onScheduledTaskMessageConfirmed(button, id);
2015-01-22 09:41:34 -07:00
} else {
var msg = Globalize.translate('ConfirmMessageScheduledTaskButton');
msg += '<br/>';
2015-09-19 19:06:56 -07:00
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
Dashboard.confirm(msg, Globalize.translate('HeaderConfirmation'), function (result) {
if (result) {
2015-05-20 10:29:26 -07:00
appStorage.setItem(key, expectedValue);
2015-06-29 11:45:42 -07:00
onScheduledTaskMessageConfirmed(button, id);
2015-01-22 09:41:34 -07:00
}
});
}
}
2015-06-29 11:45:42 -07:00
function onSocketOpen() {
2015-07-14 09:39:34 -07:00
startInterval();
2015-06-29 11:45:42 -07:00
}
function onSocketMessage(e, msg) {
if (msg.MessageType == "ScheduledTasksInfo") {
var tasks = msg.Data;
updateTasks(self, tasks);
}
}
2015-01-20 14:32:48 -07:00
var self = this;
2015-07-14 09:39:34 -07:00
var pollInterval;
function onPollIntervalFired() {
if (!ApiClient.isWebSocketOpen()) {
pollTasks(self);
}
}
function startInterval() {
if (ApiClient.isWebSocketOpen()) {
ApiClient.sendWebSocketMessage("ScheduledTasksInfoStart", "1000,1000");
}
if (pollInterval) {
clearInterval(pollInterval);
}
2015-11-12 11:05:51 -07:00
pollInterval = setInterval(onPollIntervalFired, 5000);
2015-07-14 09:39:34 -07:00
}
function stopInterval() {
if (ApiClient.isWebSocketOpen()) {
ApiClient.sendWebSocketMessage("ScheduledTasksInfoStop");
}
if (pollInterval) {
clearInterval(pollInterval);
}
}
2015-01-20 14:32:48 -07:00
if (options.panel) {
$(options.panel).hide();
}
if (options.mode == 'off') {
2015-06-29 11:45:42 -07:00
this.off('click', onButtonClick);
$(ApiClient).off("websocketmessage", onSocketMessage).off('websocketopen', onSocketOpen);
2015-07-14 09:39:34 -07:00
stopInterval();
2015-01-20 14:32:48 -07:00
2015-03-29 20:04:55 -07:00
} else if (this.length) {
2015-01-20 14:32:48 -07:00
2015-06-29 11:45:42 -07:00
this.on('click', onButtonClick);
2015-01-20 14:32:48 -07:00
pollTasks(self);
2015-07-14 09:39:34 -07:00
startInterval();
2015-01-20 14:32:48 -07:00
2015-06-29 11:45:42 -07:00
$(ApiClient).on("websocketmessage", onSocketMessage).on('websocketopen', onSocketOpen);
2015-01-20 14:32:48 -07:00
}
return this;
};