Nav apraksta

address-i18n.js 4.9KB

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