Нет описания

password-strength-meter.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* global wp, pwsL10n, wc_password_strength_meter_params */
  2. ( function( $ ) {
  3. 'use strict';
  4. /**
  5. * Password Strength Meter class.
  6. */
  7. var wc_password_strength_meter = {
  8. /**
  9. * Initialize strength meter actions.
  10. */
  11. init: function() {
  12. $( document.body )
  13. .on(
  14. 'keyup change',
  15. 'form.register #reg_password, form.checkout #account_password, ' +
  16. 'form.edit-account #password_1, form.lost_reset_password #password_1',
  17. this.strengthMeter
  18. );
  19. $( 'form.checkout #createaccount' ).trigger( 'change' );
  20. },
  21. /**
  22. * Strength Meter.
  23. */
  24. strengthMeter: function() {
  25. var wrapper = $( 'form.register, form.checkout, form.edit-account, form.lost_reset_password' ),
  26. submit = $( 'button[type="submit"]', wrapper ),
  27. field = $( '#reg_password, #account_password, #password_1', wrapper ),
  28. strength = 1,
  29. fieldValue = field.val(),
  30. stop_checkout = ! wrapper.is( 'form.checkout' ); // By default is disabled on checkout.
  31. wc_password_strength_meter.includeMeter( wrapper, field );
  32. strength = wc_password_strength_meter.checkPasswordStrength( wrapper, field );
  33. // Allow password strength meter stop checkout.
  34. if ( wc_password_strength_meter_params.stop_checkout ) {
  35. stop_checkout = true;
  36. }
  37. if (
  38. fieldValue.length > 0 &&
  39. strength < wc_password_strength_meter_params.min_password_strength &&
  40. -1 !== strength &&
  41. stop_checkout
  42. ) {
  43. submit.attr( 'disabled', 'disabled' ).addClass( 'disabled' );
  44. } else {
  45. submit.prop( 'disabled', false ).removeClass( 'disabled' );
  46. }
  47. },
  48. /**
  49. * Include meter HTML.
  50. *
  51. * @param {Object} wrapper
  52. * @param {Object} field
  53. */
  54. includeMeter: function( wrapper, field ) {
  55. var meter = wrapper.find( '.woocommerce-password-strength' );
  56. if ( '' === field.val() ) {
  57. meter.hide();
  58. $( document.body ).trigger( 'wc-password-strength-hide' );
  59. } else if ( 0 === meter.length ) {
  60. field.after( '<div class="woocommerce-password-strength" aria-live="polite"></div>' );
  61. $( document.body ).trigger( 'wc-password-strength-added' );
  62. } else {
  63. meter.show();
  64. $( document.body ).trigger( 'wc-password-strength-show' );
  65. }
  66. },
  67. /**
  68. * Check password strength.
  69. *
  70. * @param {Object} field
  71. *
  72. * @return {Int}
  73. */
  74. checkPasswordStrength: function( wrapper, field ) {
  75. var meter = wrapper.find( '.woocommerce-password-strength' ),
  76. hint = wrapper.find( '.woocommerce-password-hint' ),
  77. hint_html = '<small class="woocommerce-password-hint">' + wc_password_strength_meter_params.i18n_password_hint + '</small>',
  78. strength = wp.passwordStrength.meter( field.val(), wp.passwordStrength.userInputDisallowedList() ),
  79. error = '';
  80. // Reset.
  81. meter.removeClass( 'short bad good strong' );
  82. hint.remove();
  83. if ( meter.is( ':hidden' ) ) {
  84. return strength;
  85. }
  86. // Error to append
  87. if ( strength < wc_password_strength_meter_params.min_password_strength ) {
  88. error = ' - ' + wc_password_strength_meter_params.i18n_password_error;
  89. }
  90. switch ( strength ) {
  91. case 0 :
  92. meter.addClass( 'short' ).html( pwsL10n['short'] + error );
  93. meter.after( hint_html );
  94. break;
  95. case 1 :
  96. meter.addClass( 'bad' ).html( pwsL10n.bad + error );
  97. meter.after( hint_html );
  98. break;
  99. case 2 :
  100. meter.addClass( 'bad' ).html( pwsL10n.bad + error );
  101. meter.after( hint_html );
  102. break;
  103. case 3 :
  104. meter.addClass( 'good' ).html( pwsL10n.good + error );
  105. break;
  106. case 4 :
  107. meter.addClass( 'strong' ).html( pwsL10n.strong + error );
  108. break;
  109. case 5 :
  110. meter.addClass( 'short' ).html( pwsL10n.mismatch );
  111. break;
  112. }
  113. return strength;
  114. }
  115. };
  116. wc_password_strength_meter.init();
  117. })( jQuery );