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.

248 lines
7.5 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. $.post("/checks/cron_preview/", {schedule: schedule, tz: tz},
  106. function(data) {
  107. if (hash != currentPreviewHash) {
  108. return; // ignore stale results
  109. }
  110. $("#cron-preview" ).html(data);
  111. var haveError = $("#invalid-arguments").size() > 0;
  112. $("#update-cron-submit").prop("disabled", haveError);
  113. }
  114. );
  115. }
  116. $(".timeout-grace").click(function() {
  117. $("#update-timeout-form").attr("action", this.dataset.url);
  118. $("#update-cron-form").attr("action", this.dataset.url);
  119. // Simple
  120. periodSlider.noUiSlider.set(this.dataset.timeout);
  121. graceSlider.noUiSlider.set(this.dataset.grace);
  122. // Cron
  123. currentPreviewHash = "";
  124. $("#cron-preview").html("<p>Updating...</p>");
  125. $("#schedule").val(this.dataset.schedule);
  126. document.getElementById("tz").selectize.setValue(this.dataset.tz);
  127. var minutes = parseInt(this.dataset.grace / 60);
  128. $("#update-timeout-grace-cron").val(minutes);
  129. updateCronPreview();
  130. this.dataset.kind == "simple" ? showSimple() : showCron();
  131. $('#update-timeout-modal').modal({"show":true, "backdrop":"static"});
  132. return false;
  133. });
  134. // Wire up events for Timeout/Cron forms
  135. $(".kind-simple").click(showSimple);
  136. $(".kind-cron").click(showCron);
  137. $("#schedule").on("keyup", updateCronPreview);
  138. $("#tz").selectize({onChange: updateCronPreview});
  139. $(".check-menu-remove").click(function() {
  140. $("#remove-check-form").attr("action", this.dataset.url);
  141. $(".remove-check-name").text(this.dataset.name);
  142. $('#remove-check-modal').modal("show");
  143. return false;
  144. });
  145. $("#my-checks-tags button").click(function() {
  146. // .active has not been updated yet by bootstrap code,
  147. // so cannot use it
  148. $(this).toggleClass('checked');
  149. // Make a list of currently checked tags:
  150. var checked = [];
  151. $("#my-checks-tags button.checked").each(function(index, el) {
  152. checked.push(el.textContent);
  153. });
  154. // No checked tags: show all
  155. if (checked.length == 0) {
  156. $("#checks-table tr.checks-row").show();
  157. $("#checks-list > li").show();
  158. return;
  159. }
  160. function applyFilters(index, element) {
  161. var tags = $(".my-checks-name", element).data("tags").split(" ");
  162. for (var i=0, tag; tag=checked[i]; i++) {
  163. if (tags.indexOf(tag) == -1) {
  164. $(element).hide();
  165. return;
  166. }
  167. }
  168. $(element).show();
  169. }
  170. // Desktop: for each row, see if it needs to be shown or hidden
  171. $("#checks-table tr.checks-row").each(applyFilters);
  172. // Mobile: for each list item, see if it needs to be shown or hidden
  173. $("#checks-list > li").each(applyFilters);
  174. });
  175. $(".pause-check").click(function(e) {
  176. var url = e.target.getAttribute("data-url");
  177. $("#pause-form").attr("action", url).submit();
  178. return false;
  179. });
  180. $('[data-toggle="tooltip"]').tooltip();
  181. $(".usage-examples").click(function(e) {
  182. var a = e.target;
  183. var url = a.getAttribute("data-url");
  184. var email = a.getAttribute("data-email");
  185. $(".ex", "#show-usage-modal").text(url);
  186. $(".em", "#show-usage-modal").text(email);
  187. $("#show-usage-modal").modal("show");
  188. return false;
  189. });
  190. var clipboard = new Clipboard('button.copy-link');
  191. $("button.copy-link").mouseout(function(e) {
  192. setTimeout(function() {
  193. e.target.textContent = "copy";
  194. }, 300);
  195. })
  196. clipboard.on('success', function(e) {
  197. e.trigger.textContent = "copied!";
  198. e.clearSelection();
  199. });
  200. clipboard.on('error', function(e) {
  201. var text = e.trigger.getAttribute("data-clipboard-text");
  202. prompt("Press Ctrl+C to select:", text)
  203. });
  204. });