product-ordering.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*global ajaxurl */
  2. /**
  3. * Based on Simple Page Ordering by 10up (https://wordpress.org/plugins/simple-page-ordering/)
  4. *
  5. * Modified - products have no children (non hierarchical)
  6. */
  7. jQuery( function( $ ) {
  8. $( 'table.widefat tbody th, table.widefat tbody td' ).css( 'cursor', 'move' );
  9. $( 'table.widefat tbody' ).sortable({
  10. items: 'tr:not(.inline-edit-row)',
  11. cursor: 'move',
  12. axis: 'y',
  13. containment: 'table.widefat',
  14. scrollSensitivity: 40,
  15. helper: function( event, ui ) {
  16. ui.each( function() {
  17. $( this ).width( $( this ).width() );
  18. });
  19. return ui;
  20. },
  21. start: function( event, ui ) {
  22. ui.item.css( 'background-color', '#ffffff' );
  23. ui.item.children( 'td, th' ).css( 'border-bottom-width', '0' );
  24. ui.item.css( 'outline', '1px solid #dfdfdf' );
  25. },
  26. stop: function( event, ui ) {
  27. ui.item.removeAttr( 'style' );
  28. ui.item.children( 'td,th' ).css( 'border-bottom-width', '1px' );
  29. },
  30. update: function( event, ui ) {
  31. $( 'table.widefat tbody th, table.widefat tbody td' ).css( 'cursor', 'default' );
  32. $( 'table.widefat tbody' ).sortable( 'disable' );
  33. var postid = ui.item.find( '.check-column input' ).val();
  34. var prevpostid = ui.item.prev().find( '.check-column input' ).val();
  35. var nextpostid = ui.item.next().find( '.check-column input' ).val();
  36. // Show Spinner
  37. ui.item.find( '.check-column input' ).hide().after( '<img alt="processing" src="images/wpspin_light.gif" class="waiting" style="margin-left: 6px;" />' );
  38. // Go do the sorting stuff via ajax
  39. $.post( ajaxurl, { action: 'woocommerce_product_ordering', id: postid, previd: prevpostid, nextid: nextpostid }, function( response ) {
  40. $.each( response, function( key, value ) {
  41. $( '#inline_' + key + ' .menu_order' ).html( value );
  42. });
  43. ui.item.find( '.check-column input' ).show().siblings( 'img' ).remove();
  44. $( 'table.widefat tbody th, table.widefat tbody td' ).css( 'cursor', 'move' );
  45. $( 'table.widefat tbody' ).sortable( 'enable' );
  46. });
  47. // fix cell colors
  48. $( 'table.widefat tbody tr' ).each( function() {
  49. var i = $( 'table.widefat tbody tr' ).index( this );
  50. if ( i%2 === 0 ) {
  51. $( this ).addClass( 'alternate' );
  52. } else {
  53. $( this ).removeClass( 'alternate' );
  54. }
  55. });
  56. }
  57. });
  58. });