X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/dc1231b7312fbdca99e9e887cc2bb35a28f85cdc..refs/tags/wordpress-4.4:/wp-admin/js/edit-comments.js diff --git a/wp-admin/js/edit-comments.js b/wp-admin/js/edit-comments.js index b3e69bf7..7fc62deb 100644 --- a/wp-admin/js/edit-comments.js +++ b/wp-admin/js/edit-comments.js @@ -2,7 +2,187 @@ var setCommentsList, theList, theExtraList, commentReply; (function($) { -var getCount, updateCount, updatePending; +var getCount, updateCount, updateCountText, updatePending, updateApproved, + 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( adminCommentsL10n.docTitleCommentsCount.replace( '%s', '\\([0-9' + thousandsSeparator + ']+\\)' ) + '?' ); + // count funcs operate on a $'d element + titleDiv = titleDiv || $( '
' ); + 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], adminCommentsL10n.docTitleCommentsCount.replace( '%s', titleDiv.text() ) + ' ' ); + } + } else { + regExMatch = titleRegEx.exec( newTitle ); + if ( regExMatch ) { + newTitle = newTitle.replace( regExMatch[0], adminCommentsL10n.docTitleComments ); + } + } + 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, @@ -12,10 +192,26 @@ setCommentsList = function() { 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, + var editRow, replyID, replyButton, response, c = $( '#' + settings.element ); + if ( true !== settings.parsed ) { + response = settings.parsed.responses[0]; + } + editRow = $('#replyrow'); replyID = $('#comment_ID', editRow).val(); replyButton = $('#replybtn', editRow); @@ -33,7 +229,14 @@ setCommentsList = function() { } diff = $('#' + settings.element).is('.' + settings.dimClass) ? 1 : -1; - updatePending( diff ); + if ( response ) { + updateDashboardText( response.supplemental ); + updatePending( diff, response.supplemental.postId ); + updateApproved( -1 * diff, response.supplemental.postId ); + } else { + updatePending( diff ); + updateApproved( -1 * diff ); + } }; // Send current total, page, per_page and url @@ -81,111 +284,177 @@ setCommentsList = function() { a.attr('class', 'vim-z vim-destructive'); $('.avatar', el).first().clone().prependTo('#undo-' + id + ' .' + action + '-undo-inside'); - a.click(function(){ + a.click(function( e ){ + e.preventDefault(); list.wpList.del(this); $('#undo-' + id).css( {backgroundColor:'#ceb'} ).fadeOut(350, function(){ $(this).remove(); $('#comment-' + id).css('backgroundColor', '').fadeIn(300, function(){ $(this).show(); }); }); - return false; }); } return settings; }; - // Updates the current total (stored in the _total input) - updateTotalCount = function( total, time, setConfidentTime ) { - if ( time < lastConfidentTime ) - return; + // 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, + response = true === settings.parsed ? {} : settings.parsed.responses[0], + commentStatus = true === settings.parsed ? '' : response.supplemental.status, + commentPostId = true === settings.parsed ? '' : response.supplemental.postId, + newTotal = true === settings.parsed ? '' : response.supplemental, + + targetParent = $( settings.target ).parent(), + commentRow = $('#' + settings.element), + + spamDiff, trashDiff, pendingDiff, approvedDiff, + + approved = commentRow.hasClass( 'approved' ), + unapproved = commentRow.hasClass( 'unapproved' ), + spammed = commentRow.hasClass( 'spam' ), + trashed = commentRow.hasClass( 'trash' ); + + updateDashboardText( newTotal ); + + // the order of these checks is important + // .unspam can also have .approve or .unapprove + // .untrash can also have .approve or .unapprove + + if ( targetParent.is( 'span.undo' ) ) { + // the comment was spammed + if ( targetParent.hasClass( 'unspam' ) ) { + spamDiff = -1; + + if ( 'trash' === commentStatus ) { + trashDiff = 1; + } else if ( '1' === commentStatus ) { + approvedDiff = 1; + } else if ( '0' === commentStatus ) { + pendingDiff = 1; + } - if ( setConfidentTime ) - lastConfidentTime = time; + // the comment was trashed + } else if ( targetParent.hasClass( 'untrash' ) ) { + trashDiff = -1; - totalInput.val( total.toString() ); - }; + if ( 'spam' === commentStatus ) { + spamDiff = 1; + } else if ( '1' === commentStatus ) { + approvedDiff = 1; + } else if ( '0' === commentStatus ) { + pendingDiff = 1; + } + } - getCount = function(el) { - var n = parseInt( el.html().replace(/[^0-9]+/g, ''), 10 ); - if ( isNaN(n) ) - return 0; - return n; - }; + // user clicked "Spam" + } else if ( targetParent.is( 'span.spam' ) ) { + // the comment is currently approved + if ( approved ) { + approvedDiff = -1; + // the comment is currently pending + } else if ( unapproved ) { + pendingDiff = -1; + // the comment was in the trash + } else if ( trashed ) { + trashDiff = -1; + } + // you can't spam an item on the spam screen + spamDiff = 1; + + // user clicked "Unspam" + } else if ( targetParent.is( 'span.unspam' ) ) { + if ( approved ) { + pendingDiff = 1; + } else if ( unapproved ) { + approvedDiff = 1; + } else if ( trashed ) { + // the comment was previously approved + if ( targetParent.hasClass( 'approve' ) ) { + approvedDiff = 1; + // the comment was previously pending + } else if ( targetParent.hasClass( 'unapprove' ) ) { + pendingDiff = 1; + } + } else if ( spammed ) { + if ( targetParent.hasClass( 'approve' ) ) { + approvedDiff = 1; - 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); + } else if ( targetParent.hasClass( 'unapprove' ) ) { + pendingDiff = 1; + } + } + // you can Unspam an item on the spam screen + spamDiff = -1; + + // user clicked "Trash" + } else if ( targetParent.is( 'span.trash' ) ) { + if ( approved ) { + approvedDiff = -1; + } else if ( unapproved ) { + pendingDiff = -1; + // the comment was in the spam queue + } else if ( spammed ) { + spamDiff = -1; + } + // you can't trash an item on the trash screen + trashDiff = 1; + + // user clicked "Restore" + } else if ( targetParent.is( 'span.untrash' ) ) { + if ( approved ) { + pendingDiff = 1; + } else if ( unapproved ) { + approvedDiff = 1; + } else if ( trashed ) { + if ( targetParent.hasClass( 'approve' ) ) { + approvedDiff = 1; + } else if ( targetParent.hasClass( 'unapprove' ) ) { + pendingDiff = 1; + } + } + // you can't go from trash to spam + // you can untrash on the trash screen + trashDiff = -1; + + // User clicked "Approve" + } else if ( targetParent.is( 'span.approve:not(.unspam):not(.untrash)' ) ) { + approvedDiff = 1; + pendingDiff = -1; + + // User clicked "Unapprove" + } else if ( targetParent.is( 'span.unapprove:not(.unspam):not(.untrash)' ) ) { + approvedDiff = -1; + pendingDiff = 1; + + // User clicked "Delete Permanently" + } else if ( targetParent.is( 'span.delete' ) ) { + if ( spammed ) { + spamDiff = -1; + } else if ( trashed ) { + trashDiff = -1; } - n = n + n1; } - el.html(n); - }; - - updatePending = function( 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 ); - }); - }; - - // 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, spam, trash, pending, - untrash = $(settings.target).parent().is('span.untrash'), - unspam = $(settings.target).parent().is('span.unspam'), - unapproved = $('#' + settings.element).is('.unapproved'); - - function getUpdate(s) { - if ( $(settings.target).parent().is('span.' + s) ) - return 1; - else if ( $('#' + settings.element).is('.' + s) ) - return -1; - return 0; + if ( pendingDiff ) { + updatePending( pendingDiff, commentPostId ); + updateCountText( 'span.all-count', pendingDiff ); } - if ( untrash ) - trash = -1; - else - trash = getUpdate('trash'); - - if ( unspam ) - spam = -1; - else - spam = getUpdate('spam'); - - if ( $(settings.target).parent().is('span.unapprove') || ( ( untrash || unspam ) && unapproved ) ) { - // a comment was 'deleted' from another list (e.g. approved, spam, trash) and moved to pending, - // or a trash/spam of a pending comment was undone - pending = 1; - } else if ( unapproved ) { - // a pending comment was trashed/spammed/approved - pending = -1; + if ( approvedDiff ) { + updateApproved( approvedDiff, commentPostId ); + updateCountText( 'span.all-count', approvedDiff ); } - if ( pending ) - updatePending(pending); - - $('span.spam-count').each( function() { - var a = $(this), n = getCount(a) + spam; - updateCount(a, n); - }); + if ( spamDiff ) { + updateCountText( 'span.spam-count', spamDiff ); + } - $('span.trash-count').each( function() { - var a = $(this), n = getCount(a) + trash; - updateCount(a, n); - }); + if ( trashDiff ) { + updateCountText( 'span.trash-count', trashDiff ); + } - if ( ! $('#dashboard_right_now').length ) { + if ( ! isDashboard ) { total = totalInput.val() ? parseInt( totalInput.val(), 10 ) : 0; if ( $(settings.target).parent().is('span.undo') ) total++; @@ -195,26 +464,43 @@ setCommentsList = function() { if ( total < 0 ) total = 0; - if ( ( 'object' == typeof r ) && lastConfidentTime < settings.parsed.responses[0].supplemental.time ) { - total_items_i18n = settings.parsed.responses[0].supplemental.total_items_i18n || ''; - if ( total_items_i18n ) { - $('.displaying-num').text( total_items_i18n ); - $('.total-pages').text( settings.parsed.responses[0].supplemental.total_pages_i18n ); - $('.tablenav-pages').find('.next-page, .last-page').toggleClass('disabled', settings.parsed.responses[0].supplemental.total_pages == $('.current-page').val()); + if ( 'object' === typeof r ) { + if ( response.supplemental.total_items_i18n && lastConfidentTime < response.supplemental.time ) { + total_items_i18n = response.supplemental.total_items_i18n || ''; + if ( total_items_i18n ) { + $('.displaying-num').text( total_items_i18n ); + $('.total-pages').text( response.supplemental.total_pages_i18n ); + $('.tablenav-pages').find('.next-page, .last-page').toggleClass('disabled', response.supplemental.total_pages == $('.current-page').val()); + } + updateTotalCount( total, response.supplemental.time, true ); + } else if ( response.supplemental.time ) { + updateTotalCount( total, response.supplemental.time, false ); } - updateTotalCount( total, settings.parsed.responses[0].supplemental.time, true ); } else { updateTotalCount( total, r, false ); } } - if ( ! theExtraList || theExtraList.size() === 0 || theExtraList.children().size() === 0 || untrash || unspam ) { + if ( ! theExtraList || theExtraList.size() === 0 || theExtraList.children().size() === 0 ) { return; } - theList.get(0).wpList.add( theExtraList.children(':eq(0)').remove().clone() ); + theList.get(0).wpList.add( theExtraList.children( ':eq(0):not(.no-items)' ).remove().clone() ); refillTheExtraList(); + + animated = $( ':animated', '#the-comment-list' ); + animatedCallback = function () { + if ( ! $( '#the-comment-list tr:visible' ).length ) { + theList.get(0).wpList.add( theExtraList.find( '.no-items' ).clone() ); + } + }; + + if ( animated.length ) { + animated.promise().done( animatedCallback ); + } else { + animatedCallback(); + } }; refillTheExtraList = function(ev) { @@ -279,7 +565,7 @@ commentReply = { $('a.cancel', row).click(function() { return commentReply.revert(); }); $('a.save', row).click(function() { return commentReply.send(); }); - $('input#author, input#author-email, input#author-url', row).keypress(function(e){ + $( 'input#author-name, input#author-email, input#author-url', row ).keypress( function( e ) { if ( e.which == 13 ) { commentReply.send(); e.preventDefault(); @@ -373,7 +659,7 @@ commentReply = { action = action || 'replyto'; act = 'edit' == action ? 'edit' : 'replyto'; act = t.act = act + '-comment'; - colspanVal = $( 'th:visible, td:visible', c ).length; + colspanVal = $( '> th:visible, > td:visible', c ).length; // Make sure it's actually a table and there's a `colspan` value to apply. if ( editRow.hasClass( 'inline-edit-row' ) && 0 !== colspanVal ) { @@ -385,12 +671,12 @@ commentReply = { $('#comment_ID', editRow).val(comment_id); if ( action == 'edit' ) { - $('#author', editRow).val( $('div.author', rowData).text() ); + $( '#author-name', editRow ).val( $( 'div.author', rowData ).text() ); $('#author-email', editRow).val( $('div.author-email', rowData).text() ); $('#author-url', editRow).val( $('div.author-url', rowData).text() ); $('#status', editRow).val( $('div.comment_status', rowData).text() ); $('#replycontent', editRow).val( $('textarea.comment', rowData).val() ); - $('#edithead, #savebtn', editRow).show(); + $( '#edithead, #editlegend, #savebtn', editRow ).show(); $('#replyhead, #replybtn, #addhead, #addbtn', editRow).hide(); if ( h > 120 ) { @@ -405,12 +691,12 @@ commentReply = { }); } else if ( action == 'add' ) { $('#addhead, #addbtn', editRow).show(); - $('#replyhead, #replybtn, #edithead, #savebtn', editRow).hide(); + $( '#replyhead, #replybtn, #edithead, #editlegend, #savebtn', editRow ) .hide(); $('#the-comment-list').prepend(editRow); $('#replyrow').fadeIn(300); } else { replyButton = $('#replybtn', editRow); - $('#edithead, #savebtn, #addhead, #addbtn', editRow).hide(); + $( '#edithead, #editlegend, #savebtn, #addhead, #addbtn', editRow ).hide(); $('#replyhead, #replybtn', editRow).show(); c.after(editRow); @@ -500,7 +786,7 @@ commentReply = { if ( r.supplemental.parent_approved ) { pid = $('#comment-' + r.supplemental.parent_approved); - updatePending( -1 ); + updatePending( -1, r.supplemental.parent_post_id ); if ( this.comments_listing == 'moderated' ) { pid.animate( { 'backgroundColor':'#CCEEBB' }, 400, function(){ @@ -510,6 +796,15 @@ commentReply = { } } + if ( r.supplemental.i18n_comments_text ) { + 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 $(c).hide(); $('#replyrow').after(c); @@ -559,7 +854,10 @@ $(document).ready(function(){ setCommentsList(); commentReply.init(); - $(document).delegate('span.delete a.delete', 'click', function(){return false;}); + + $(document).on( 'click', 'span.delete a.delete', function( e ) { + e.preventDefault(); + }); if ( typeof $.table_hotkeys != 'undefined' ) { make_hotkeys_redirect = function(which) {