add_mail_acc.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. App.Actions.MAIL_ACC.enable_unlimited = function (elm, source_elm) {
  2. $(elm).data('checked', true);
  3. $(elm).data('prev_value', $(elm).val()); // save prev value in order to restore if needed
  4. $(elm).val(Alpine.store('globals').UNLIM_TRANSLATED_VALUE);
  5. $(elm).attr('disabled', true);
  6. $(source_elm).css('opacity', '1');
  7. };
  8. App.Actions.MAIL_ACC.disable_unlimited = function (elm, source_elm) {
  9. $(elm).data('checked', false);
  10. if ($(elm).data('prev_value') && $(elm).data('prev_value').trim() != '') {
  11. var prev_value = $(elm).data('prev_value').trim();
  12. $(elm).val(prev_value);
  13. if (Alpine.store('globals').isUnlimitedValue(prev_value)) {
  14. $(elm).val('0');
  15. }
  16. } else {
  17. if (Alpine.store('globals').isUnlimitedValue($(elm).val())) {
  18. $(elm).val('0');
  19. }
  20. }
  21. $(elm).attr('disabled', false);
  22. $(source_elm).css('opacity', '0.5');
  23. };
  24. App.Actions.MAIL_ACC.toggle_unlimited_feature = function (evt) {
  25. var elm = $(evt.target);
  26. var ref = elm.prev('.form-control');
  27. if (!$(ref).data('checked')) {
  28. App.Actions.MAIL_ACC.enable_unlimited(ref, elm);
  29. } else {
  30. App.Actions.MAIL_ACC.disable_unlimited(ref, elm);
  31. }
  32. };
  33. App.Listeners.MAIL_ACC.checkbox_unlimited_feature = function () {
  34. $('.unlim-trigger').on('click', App.Actions.MAIL_ACC.toggle_unlimited_feature);
  35. };
  36. App.Listeners.MAIL_ACC.init = function () {
  37. $('.unlim-trigger').each(function (i, elm) {
  38. var ref = $(elm).prev('.form-control');
  39. if (Alpine.store('globals').isUnlimitedValue($(ref).val())) {
  40. App.Actions.MAIL_ACC.enable_unlimited(ref, elm);
  41. } else {
  42. $(ref).data('prev_value', $(ref).val());
  43. App.Actions.MAIL_ACC.disable_unlimited(ref, elm);
  44. }
  45. });
  46. };
  47. //
  48. // Page entry point
  49. // Trigger listeners
  50. App.Listeners.MAIL_ACC.init();
  51. App.Listeners.MAIL_ACC.checkbox_unlimited_feature();
  52. $('#v_blackhole').on('click', function () {
  53. if ($('#v_blackhole').is(':checked')) {
  54. $('#v_fwd').prop('disabled', true);
  55. $('#v_fwd_for').prop('checked', true);
  56. $('#id_fwd_for').hide();
  57. } else {
  58. $('#v_fwd').prop('disabled', false);
  59. $('#id_fwd_for').show();
  60. }
  61. });
  62. $('form[name="v_quota"]').on('submit', function () {
  63. $('input:disabled').each(function (i, elm) {
  64. $(elm).attr('disabled', false);
  65. if (Alpine.store('globals').isUnlimitedValue($(elm).val())) {
  66. $(elm).val(Alpine.store('globals').UNLIM_VALUE);
  67. }
  68. });
  69. });
  70. App.Actions.MAIL_ACC.update_password_meter = function () {
  71. var password = $('input[name="v_password"]').val();
  72. var min_small = new RegExp(/^(?=.*[a-z]).+$/);
  73. var min_cap = new RegExp(/^(?=.*[A-Z]).+$/);
  74. var min_num = new RegExp(/^(?=.*\d).+$/);
  75. var min_length = 8;
  76. var score = 0;
  77. if (password.length >= min_length) {
  78. score = score + 1;
  79. }
  80. if (min_small.test(password)) {
  81. score = score + 1;
  82. }
  83. if (min_cap.test(password)) {
  84. score = score + 1;
  85. }
  86. if (min_num.test(password)) {
  87. score = score + 1;
  88. }
  89. $('.js-password-meter').val(score);
  90. };
  91. App.Listeners.MAIL_ACC.keypress_v_password = function () {
  92. var ref = $('input[name="v_password"]');
  93. ref.bind('keypress input', function (evt) {
  94. clearTimeout(window.frp_usr_tmt);
  95. window.frp_usr_tmt = setTimeout(function () {
  96. var elm = $(evt.target);
  97. App.Actions.MAIL_ACC.update_password_meter(elm, $(elm).val());
  98. }, 100);
  99. });
  100. };
  101. App.Listeners.MAIL_ACC.keypress_v_password();
  102. applyRandomPassword = function (min_length = 16) {
  103. var randomPassword = randomString(min_length);
  104. $('input[name=v_password]').val(randomPassword);
  105. if ($('input[name=v_password]').attr('type') == 'text')
  106. $('.js-password-output').text(randomPassword);
  107. else $('.js-password-output').text(Array(randomPassword.length + 1).join('*'));
  108. App.Actions.MAIL_ACC.update_password_meter();
  109. generate_mail_credentials();
  110. };
  111. generate_mail_credentials = function () {
  112. var div = $('.js-mail-info').clone();
  113. div.find('#mail_configuration').remove();
  114. var output = div.text();
  115. output = output.replace(/(?:\r\n|\r|\n|\t)/g, '|');
  116. output = output.replace(/ {2}/g, '');
  117. output = output.replace(/\|\|/g, '|');
  118. output = output.replace(/\|\|/g, '|');
  119. output = output.replace(/\|\|/g, '|');
  120. output = output.replace(/^\|+/g, '');
  121. output = output.replace(/\|$/, '');
  122. output = output.replace(/ $/, '');
  123. output = output.replace(/:\|/g, ': ');
  124. output = output.replace(/\|/g, '\n');
  125. $('.js-hidden-credentials').val(output);
  126. };
  127. $(document).ready(function () {
  128. $('.js-account-output').text($('input[name=v_account]').val());
  129. $('.js-password-output').text($('input[name=v_password]').val());
  130. generate_mail_credentials();
  131. $('input[name=v_account]').change(function () {
  132. $('.js-account-output').text($(this).val());
  133. generate_mail_credentials();
  134. });
  135. $('input[name=v_password]').change(function () {
  136. if ($('input[name=v_password]').attr('type') == 'text')
  137. $('.js-password-output').text($(this).val());
  138. else $('.js-password-output').text(Array($(this).val().length + 1).join('*'));
  139. generate_mail_credentials();
  140. });
  141. $('.toggle-psw-visibility-icon').click(function () {
  142. $('.js-password-output').text($('input[name=v_password]').val());
  143. generate_mail_credentials();
  144. });
  145. $('#mail_configuration').change(function (evt) {
  146. var opt = $(evt.target).find('option:selected');
  147. switch (opt.attr('v_type')) {
  148. case 'hostname':
  149. $('#td_imap_hostname').text(opt.attr('domain'));
  150. $('#td_smtp_hostname').text(opt.attr('domain'));
  151. break;
  152. case 'starttls':
  153. $('#td_imap_port').text('143');
  154. $('#td_imap_encryption').text('STARTTLS');
  155. $('#td_smtp_port').text('587');
  156. $('#td_smtp_encryption').text('STARTTLS');
  157. break;
  158. case 'ssl':
  159. $('#td_imap_port').text('993');
  160. $('#td_imap_encryption').text('SSL / TLS');
  161. $('#td_smtp_port').text('465');
  162. $('#td_smtp_encryption').text('SSL / TLS');
  163. break;
  164. case 'no_encryption':
  165. $('#td_imap_hostname').text(opt.attr('domain'));
  166. $('#td_smtp_hostname').text(opt.attr('domain'));
  167. $('#td_imap_port').text('143');
  168. $('#td_imap_encryption').text(opt.attr('no_encryption'));
  169. $('#td_smtp_port').text('25');
  170. $('#td_smtp_encryption').text(opt.attr('no_encryption'));
  171. break;
  172. }
  173. generate_mail_credentials();
  174. });
  175. });