diff --git a/src/controllers/scheduledtaskspage.js b/src/controllers/scheduledtaskspage.js index 1580f1a4c3..086cc2634a 100644 --- a/src/controllers/scheduledtaskspage.js +++ b/src/controllers/scheduledtaskspage.js @@ -3,21 +3,63 @@ define(["jQuery", "loading", "events", "globalize", "serverNotifications", "huma function reloadList(page) { ApiClient.getScheduledTasks({ - isHidden: !1 + isHidden: false }).then(function(tasks) { - populateList(page, tasks), loading.hide() + populateList(page, tasks); + loading.hide(); }) } function populateList(page, tasks) { tasks = tasks.sort(function(a, b) { - return a = a.Category + " " + a.Name, b = b.Category + " " + b.Name, a == b ? 0 : a < b ? -1 : 1 + a = a.Category + " " + a.Name; + b = b.Category + " " + b.Name; + return a == b ? 0 : a < b ? -1 : 1; }); - for (var currentCategory, html = "", i = 0, length = tasks.length; i < length; i++) { + + var currentCategory; + var html = ""; + for (var i = 0; i < tasks.length; i++) { var task = tasks[i]; - task.Category != currentCategory && (currentCategory = task.Category, currentCategory && (html += "", html += ""), html += '
', html += '
', html += '

', html += currentCategory, html += "

", 0 === i && (html += '' + globalize.translate("Help") + ""), html += "
", html += '
'), html += '
', html += "", html += 'schedule', html += "", html += '", "Idle" == task.State ? html += '' : "Running" == task.State ? html += '' : html += '', html += "
" + if (task.Category != currentCategory) { + currentCategory = task.Category; + if (currentCategory) { + html += "
"; + html += "
"; + } + html += '
'; + html += '
'; + html += '

'; + html += currentCategory; + html += "

"; + if (i === 0) { + html += '' + globalize.translate("Help") + ""; + } + html += "
"; + html += '
'; + } + html += '
'; + html += ""; + html += 'schedule'; + html += ""; + html += '"; + if (task.State === "Running") { + html += ''; + } else { + html += ''; + } + html += "
"; } - tasks.length && (html += "
", html += "
"), page.querySelector(".divScheduledTasks").innerHTML = html + if (tasks.length) { + html += ""; + html += ""; + } + page.querySelector(".divScheduledTasks").innerHTML = html; } function humane_elapsed(firstDateStr, secondDateStr) { @@ -34,8 +76,18 @@ define(["jQuery", "loading", "events", "globalize", "serverNotifications", "huma function getTaskProgressHtml(task) { var html = ""; - if ("Idle" == task.State) task.LastExecutionResult && (html += globalize.translate("LabelScheduledTaskLastRan").replace("{0}", humane_date(task.LastExecutionResult.EndTimeUtc)).replace("{1}", humane_elapsed(task.LastExecutionResult.StartTimeUtc, task.LastExecutionResult.EndTimeUtc)), "Failed" == task.LastExecutionResult.Status ? html += " (" + globalize.translate("LabelFailed") + ")" : "Cancelled" == task.LastExecutionResult.Status ? html += " (" + globalize.translate("LabelCancelled") + ")" : "Aborted" == task.LastExecutionResult.Status && (html += " " + globalize.translate("LabelAbortedByServerShutdown") + "")); - else if ("Running" == task.State) { + if (task.State === "Idle") { + if (task.LastExecutionResult) { + html += globalize.translate("LabelScheduledTaskLastRan").replace("{0}", humane_date(task.LastExecutionResult.EndTimeUtc)).replace("{1}", humane_elapsed(task.LastExecutionResult.StartTimeUtc, task.LastExecutionResult.EndTimeUtc)); + if (task.LastExecutionResult.Status === "Failed") { + html += " (" + globalize.translate("LabelFailed") + ")"; + } else if (task.LastExecutionResult.Status === "Cancelled") { + html += " (" + globalize.translate("LabelCancelled") + ")"; + } else if (task.LastExecutionResult.Status === "Aborted") { + html += " " + globalize.translate("LabelAbortedByServerShutdown") + ""; + } + } + } else if (task.State === "Running") { var progress = (task.CurrentProgressPercentage || 0).toFixed(1); html += '
'; html += '
'; @@ -47,53 +99,88 @@ define(["jQuery", "loading", "events", "globalize", "serverNotifications", "huma } else { html += "" + globalize.translate("LabelStopping") + ""; } - return html + return html; } function updateTaskButton(elem, state) { - "Idle" == state ? (elem.classList.add("btnStartTask"), elem.classList.remove("btnStopTask"), elem.classList.remove("hide"), elem.querySelector("i").innerHTML = "play_arrow", elem.title = globalize.translate("ButtonStart")) : "Running" == state ? (elem.classList.remove("btnStartTask"), elem.classList.add("btnStopTask"), elem.classList.remove("hide"), elem.querySelector("i").innerHTML = "stop", elem.title = globalize.translate("ButtonStop")) : (elem.classList.add("btnStartTask"), elem.classList.remove("btnStopTask"), elem.classList.add("hide"), elem.querySelector("i").innerHTML = "play_arrow", elem.title = globalize.translate("ButtonStart")), $(elem).parents(".listItem")[0].setAttribute("data-status", state) + if (state === "Running") { + elem.classList.remove("btnStartTask"); + elem.classList.add("btnStopTask"); + elem.classList.remove("hide"); + elem.querySelector("i").innerHTML = "stop"; + elem.title = globalize.translate("ButtonStop"); + } else { + elem.classList.add("btnStartTask"); + elem.classList.remove("btnStopTask"); + elem.classList.add("hide"); + elem.querySelector("i").innerHTML = "play_arrow"; + elem.title = globalize.translate("ButtonStart"); + } + $(elem).parents(".listItem")[0].setAttribute("data-status", state); } + return function(view, params) { function updateTasks(tasks) { - for (var i = 0, length = tasks.length; i < length; i++) { + for (var i = 0; i < tasks.length; i++) { var task = tasks[i]; view.querySelector("#taskProgress" + task.Id).innerHTML = getTaskProgressHtml(task); - updateTaskButton(view.querySelector("#btnTask" + task.Id), task.State) + updateTaskButton(view.querySelector("#btnTask" + task.Id), task.State); } } function onPollIntervalFired() { - ApiClient.isMessageChannelOpen() || reloadList(view) + if (!ApiClient.isMessageChannelOpen()) { + reloadList(view); + } } function onScheduledTasksUpdate(e, apiClient, info) { - apiClient.serverId() === serverId && updateTasks(info) + if (apiClient.serverId() === serverId) { + updateTasks(info); + } } function startInterval() { - ApiClient.sendMessage("ScheduledTasksInfoStart", "1000,1000"), pollInterval && clearInterval(pollInterval), pollInterval = setInterval(onPollIntervalFired, 1e4) + ApiClient.sendMessage("ScheduledTasksInfoStart", "1000,1000"); + pollInterval && clearInterval(pollInterval); + pollInterval = setInterval(onPollIntervalFired, 1e4); } function stopInterval() { - ApiClient.sendMessage("ScheduledTasksInfoStop"), pollInterval && clearInterval(pollInterval) + ApiClient.sendMessage("ScheduledTasksInfoStop"); + pollInterval && clearInterval(pollInterval); } + var pollInterval, serverId = ApiClient.serverId(); + $(".divScheduledTasks", view).on("click", ".btnStartTask", function() { - var button = this, - id = button.getAttribute("data-taskid"); + var button = this; + var id = button.getAttribute("data-taskid"); ApiClient.startScheduledTask(id).then(function() { - updateTaskButton(button, "Running"), reloadList(view) + updateTaskButton(button, "Running"); + reloadList(view); }) - }).on("click", ".btnStopTask", function() { - var button = this, - id = button.getAttribute("data-taskid"); + }); + + $(".divScheduledTasks", view).on("click", ".btnStopTask", function() { + var button = this; + var id = button.getAttribute("data-taskid"); ApiClient.stopScheduledTask(id).then(function() { - updateTaskButton(button, ""), reloadList(view) + updateTaskButton(button, ""); + reloadList(view); }) - }), view.addEventListener("viewbeforehide", function() { - events.off(serverNotifications, "ScheduledTasksInfo", onScheduledTasksUpdate), stopInterval() - }), view.addEventListener("viewshow", function() { - loading.show(), startInterval(), reloadList(view), events.on(serverNotifications, "ScheduledTasksInfo", onScheduledTasksUpdate) - }) + }); + + view.addEventListener("viewbeforehide", function() { + events.off(serverNotifications, "ScheduledTasksInfo", onScheduledTasksUpdate); + stopInterval(); + }); + + view.addEventListener("viewshow", function() { + loading.show(); + startInterval(); + reloadList(view); + events.on(serverNotifications, "ScheduledTasksInfo", onScheduledTasksUpdate); + }); } });