add-payment-method.js
3 years ago
add-payment-method.min.js
3 years ago
add-to-cart-variation.js
3 years ago
add-to-cart-variation.min.js
3 years ago
add-to-cart.js
3 years ago
add-to-cart.min.js
3 years ago
address-i18n.js
5 years ago
address-i18n.min.js
3 years ago
cart-fragments.js
3 years ago
cart-fragments.min.js
3 years ago
cart.js
3 years ago
cart.min.js
3 years ago
checkout.js
3 years ago
checkout.min.js
3 years ago
country-select.js
5 years ago
country-select.min.js
3 years ago
credit-card-form.js
8 years ago
credit-card-form.min.js
8 years ago
geolocation.js
3 years ago
geolocation.min.js
3 years ago
lost-password.js
8 years ago
lost-password.min.js
8 years ago
password-strength-meter.js
4 years ago
password-strength-meter.min.js
3 years ago
price-slider.js
5 years ago
price-slider.min.js
3 years ago
single-product.js
4 years ago
single-product.min.js
3 years ago
tokenization-form.js
5 years ago
tokenization-form.min.js
5 years ago
woocommerce.js
5 years ago
woocommerce.min.js
5 years ago
address-i18n.js
152 lines
| 1 | /*global wc_address_i18n_params */ |
| 2 | jQuery( function( $ ) { |
| 3 | |
| 4 | // wc_address_i18n_params is required to continue, ensure the object exists |
| 5 | if ( typeof wc_address_i18n_params === 'undefined' ) { |
| 6 | return false; |
| 7 | } |
| 8 | |
| 9 | var locale_json = wc_address_i18n_params.locale.replace( /"/g, '"' ), locale = JSON.parse( locale_json ); |
| 10 | |
| 11 | function field_is_required( field, is_required ) { |
| 12 | if ( is_required ) { |
| 13 | field.find( 'label .optional' ).remove(); |
| 14 | field.addClass( 'validate-required' ); |
| 15 | |
| 16 | if ( field.find( 'label .required' ).length === 0 ) { |
| 17 | field.find( 'label' ).append( |
| 18 | ' <abbr class="required" title="' + |
| 19 | wc_address_i18n_params.i18n_required_text + |
| 20 | '">*</abbr>' |
| 21 | ); |
| 22 | } |
| 23 | } else { |
| 24 | field.find( 'label .required' ).remove(); |
| 25 | field.removeClass( 'validate-required woocommerce-invalid woocommerce-invalid-required-field' ); |
| 26 | |
| 27 | if ( field.find( 'label .optional' ).length === 0 ) { |
| 28 | field.find( 'label' ).append( ' <span class="optional">(' + wc_address_i18n_params.i18n_optional_text + ')</span>' ); |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | // Handle locale |
| 34 | $( document.body ) |
| 35 | .on( 'country_to_state_changing', function( event, country, wrapper ) { |
| 36 | var thisform = wrapper, thislocale; |
| 37 | |
| 38 | if ( typeof locale[ country ] !== 'undefined' ) { |
| 39 | thislocale = locale[ country ]; |
| 40 | } else { |
| 41 | thislocale = locale['default']; |
| 42 | } |
| 43 | |
| 44 | var $postcodefield = thisform.find( '#billing_postcode_field, #shipping_postcode_field' ), |
| 45 | $cityfield = thisform.find( '#billing_city_field, #shipping_city_field' ), |
| 46 | $statefield = thisform.find( '#billing_state_field, #shipping_state_field' ); |
| 47 | |
| 48 | if ( ! $postcodefield.attr( 'data-o_class' ) ) { |
| 49 | $postcodefield.attr( 'data-o_class', $postcodefield.attr( 'class' ) ); |
| 50 | $cityfield.attr( 'data-o_class', $cityfield.attr( 'class' ) ); |
| 51 | $statefield.attr( 'data-o_class', $statefield.attr( 'class' ) ); |
| 52 | } |
| 53 | |
| 54 | var locale_fields = JSON.parse( wc_address_i18n_params.locale_fields ); |
| 55 | |
| 56 | $.each( locale_fields, function( key, value ) { |
| 57 | |
| 58 | var field = thisform.find( value ), |
| 59 | fieldLocale = $.extend( true, {}, locale['default'][ key ], thislocale[ key ] ); |
| 60 | |
| 61 | // Labels. |
| 62 | if ( typeof fieldLocale.label !== 'undefined' ) { |
| 63 | field.find( 'label' ).html( fieldLocale.label ); |
| 64 | } |
| 65 | |
| 66 | // Placeholders. |
| 67 | if ( typeof fieldLocale.placeholder !== 'undefined' ) { |
| 68 | field.find( ':input' ).attr( 'placeholder', fieldLocale.placeholder ); |
| 69 | field.find( ':input' ).attr( 'data-placeholder', fieldLocale.placeholder ); |
| 70 | field.find( '.select2-selection__placeholder' ).text( fieldLocale.placeholder ); |
| 71 | } |
| 72 | |
| 73 | // Use the i18n label as a placeholder if there is no label element and no i18n placeholder. |
| 74 | if ( |
| 75 | typeof fieldLocale.placeholder === 'undefined' && |
| 76 | typeof fieldLocale.label !== 'undefined' && |
| 77 | ! field.find( 'label' ).length |
| 78 | ) { |
| 79 | field.find( ':input' ).attr( 'placeholder', fieldLocale.label ); |
| 80 | field.find( ':input' ).attr( 'data-placeholder', fieldLocale.label ); |
| 81 | field.find( '.select2-selection__placeholder' ).text( fieldLocale.label ); |
| 82 | } |
| 83 | |
| 84 | // Required. |
| 85 | if ( typeof fieldLocale.required !== 'undefined' ) { |
| 86 | field_is_required( field, fieldLocale.required ); |
| 87 | } else { |
| 88 | field_is_required( field, false ); |
| 89 | } |
| 90 | |
| 91 | // Priority. |
| 92 | if ( typeof fieldLocale.priority !== 'undefined' ) { |
| 93 | field.data( 'priority', fieldLocale.priority ); |
| 94 | } |
| 95 | |
| 96 | // Hidden fields. |
| 97 | if ( 'state' !== key ) { |
| 98 | if ( typeof fieldLocale.hidden !== 'undefined' && true === fieldLocale.hidden ) { |
| 99 | field.hide().find( ':input' ).val( '' ); |
| 100 | } else { |
| 101 | field.show(); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Class changes. |
| 106 | if ( Array.isArray( fieldLocale.class ) ) { |
| 107 | field.removeClass( 'form-row-first form-row-last form-row-wide' ); |
| 108 | field.addClass( fieldLocale.class.join( ' ' ) ); |
| 109 | } |
| 110 | }); |
| 111 | |
| 112 | var fieldsets = $( |
| 113 | '.woocommerce-billing-fields__field-wrapper,' + |
| 114 | '.woocommerce-shipping-fields__field-wrapper,' + |
| 115 | '.woocommerce-address-fields__field-wrapper,' + |
| 116 | '.woocommerce-additional-fields__field-wrapper .woocommerce-account-fields' |
| 117 | ); |
| 118 | |
| 119 | fieldsets.each( function( index, fieldset ) { |
| 120 | var rows = $( fieldset ).find( '.form-row' ); |
| 121 | var wrapper = rows.first().parent(); |
| 122 | |
| 123 | // Before sorting, ensure all fields have a priority for bW compatibility. |
| 124 | var last_priority = 0; |
| 125 | |
| 126 | rows.each( function() { |
| 127 | if ( ! $( this ).data( 'priority' ) ) { |
| 128 | $( this ).data( 'priority', last_priority + 1 ); |
| 129 | } |
| 130 | last_priority = $( this ).data( 'priority' ); |
| 131 | } ); |
| 132 | |
| 133 | // Sort the fields. |
| 134 | rows.sort( function( a, b ) { |
| 135 | var asort = parseInt( $( a ).data( 'priority' ), 10 ), |
| 136 | bsort = parseInt( $( b ).data( 'priority' ), 10 ); |
| 137 | |
| 138 | if ( asort > bsort ) { |
| 139 | return 1; |
| 140 | } |
| 141 | if ( asort < bsort ) { |
| 142 | return -1; |
| 143 | } |
| 144 | return 0; |
| 145 | }); |
| 146 | |
| 147 | rows.detach().appendTo( wrapper ); |
| 148 | }); |
| 149 | }) |
| 150 | .trigger( 'wc_address_i18n_ready' ); |
| 151 | }); |
| 152 |