1/* global wp, pwsL10n, wc_password_strength_meter_params */ 2jQuery( 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 ).on( 13 'keyup change', 14 'form.register #reg_password, form.checkout #account_password, ' + 15 'form.edit-account #password_1, form.lost_reset_password #password_1', 16 this.strengthMeter 17 ); 18 $( 'form.checkout #createaccount' ).trigger( 'change' ); 19 }, 20 21 /** 22 * Strength Meter. 23 */ 24 strengthMeter: function () { 25 var wrapper = $( 26 'form.register, form.checkout, form.edit-account, form.lost_reset_password' 27 ), 28 submit = $( 'button[type="submit"]', wrapper ), 29 field = $( 30 '#reg_password, #account_password, #password_1', 31 wrapper 32 ), 33 strength = 1, 34 fieldValue = field.val(), 35 stop_checkout = ! wrapper.is( 'form.checkout' ); // By default is disabled on checkout. 36 37 wc_password_strength_meter.includeMeter( wrapper, field ); 38 39 strength = wc_password_strength_meter.checkPasswordStrength( 40 wrapper, 41 field 42 ); 43 44 // Allow password strength meter stop checkout. 45 if ( wc_password_strength_meter_params.stop_checkout ) { 46 stop_checkout = true; 47 } 48 49 if ( 50 fieldValue.length > 0 && 51 strength < 52 wc_password_strength_meter_params.min_password_strength && 53 -1 !== strength && 54 stop_checkout 55 ) { 56 submit.attr( 'disabled', 'disabled' ).addClass( 'disabled' ); 57 } else { 58 submit.prop( 'disabled', false ).removeClass( 'disabled' ); 59 } 60 }, 61 62 /** 63 * Include meter HTML. 64 * 65 * @param {Object} wrapper 66 * @param {Object} field 67 */ 68 includeMeter: function ( wrapper, field ) { 69 var meter = wrapper.find( '.woocommerce-password-strength' ); 70 71 if ( '' === field.val() ) { 72 meter.hide(); 73 $( document.body ).trigger( 'wc-password-strength-hide' ); 74 field.removeAttr( 'aria-describedby' ); 75 } else if ( 0 === meter.length ) { 76 var meter = 77 '<div id="password_strength" class="woocommerce-password-strength" role="alert"></div>'; 78 var wrapper = field.parent(); 79 // If the field is inside a password-input, inject the meter after the password-input. 80 if ( wrapper.is( '.password-input' ) ) { 81 wrapper.after( meter ); 82 } else { 83 field.after( meter ); 84 } 85 field.attr( 'aria-describedby', 'password_strength' ); 86 $( document.body ).trigger( 'wc-password-strength-added' ); 87 } else { 88 meter.show(); 89 $( document.body ).trigger( 'wc-password-strength-show' ); 90 } 91 }, 92 93 /** 94 * Check password strength. 95 * 96 * @param {Object} field 97 * 98 * @return {Int} 99 */ 100 checkPasswordStrength: function ( wrapper, field ) { 101 var meter = wrapper.find( '.woocommerce-password-strength' ), 102 hint = wrapper.find( '.woocommerce-password-hint' ), 103 hint_html = 104 '<small class="woocommerce-password-hint">' + 105 wc_password_strength_meter_params.i18n_password_hint + 106 '</small>', 107 strength = wp.passwordStrength.meter( 108 field.val(), 109 wp.passwordStrength.userInputDisallowedList() 110 ), 111 error = ''; 112 113 // Reset. 114 meter.removeClass( 'short bad good strong' ); 115 hint.remove(); 116 117 if ( meter.is( ':hidden' ) ) { 118 return strength; 119 } 120 121 // Error to append 122 if ( 123 strength < 124 wc_password_strength_meter_params.min_password_strength 125 ) { 126 error = 127 ' - ' + 128 wc_password_strength_meter_params.i18n_password_error; 129 } 130 131 switch ( strength ) { 132 case 0: 133 meter 134 .addClass( 'short' ) 135 .html( pwsL10n[ 'short' ] + error ); 136 meter.after( hint_html ); 137 break; 138 case 1: 139 meter.addClass( 'bad' ).html( pwsL10n.bad + error ); 140 meter.after( hint_html ); 141 break; 142 case 2: 143 meter.addClass( 'bad' ).html( pwsL10n.bad + error ); 144 meter.after( hint_html ); 145 break; 146 case 3: 147 meter.addClass( 'good' ).html( pwsL10n.good + error ); 148 break; 149 case 4: 150 meter.addClass( 'strong' ).html( pwsL10n.strong + error ); 151 break; 152 case 5: 153 meter.addClass( 'short' ).html( pwsL10n.mismatch ); 154 break; 155 } 156 157 return strength; 158 }, 159 }; 160 161 wc_password_strength_meter.init(); 162} );
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.