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.

277 lines
8.6 KiB

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