Skip to content

Commit 3cc5625

Browse files
committed
Don't duplicate table nav update code
Simplifies and avoids repetition.
1 parent d3145cd commit 3cc5625

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

src/js/_enqueues/admin/tags.js

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,7 @@ jQuery( function($) {
6161
}
6262
tr.fadeOut('normal', function() {
6363
tr.remove();
64-
65-
if ( $('#the-list tr').length === 0 ) {
66-
$('#the-list').append(
67-
'<tr class="no-items"><td class="colspanchange" colspan="5">' +
68-
wp.i18n.__( 'No tags found.' ) +
69-
'</td></tr>'
70-
);
71-
72-
$('.tablenav').hide();
73-
$('p.search-box').hide();
74-
}
75-
76-
var currentCount = parseInt( $('.tablenav-pages .displaying-num').first().text().match(/\d+/) ) || 0;
77-
var itemCount = currentCount - 1 || 0;
78-
var itemText = wp.i18n._n( '%d item', '%d items', itemCount );
79-
$('.tablenav-pages .displaying-num').text( itemText );
64+
updateTableNavCount();
8065
});
8166

8267
/**
@@ -120,6 +105,33 @@ jQuery( function($) {
120105
tr.find( ':input, a' ).prop( 'disabled', false ).removeAttr( 'tabindex' );
121106
}
122107

108+
/**
109+
* Update the row count for table navigation..
110+
*
111+
* @return {void}
112+
*/
113+
function updateTableNavCount( action = 'remove' ) {
114+
var currentCount = parseInt( $('.tablenav-pages .displaying-num').first().text().match(/\d+/) ) || 0;
115+
var itemCount = ( 'remove' === action ) ? currentCount - 1 || 0 : currentCount + 1;
116+
var itemText = wp.i18n.sprintf( wp.i18n._n( '%d item', '%d items', itemCount ), itemCount );
117+
$('.tablenav-pages .displaying-num').text( itemText );
118+
// Show the tablenav if row count positive.
119+
if ( itemCount === 1 ) {
120+
$('.tablenav').show();
121+
$('p.search-box').show();
122+
}
123+
if ( $('#the-list tr').length === 0 ) {
124+
$('#the-list').append(
125+
'<tr class="no-items"><td class="colspanchange" colspan="5">' +
126+
wp.i18n.__( 'No tags found.' ) +
127+
'</td></tr>'
128+
);
129+
130+
$('.tablenav').hide();
131+
$('p.search-box').hide();
132+
}
133+
}
134+
123135
/**
124136
* Adds a deletion confirmation when removing a tag.
125137
*
@@ -210,15 +222,7 @@ jQuery( function($) {
210222

211223
$('input:not([type="checkbox"]):not([type="radio"]):not([type="button"]):not([type="submit"]):not([type="reset"]):visible, textarea:visible', form).val('');
212224

213-
var currentCount = parseInt( $('.tablenav-pages .displaying-num').first().text().match(/\d+/) ) || 0;
214-
var itemCount = currentCount + 1 || 0;
215-
var itemText = wp.i18n._n( '%d item', '%d items', itemCount );
216-
$('.tablenav-pages .displaying-num').text( itemText );
217-
218-
if ( itemCount === 1 ) {
219-
$('.tablenav').show();
220-
$('p.search-box').show();
221-
}
225+
updateTableNavCount( 'add' );
222226
});
223227

224228
return false;

0 commit comments

Comments
 (0)