014-mailcow.js 7.8 KB

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