| 1 |
/*! |
| 2 |
* Posts Data Table |
| 3 |
* Copyright 2016 Barn2 Media Ltd |
| 4 |
*/ |
| 5 |
|
| 6 |
(function($) { |
| 7 |
|
| 8 |
$(document).ready(function() { |
| 9 |
|
| 10 |
var tables = $('.posts-data-table'); |
| 11 |
var adminBarVisible = $('#wpadminbar').length; |
| 12 |
|
| 13 |
tables.each( function() { |
| 14 |
|
| 15 |
var config = { |
| 16 |
responsive: true, |
| 17 |
processing: true // display 'processing' indicator when loading |
| 18 |
}; |
| 19 |
|
| 20 |
// Set language - defaults to English if not specified |
| 21 |
if ( (typeof posts_data_table !== 'undefined') && posts_data_table.langurl ) { |
| 22 |
config.language = { url: posts_data_table.langurl }; |
| 23 |
} |
| 24 |
|
| 25 |
// Initialise DataTable |
| 26 |
var table = $(this).DataTable( config ); |
| 27 |
|
| 28 |
// If scroll offset defined, animate back to top of table on next/previous page event |
| 29 |
$(this).on('page.dt', function() { |
| 30 |
if ( $(this).data('scroll-offset') !== false ) { |
| 31 |
var tableOffset = $(this).parent().offset().top - $(this).data('scroll-offset'); |
| 32 |
if (adminBarVisible) { // Adjust offset for WP admin bar |
| 33 |
tableOffset -= 32; |
| 34 |
} |
| 35 |
$('html,body').animate({scrollTop: tableOffset}, 300); |
| 36 |
} |
| 37 |
}); |
| 38 |
|
| 39 |
// If 'search on click' feature enabled then add click handler for links in category and author columns. |
| 40 |
// When a category or author is clicked, the table will filter by that value |
| 41 |
if ( $(this).data('click-filter') ) { |
| 42 |
table.columns( ['category:name', 'author:name'] ).nodes().to$().each(function() { |
| 43 |
$(this).children('a').on('click', function() { |
| 44 |
table.search( $(this).text() ).draw(); |
| 45 |
return false; |
| 46 |
}); |
| 47 |
}); |
| 48 |
} |
| 49 |
|
| 50 |
}); // each table |
| 51 |
|
| 52 |
}); // end document.ready |
| 53 |
|
| 54 |
})(jQuery); |