Bez popisu

users.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*global wc_users_params */
  2. jQuery( function ( $ ) {
  3. /**
  4. * Users country and state fields
  5. */
  6. var wc_users_fields = {
  7. states: null,
  8. init: function() {
  9. if ( typeof wc_users_params.countries !== 'undefined' ) {
  10. /* State/Country select boxes */
  11. this.states = JSON.parse( wc_users_params.countries.replace( /"/g, '"' ) );
  12. }
  13. $( '.js_field-country' ).selectWoo().on( 'change', this.change_country );
  14. $( '.js_field-country' ).trigger( 'change', [ true ] );
  15. $( document.body ).on( 'change', 'select.js_field-state', this.change_state );
  16. $( document.body ).on( 'click', 'button.js_copy-billing', this.copy_billing );
  17. },
  18. change_country: function( e, stickValue ) {
  19. // Check for stickValue before using it
  20. if ( typeof stickValue === 'undefined' ) {
  21. stickValue = false;
  22. }
  23. // Prevent if we don't have the metabox data
  24. if ( wc_users_fields.states === null ) {
  25. return;
  26. }
  27. var $this = $( this ),
  28. country = $this.val(),
  29. $state = $this.parents( '.form-table' ).find( ':input.js_field-state' ),
  30. $parent = $state.parent(),
  31. input_name = $state.attr( 'name' ),
  32. input_id = $state.attr( 'id' ),
  33. stickstatefield = 'woocommerce.stickState-' + country,
  34. value = $this.data( stickstatefield ) ? $this.data( stickstatefield ) : $state.val(),
  35. placeholder = $state.attr( 'placeholder' ),
  36. $newstate;
  37. if ( stickValue ){
  38. $this.data( 'woocommerce.stickState-' + country, value );
  39. }
  40. // Remove the previous DOM element
  41. $parent.show().find( '.select2-container' ).remove();
  42. if ( ! $.isEmptyObject( wc_users_fields.states[ country ] ) ) {
  43. var state = wc_users_fields.states[ country ],
  44. $defaultOption = $( '<option value=""></option>' )
  45. .text( wc_users_fields.i18n_select_state_text );
  46. $newstate = $( '<select style="width: 25em;"></select>' )
  47. .prop( 'id', input_id )
  48. .prop( 'name', input_name )
  49. .prop( 'placeholder', placeholder )
  50. .addClass( 'js_field-state' )
  51. .append( $defaultOption );
  52. $.each( state, function( index ) {
  53. var $option = $( '<option></option>' )
  54. .prop( 'value', index )
  55. .text( state[ index ] );
  56. $newstate.append( $option );
  57. } );
  58. $newstate.val( value );
  59. $state.replaceWith( $newstate );
  60. $newstate.show().selectWoo().hide().trigger( 'change' );
  61. } else {
  62. $newstate = $( '<input type="text" />' )
  63. .prop( 'id', input_id )
  64. .prop( 'name', input_name )
  65. .prop( 'placeholder', placeholder )
  66. .addClass( 'js_field-state regular-text' )
  67. .val( value );
  68. $state.replaceWith( $newstate );
  69. }
  70. // This event has a typo - deprecated in 2.5.0
  71. $( document.body ).trigger( 'contry-change.woocommerce', [country, $( this ).closest( 'div' )] );
  72. $( document.body ).trigger( 'country-change.woocommerce', [country, $( this ).closest( 'div' )] );
  73. },
  74. change_state: function() {
  75. // Here we will find if state value on a select has changed and stick it to the country data
  76. var $this = $( this ),
  77. state = $this.val(),
  78. $country = $this.parents( '.form-table' ).find( ':input.js_field-country' ),
  79. country = $country.val();
  80. $country.data( 'woocommerce.stickState-' + country, state );
  81. },
  82. copy_billing: function( event ) {
  83. event.preventDefault();
  84. $( '#fieldset-billing' ).find( 'input, select' ).each( function( i, el ) {
  85. // The address keys match up, except for the prefix
  86. var shipName = el.name.replace( /^billing_/, 'shipping_' );
  87. // Swap prefix, then check if there are any elements
  88. var shipEl = $( '[name="' + shipName + '"]' );
  89. // No corresponding shipping field, skip this item
  90. if ( ! shipEl.length ) {
  91. return;
  92. }
  93. // Found a matching shipping element, update the value
  94. shipEl.val( el.value ).trigger( 'change' );
  95. } );
  96. }
  97. };
  98. wc_users_fields.init();
  99. });