define(['appStorage', 'jQuery'], function (appStorage, $) {
$.fn.taskButton = function (options) {
function pollTasks(button) {
ApiClient.getScheduledTasks({
IsEnabled: true
}).then(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;
}
if (task.State == 'Idle') {
$(button).removeAttr('disabled');
} else {
$(button).attr('disabled', 'disabled');
}
$(button).attr('data-taskid', task.Id);
var progress = (task.CurrentProgressPercentage || 0).toFixed(1);
if (options.progressElem) {
options.progressElem.value = progress;
if (task.State == 'Running') {
options.progressElem.classList.remove('hide');
} else {
options.progressElem.classList.add('hide');
}
}
if (options.lastResultElem) {
var lastResult = task.LastExecutionResult ? task.LastExecutionResult.Status : '';
if (lastResult == "Failed") {
options.lastResultElem.html('' + Globalize.translate('LabelFailed') + '');
}
else if (lastResult == "Cancelled") {
options.lastResultElem.html('' + Globalize.translate('LabelCancelled') + '');
}
else if (lastResult == "Aborted") {
options.lastResultElem.html('' + Globalize.translate('LabelAbortedByServerShutdown') + '');
} else {
options.lastResultElem.html(lastResult);
}
}
}
function onScheduledTaskMessageConfirmed(instance, id) {
ApiClient.startScheduledTask(id).then(function () {
pollTasks(instance);
});
}
function onButtonClick() {
var button = this;
var id = button.getAttribute('data-taskid');
var key = 'scheduledTaskButton' + options.taskKey;
var expectedValue = new Date().getMonth() + '5';
if (appStorage.getItem(key) == expectedValue) {
onScheduledTaskMessageConfirmed(button, id);
} else {
var msg = Globalize.translate('ConfirmMessageScheduledTaskButton');
msg += '
';
msg += '