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.

270 lines
8.2 KiB

10 years ago
10 years ago
9 years ago
10 years ago
  1. $(function () {
  2. $(".my-checks-name").click(function() {
  3. $("#update-name-form").attr("action", this.dataset.url);
  4. $("#update-name-input").val(this.dataset.name);
  5. $("#update-tags-input").val(this.dataset.tags);
  6. $('#update-name-modal').modal("show");
  7. $("#update-name-input").focus();
  8. return false;
  9. });
  10. var MINUTE = {name: "minute", nsecs: 60};
  11. var HOUR = {name: "hour", nsecs: MINUTE.nsecs * 60};
  12. var DAY = {name: "day", nsecs: HOUR.nsecs * 24};
  13. var WEEK = {name: "week", nsecs: DAY.nsecs * 7};
  14. var UNITS = [WEEK, DAY, HOUR, MINUTE];
  15. var secsToText = function(total) {
  16. var remainingSeconds = Math.floor(total);
  17. var result = "";
  18. for (var i=0, unit; unit=UNITS[i]; i++) {
  19. if (unit === WEEK && remainingSeconds % unit.nsecs != 0) {
  20. // Say "8 days" instead of "1 week 1 day"
  21. continue
  22. }
  23. var count = Math.floor(remainingSeconds / unit.nsecs);
  24. remainingSeconds = remainingSeconds % unit.nsecs;
  25. if (count == 1) {
  26. result += "1 " + unit.name + " ";
  27. }
  28. if (count > 1) {
  29. result += count + " " + unit.name + "s ";
  30. }
  31. }
  32. return result;
  33. }
  34. var periodSlider = document.getElementById("period-slider");
  35. noUiSlider.create(periodSlider, {
  36. start: [20],
  37. connect: "lower",
  38. range: {
  39. 'min': [60, 60],
  40. '33%': [3600, 3600],
  41. '66%': [86400, 86400],
  42. '83%': [604800, 604800],
  43. 'max': 2592000,
  44. },
  45. pips: {
  46. mode: 'values',
  47. values: [60, 1800, 3600, 43200, 86400, 604800, 2592000],
  48. density: 4,
  49. format: {
  50. to: secsToText,
  51. from: function() {}
  52. }
  53. }
  54. });
  55. periodSlider.noUiSlider.on("update", function(a, b, value) {
  56. var rounded = Math.round(value);
  57. $("#period-slider-value").text(secsToText(rounded));
  58. $("#update-timeout-timeout").val(rounded);
  59. });
  60. var graceSlider = document.getElementById("grace-slider");
  61. noUiSlider.create(graceSlider, {
  62. start: [20],
  63. connect: "lower",
  64. range: {
  65. 'min': [60, 60],
  66. '33%': [3600, 3600],
  67. '66%': [86400, 86400],
  68. '83%': [604800, 604800],
  69. 'max': 2592000,
  70. },
  71. pips: {
  72. mode: 'values',
  73. values: [60, 1800, 3600, 43200, 86400, 604800, 2592000],
  74. density: 4,
  75. format: {
  76. to: secsToText,
  77. from: function() {}
  78. }
  79. }
  80. });
  81. graceSlider.noUiSlider.on("update", function(a, b, value) {
  82. var rounded = Math.round(value);
  83. $("#grace-slider-value").text(secsToText(rounded));
  84. $("#update-timeout-grace").val(rounded);
  85. });
  86. function showSimple() {
  87. $("#update-timeout-form").show();
  88. $("#update-cron-form").hide();
  89. }
  90. function showCron() {
  91. $("#update-timeout-form").hide();
  92. $("#update-cron-form").show();
  93. }
  94. var currentPreviewHash = "";
  95. function updateCronPreview() {
  96. var schedule = $("#schedule").val();
  97. var tz = $("#tz").val();
  98. var hash = schedule + tz;
  99. // Don't try preview with empty values, or if values have not changed
  100. if (!schedule || !tz || hash == currentPreviewHash)
  101. return;
  102. // OK, we're good
  103. currentPreviewHash = hash;
  104. $("#cron-preview-title").text("Updating...");
  105. var token = $('input[name=csrfmiddlewaretoken]').val();
  106. $.ajax({
  107. url: "/checks/cron_preview/",
  108. type: "post",
  109. headers: {"X-CSRFToken": token},
  110. data: {schedule: schedule, tz: tz},
  111. success: function(data) {
  112. if (hash != currentPreviewHash) {
  113. return; // ignore stale results
  114. }
  115. $("#cron-preview" ).html(data);
  116. var haveError = $("#invalid-arguments").size() > 0;
  117. $("#update-cron-submit").prop("disabled", haveError);
  118. }
  119. });
  120. }
  121. $(".timeout-grace").click(function() {
  122. $("#update-timeout-form").attr("action", this.dataset.url);
  123. $("#update-cron-form").attr("action", this.dataset.url);
  124. // Simple
  125. periodSlider.noUiSlider.set(this.dataset.timeout);
  126. graceSlider.noUiSlider.set(this.dataset.grace);
  127. // Cron
  128. currentPreviewHash = "";
  129. $("#cron-preview").html("<p>Updating...</p>");
  130. $("#schedule").val(this.dataset.schedule);
  131. document.getElementById("tz").selectize.setValue(this.dataset.tz);
  132. var minutes = parseInt(this.dataset.grace / 60);
  133. $("#update-timeout-grace-cron").val(minutes);
  134. updateCronPreview();
  135. this.dataset.kind == "simple" ? showSimple() : showCron();
  136. $('#update-timeout-modal').modal({"show":true, "backdrop":"static"});
  137. return false;
  138. });
  139. // Wire up events for Timeout/Cron forms
  140. $(".kind-simple").click(showSimple);
  141. $(".kind-cron").click(showCron);
  142. $("#schedule").on("keyup", updateCronPreview);
  143. $("#tz").selectize({onChange: updateCronPreview});
  144. $(".check-menu-remove").click(function() {
  145. $("#remove-check-form").attr("action", this.dataset.url);
  146. $(".remove-check-name").text(this.dataset.name);
  147. $('#remove-check-modal').modal("show");
  148. return false;
  149. });
  150. $(".last-ping").click(function() {
  151. $("#last-ping-body").text("Updating...");
  152. $('#last-ping-modal').modal("show");
  153. var token = $('input[name=csrfmiddlewaretoken]').val();
  154. $.ajax({
  155. url: this.dataset.url,
  156. type: "post",
  157. headers: {"X-CSRFToken": token},
  158. success: function(data) {
  159. $("#last-ping-body" ).html(data);
  160. }
  161. });
  162. return false;
  163. });
  164. $("#my-checks-tags button").click(function() {
  165. // .active has not been updated yet by bootstrap code,
  166. // so cannot use it
  167. $(this).toggleClass('checked');
  168. // Make a list of currently checked tags:
  169. var checked = [];
  170. $("#my-checks-tags button.checked").each(function(index, el) {
  171. checked.push(el.textContent);
  172. });
  173. // No checked tags: show all
  174. if (checked.length == 0) {
  175. $("#checks-table tr.checks-row").show();
  176. $("#checks-list > li").show();
  177. return;
  178. }
  179. function applyFilters(index, element) {
  180. var tags = $(".my-checks-name", element).data("tags").split(" ");
  181. for (var i=0, tag; tag=checked[i]; i++) {
  182. if (tags.indexOf(tag) == -1) {
  183. $(element).hide();
  184. return;
  185. }
  186. }
  187. $(element).show();
  188. }
  189. // Desktop: for each row, see if it needs to be shown or hidden
  190. $("#checks-table tr.checks-row").each(applyFilters);
  191. // Mobile: for each list item, see if it needs to be shown or hidden
  192. $("#checks-list > li").each(applyFilters);
  193. });
  194. $(".pause-check").click(function(e) {
  195. var url = e.target.getAttribute("data-url");
  196. $("#pause-form").attr("action", url).submit();
  197. return false;
  198. });
  199. $('[data-toggle="tooltip"]').tooltip();
  200. $(".usage-examples").click(function(e) {
  201. var a = e.target;
  202. var url = a.getAttribute("data-url");
  203. var email = a.getAttribute("data-email");
  204. $(".ex", "#show-usage-modal").text(url);
  205. $(".em", "#show-usage-modal").text(email);
  206. $("#show-usage-modal").modal("show");
  207. return false;
  208. });
  209. var clipboard = new Clipboard('button.copy-link');
  210. $("button.copy-link").mouseout(function(e) {
  211. setTimeout(function() {
  212. e.target.textContent = "copy";
  213. }, 300);
  214. })
  215. clipboard.on('success', function(e) {
  216. e.trigger.textContent = "copied!";
  217. e.clearSelection();
  218. });
  219. clipboard.on('error', function(e) {
  220. var text = e.trigger.getAttribute("data-clipboard-text");
  221. prompt("Press Ctrl+C to select:", text)
  222. });
  223. });