| 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 |
// Bug in current version (v2.0.0) of Responsive extension so need to set column priorities manually from data-priority attribute |
| 16 |
// More info: https://datatables.net/forums/discussion/31630/attribute-data-priority-not-working |
| 17 |
var columnDefs = []; |
| 18 |
$(this).find('th').each(function( index ) { |
| 19 |
var priority = $(this).data('priority'); |
| 20 |
if ( priority ) { |
| 21 |
columnDefs.push( { responsivePriority: priority, targets: index } ); |
| 22 |
} |
| 23 |
}); |
| 24 |
|
| 25 |
var config = { |
| 26 |
responsive: true, |
| 27 |
columnDefs: columnDefs |
| 28 |
}; |
| 29 |
|
| 30 |
// Set language - defaults to English if not specified |
| 31 |
if ( (typeof posts_data_table !== 'undefined') && posts_data_table.langurl ) { |
| 32 |
config.language = { url: posts_data_table.langurl }; |
| 33 |
} |
| 34 |
|
| 35 |
// Initialise DataTable |
| 36 |
var table = $(this).DataTable( config ); |
| 37 |
|
| 38 |
// If scroll offset defined, animate back to top of table on next/previous page event |
| 39 |
$(this).on('page.dt', function() { |
| 40 |
console.log($(this).data('scroll-offset')); |
| 41 |
if ( $(this).data('scroll-offset') !== false ) { |
| 42 |
var tableOffset = $(this).parent().offset().top - $(this).data('scroll-offset'); |
| 43 |
if (adminBarVisible) { // Adjust offset for WP admin bar |
| 44 |
tableOffset -= 32; |
| 45 |
} |
| 46 |
$('html,body').animate({scrollTop: tableOffset}, 300); |
| 47 |
} |
| 48 |
}); |
| 49 |
|
| 50 |
// If 'search on click' feature enabled then add click handler for links in category and author columns. |
| 51 |
// When a category or author is clicked, the table will filter by that value |
| 52 |
if ( $(this).data('click-filter') ) { |
| 53 |
table.columns( ['category:name', 'author:name'] ).nodes().to$().each(function() { |
| 54 |
$(this).children('a').on('click', function() { |
| 55 |
table.search( $(this).text() ).draw(); |
| 56 |
return false; |
| 57 |
}); |
| 58 |
}); |
| 59 |
} |
| 60 |
|
| 61 |
}); // each table |
| 62 |
|
| 63 |
}); // end document.ready |
| 64 |
|
| 65 |
})(jQuery); |