You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
7.2 KiB

10 years ago
  1. $(function () {
  2. var base = document.getElementById("base-url").getAttribute("href").slice(0, -1);
  3. $(".my-checks-name").click(function() {
  4. var code = $(this).closest("tr.checks-row").attr("id");
  5. var url = base + "/checks/" + code + "/name/";
  6. $("#update-name-form").attr("action", url);
  7. $("#update-name-input").val(this.dataset.name);
  8. $("#update-tags-input").val(this.dataset.tags);
  9. $("#update-desc-input").val(this.dataset.desc);
  10. $('#update-name-modal').modal("show");
  11. $("#update-name-input").focus();
  12. return false;
  13. });
  14. $(".integrations").tooltip({
  15. container: "body",
  16. selector: "span",
  17. title: function() {
  18. var idx = $(this).index();
  19. return $("#ch-" + idx).data("title");
  20. }
  21. });
  22. $(".integrations").on("click", "span", function() {
  23. var isOff = $(this).toggleClass("off").hasClass("off");
  24. var token = $('input[name=csrfmiddlewaretoken]').val();
  25. var idx = $(this).index();
  26. var checkCode = $(this).closest("tr.checks-row").attr("id");
  27. var channelCode = $("#ch-" + idx).data("code");
  28. var url = base + "/checks/" + checkCode + "/channels/" + channelCode + "/enabled";
  29. $.ajax({
  30. url: url,
  31. type: "post",
  32. headers: {"X-CSRFToken": token},
  33. data: {"state": isOff ? "off" : "on"}
  34. });
  35. return false;
  36. });
  37. $(".last-ping").on("click", function() {
  38. if (this.innerText == "Never") {
  39. return false;
  40. }
  41. $("#ping-details-body").text("Updating...");
  42. $('#ping-details-modal').modal("show");
  43. var code = $(this).closest("tr.checks-row").attr("id");
  44. var lastPingUrl = base + "/checks/" + code + "/last_ping/";
  45. $.get(lastPingUrl, function(data) {
  46. $("#ping-details-body" ).html(data);
  47. });
  48. var logUrl = base + "/checks/" + code + "/log/";
  49. $("#ping-details-log").attr("href", logUrl);
  50. return false;
  51. });
  52. function applyFilters() {
  53. // Make a list of currently checked tags:
  54. var checked = [];
  55. var qs = [];
  56. $("#my-checks-tags .checked").each(function(index, el) {
  57. checked.push(el.textContent);
  58. qs.push({"name": "tag", "value": el.textContent});
  59. });
  60. var search = $("#search").val().toLowerCase();
  61. if (search) {
  62. qs.push({"name": "search", "value": search});
  63. }
  64. // Update hash
  65. if (window.history && window.history.replaceState) {
  66. var url = $("#checks-table").data("list-url");
  67. if (qs.length) {
  68. url += "?" + $.param(qs);
  69. }
  70. window.history.replaceState({}, "", url);
  71. }
  72. // No checked tags and no search string: show all
  73. if (checked.length == 0 && !search) {
  74. $("#checks-table tr.checks-row").show();
  75. return;
  76. }
  77. function applySingle(index, element) {
  78. if (search) {
  79. var code = element.getAttribute("id");
  80. var name = $(".my-checks-name", element).attr("data-name").toLowerCase();
  81. if (name.indexOf(search) == -1 && code.indexOf(search) == -1) {
  82. $(element).hide();
  83. return;
  84. }
  85. }
  86. if (checked.length) {
  87. // use attr(), as data() tries converting strings to JS types:
  88. // (e.g., "123" -> 123)
  89. var tags = $(".my-checks-name", element).attr("data-tags").split(" ");
  90. for (var i=0, tag; tag=checked[i]; i++) {
  91. if (tags.indexOf(tag) == -1) {
  92. $(element).hide();
  93. return;
  94. }
  95. }
  96. }
  97. $(element).show();
  98. }
  99. // For each row, see if it needs to be shown or hidden
  100. $("#checks-table tr.checks-row").each(applySingle);
  101. }
  102. // User clicks on tags: apply filters
  103. $("#my-checks-tags div").click(function() {
  104. $(this).toggleClass('checked');
  105. applyFilters();
  106. });
  107. // User changes the search string: apply filters
  108. $("#search").keyup(applyFilters);
  109. $(".show-log").click(function(e) {
  110. var code = $(this).closest("tr.checks-row").attr("id");
  111. var url = base + "/checks/" + code + "/details/";
  112. window.location = url;
  113. return false;
  114. });
  115. $('[data-toggle="tooltip"]').tooltip({
  116. html: true,
  117. container: "body",
  118. title: function() {
  119. var cssClasses = this.getAttribute("class");
  120. if (cssClasses.indexOf("icon-new") > -1)
  121. return "New. Has never received a ping.";
  122. if (cssClasses.indexOf("icon-paused") > -1)
  123. return "Monitoring paused. Ping to resume.";
  124. if (cssClasses.indexOf("sort-name") > -1)
  125. return "Sort by name<br />(but failed always first)";
  126. if (cssClasses.indexOf("sort-last-ping") > -1)
  127. return "Sort by last ping<br />(but failed always first)";
  128. }
  129. });
  130. // Schedule refresh to run every 3s when tab is visible and user
  131. // is active, every 60s otherwise
  132. var lastStatus = {};
  133. var lastPing = {};
  134. var statusUrl = $("#checks-table").data("status-url");
  135. function refreshStatus() {
  136. $.ajax({
  137. url: statusUrl,
  138. dataType: "json",
  139. timeout: 2000,
  140. success: function(data) {
  141. for(var i=0, el; el=data.details[i]; i++) {
  142. if (lastStatus[el.code] != el.status) {
  143. lastStatus[el.code] = el.status;
  144. $("#" + el.code + " span.status").attr("class", "status icon-" + el.status);
  145. $("#" + el.code + " .pause-li").toggleClass("disabled", el.status == "paused");
  146. }
  147. if (lastPing[el.code] != el.last_ping) {
  148. lastPing[el.code] = el.last_ping;
  149. $("#lpd-" + el.code).html(el.last_ping);
  150. }
  151. }
  152. $("#my-checks-tags div").each(function(a) {
  153. var status = data.tags[this.innerText];
  154. if (lastStatus[this.innerText] == status)
  155. return;
  156. $(this).removeClass("up grace down").addClass(status);
  157. lastStatus[this.innerText] = status;
  158. });
  159. if (document.title != data.title) {
  160. document.title = data.title;
  161. }
  162. }
  163. });
  164. }
  165. // Schedule regular status updates:
  166. if (statusUrl) {
  167. adaptiveSetInterval(refreshStatus);
  168. }
  169. // Copy to clipboard
  170. var clipboard = new Clipboard('button.copy-link');
  171. $("button.copy-link").mouseout(function(e) {
  172. setTimeout(function() {
  173. e.target.textContent = "copy";
  174. }, 300);
  175. })
  176. clipboard.on('success', function(e) {
  177. e.trigger.textContent = "copied!";
  178. e.clearSelection();
  179. });
  180. clipboard.on('error', function(e) {
  181. var text = e.trigger.getAttribute("data-clipboard-text");
  182. prompt("Press Ctrl+C to select:", text)
  183. });
  184. });