| 1 |
|
| 2 |
( function( $ ) { |
| 3 |
|
| 4 |
$( document ).ready( function() { |
| 5 |
|
| 6 |
let tables = $( '.posts-data-table' ); |
| 7 |
|
| 8 |
const adminBar = $( '#wpadminbar' ), |
| 9 |
clickFilterColumns = ['categories', 'tags', 'author']; |
| 10 |
|
| 11 |
tables.each( function() { |
| 12 |
let $table = $( this ), |
| 13 |
config = { |
| 14 |
responsive: true, |
| 15 |
processing: true // display 'processing' indicator when loading |
| 16 |
}; |
| 17 |
|
| 18 |
// Set language - defaults to English if not specified |
| 19 |
if ( ( typeof posts_data_table !== 'undefined' ) && posts_data_table.langurl ) { |
| 20 |
config.language = { url: posts_data_table.langurl }; |
| 21 |
} |
| 22 |
|
| 23 |
// Initialise DataTable |
| 24 |
let table = $table.DataTable( config ); |
| 25 |
|
| 26 |
// If scroll offset defined, animate back to top of table on next/previous page event |
| 27 |
$table.on( 'page.dt', function() { |
| 28 |
if ( $( this ).data( 'scroll-offset' ) !== false ) { |
| 29 |
let tableOffset = $( this ).parent().offset().top - $( this ).data( 'scroll-offset' ); |
| 30 |
|
| 31 |
if ( adminBar.length ) { // Adjust offset for WP admin bar |
| 32 |
let adminBarHeight = adminBar.outerHeight(); |
| 33 |
tableOffset -= ( adminBarHeight ? adminBarHeight : 32 ); |
| 34 |
} |
| 35 |
|
| 36 |
$( 'html,body' ).animate( { scrollTop: tableOffset }, 300 ); |
| 37 |
} |
| 38 |
} ); |
| 39 |
|
| 40 |
// If 'search on click' enabled then add click handler for links in category, author and tags columns. |
| 41 |
// When clicked, the table will filter by that value. |
| 42 |
if ( $table.data( 'click-filter' ) ) { |
| 43 |
$table.on( 'click', 'a', function() { |
| 44 |
let $link = $( this ), |
| 45 |
idx = table.cell( $link.closest( 'td' ).get( 0 ) ).index().column, // get the column index |
| 46 |
header = table.column( idx ).header(), // get the header cell |
| 47 |
columnName = $( header ).data( 'name' ); // get the column name from header |
| 48 |
|
| 49 |
// Is the column click filterable? |
| 50 |
if ( -1 !== clickFilterColumns.indexOf( columnName ) ) { |
| 51 |
table.search( $link.text() ).draw(); |
| 52 |
return false; |
| 53 |
} |
| 54 |
|
| 55 |
return true; |
| 56 |
} ); |
| 57 |
} |
| 58 |
|
| 59 |
} ); // each table |
| 60 |
|
| 61 |
} ); // end document.ready |
| 62 |
|
| 63 |
} )( jQuery ); |