price-slider.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* global woocommerce_price_slider_params, accounting */
  2. jQuery( function( $ ) {
  3. // woocommerce_price_slider_params is required to continue, ensure the object exists
  4. if ( typeof woocommerce_price_slider_params === 'undefined' ) {
  5. return false;
  6. }
  7. $( document.body ).bind( 'price_slider_create price_slider_slide', function( event, min, max ) {
  8. $( '.price_slider_amount span.from' ).html( accounting.formatMoney( min, {
  9. symbol: woocommerce_price_slider_params.currency_format_symbol,
  10. decimal: woocommerce_price_slider_params.currency_format_decimal_sep,
  11. thousand: woocommerce_price_slider_params.currency_format_thousand_sep,
  12. precision: woocommerce_price_slider_params.currency_format_num_decimals,
  13. format: woocommerce_price_slider_params.currency_format
  14. } ) );
  15. $( '.price_slider_amount span.to' ).html( accounting.formatMoney( max, {
  16. symbol: woocommerce_price_slider_params.currency_format_symbol,
  17. decimal: woocommerce_price_slider_params.currency_format_decimal_sep,
  18. thousand: woocommerce_price_slider_params.currency_format_thousand_sep,
  19. precision: woocommerce_price_slider_params.currency_format_num_decimals,
  20. format: woocommerce_price_slider_params.currency_format
  21. } ) );
  22. $( document.body ).trigger( 'price_slider_updated', [ min, max ] );
  23. });
  24. function init_price_filter() {
  25. $( 'input#min_price, input#max_price' ).hide();
  26. $( '.price_slider, .price_label' ).show();
  27. var min_price = $( '.price_slider_amount #min_price' ).data( 'min' ),
  28. max_price = $( '.price_slider_amount #max_price' ).data( 'max' ),
  29. current_min_price = $( '.price_slider_amount #min_price' ).val(),
  30. current_max_price = $( '.price_slider_amount #max_price' ).val();
  31. $( '.price_slider:not(.ui-slider)' ).slider({
  32. range: true,
  33. animate: true,
  34. min: min_price,
  35. max: max_price,
  36. values: [ current_min_price, current_max_price ],
  37. create: function() {
  38. $( '.price_slider_amount #min_price' ).val( current_min_price );
  39. $( '.price_slider_amount #max_price' ).val( current_max_price );
  40. $( document.body ).trigger( 'price_slider_create', [ current_min_price, current_max_price ] );
  41. },
  42. slide: function( event, ui ) {
  43. $( 'input#min_price' ).val( ui.values[0] );
  44. $( 'input#max_price' ).val( ui.values[1] );
  45. $( document.body ).trigger( 'price_slider_slide', [ ui.values[0], ui.values[1] ] );
  46. },
  47. change: function( event, ui ) {
  48. $( document.body ).trigger( 'price_slider_change', [ ui.values[0], ui.values[1] ] );
  49. }
  50. });
  51. }
  52. init_price_filter();
  53. var hasSelectiveRefresh = (
  54. 'undefined' !== typeof wp &&
  55. wp.customize &&
  56. wp.customize.selectiveRefresh &&
  57. wp.customize.widgetsPreview &&
  58. wp.customize.widgetsPreview.WidgetPartial
  59. );
  60. if ( hasSelectiveRefresh ) {
  61. wp.customize.selectiveRefresh.bind( 'partial-content-rendered', function() {
  62. init_price_filter();
  63. } );
  64. }
  65. });