mailcow.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. $(document).ready(function() {
  2. // mailcow alert box generator
  3. window.mailcow_alert_box = function(message, type) {
  4. msg = $('<span/>').text(message).text();
  5. if (type == 'danger') {
  6. auto_hide = 0;
  7. $('#' + localStorage.getItem("add_modal")).modal('show');
  8. localStorage.removeItem("add_modal");
  9. } else {
  10. auto_hide = 5000;
  11. }
  12. $.notify({message: msg},{z_index: 20000, delay: auto_hide, type: type,placement: {from: "bottom",align: "right"},animate: {enter: 'animated fadeInUp',exit: 'animated fadeOutDown'}});
  13. }
  14. // https://stackoverflow.com/questions/4399005/implementing-jquerys-shake-effect-with-animate
  15. function shake(div,interval=100,distance=10,times=4) {
  16. $(div).css('position','relative');
  17. for(var iter=0;iter<(times+1);iter++){
  18. $(div).animate({ left: ((iter%2==0 ? distance : distance*-1))}, interval);
  19. }
  20. $(div).animate({ left: 0},interval);
  21. }
  22. // form cache
  23. $('[data-cached-form="true"]').formcache({key: $(this).data('id')});
  24. // tooltips
  25. $(function () {
  26. $('[data-toggle="tooltip"]').tooltip()
  27. });
  28. // remember last navigation pill
  29. (function () {
  30. 'use strict';
  31. if ($('a[data-toggle="tab"]').length) {
  32. $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
  33. if ($(this).data('dont-remember') == 1) {
  34. return true;
  35. }
  36. var id = $(this).parents('[role="tablist"]').attr('id');
  37. var key = 'lastTag';
  38. if (id) {
  39. key += ':' + id;
  40. }
  41. localStorage.setItem(key, $(e.target).attr('href'));
  42. });
  43. $('[role="tablist"]').each(function (idx, elem) {
  44. var id = $(elem).attr('id');
  45. var key = 'lastTag';
  46. if (id) {
  47. key += ':' + id;
  48. }
  49. var lastTab = localStorage.getItem(key);
  50. if (lastTab) {
  51. $('[href="' + lastTab + '"]').tab('show');
  52. }
  53. });
  54. }
  55. })();
  56. // IE fix to hide scrollbars when table body is empty
  57. $('tbody').filter(function (index) {
  58. return $(this).children().length < 1;
  59. }).remove();
  60. // selectpicker
  61. $('select').selectpicker();
  62. // haveibeenpwned?
  63. $('[data-hibp]').after('<p class="small haveibeenpwned">↪ Check against haveibeenpwned.com</p><span class="hibp-out"></span>');
  64. $('[data-hibp]').on('input', function() {
  65. out_field = $(this).next('.haveibeenpwned').next('.hibp-out').text('').attr('class', 'hibp-out');
  66. });
  67. $('.haveibeenpwned:not(.task-running)').on('click', function() {
  68. var hibp_field = $(this)
  69. $(hibp_field).addClass('task-running');
  70. var hibp_result = $(hibp_field).next('.hibp-out')
  71. var password_field = $(this).prev('[data-hibp]')
  72. if ($(password_field).val() == '') {
  73. shake(password_field);
  74. }
  75. else {
  76. $(hibp_result).attr('class', 'hibp-out label label-info');
  77. $(hibp_result).text(lang_footer.loading);
  78. var password_digest = $.sha1($(password_field).val())
  79. var digest_five = password_digest.substring(0, 5).toUpperCase();
  80. var queryURL = "https://api.pwnedpasswords.com/range/" + digest_five;
  81. var compl_digest = password_digest.substring(5, 41).toUpperCase();
  82. $.ajax({
  83. url: queryURL,
  84. type: 'GET',
  85. success: function(res) {
  86. if (res.search(compl_digest) > -1){
  87. $(hibp_result).removeClass('label label-info').addClass('label label-danger');
  88. $(hibp_result).text(lang_footer.hibp_nok)
  89. } else {
  90. $(hibp_result).removeClass('label label-info').addClass('label label-success');
  91. $(hibp_result).text(lang_footer.hibp_ok)
  92. }
  93. $(hibp_field).removeClass('task-running');
  94. },
  95. error: function(xhr, status, error) {
  96. $(hibp_result).removeClass('label label-info').addClass('label label-warning');
  97. $(hibp_result).text('API error: ' + xhr.responseText)
  98. $(hibp_field).removeClass('task-running');
  99. }
  100. });
  101. }
  102. });
  103. // Disable disallowed inputs
  104. $('[data-acl="0"]').each(function(event){
  105. if ($(this).is("a")) {
  106. $(this).removeAttr("data-toggle");
  107. $(this).removeAttr("data-target");
  108. }
  109. if ($(this).hasClass('btn-group')) {
  110. $(this).find('a').each(function(){
  111. $(this).removeClass('dropdown-toggle')
  112. .removeAttr('data-toggle')
  113. .removeAttr('id')
  114. .attr("disabled", true);
  115. $(this).click(function(event) {
  116. event.preventDefault();
  117. return;
  118. });
  119. });
  120. $(this).find('button').each(function() {
  121. $(this).attr("disabled", true);
  122. });
  123. } else if ($(this).hasClass('input-group')) {
  124. $(this).find('input').each(function() {
  125. $(this).removeClass('dropdown-toggle')
  126. .removeAttr('data-toggle')
  127. .attr("disabled", true);
  128. $(this).click(function(event) {
  129. event.preventDefault();
  130. });
  131. });
  132. $(this).find('button').each(function() {
  133. $(this).attr("disabled", true);
  134. });
  135. } else if ($(this).hasClass('btn')) {
  136. $(this).attr("disabled", true);
  137. } else if ($(this).attr('data-provide', 'slider')) {
  138. $(this).slider("disable");
  139. }
  140. $(this).data("toggle", "tooltip");
  141. $(this).attr("title", lang_acl.prohibited);
  142. $(this).tooltip();
  143. });
  144. // disable submit after submitting form (not API driven buttons)
  145. $('form').submit(function() {
  146. if ($('form button[type="submit"]').data('submitted') == '1') {
  147. return false;
  148. } else {
  149. $(this).find('button[type="submit"]').first().text(lang_footer.loading);
  150. $('form button[type="submit"]').attr('data-submitted', '1');
  151. function disableF5(e) { if ((e.which || e.keyCode) == 116 || (e.which || e.keyCode) == 82) e.preventDefault(); };
  152. $(document).on("keydown", disableF5);
  153. }
  154. });
  155. // Textarea line numbers
  156. $(".textarea-code").numberedtextarea({allowTabChar: true});
  157. // trigger container restart
  158. $('#RestartContainer').on('show.bs.modal', function(e) {
  159. var container = $(e.relatedTarget).data('container');
  160. $('#containerName').text(container);
  161. $('#triggerRestartContainer').click(function(){
  162. $(this).prop("disabled",true);
  163. $(this).html('<span class="glyphicon glyphicon-refresh glyphicon-spin"></span> ');
  164. $('#statusTriggerRestartContainer').html(lang_footer.restarting_container);
  165. $.ajax({
  166. method: 'get',
  167. url: '/inc/ajax/container_ctrl.php',
  168. timeout: docker_timeout,
  169. data: {
  170. 'service': container,
  171. 'action': 'restart'
  172. }
  173. })
  174. .always( function (data, status) {
  175. $('#statusTriggerRestartContainer').append(data);
  176. var htmlResponse = $.parseHTML(data)
  177. if ($(htmlResponse).find('span').hasClass('text-success')) {
  178. $('#triggerRestartContainer').html('<span class="glyphicon glyphicon-ok"></span> ');
  179. setTimeout(function(){
  180. $('#RestartContainer').modal('toggle');
  181. window.location = window.location.href.split("#")[0];
  182. }, 1200);
  183. } else {
  184. $('#triggerRestartContainer').html('<span class="glyphicon glyphicon-remove"></span> ');
  185. }
  186. })
  187. });
  188. })
  189. });