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

176 lines
4.7 KiB
JavaScript
Raw Normal View History

2015-12-14 08:43:03 -07:00
$.fn.taskButton = function (options) {
2015-01-20 14:32:48 -07:00
function pollTasks(button) {
ApiClient.getScheduledTasks({
IsEnabled: true
2015-12-14 08:43:03 -07:00
}).then(function (tasks) {
2015-01-20 14:32:48 -07:00
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-12-14 08:43:03 -07:00
if (task.State == 'Idle') {
$(button).removeAttr('disabled');
} else {
$(button).attr('disabled', 'disabled');
}
$(button).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) {
2015-12-14 08:43:03 -07:00
ApiClient.startScheduledTask(id).then(function () {
2015-01-22 09:41:34 -07:00
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);
2015-12-23 10:46:01 -07:00
Events.off(ApiClient, 'websocketmessage', onSocketMessage);
Events.off(ApiClient, '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-12-23 10:46:01 -07:00
Events.on(ApiClient, 'websocketmessage', onSocketMessage);
Events.on(ApiClient, 'websocketopen', onSocketOpen);
2015-01-20 14:32:48 -07:00
}
return this;
};