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.

221 lines
7.1 KiB

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