term-ordering.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*global ajaxurl, woocommerce_term_ordering_params */
  2. /* Modifided script from the simple-page-ordering plugin */
  3. jQuery( function( $ ) {
  4. var table_selector = 'table.wp-list-table',
  5. item_selector = 'tbody tr:not(.inline-edit-row)',
  6. term_id_selector = '.column-handle input[name="term_id"]',
  7. column_handle = '<td class="column-handle"></td>';
  8. if ( 0 === $( table_selector ).find( '.column-handle' ).length ) {
  9. $( table_selector ).find( 'tr:not(.inline-edit-row)' ).append( column_handle );
  10. term_id_selector = '.check-column input';
  11. }
  12. $( table_selector ).find( '.column-handle' ).show();
  13. $.wc_add_missing_sort_handles = function() {
  14. var all_table_rows = $( table_selector ).find('tbody > tr');
  15. var rows_with_handle = $( table_selector ).find('tbody > tr > td.column-handle').parent();
  16. if ( all_table_rows.length !== rows_with_handle.length ) {
  17. all_table_rows.each(function(index, elem){
  18. if ( ! rows_with_handle.is( elem ) ) {
  19. $( elem ).append( column_handle );
  20. }
  21. });
  22. }
  23. $( table_selector ).find( '.column-handle' ).show();
  24. };
  25. $( document ).ajaxComplete( function( event, request, options ) {
  26. if ( request && 4 === request.readyState && 200 === request.status && options.data && ( 0 <= options.data.indexOf( '_inline_edit' ) || 0 <= options.data.indexOf( 'add-tag' ) ) ) {
  27. $.wc_add_missing_sort_handles();
  28. $( document.body ).trigger( 'init_tooltips' );
  29. }
  30. } );
  31. $( table_selector ).sortable({
  32. items: item_selector,
  33. cursor: 'move',
  34. handle: '.column-handle',
  35. axis: 'y',
  36. forcePlaceholderSize: true,
  37. helper: 'clone',
  38. opacity: 0.65,
  39. placeholder: 'product-cat-placeholder',
  40. scrollSensitivity: 40,
  41. start: function( event, ui ) {
  42. if ( ! ui.item.hasClass( 'alternate' ) ) {
  43. ui.item.css( 'background-color', '#ffffff' );
  44. }
  45. ui.item.children( 'td, th' ).css( 'border-bottom-width', '0' );
  46. ui.item.css( 'outline', '1px solid #aaa' );
  47. },
  48. stop: function( event, ui ) {
  49. ui.item.removeAttr( 'style' );
  50. ui.item.children( 'td, th' ).css( 'border-bottom-width', '1px' );
  51. },
  52. update: function( event, ui ) {
  53. var termid = ui.item.find( term_id_selector ).val(); // this post id
  54. var termparent = ui.item.find( '.parent' ).html(); // post parent
  55. var prevtermid = ui.item.prev().find( term_id_selector ).val();
  56. var nexttermid = ui.item.next().find( term_id_selector ).val();
  57. // Can only sort in same tree
  58. var prevtermparent, nexttermparent;
  59. if ( prevtermid !== undefined ) {
  60. prevtermparent = ui.item.prev().find( '.parent' ).html();
  61. if ( prevtermparent !== termparent) {
  62. prevtermid = undefined;
  63. }
  64. }
  65. if ( nexttermid !== undefined ) {
  66. nexttermparent = ui.item.next().find( '.parent' ).html();
  67. if ( nexttermparent !== termparent) {
  68. nexttermid = undefined;
  69. }
  70. }
  71. // If previous and next not at same tree level, or next not at same tree level and the previous is the parent of the next, or just moved item beneath its own children
  72. if ( ( prevtermid === undefined && nexttermid === undefined ) || ( nexttermid === undefined && nexttermparent === prevtermid ) || ( nexttermid !== undefined && prevtermparent === termid ) ) {
  73. $( table_selector ).sortable( 'cancel' );
  74. return;
  75. }
  76. // Show Spinner
  77. ui.item.find( '.check-column input' ).hide();
  78. ui.item.find( '.check-column' ).append( '<img alt="processing" src="images/wpspin_light.gif" class="waiting" style="margin-left: 6px;" />' );
  79. // Go do the sorting stuff via ajax
  80. $.post( ajaxurl, { action: 'woocommerce_term_ordering', id: termid, nextid: nexttermid, thetaxonomy: woocommerce_term_ordering_params.taxonomy }, function(response){
  81. if ( response === 'children' ) {
  82. window.location.reload();
  83. } else {
  84. ui.item.find( '.check-column input' ).show();
  85. ui.item.find( '.check-column' ).find( 'img' ).remove();
  86. }
  87. });
  88. // Fix cell colors
  89. $( 'table.widefat tbody tr' ).each( function() {
  90. var i = jQuery( 'table.widefat tbody tr' ).index( this );
  91. if ( i%2 === 0 ) {
  92. jQuery( this ).addClass( 'alternate' );
  93. } else {
  94. jQuery( this ).removeClass( 'alternate' );
  95. }
  96. });
  97. }
  98. });
  99. });