From 04ad7a874610da4239b13a431366303bb34c5b23 Mon Sep 17 00:00:00 2001 From: Lem Smyth Date: Sat, 19 Feb 2022 16:56:57 -0600 Subject: [PATCH] enqueue js sort table --- admin/js/table-sort.js | 76 ++++++++++++++++++++++++++++++++++++++++++ classes/LBRY_Admin.php | 22 +++++++++--- 2 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 admin/js/table-sort.js diff --git a/admin/js/table-sort.js b/admin/js/table-sort.js new file mode 100644 index 0000000..7912668 --- /dev/null +++ b/admin/js/table-sort.js @@ -0,0 +1,76 @@ +jQuery(document).ready(function($) { + var compare = { + name: function(a, b) { + a = a.replace(/^@/, '') && a.replace(/-/g, ''); + b = b.replace(/^@/, '') && b.replace(/-/g, ''); + + if (a < b) { + return -1; + } else { + return a > b ? 1 : 0; + } + }, + lbryurl: function(a, b) { + a = a.replace(/^lbry:\/\/@/i, '') && a.replace(/#[a-zA-Z0-9]+/, '') && a.replace(/-/g, ''); + b = b.replace(/^lbry:\/\/@/i, '') && b.replace(/#[a-zA-Z0-9]+/, '') && b.replace(/-/g, ''); + + if (a < b) { + return -1; + } else { + return a > b ? 1 : 0; + } + }, + amount: function(a, b) { + a = a.split('.'); + b = b.split('.'); + + a = Number(a[0]) + Number(a[1]); + b = Number(b[0]) + Number(b[1]); + + return a - b; + }, + number: function(a, b) { + a = Number(a); + b = Number(b); + + return a - b; + }, + date: function(a, b) { + a = new Date(a); + b = new Date(b); + + return a - b; + } + }; + $('.lbry-channel-table').each(function() { + var $table = $(this); + var $tbody = $table.find('tbody'); + var $controls = $table.find('th'); + var rows = $tbody.find('tr').toArray(); + + $controls.on('click', function() { + var $header = $(this); + var order = $header.data('sort'); + var column; + + if ($header.is('.ascending') || $header.is('.descending')) { + $header.toggleClass('ascending descending'); + $tbody.append(rows.reverse()); + } else { + $header.addClass('ascending'); + $header.siblings().removeClass('ascending descending'); + if (compare.hasOwnProperty(order)) { + column = $controls.index(this); + + rows.sort(function(a, b) { + a = $(a).find('td').eq(column).text(); + b = $(b).find('td').eq(column).text(); + return compare[order](a, b); + }); + + $tbody.append(rows); + } + } + }); + }); +}); \ No newline at end of file diff --git a/classes/LBRY_Admin.php b/classes/LBRY_Admin.php index 88aabc0..3371f38 100644 --- a/classes/LBRY_Admin.php +++ b/classes/LBRY_Admin.php @@ -51,6 +51,20 @@ class LBRY_Admin } } add_action( 'admin_enqueue_scripts', 'load_admin_stylesheet' ); + + // Admin JS enqueue + function load_admin_script() { + if ( ( $_GET['page'] == 'lbrypress') && ( $_GET['tab'] == 'channels' ) ) { + wp_enqueue_script( + 'lbry-table-sort', + plugins_url( '/admin/js/table-sort.js', LBRY_PLUGIN_FILE ), + array('jquery'), + LBRY_VERSION, + true + ); + } + } + add_action( 'admin_enqueue_scripts', 'load_admin_script' ); // Admin Error Notices function lbry_plugin_not_configured_notice() { @@ -258,10 +272,10 @@ class LBRY_Admin - - - - + + + +
ChannelLBRY URLPostsSupportsChannelLBRY URLPostsSupports