PluginProbe
WooCommerce / 11.1.0
WooCommerce v11.1.0
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / assets / js / admin / term-ordering.js
term-ordering.js
150 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /*global ajaxurl, woocommerce_term_ordering_params */
2
3 /* Modifided script from the simple-page-ordering plugin */
4 jQuery( function( $ ) {
5
6 var table_selector = 'table.wp-list-table',
7 item_selector = 'tbody tr:not(.inline-edit-row)',
8 term_id_selector = '.column-handle input[name="term_id"]',
9 column_handle = '<td class="column-handle"></td>';
10
11 if ( 0 === $( table_selector ).find( '.column-handle' ).length ) {
12 $( table_selector ).find( 'tr:not(.inline-edit-row)' ).append( column_handle );
13
14 term_id_selector = '.check-column input';
15 }
16
17 // Stand-in wcTracks.recordEvent in case tracks is not available (for any reason).
18 window.wcTracks = window.wcTracks || {};
19 window.wcTracks.recordEvent = window.wcTracks.recordEvent || function() { };
20
21 $( table_selector ).find( '.column-handle' ).show();
22
23 $.wc_add_missing_sort_handles = function() {
24 var all_table_rows = $( table_selector ).find('tbody > tr');
25 var rows_with_handle = $( table_selector ).find('tbody > tr > td.column-handle').parent();
26 if ( all_table_rows.length !== rows_with_handle.length ) {
27 all_table_rows.each(function(index, elem){
28 if ( ! rows_with_handle.is( elem ) ) {
29 $( elem ).append( column_handle );
30 }
31 });
32 }
33 $( table_selector ).find( '.column-handle' ).show();
34 };
35
36 $( document ).ajaxComplete( function( event, request, options ) {
37 if (
38 request &&
39 4 === request.readyState &&
40 200 === request.status &&
41 options.data &&
42 ( 0 <= options.data.indexOf( '_inline_edit' ) || 0 <= options.data.indexOf( 'add-tag' ) )
43 ) {
44 $.wc_add_missing_sort_handles();
45 $( document.body ).trigger( 'init_tooltips' );
46 }
47 } );
48
49 $( table_selector ).sortable({
50 items: item_selector,
51 cursor: 'move',
52 handle: '.column-handle',
53 axis: 'y',
54 forcePlaceholderSize: true,
55 helper: 'clone',
56 opacity: 0.65,
57 placeholder: 'product-cat-placeholder',
58 scrollSensitivity: 40,
59 start: function( event, ui ) {
60 if ( ! ui.item.hasClass( 'alternate' ) ) {
61 ui.item.css( 'background-color', '#ffffff' );
62 }
63 ui.item.children( 'td, th' ).css( 'border-bottom-width', '0' );
64 ui.item.css( 'outline', '1px solid #aaa' );
65 },
66 stop: function( event, ui ) {
67 ui.item.removeAttr( 'style' );
68 ui.item.children( 'td, th' ).css( 'border-bottom-width', '1px' );
69 },
70 update: function( event, ui ) {
71 var termid = ui.item.find( term_id_selector ).val(); // this post id
72 var termparent = ui.item.find( '.parent' ).html(); // post parent
73
74 var prevtermid = ui.item.prev().find( term_id_selector ).val();
75 var nexttermid = ui.item.next().find( term_id_selector ).val();
76
77 // Can only sort in same tree
78 var prevtermparent, nexttermparent;
79 if ( prevtermid !== undefined ) {
80 prevtermparent = ui.item.prev().find( '.parent' ).html();
81 if ( prevtermparent !== termparent) {
82 prevtermid = undefined;
83 }
84 }
85
86 if ( nexttermid !== undefined ) {
87 nexttermparent = ui.item.next().find( '.parent' ).html();
88 if ( nexttermparent !== termparent) {
89 nexttermid = undefined;
90 }
91 }
92
93 // If previous and next not at same tree level, or next not at same tree level and
94 // the previous is the parent of the next, or just moved item beneath its own children.
95 if (
96 ( prevtermid === undefined && nexttermid === undefined ) ||
97 ( nexttermid === undefined && nexttermparent === prevtermid ) ||
98 ( nexttermid !== undefined && prevtermparent === termid )
99 ) {
100 $( table_selector ).sortable( 'cancel' );
101 return;
102 }
103
104 window.wcTracks.recordEvent( 'product_attributes_ordering_term', {
105 is_category:
106 woocommerce_term_ordering_params.taxonomy === 'product_cat'
107 ? 'yes'
108 : 'no',
109 } );
110
111 // Show Spinner
112 ui.item.find( '.check-column input' ).hide();
113 ui.item
114 .find( '.check-column' )
115 .append( '<img alt="processing" src="images/wpspin_light.gif" class="waiting" style="margin-left: 6px;" />' );
116
117 // Go do the sorting stuff via ajax.
118 $.post(
119 ajaxurl,
120 {
121 action: 'woocommerce_term_ordering',
122 security: woocommerce_term_ordering_params.nonce,
123 id: termid,
124 nextid: nexttermid,
125 thetaxonomy: woocommerce_term_ordering_params.taxonomy
126 },
127 function(response) {
128 if ( response === 'children' ) {
129 window.location.reload();
130 } else {
131 ui.item.find( '.check-column input' ).show();
132 ui.item.find( '.check-column' ).find( 'img' ).remove();
133 }
134 }
135 );
136
137 // Fix cell colors
138 $( 'table.widefat tbody tr' ).each( function() {
139 var i = jQuery( 'table.widefat tbody tr' ).index( this );
140 if ( i%2 === 0 ) {
141 jQuery( this ).addClass( 'alternate' );
142 } else {
143 jQuery( this ).removeClass( 'alternate' );
144 }
145 });
146 }
147 });
148
149 });
150