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.

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