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.

287 lines
9.0 KiB

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