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.

255 lines
8.1 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").click(function(e) {
  117. var code = $(this).closest("tr.checks-row").attr("id");
  118. $("#" + code + " span.status").attr("class", "status icon-paused");
  119. var url = base + "/checks/" + code + "/pause/";
  120. var token = $('input[name=csrfmiddlewaretoken]').val();
  121. $.ajax({
  122. url: url,
  123. type: "post",
  124. headers: {"X-CSRFToken": token}
  125. });
  126. return false;
  127. });
  128. $('[data-toggle="tooltip"]').tooltip({
  129. html: true,
  130. container: "body",
  131. title: function() {
  132. var cssClasses = this.getAttribute("class");
  133. if (cssClasses.indexOf("icon-new") > -1)
  134. return "New. Has never received a ping.";
  135. if (cssClasses.indexOf("icon-paused") > -1)
  136. return "Monitoring paused. Ping to resume.";
  137. if (cssClasses.indexOf("sort-name") > -1)
  138. return "Sort by name<br />(but failed always first)";
  139. if (cssClasses.indexOf("sort-last-ping") > -1)
  140. return "Sort by last ping<br />(but failed always first)";
  141. }
  142. });
  143. // Schedule refresh to run every 3s when tab is visible and user
  144. // is active, every 60s otherwise
  145. var lastStatus = {};
  146. var lastPing = {};
  147. var statusUrl = $("#checks-table").data("status-url");
  148. function refreshStatus() {
  149. $.ajax({
  150. url: statusUrl,
  151. dataType: "json",
  152. timeout: 2000,
  153. success: function(data) {
  154. for(var i=0, el; el=data.details[i]; i++) {
  155. if (lastStatus[el.code] != el.status) {
  156. lastStatus[el.code] = el.status;
  157. $("#" + el.code + " span.status").attr("class", "status icon-" + el.status);
  158. }
  159. if (lastPing[el.code] != el.last_ping) {
  160. lastPing[el.code] = el.last_ping;
  161. $("#lpd-" + el.code).html(el.last_ping);
  162. }
  163. }
  164. $("#my-checks-tags div").each(function(a) {
  165. var status = data.tags[this.innerText];
  166. if (lastStatus[this.innerText] == status)
  167. return;
  168. $(this).removeClass("up grace down").addClass(status);
  169. lastStatus[this.innerText] = status;
  170. });
  171. if (document.title != data.title) {
  172. document.title = data.title;
  173. }
  174. }
  175. });
  176. }
  177. // Schedule regular status updates:
  178. if (statusUrl) {
  179. adaptiveSetInterval(refreshStatus);
  180. }
  181. // Copy to clipboard
  182. var clipboard = new Clipboard('button.copy-link');
  183. $("button.copy-link").mouseout(function(e) {
  184. setTimeout(function() {
  185. e.target.textContent = "copy";
  186. }, 300);
  187. })
  188. clipboard.on('success', function(e) {
  189. e.trigger.textContent = "copied!";
  190. e.clearSelection();
  191. });
  192. clipboard.on('error', function(e) {
  193. var text = e.trigger.getAttribute("data-clipboard-text");
  194. prompt("Press Ctrl+C to select:", text)
  195. });
  196. // Configure Selectize for entering tags
  197. function divToOption() {
  198. return {value: this.textContent};
  199. }
  200. $("#update-tags-input").selectize({
  201. create: true,
  202. createOnBlur: true,
  203. delimiter: " ",
  204. labelField: "value",
  205. searchField: ["value"],
  206. hideSelected: true,
  207. highlight: false,
  208. options: $("#my-checks-tags div").map(divToOption).get()
  209. });
  210. });