ScheduledTasksPage.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. var ScheduledTasksPage = {
  2. onPageShow: function () {
  3. Dashboard.showLoadingMsg();
  4. ScheduledTasksPage.reloadList(true);
  5. $(document).on("websocketmessage", ScheduledTasksPage.onWebSocketMessage).on("websocketopen", ScheduledTasksPage.onWebSocketConnectionChange).on("websocketerror", ScheduledTasksPage.onWebSocketConnectionChange).on("websocketclose", ScheduledTasksPage.onWebSocketConnectionChange);
  6. },
  7. onPageHide: function () {
  8. $(document).off("websocketmessage", ScheduledTasksPage.onWebSocketMessage).off("websocketopen", ScheduledTasksPage.onWebSocketConnectionChange).off("websocketerror", ScheduledTasksPage.onWebSocketConnectionChange).off("websocketclose", ScheduledTasksPage.onWebSocketConnectionChange);
  9. ScheduledTasksPage.stopInterval();
  10. },
  11. startInterval: function () {
  12. if (Dashboard.isWebSocketOpen()) {
  13. Dashboard.sendWebSocketMessage("ScheduledTasksInfoStart", "1500,1500");
  14. }
  15. },
  16. stopInterval: function () {
  17. if (Dashboard.isWebSocketOpen()) {
  18. Dashboard.sendWebSocketMessage("ScheduledTasksInfoStop");
  19. }
  20. },
  21. onWebSocketMessage: function (e, msg) {
  22. if (msg.MessageType == "ScheduledTasksInfo") {
  23. ScheduledTasksPage.populateList(msg.Data);
  24. }
  25. },
  26. onWebSocketConnectionChange: function() {
  27. ScheduledTasksPage.reloadList(true);
  28. },
  29. reloadList: function (updateInterval) {
  30. if (updateInterval) {
  31. ScheduledTasksPage.stopInterval();
  32. }
  33. ApiClient.getScheduledTasks().done(function (tasks) {
  34. ScheduledTasksPage.populateList(tasks);
  35. Dashboard.hideLoadingMsg();
  36. if (updateInterval) {
  37. ScheduledTasksPage.startInterval();
  38. }
  39. });
  40. },
  41. populateList: function (tasks) {
  42. tasks = tasks.sort(function (a, b) {
  43. a = a.Category + " " + a.Name;
  44. b = b.Category + " " + b.Name;
  45. if (a == b) {
  46. return 0;
  47. }
  48. if (a < b) {
  49. return -1;
  50. }
  51. return 1;
  52. });
  53. var page = $($.mobile.activePage);
  54. var html = "";
  55. var currentCategory;
  56. for (var i = 0, length = tasks.length; i < length; i++) {
  57. var task = tasks[i];
  58. if (task.Category != currentCategory) {
  59. currentCategory = task.Category;
  60. html += "<li data-role='list-divider'>" + currentCategory + "</li>";
  61. }
  62. html += "<li>";
  63. html += "<a href='scheduledTask.html?id=" + task.Id + "'>";
  64. html += "<h3>" + task.Name + "</h3>";
  65. if (task.State == "Idle") {
  66. if (task.LastExecutionResult) {
  67. var text = "Last ran " + humane_date(task.LastExecutionResult.EndTimeUtc) + ', taking ' + humane_elapsed(task.LastExecutionResult.StartTimeUtc, task.LastExecutionResult.EndTimeUtc);
  68. if (task.LastExecutionResult.Status == "Failed") {
  69. text += " <span style='color:#FF0000;'>(failed)</span>";
  70. }
  71. else if (task.LastExecutionResult.Status == "Cancelled") {
  72. text += " <span style='color:#0026FF;'>(cancelled)</span>";
  73. }
  74. else if (task.LastExecutionResult.Status == "Aborted") {
  75. text += " <span style='color:#FF0000;'>(Aborted by server shutdown)</span>";
  76. }
  77. html += "<p>" + text + "</p>";
  78. }
  79. html += "<a href='#' data-icon='play' onclick='ScheduledTasksPage.startTask(\"" + task.Id + "\");'>Start</a>";
  80. }
  81. else if (task.State == "Running") {
  82. var progress = Math.round(task.CurrentProgressPercentage || 0);
  83. html += '<p><progress max="100" value="' + progress + '" title="' + progress + '%">';
  84. html += '' + progress + '%';
  85. html += '</progress>';
  86. html += "<span style='color:#009F00;margin-left:5px;'>" + progress + "%</span>";
  87. html += '</p>';
  88. html += "<a href='#' data-icon='stop' onclick='ScheduledTasksPage.stopTask(\"" + task.Id + "\");'>Stop</a>";
  89. } else {
  90. html += "<p style='color:#FF0000;'>Stopping</p>";
  91. html += "<a href='#' data-icon='play' style='visibility:hidden;'>Start</a>";
  92. }
  93. html += "</a>";
  94. html += "</li>";
  95. }
  96. $('#ulScheduledTasks', page).html(html).listview('refresh');
  97. },
  98. startTask: function (id) {
  99. Dashboard.showLoadingMsg();
  100. ApiClient.startScheduledTask(id).done(function (result) {
  101. ScheduledTasksPage.reloadList();
  102. });
  103. },
  104. stopTask: function (id) {
  105. Dashboard.showLoadingMsg();
  106. ApiClient.stopScheduledTask(id).done(function (result) {
  107. ScheduledTasksPage.reloadList();
  108. });
  109. }
  110. };
  111. $(document).on('pageshow', "#scheduledTasksPage", ScheduledTasksPage.onPageShow).on('pagehide', "#scheduledTasksPage", ScheduledTasksPage.onPageHide);