nova-drag-drop.js
67 lines
| 1 | /* global _novaDragDrop */ |
| 2 | |
| 3 | ( function ( $ ) { |
| 4 | var list; |
| 5 | |
| 6 | function init() { |
| 7 | list = $( '#the-list' ); |
| 8 | dragMenus(); |
| 9 | addNonce(); |
| 10 | addSubmitButton(); |
| 11 | changeToPost(); |
| 12 | } |
| 13 | |
| 14 | function dragMenus() { |
| 15 | list.sortable( { |
| 16 | cancel: '.no-items, .inline-edit-row', |
| 17 | stop: function ( event, ui ) { |
| 18 | if ( ui.item.is( ':first-child' ) ) { |
| 19 | return list.sortable( 'cancel' ); |
| 20 | } |
| 21 | // |
| 22 | reOrder(); |
| 23 | }, |
| 24 | } ); |
| 25 | } |
| 26 | |
| 27 | function reOrder() { |
| 28 | list.find( '.menu-label-row' ).each( function () { |
| 29 | var term_id = $( this ).data( 'term_id' ); |
| 30 | $( this ) |
| 31 | .nextUntil( '.menu-label-row' ) |
| 32 | .each( function ( i ) { |
| 33 | var row = $( this ); |
| 34 | row.find( '.menu-order-value' ).val( i ); |
| 35 | row.find( '.nova-menu-term' ).val( term_id ); |
| 36 | } ); |
| 37 | } ); |
| 38 | } |
| 39 | |
| 40 | function addSubmitButton() { |
| 41 | $( '.tablenav' ).prepend( |
| 42 | '<input type="submit" class="button-primary button-reorder alignright" value="' + |
| 43 | _novaDragDrop.reorder + |
| 44 | '" name="' + |
| 45 | _novaDragDrop.reorderName + |
| 46 | '" />' |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | function addNonce() { |
| 51 | $( '#posts-filter' ).append( |
| 52 | '<input type="hidden" name="' + |
| 53 | _novaDragDrop.nonceName + |
| 54 | '" value="' + |
| 55 | _novaDragDrop.nonce + |
| 56 | '" />' |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | function changeToPost() { |
| 61 | $( '#posts-filter' ).attr( 'method', 'post' ); |
| 62 | } |
| 63 | |
| 64 | // do it |
| 65 | $( document ).ready( init ); |
| 66 | } )( jQuery ); |
| 67 |