woocommerce.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. ( function( $, undefined ) {
  2. 'use strict';
  3. $( function() {
  4. var dropdown = $( '.fixed-header-box .cart-dropdown' ),
  5. link = $( '.vamtam-cart-dropdown-link' ),
  6. count = $( '.products', link ),
  7. widget = $( '.widget', dropdown ),
  8. isVisible = false;
  9. $( document.body ).on( 'added_to_cart removed_from_cart wc_fragments_refreshed wc_fragments_loaded', function() {
  10. var count_val = parseInt( Cookies.get( 'woocommerce_items_in_cart' ) || 0, 10 );
  11. if ( count_val > 0 ) {
  12. var count_real = 0;
  13. var spans = document.querySelector( '.widget_shopping_cart' ).querySelectorAll( 'li .quantity' );
  14. for ( var i = 0; i < spans.length; i++ ) {
  15. count_real += parseInt( spans[i].innerHTML.split( '<span' )[0].replace( /[^\d]/g, '' ), 10 );
  16. }
  17. // sanitize count_real - if it's not a number, then don't show the counter at all
  18. count_real = count_real >= 0 ? count_real : '';
  19. count.text( count_real );
  20. count.removeClass( 'cart-empty' );
  21. dropdown.removeClass( 'hidden' );
  22. } else {
  23. var show_if_empty = dropdown.hasClass( 'show-if-empty' );
  24. count.addClass( 'cart-empty' );
  25. count.text( '0' );
  26. dropdown.toggleClass( 'hidden', ! show_if_empty );
  27. }
  28. } );
  29. var open = 0;
  30. var showCart = function() {
  31. open = +new Date();
  32. dropdown.addClass( 'state-hover' );
  33. widget.stop( true, true ).fadeIn( 300, function() {
  34. isVisible = true;
  35. } );
  36. };
  37. var hideCart = function() {
  38. var elapsed = new Date() - open;
  39. if( elapsed > 1000 ) {
  40. dropdown.removeClass( 'state-hover' );
  41. widget.stop( true, true ).fadeOut( 300, function() {
  42. isVisible = false;
  43. } );
  44. } else {
  45. setTimeout( function() {
  46. if( !dropdown.is( ':hover' ) ) {
  47. hideCart();
  48. }
  49. }, 1000 - elapsed );
  50. }
  51. };
  52. dropdown.on( 'mouseenter', function() {
  53. showCart();
  54. } ).on( 'mouseleave', function() {
  55. hideCart();
  56. } );
  57. link.not( '.no-dropdown' ).bind( 'click', function( e ) {
  58. if( isVisible ) {
  59. hideCart();
  60. } else {
  61. showCart();
  62. }
  63. e.preventDefault();
  64. } );
  65. } );
  66. } )( jQuery );