Comments List Table JS: move the dynamic count functions into closure scope so that setCommentsList AND commentReply can access them. Update counts when replying inline.

Fixes #33403.


git-svn-id: https://develop.svn.wordpress.org/trunk@34517 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2015-09-24 20:15:54 +00:00
parent 1e42aeb3c4
commit 5b5b714694

View File

@ -3,17 +3,206 @@ var setCommentsList, theList, theExtraList, commentReply;
(function($) {
var getCount, updateCount, updateCountText, updatePending, updateApproved,
updateHtmlTitle, updateDashboardText, adminTitle = document.title;
updateHtmlTitle, updateDashboardText, adminTitle = document.title,
isDashboard = $('#dashboard_right_now').length,
titleDiv, titleRegEx;
getCount = function(el) {
var n = parseInt( el.html().replace(/[^0-9]+/g, ''), 10 );
if ( isNaN(n) ) {
return 0;
}
return n;
};
updateCount = function(el, n) {
var n1 = '';
if ( isNaN(n) ) {
return;
}
n = n < 1 ? '0' : n.toString();
if ( n.length > 3 ) {
while ( n.length > 3 ) {
n1 = thousandsSeparator + n.substr(n.length - 3) + n1;
n = n.substr(0, n.length - 3);
}
n = n + n1;
}
el.html(n);
};
updateApproved = function( diff, commentPostId ) {
var postSelector = '.post-com-count-' + commentPostId,
noClass = 'comment-count-no-comments',
approvedClass = 'comment-count-approved',
approved,
noComments;
updateCountText( 'span.approved-count', diff );
if ( ! commentPostId ) {
return;
}
// cache selectors to not get dupes
approved = $( 'span.' + approvedClass, postSelector );
noComments = $( 'span.' + noClass, postSelector );
approved.each(function() {
var a = $(this), n = getCount(a) + diff;
if ( n < 1 )
n = 0;
if ( 0 === n ) {
a.removeClass( approvedClass ).addClass( noClass );
} else {
a.addClass( approvedClass ).removeClass( noClass );
}
updateCount( a, n );
});
noComments.each(function() {
var a = $(this);
if ( diff > 0 ) {
a.removeClass( noClass ).addClass( approvedClass );
} else {
a.addClass( noClass ).removeClass( approvedClass );
}
updateCount( a, diff );
});
};
updateCountText = function( selector, diff ) {
$( selector ).each(function() {
var a = $(this), n = getCount(a) + diff;
if ( n < 1 ) {
n = 0;
}
updateCount( a, n );
});
};
updateDashboardText = function ( response ) {
if ( ! isDashboard || ! response || ! response.i18n_comments_text ) {
return;
}
var rightNow = $( '#dashboard_right_now' );
$( '.comment-count a', rightNow ).text( response.i18n_comments_text );
$( '.comment-mod-count a', rightNow ).text( response.i18n_moderation_text )
.parent()
[ response.in_moderation > 0 ? 'removeClass' : 'addClass' ]( 'hidden' );
};
updateHtmlTitle = function ( diff ) {
var newTitle, regExMatch, titleCount, commentFrag;
titleRegEx = titleRegEx || new RegExp( 'Comments (\\([0-9' + thousandsSeparator + ']+\\))?' );
// count funcs operate on a $'d element
titleDiv = titleDiv || $( '<div />' );
newTitle = adminTitle;
commentFrag = titleRegEx.exec( document.title );
if ( commentFrag ) {
commentFrag = commentFrag[0];
titleDiv.html( commentFrag );
titleCount = getCount( titleDiv ) + diff;
} else {
titleDiv.html( 0 );
titleCount = diff;
}
if ( titleCount >= 1 ) {
updateCount( titleDiv, titleCount );
regExMatch = titleRegEx.exec( document.title );
if ( regExMatch ) {
newTitle = document.title.replace( regExMatch[0], 'Comments (' + titleDiv.text() + ') ' );
}
} else {
regExMatch = titleRegEx.exec( newTitle );
if ( regExMatch ) {
newTitle = newTitle.replace( regExMatch[0], 'Comments' );
}
}
document.title = newTitle;
};
updatePending = function( diff, commentPostId ) {
var postSelector = '.post-com-count-' + commentPostId,
noClass = 'comment-count-no-pending',
noParentClass = 'post-com-count-no-pending',
pendingClass = 'comment-count-pending',
pending,
noPending;
if ( ! isDashboard ) {
updateHtmlTitle( diff );
}
$( 'span.pending-count' ).each(function() {
var a = $(this), n = getCount(a) + diff;
if ( n < 1 )
n = 0;
a.closest('.awaiting-mod')[ 0 === n ? 'addClass' : 'removeClass' ]('count-0');
updateCount( a, n );
});
if ( ! commentPostId ) {
return;
}
// cache selectors to not get dupes
pending = $( 'span.' + pendingClass, postSelector );
noPending = $( 'span.' + noClass, postSelector );
pending.each(function() {
var a = $(this), n = getCount(a) + diff;
if ( n < 1 )
n = 0;
if ( 0 === n ) {
a.parent().addClass( noParentClass );
a.removeClass( pendingClass ).addClass( noClass );
} else {
a.parent().removeClass( noParentClass );
a.addClass( pendingClass ).removeClass( noClass );
}
updateCount( a, n );
});
noPending.each(function() {
var a = $(this);
if ( diff > 0 ) {
a.parent().removeClass( noParentClass );
a.removeClass( noClass ).addClass( pendingClass );
} else {
a.parent().addClass( noParentClass );
a.addClass( noClass ).removeClass( pendingClass );
}
updateCount( a, diff );
});
};
setCommentsList = function() {
var totalInput, perPageInput, pageInput, dimAfter, delBefore, updateTotalCount, delAfter, refillTheExtraList, diff,
lastConfidentTime = 0, titleDiv, titleRegEx,
isDashboard = $('#dashboard_right_now').length;
lastConfidentTime = 0;
totalInput = $('input[name="_total"]', '#comments-form');
perPageInput = $('input[name="_per_page"]', '#comments-form');
pageInput = $('input[name="_page"]', '#comments-form');
// Updates the current total (stored in the _total input)
updateTotalCount = function( total, time, setConfidentTime ) {
if ( time < lastConfidentTime )
return;
if ( setConfidentTime )
lastConfidentTime = time;
totalInput.val( total.toString() );
};
// this fires when viewing "All"
dimAfter = function( r, settings ) {
var editRow, replyID, replyButton, response,
@ -108,192 +297,6 @@ setCommentsList = function() {
return settings;
};
// Updates the current total (stored in the _total input)
updateTotalCount = function( total, time, setConfidentTime ) {
if ( time < lastConfidentTime )
return;
if ( setConfidentTime )
lastConfidentTime = time;
totalInput.val( total.toString() );
};
getCount = function(el) {
var n = parseInt( el.html().replace(/[^0-9]+/g, ''), 10 );
if ( isNaN(n) )
return 0;
return n;
};
updateCount = function(el, n) {
var n1 = '';
if ( isNaN(n) )
return;
n = n < 1 ? '0' : n.toString();
if ( n.length > 3 ) {
while ( n.length > 3 ) {
n1 = thousandsSeparator + n.substr(n.length - 3) + n1;
n = n.substr(0, n.length - 3);
}
n = n + n1;
}
el.html(n);
};
updateHtmlTitle = function ( diff ) {
var newTitle, regExMatch, titleCount, commentFrag;
titleRegEx = titleRegEx || new RegExp( 'Comments (\\([0-9' + thousandsSeparator + ']+\\))?' );
// count funcs operate on a $'d element
titleDiv = titleDiv || $( '<div />' );
newTitle = adminTitle;
commentFrag = titleRegEx.exec( document.title );
if ( commentFrag ) {
commentFrag = commentFrag[0];
titleDiv.html( commentFrag );
titleCount = getCount( titleDiv ) + diff;
} else {
titleDiv.html( 0 );
titleCount = diff;
}
if ( titleCount >= 1 ) {
updateCount( titleDiv, titleCount );
regExMatch = titleRegEx.exec( document.title );
if ( regExMatch ) {
newTitle = document.title.replace( regExMatch[0], 'Comments (' + titleDiv.text() + ') ' );
}
} else {
regExMatch = titleRegEx.exec( newTitle );
if ( regExMatch ) {
newTitle = newTitle.replace( regExMatch[0], 'Comments' );
}
}
document.title = newTitle;
};
updatePending = function( diff, commentPostId ) {
var postSelector = '.post-com-count-' + commentPostId,
noClass = 'comment-count-no-pending',
noParentClass = 'post-com-count-no-pending',
pendingClass = 'comment-count-pending',
pending,
noPending;
if ( ! isDashboard ) {
updateHtmlTitle( diff );
}
$( 'span.pending-count' ).each(function() {
var a = $(this), n = getCount(a) + diff;
if ( n < 1 )
n = 0;
a.closest('.awaiting-mod')[ 0 === n ? 'addClass' : 'removeClass' ]('count-0');
updateCount( a, n );
});
if ( ! commentPostId ) {
return;
}
// cache selectors to not get dupes
pending = $( 'span.' + pendingClass, postSelector );
noPending = $( 'span.' + noClass, postSelector );
pending.each(function() {
var a = $(this), n = getCount(a) + diff;
if ( n < 1 )
n = 0;
if ( 0 === n ) {
a.parent().addClass( noParentClass );
a.removeClass( pendingClass ).addClass( noClass );
} else {
a.parent().removeClass( noParentClass );
a.addClass( pendingClass ).removeClass( noClass );
}
updateCount( a, n );
});
noPending.each(function() {
var a = $(this);
if ( diff > 0 ) {
a.parent().removeClass( noParentClass );
a.removeClass( noClass ).addClass( pendingClass );
} else {
a.parent().addClass( noParentClass );
a.addClass( noClass ).removeClass( pendingClass );
}
updateCount( a, diff );
});
};
updateApproved = function( diff, commentPostId ) {
var postSelector = '.post-com-count-' + commentPostId,
noClass = 'comment-count-no-comments',
approvedClass = 'comment-count-approved',
approved,
noComments;
updateCountText( 'span.approved-count', diff );
if ( ! commentPostId ) {
return;
}
// cache selectors to not get dupes
approved = $( 'span.' + approvedClass, postSelector );
noComments = $( 'span.' + noClass, postSelector );
approved.each(function() {
var a = $(this), n = getCount(a) + diff;
if ( n < 1 )
n = 0;
if ( 0 === n ) {
a.removeClass( approvedClass ).addClass( noClass );
} else {
a.addClass( approvedClass ).removeClass( noClass );
}
updateCount( a, n );
});
noComments.each(function() {
var a = $(this);
if ( diff > 0 ) {
a.removeClass( noClass ).addClass( approvedClass );
} else {
a.addClass( noClass ).removeClass( approvedClass );
}
updateCount( a, diff );
});
};
updateCountText = function( selector, diff ) {
$( selector ).each(function() {
var a = $(this), n = getCount(a) + diff;
if ( n < 1 ) {
n = 0;
}
updateCount( a, n );
});
};
updateDashboardText = function ( response ) {
if ( ! isDashboard || ! response || ! response.i18n_comments_text ) {
return;
}
var rightNow = $( '#dashboard_right_now' );
$( '.comment-count a', rightNow ).text( response.i18n_comments_text );
$( '.comment-mod-count a', rightNow ).text( response.i18n_moderation_text )
.parent()
[ response.in_moderation > 0 ? 'removeClass' : 'addClass' ]( 'hidden' );
};
// In admin-ajax.php, we send back the unix time stamp instead of 1 on success
delAfter = function( r, settings ) {
var total_items_i18n, total, animated, animatedCallback,
@ -794,7 +797,12 @@ commentReply = {
}
if ( r.supplemental.i18n_comments_text ) {
updateDashboardText( r.supplemental );
if ( isDashboard ) {
updateDashboardText( r.supplemental );
} else {
updateApproved( 1, r.supplemental.parent_post_id );
updateCountText( 'span.all-count', 1 );
}
}
c = $.trim(r.data); // Trim leading whitespaces