Нет описания

term-ordering.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 (
  27. request &&
  28. 4 === request.readyState &&
  29. 200 === request.status &&
  30. options.data &&
  31. ( 0 <= options.data.indexOf( '_inline_edit' ) || 0 <= options.data.indexOf( 'add-tag' ) )
  32. ) {
  33. $.wc_add_missing_sort_handles();
  34. $( document.body ).trigger( 'init_tooltips' );
  35. }
  36. } );
  37. $( table_selector ).sortable({
  38. items: item_selector,
  39. cursor: 'move',
  40. handle: '.column-handle',
  41. axis: 'y',
  42. forcePlaceholderSize: true,
  43. helper: 'clone',
  44. opacity: 0.65,
  45. placeholder: 'product-cat-placeholder',
  46. scrollSensitivity: 40,
  47. start: function( event, ui ) {
  48. if ( ! ui.item.hasClass( 'alternate' ) ) {
  49. ui.item.css( 'background-color', '#ffffff' );
  50. }
  51. ui.item.children( 'td, th' ).css( 'border-bottom-width', '0' );
  52. ui.item.css( 'outline', '1px solid #aaa' );
  53. },
  54. stop: function( event, ui ) {
  55. ui.item.removeAttr( 'style' );
  56. ui.item.children( 'td, th' ).css( 'border-bottom-width', '1px' );
  57. },
  58. update: function( event, ui ) {
  59. var termid = ui.item.find( term_id_selector ).val(); // this post id
  60. var termparent = ui.item.find( '.parent' ).html(); // post parent
  61. var prevtermid = ui.item.prev().find( term_id_selector ).val();
  62. var nexttermid = ui.item.next().find( term_id_selector ).val();
  63. // Can only sort in same tree
  64. var prevtermparent, nexttermparent;
  65. if ( prevtermid !== undefined ) {
  66. prevtermparent = ui.item.prev().find( '.parent' ).html();
  67. if ( prevtermparent !== termparent) {
  68. prevtermid = undefined;
  69. }
  70. }
  71. if ( nexttermid !== undefined ) {
  72. nexttermparent = ui.item.next().find( '.parent' ).html();
  73. if ( nexttermparent !== termparent) {
  74. nexttermid = undefined;
  75. }
  76. }
  77. // If previous and next not at same tree level, or next not at same tree level and
  78. // the previous is the parent of the next, or just moved item beneath its own children.
  79. if (
  80. ( prevtermid === undefined && nexttermid === undefined ) ||
  81. ( nexttermid === undefined && nexttermparent === prevtermid ) ||
  82. ( nexttermid !== undefined && prevtermparent === termid )
  83. ) {
  84. $( table_selector ).sortable( 'cancel' );
  85. return;
  86. }
  87. // Show Spinner
  88. ui.item.find( '.check-column input' ).hide();
  89. ui.item
  90. .find( '.check-column' )
  91. .append( '<img alt="processing" src="images/wpspin_light.gif" class="waiting" style="margin-left: 6px;" />' );
  92. // Go do the sorting stuff via ajax.
  93. $.post(
  94. ajaxurl,
  95. {
  96. action: 'woocommerce_term_ordering',
  97. id: termid,
  98. nextid: nexttermid,
  99. thetaxonomy: woocommerce_term_ordering_params.taxonomy
  100. },
  101. function(response) {
  102. if ( response === 'children' ) {
  103. window.location.reload();
  104. } else {
  105. ui.item.find( '.check-column input' ).show();
  106. ui.item.find( '.check-column' ).find( 'img' ).remove();
  107. }
  108. }
  109. );
  110. // Fix cell colors
  111. $( 'table.widefat tbody tr' ).each( function() {
  112. var i = jQuery( 'table.widefat tbody tr' ).index( this );
  113. if ( i%2 === 0 ) {
  114. jQuery( this ).addClass( 'alternate' );
  115. } else {
  116. jQuery( this ).removeClass( 'alternate' );
  117. }
  118. });
  119. }
  120. });
  121. });