address-i18n.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*global wc_address_i18n_params */
  2. jQuery( function( $ ) {
  3. // wc_address_i18n_params is required to continue, ensure the object exists
  4. if ( typeof wc_address_i18n_params === 'undefined' ) {
  5. return false;
  6. }
  7. var locale_json = wc_address_i18n_params.locale.replace( /"/g, '"' ),
  8. locale = $.parseJSON( locale_json );
  9. function field_is_required( field, is_required ) {
  10. if ( is_required ) {
  11. field.find( 'label .optional' ).remove();
  12. field.addClass( 'validate-required' );
  13. if ( field.find( 'label .required' ).length === 0 ) {
  14. field.find( 'label' ).append( '&nbsp;<abbr class="required" title="' + wc_address_i18n_params.i18n_required_text + '">*</abbr>' );
  15. }
  16. } else {
  17. field.find( 'label .required' ).remove();
  18. field.removeClass( 'validate-required' );
  19. if ( field.find( 'label .optional' ).length === 0 ) {
  20. field.find( 'label' ).append( '&nbsp;<span class="optional">(' + wc_address_i18n_params.i18n_optional_text + ')</span>' );
  21. }
  22. }
  23. }
  24. $( document.body )
  25. // Handle locale
  26. .bind( 'country_to_state_changing', function( event, country, wrapper ) {
  27. var thisform = wrapper, thislocale;
  28. if ( typeof locale[ country ] !== 'undefined' ) {
  29. thislocale = locale[ country ];
  30. } else {
  31. thislocale = locale['default'];
  32. }
  33. var $postcodefield = thisform.find( '#billing_postcode_field, #shipping_postcode_field' ),
  34. $cityfield = thisform.find( '#billing_city_field, #shipping_city_field' ),
  35. $statefield = thisform.find( '#billing_state_field, #shipping_state_field' );
  36. if ( ! $postcodefield.attr( 'data-o_class' ) ) {
  37. $postcodefield.attr( 'data-o_class', $postcodefield.attr( 'class' ) );
  38. $cityfield.attr( 'data-o_class', $cityfield.attr( 'class' ) );
  39. $statefield.attr( 'data-o_class', $statefield.attr( 'class' ) );
  40. }
  41. var locale_fields = $.parseJSON( wc_address_i18n_params.locale_fields );
  42. $.each( locale_fields, function( key, value ) {
  43. var field = thisform.find( value ),
  44. fieldLocale = $.extend( true, {}, locale['default'][ key ], thislocale[ key ] );
  45. // Labels.
  46. if ( typeof fieldLocale.label !== 'undefined' ) {
  47. field.find( 'label' ).html( fieldLocale.label );
  48. }
  49. // Placeholders.
  50. if ( typeof fieldLocale.placeholder !== 'undefined' ) {
  51. field.find( 'input' ).attr( 'placeholder', fieldLocale.placeholder );
  52. field.find( '.select2-selection__placeholder' ).text( fieldLocale.placeholder );
  53. }
  54. // Use the i18n label as a placeholder if there is no label element and no i18n placeholder.
  55. if ( typeof fieldLocale.placeholder === 'undefined' && typeof fieldLocale.label !== 'undefined' && ! field.find( 'label' ).length ) {
  56. field.find( 'input' ).attr( 'placeholder', fieldLocale.label );
  57. field.find( '.select2-selection__placeholder' ).text( fieldLocale.label );
  58. }
  59. // Required.
  60. if ( typeof fieldLocale.required !== 'undefined' ) {
  61. field_is_required( field, fieldLocale.required );
  62. } else {
  63. field_is_required( field, false );
  64. }
  65. // Priority.
  66. if ( typeof fieldLocale.priority !== 'undefined' ) {
  67. field.data( 'priority', fieldLocale.priority );
  68. }
  69. // Hidden fields.
  70. if ( 'state' !== key ) {
  71. if ( typeof fieldLocale.hidden !== 'undefined' && true === fieldLocale.hidden ) {
  72. field.hide().find( 'input' ).val( '' );
  73. } else {
  74. field.show();
  75. }
  76. }
  77. });
  78. var fieldsets = $('.woocommerce-billing-fields__field-wrapper, .woocommerce-shipping-fields__field-wrapper, .woocommerce-address-fields__field-wrapper, .woocommerce-additional-fields__field-wrapper .woocommerce-account-fields');
  79. fieldsets.each( function( index, fieldset ) {
  80. var rows = $( fieldset ).find( '.form-row' );
  81. var wrapper = rows.first().parent();
  82. // Before sorting, ensure all fields have a priority for bW compatibility.
  83. var last_priority = 0;
  84. rows.each( function() {
  85. if ( ! $( this ).data( 'priority' ) ) {
  86. $( this ).data( 'priority', last_priority + 1 );
  87. }
  88. last_priority = $( this ).data( 'priority' );
  89. } );
  90. // Sort the fields.
  91. rows.sort( function( a, b ) {
  92. var asort = $( a ).data( 'priority' ),
  93. bsort = $( b ).data( 'priority' );
  94. if ( asort > bsort ) {
  95. return 1;
  96. }
  97. if ( asort < bsort ) {
  98. return -1;
  99. }
  100. return 0;
  101. });
  102. rows.detach().appendTo( wrapper );
  103. } );
  104. });
  105. });