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.

202 lines
5.8 KiB

10 years ago
9 years ago
10 years ago
10 years ago
9 years ago
10 years ago
  1. $(function () {
  2. var MINUTE = {name: "minute", nsecs: 60};
  3. var HOUR = {name: "hour", nsecs: MINUTE.nsecs * 60};
  4. var DAY = {name: "day", nsecs: HOUR.nsecs * 24};
  5. var WEEK = {name: "week", nsecs: DAY.nsecs * 7};
  6. var UNITS = [WEEK, DAY, HOUR, MINUTE];
  7. var secsToText = function(total) {
  8. var remainingSeconds = Math.floor(total);
  9. var result = "";
  10. for (var i=0, unit; unit=UNITS[i]; i++) {
  11. if (unit === WEEK && remainingSeconds % unit.nsecs != 0) {
  12. // Say "8 days" instead of "1 week 1 day"
  13. continue
  14. }
  15. var count = Math.floor(remainingSeconds / unit.nsecs);
  16. remainingSeconds = remainingSeconds % unit.nsecs;
  17. if (count == 1) {
  18. result += "1 " + unit.name + " ";
  19. }
  20. if (count > 1) {
  21. result += count + " " + unit.name + "s ";
  22. }
  23. }
  24. return result;
  25. }
  26. var periodSlider = document.getElementById("period-slider");
  27. noUiSlider.create(periodSlider, {
  28. start: [20],
  29. connect: "lower",
  30. range: {
  31. 'min': [60, 60],
  32. '33%': [3600, 3600],
  33. '66%': [86400, 86400],
  34. '83%': [604800, 604800],
  35. 'max': 2592000,
  36. },
  37. pips: {
  38. mode: 'values',
  39. values: [60, 1800, 3600, 43200, 86400, 604800, 2592000],
  40. density: 4,
  41. format: {
  42. to: secsToText,
  43. from: function() {}
  44. }
  45. }
  46. });
  47. periodSlider.noUiSlider.on("update", function(a, b, value) {
  48. var rounded = Math.round(value);
  49. $("#period-slider-value").text(secsToText(rounded));
  50. $("#update-timeout-timeout").val(rounded);
  51. });
  52. var graceSlider = document.getElementById("grace-slider");
  53. noUiSlider.create(graceSlider, {
  54. start: [20],
  55. connect: "lower",
  56. range: {
  57. 'min': [60, 60],
  58. '33%': [3600, 3600],
  59. '66%': [86400, 86400],
  60. '83%': [604800, 604800],
  61. 'max': 2592000,
  62. },
  63. pips: {
  64. mode: 'values',
  65. values: [60, 1800, 3600, 43200, 86400, 604800, 2592000],
  66. density: 4,
  67. format: {
  68. to: secsToText,
  69. from: function() {}
  70. }
  71. }
  72. });
  73. graceSlider.noUiSlider.on("update", function(a, b, value) {
  74. var rounded = Math.round(value);
  75. $("#grace-slider-value").text(secsToText(rounded));
  76. $("#update-timeout-grace").val(rounded);
  77. });
  78. $('[data-toggle="tooltip"]').tooltip();
  79. $(".my-checks-name").click(function() {
  80. var $this = $(this);
  81. $("#update-name-form").attr("action", $this.data("url"));
  82. $("#update-name-input").val($this.data("name"));
  83. $("#update-tags-input").val($this.data("tags"));
  84. $('#update-name-modal').modal("show");
  85. $("#update-name-input").focus();
  86. return false;
  87. });
  88. $(".timeout-grace").click(function() {
  89. var $this = $(this);
  90. $("#update-timeout-form").attr("action", $this.data("url"));
  91. periodSlider.noUiSlider.set($this.data("timeout"))
  92. graceSlider.noUiSlider.set($this.data("grace"))
  93. $('#update-timeout-modal').modal({"show":true, "backdrop":"static"});
  94. return false;
  95. });
  96. $(".check-menu-remove").click(function() {
  97. var $this = $(this);
  98. $("#remove-check-form").attr("action", $this.data("url"));
  99. $(".remove-check-name").text($this.data("name"));
  100. $('#remove-check-modal').modal("show");
  101. return false;
  102. });
  103. $("#my-checks-tags button").click(function() {
  104. // .active has not been updated yet by bootstrap code,
  105. // so cannot use it
  106. $(this).toggleClass('checked');
  107. // Make a list of currently checked tags:
  108. var checked = [];
  109. $("#my-checks-tags button.checked").each(function(index, el) {
  110. checked.push(el.textContent);
  111. });
  112. // No checked tags: show all
  113. if (checked.length == 0) {
  114. $("#checks-table tr.checks-row").show();
  115. $("#checks-list > li").show();
  116. return;
  117. }
  118. function applyFilters(index, element) {
  119. var tags = $(".my-checks-name", element).data("tags").split(" ");
  120. for (var i=0, tag; tag=checked[i]; i++) {
  121. if (tags.indexOf(tag) == -1) {
  122. $(element).hide();
  123. return;
  124. }
  125. }
  126. $(element).show();
  127. }
  128. // Desktop: for each row, see if it needs to be shown or hidden
  129. $("#checks-table tr.checks-row").each(applyFilters);
  130. // Mobile: for each list item, see if it needs to be shown or hidden
  131. $("#checks-list > li").each(applyFilters);
  132. });
  133. $(".pause-check").click(function(e) {
  134. var url = e.target.getAttribute("data-url");
  135. $("#pause-form").attr("action", url).submit();
  136. return false;
  137. });
  138. $(".usage-examples").click(function(e) {
  139. var a = e.target;
  140. var url = a.getAttribute("data-url");
  141. var email = a.getAttribute("data-email");
  142. $(".ex", "#show-usage-modal").text(url);
  143. $(".em", "#show-usage-modal").text(email);
  144. $("#show-usage-modal").modal("show");
  145. return false;
  146. });
  147. var clipboard = new Clipboard('button.copy-link');
  148. $("button.copy-link").mouseout(function(e) {
  149. setTimeout(function() {
  150. e.target.textContent = "copy";
  151. }, 300);
  152. })
  153. clipboard.on('success', function(e) {
  154. e.trigger.textContent = "copied!";
  155. e.clearSelection();
  156. });
  157. clipboard.on('error', function(e) {
  158. var text = e.trigger.getAttribute("data-clipboard-text");
  159. prompt("Press Ctrl+C to select:", text)
  160. });
  161. });