014-mailcow.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. $(".generate_password").click(function( event ) {
  15. event.preventDefault();
  16. $('[data-hibp]').trigger('input');
  17. if (typeof($(this).closest("form").data('pwgen-length')) == "number") {
  18. var random_passwd = GPW.pronounceable($(this).closest("form").data('pwgen-length'))
  19. }
  20. else {
  21. var random_passwd = GPW.pronounceable(8)
  22. }
  23. $(this).closest("form").find('[data-pwgen-field]').attr('type', 'text');
  24. $(this).closest("form").find('[data-pwgen-field]').val(random_passwd);
  25. });
  26. function str_rot13(str) {
  27. return (str + '').replace(/[a-z]/gi, function(s){
  28. return String.fromCharCode(s.charCodeAt(0) + (s.toLowerCase() < 'n' ? 13 : -13))
  29. })
  30. }
  31. $(".rot-enc").html(function(){
  32. return str_rot13($(this).html())
  33. });
  34. // https://stackoverflow.com/questions/4399005/implementing-jquerys-shake-effect-with-animate
  35. function shake(div,interval,distance,times) {
  36. if(typeof interval === 'undefined') {
  37. interval = 100;
  38. }
  39. if(typeof distance === 'undefined') {
  40. distance = 10;
  41. }
  42. if(typeof times === 'undefined') {
  43. times = 4;
  44. }
  45. $(div).css('position','relative');
  46. for(var iter=0;iter<(times+1);iter++){
  47. $(div).animate({ left: ((iter%2==0 ? distance : distance*-1))}, interval);
  48. }
  49. $(div).animate({ left: 0},interval);
  50. }
  51. // form cache
  52. $('[data-cached-form="true"]').formcache({key: $(this).data('id')});
  53. // tooltips
  54. $(function () {
  55. $('[data-toggle="tooltip"]').tooltip()
  56. });
  57. // remember last navigation pill
  58. (function () {
  59. 'use strict';
  60. if ($('a[data-toggle="tab"]').length) {
  61. $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
  62. if ($(this).data('dont-remember') == 1) {
  63. return true;
  64. }
  65. var id = $(this).parents('[role="tablist"]').attr('id');
  66. var key = 'lastTag';
  67. if (id) {
  68. key += ':' + id;
  69. }
  70. localStorage.setItem(key, $(e.target).attr('href'));
  71. });
  72. $('[role="tablist"]').each(function (idx, elem) {
  73. var id = $(elem).attr('id');
  74. var key = 'lastTag';
  75. if (id) {
  76. key += ':' + id;
  77. }
  78. var lastTab = localStorage.getItem(key);
  79. if (lastTab) {
  80. $('[href="' + lastTab + '"]').tab('show');
  81. }
  82. });
  83. }
  84. })();
  85. // IE fix to hide scrollbars when table body is empty
  86. $('tbody').filter(function (index) {
  87. return $(this).children().length < 1;
  88. }).remove();
  89. // selectpicker
  90. $('select').selectpicker({
  91. 'styleBase': 'btn btn-xs-lg',
  92. 'noneSelectedText': lang_footer.nothing_selected
  93. });
  94. // haveibeenpwned and passwd policy
  95. $.ajax({
  96. url: '/api/v1/get/passwordpolicy/html',
  97. type: 'GET',
  98. success: function(res) {
  99. $(".hibp-out").after(res);
  100. }
  101. });
  102. $('[data-hibp]').after('<p class="small haveibeenpwned"><i class="bi bi-shield-fill-exclamation"></i> ' + lang_footer.hibp_check + '</p><span class="hibp-out"></span>');
  103. $('[data-hibp]').on('input', function() {
  104. out_field = $(this).next('.haveibeenpwned').next('.hibp-out').text('').attr('class', 'hibp-out');
  105. });
  106. $('.haveibeenpwned:not(.task-running)').on('click', function() {
  107. var hibp_field = $(this)
  108. $(hibp_field).addClass('task-running');
  109. var hibp_result = $(hibp_field).next('.hibp-out')
  110. var password_field = $(this).prev('[data-hibp]')
  111. if ($(password_field).val() == '') {
  112. shake(password_field);
  113. }
  114. else {
  115. $(hibp_result).attr('class', 'hibp-out label label-info');
  116. $(hibp_result).text(lang_footer.loading);
  117. var password_digest = $.sha1($(password_field).val())
  118. var digest_five = password_digest.substring(0, 5).toUpperCase();
  119. var queryURL = "https://api.pwnedpasswords.com/range/" + digest_five;
  120. var compl_digest = password_digest.substring(5, 41).toUpperCase();
  121. $.ajax({
  122. url: queryURL,
  123. type: 'GET',
  124. success: function(res) {
  125. if (res.search(compl_digest) > -1){
  126. $(hibp_result).removeClass('label label-info').addClass('label label-danger');
  127. $(hibp_result).text(lang_footer.hibp_nok)
  128. } else {
  129. $(hibp_result).removeClass('label label-info').addClass('label label-success');
  130. $(hibp_result).text(lang_footer.hibp_ok)
  131. }
  132. $(hibp_field).removeClass('task-running');
  133. },
  134. error: function(xhr, status, error) {
  135. $(hibp_result).removeClass('label label-info').addClass('label label-warning');
  136. $(hibp_result).text('API error: ' + xhr.responseText)
  137. $(hibp_field).removeClass('task-running');
  138. }
  139. });
  140. }
  141. });
  142. // Disable disallowed inputs
  143. $('[data-acl="0"]').each(function(event){
  144. if ($(this).is("a")) {
  145. $(this).removeAttr("data-toggle");
  146. $(this).removeAttr("data-target");
  147. $(this).removeAttr("data-action");
  148. $(this).click(function(event) {
  149. event.preventDefault();
  150. });
  151. }
  152. if ($(this).is("select")) {
  153. $(this).selectpicker('destroy');
  154. $(this).replaceWith(function() {
  155. return '<label class="control-label"><b>' + this.innerText + '</b></label>';
  156. });
  157. }
  158. if ($(this).hasClass('btn-group')) {
  159. $(this).find('a').each(function(){
  160. $(this).removeClass('dropdown-toggle')
  161. .removeAttr('data-toggle')
  162. .removeAttr('data-target')
  163. .removeAttr('data-action')
  164. .removeAttr('id')
  165. .attr("disabled", true);
  166. $(this).click(function(event) {
  167. event.preventDefault();
  168. return;
  169. });
  170. });
  171. $(this).find('button').each(function() {
  172. $(this).attr("disabled", true);
  173. });
  174. } else if ($(this).hasClass('input-group')) {
  175. $(this).find('input').each(function() {
  176. $(this).removeClass('dropdown-toggle')
  177. .removeAttr('data-toggle')
  178. .attr("disabled", true);
  179. $(this).click(function(event) {
  180. event.preventDefault();
  181. });
  182. });
  183. $(this).find('button').each(function() {
  184. $(this).attr("disabled", true);
  185. });
  186. } else if ($(this).hasClass('form-group')) {
  187. $(this).find('input').each(function() {
  188. $(this).attr("disabled", true);
  189. });
  190. } else if ($(this).hasClass('btn')) {
  191. $(this).attr("disabled", true);
  192. } else if ($(this).attr('data-provide') == 'slider') {
  193. $(this).attr('disabled', true);
  194. } else if ($(this).is(':checkbox')) {
  195. $(this).attr("disabled", true);
  196. }
  197. $(this).data("toggle", "tooltip");
  198. $(this).attr("title", lang_acl.prohibited);
  199. $(this).tooltip();
  200. });
  201. // disable submit after submitting form (not API driven buttons)
  202. $('form').submit(function() {
  203. if ($('form button[type="submit"]').data('submitted') == '1') {
  204. return false;
  205. } else {
  206. $(this).find('button[type="submit"]').first().text(lang_footer.loading);
  207. $('form button[type="submit"]').attr('data-submitted', '1');
  208. function disableF5(e) { if ((e.which || e.keyCode) == 116 || (e.which || e.keyCode) == 82) e.preventDefault(); };
  209. $(document).on("keydown", disableF5);
  210. }
  211. });
  212. // Textarea line numbers
  213. $(".textarea-code").numberedtextarea({allowTabChar: true});
  214. // trigger container restart
  215. $('#RestartContainer').on('show.bs.modal', function(e) {
  216. var container = $(e.relatedTarget).data('container');
  217. $('#containerName').text(container);
  218. $('#triggerRestartContainer').click(function(){
  219. $(this).prop("disabled",true);
  220. $(this).html('<i class="bi bi-arrow-repeat icon-spin"></i> ');
  221. $('#statusTriggerRestartContainer').html(lang_footer.restarting_container);
  222. $.ajax({
  223. method: 'get',
  224. url: '/inc/ajax/container_ctrl.php',
  225. timeout: docker_timeout,
  226. data: {
  227. 'service': container,
  228. 'action': 'restart'
  229. }
  230. })
  231. .always( function (data, status) {
  232. $('#statusTriggerRestartContainer').append(data);
  233. var htmlResponse = $.parseHTML(data)
  234. if ($(htmlResponse).find('span').hasClass('text-success')) {
  235. $('#triggerRestartContainer').html('<i class="bi bi-check-lg"></i> ');
  236. setTimeout(function(){
  237. $('#RestartContainer').modal('toggle');
  238. window.location = window.location.href.split("#")[0];
  239. }, 1200);
  240. } else {
  241. $('#triggerRestartContainer').html('<i class="bi bi-slash-lg"></i> ');
  242. }
  243. })
  244. });
  245. })
  246. // responsive tabs
  247. $('.responsive-tabs').tabCollapse({
  248. tabsClass: 'hidden-xs',
  249. accordionClass: 'js-tabcollapse-panel-group visible-xs'
  250. });
  251. $(document).on("shown.bs.collapse shown.bs.tab", function (e) {
  252. var target = $(e.target);
  253. if($(window).width() <= 767) {
  254. var offset = target.offset().top - 112;
  255. $("html, body").stop().animate({
  256. scrollTop: offset
  257. }, 300);
  258. }
  259. if(target.hasClass('panel-collapse')){
  260. var id = e.target.id.replace(/-collapse$/g, '');
  261. if(id){
  262. localStorage.setItem('lastTag', '#'+id);
  263. }
  264. }
  265. });
  266. });