(function ($, document, window) {
function reloadList(page, updateInterval) {
if (updateInterval) {
stopInterval();
}
ApiClient.getScheduledTasks({isHidden: false}).done(function (tasks) {
if (updateInterval) {
populateList(page, tasks);
} else {
updateTasks(page, tasks);
}
Dashboard.hideLoadingMsg();
if (updateInterval) {
startInterval();
}
});
}
function populateList(page, tasks) {
tasks = tasks.sort(function (a, b) {
a = a.Category + " " + a.Name;
b = b.Category + " " + b.Name;
if (a == b) {
return 0;
}
if (a < b) {
return -1;
}
return 1;
});
var html = "";
html += '
";
$('#divScheduledTasks', page).html(html).trigger('create');
}
function getTaskProgressHtml(task) {
var html = '';
if (task.State == "Idle") {
if (task.LastExecutionResult) {
html += "Last ran " + humane_date(task.LastExecutionResult.EndTimeUtc) + ', taking ' + humane_elapsed(task.LastExecutionResult.StartTimeUtc, task.LastExecutionResult.EndTimeUtc);
if (task.LastExecutionResult.Status == "Failed") {
html += " (failed)";
}
else if (task.LastExecutionResult.Status == "Cancelled") {
html += " (cancelled)";
}
else if (task.LastExecutionResult.Status == "Aborted") {
html += " (Aborted by server shutdown)";
}
}
}
else if (task.State == "Running") {
var progress = (task.CurrentProgressPercentage || 0).toFixed(1);
html += '';
html += "" + progress + "%";
} else {
html += "Stopping";
}
return html;
}
function onWebSocketMessage(e, msg) {
if (msg.MessageType == "ScheduledTasksInfo") {
var tasks = msg.Data;
var page = $.mobile.activePage;
updateTasks(page, tasks);
}
}
function updateTasks(page, tasks) {
for (var i = 0, length = tasks.length; i < length; i++) {
var task = tasks[i];
$('#' + task.Id, page).html(getTaskProgressHtml(task));
var btnTask = $('#btnTask' + task.Id, page);
updateTaskButton(btnTask, task.State);
}
}
function updateTaskButton(btnTask, state) {
var elem;
if (state == "Idle") {
elem = btnTask.addClass('btnStartTask').removeClass('btnStopTask').show().data("icon", "play").attr("title", "Start");
elem.removeClass('ui-icon-stop').addClass('ui-icon-play');
}
else if (state == "Running") {
elem = btnTask.addClass('btnStopTask').removeClass('btnStartTask').show().data("icon", "stop").attr("title", "Stop");
elem.removeClass('ui-icon-play').addClass('ui-icon-stop');
} else {
elem = btnTask.addClass('btnStartTask').removeClass('btnStopTask').hide().data("icon", "play").attr("title", "Start");
elem.removeClass('ui-icon-stop').addClass('ui-icon-play');
}
}
function onWebSocketConnectionChange() {
reloadList($.mobile.activePage, true);
}
function startInterval() {
if (ApiClient.isWebSocketOpen()) {
ApiClient.sendWebSocketMessage("ScheduledTasksInfoStart", "1500,1500");
}
}
function stopInterval() {
if (ApiClient.isWebSocketOpen()) {
ApiClient.sendWebSocketMessage("ScheduledTasksInfoStop");
}
}
$(document).on('pageshow', "#scheduledTasksPage", function () {
var page = this;
Dashboard.showLoadingMsg();
reloadList(page, true);
$(ApiClient).on("websocketmessage", onWebSocketMessage).on("websocketopen", onWebSocketConnectionChange);
$('#divScheduledTasks', page).on('click', '.btnStartTask', function () {
var button = this;
var id = button.getAttribute('data-taskid');
ApiClient.startScheduledTask(id).done(function () {
updateTaskButton($(button), "Running");
reloadList(page);
});
}).on('click', '.btnStopTask', function () {
var button = this;
var id = button.getAttribute('data-taskid');
ApiClient.stopScheduledTask(id).done(function () {
updateTaskButton($(button), "");
reloadList(page);
});
});
}).on('pagehide', "#scheduledTasksPage", function () {
var page = this;
$(ApiClient).off("websocketmessage", onWebSocketMessage).off("websocketopen", onWebSocketConnectionChange).off("websocketerror", onWebSocketConnectionChange).off("websocketclose", onWebSocketConnectionChange);
stopInterval();
$('#divScheduledTasks', page).off('click', '.btnStartTask').off('click', '.btnStopTask');
});
})(jQuery, document, window);