home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress / wp-admin / js / edit-comments.js < prev    next >
Encoding:
JavaScript  |  2017-10-02  |  27.3 KB  |  977 lines

  1. /* global adminCommentsL10n, thousandsSeparator, list_args, QTags, ajaxurl, wpAjax */
  2. var setCommentsList, theList, theExtraList, commentReply;
  3.  
  4. (function($) {
  5. var getCount, updateCount, updateCountText, updatePending, updateApproved,
  6.     updateHtmlTitle, updateDashboardText, adminTitle = document.title,
  7.     isDashboard = $('#dashboard_right_now').length,
  8.     titleDiv, titleRegEx;
  9.  
  10.     getCount = function(el) {
  11.         var n = parseInt( el.html().replace(/[^0-9]+/g, ''), 10 );
  12.         if ( isNaN(n) ) {
  13.             return 0;
  14.         }
  15.         return n;
  16.     };
  17.  
  18.     updateCount = function(el, n) {
  19.         var n1 = '';
  20.         if ( isNaN(n) ) {
  21.             return;
  22.         }
  23.         n = n < 1 ? '0' : n.toString();
  24.         if ( n.length > 3 ) {
  25.             while ( n.length > 3 ) {
  26.                 n1 = thousandsSeparator + n.substr(n.length - 3) + n1;
  27.                 n = n.substr(0, n.length - 3);
  28.             }
  29.             n = n + n1;
  30.         }
  31.         el.html(n);
  32.     };
  33.  
  34.     updateApproved = function( diff, commentPostId ) {
  35.         var postSelector = '.post-com-count-' + commentPostId,
  36.             noClass = 'comment-count-no-comments',
  37.             approvedClass = 'comment-count-approved',
  38.             approved,
  39.             noComments;
  40.  
  41.         updateCountText( 'span.approved-count', diff );
  42.  
  43.         if ( ! commentPostId ) {
  44.             return;
  45.         }
  46.  
  47.         // cache selectors to not get dupes
  48.         approved = $( 'span.' + approvedClass, postSelector );
  49.         noComments = $( 'span.' + noClass, postSelector );
  50.  
  51.         approved.each(function() {
  52.             var a = $(this), n = getCount(a) + diff;
  53.             if ( n < 1 )
  54.                 n = 0;
  55.  
  56.             if ( 0 === n ) {
  57.                 a.removeClass( approvedClass ).addClass( noClass );
  58.             } else {
  59.                 a.addClass( approvedClass ).removeClass( noClass );
  60.             }
  61.             updateCount( a, n );
  62.         });
  63.  
  64.         noComments.each(function() {
  65.             var a = $(this);
  66.             if ( diff > 0 ) {
  67.                 a.removeClass( noClass ).addClass( approvedClass );
  68.             } else {
  69.                 a.addClass( noClass ).removeClass( approvedClass );
  70.             }
  71.             updateCount( a, diff );
  72.         });
  73.     };
  74.  
  75.     updateCountText = function( selector, diff ) {
  76.         $( selector ).each(function() {
  77.             var a = $(this), n = getCount(a) + diff;
  78.             if ( n < 1 ) {
  79.                 n = 0;
  80.             }
  81.             updateCount( a, n );
  82.         });
  83.     };
  84.  
  85.     updateDashboardText = function ( response ) {
  86.         if ( ! isDashboard || ! response || ! response.i18n_comments_text ) {
  87.             return;
  88.         }
  89.  
  90.         var rightNow = $( '#dashboard_right_now' );
  91.  
  92.         $( '.comment-count a', rightNow ).text( response.i18n_comments_text );
  93.         $( '.comment-mod-count a', rightNow ).text( response.i18n_moderation_text )
  94.             .parent()
  95.             [ response.in_moderation > 0 ? 'removeClass' : 'addClass' ]( 'hidden' );
  96.     };
  97.  
  98.     updateHtmlTitle = function ( diff ) {
  99.         var newTitle, regExMatch, titleCount, commentFrag;
  100.  
  101.         titleRegEx = titleRegEx || new RegExp( adminCommentsL10n.docTitleCommentsCount.replace( '%s', '\\([0-9' + thousandsSeparator + ']+\\)' ) + '?' );
  102.         // count funcs operate on a $'d element
  103.         titleDiv = titleDiv || $( '<div />' );
  104.         newTitle = adminTitle;
  105.  
  106.         commentFrag = titleRegEx.exec( document.title );
  107.         if ( commentFrag ) {
  108.             commentFrag = commentFrag[0];
  109.             titleDiv.html( commentFrag );
  110.             titleCount = getCount( titleDiv ) + diff;
  111.         } else {
  112.             titleDiv.html( 0 );
  113.             titleCount = diff;
  114.         }
  115.  
  116.         if ( titleCount >= 1 ) {
  117.             updateCount( titleDiv, titleCount );
  118.             regExMatch = titleRegEx.exec( document.title );
  119.             if ( regExMatch ) {
  120.                 newTitle = document.title.replace( regExMatch[0], adminCommentsL10n.docTitleCommentsCount.replace( '%s', titleDiv.text() ) + ' ' );
  121.             }
  122.         } else {
  123.             regExMatch = titleRegEx.exec( newTitle );
  124.             if ( regExMatch ) {
  125.                 newTitle = newTitle.replace( regExMatch[0], adminCommentsL10n.docTitleComments );
  126.             }
  127.         }
  128.         document.title = newTitle;
  129.     };
  130.  
  131.     updatePending = function( diff, commentPostId ) {
  132.         var postSelector = '.post-com-count-' + commentPostId,
  133.             noClass = 'comment-count-no-pending',
  134.             noParentClass = 'post-com-count-no-pending',
  135.             pendingClass = 'comment-count-pending',
  136.             pending,
  137.             noPending;
  138.  
  139.         if ( ! isDashboard ) {
  140.             updateHtmlTitle( diff );
  141.         }
  142.  
  143.         $( 'span.pending-count' ).each(function() {
  144.             var a = $(this), n = getCount(a) + diff;
  145.             if ( n < 1 )
  146.                 n = 0;
  147.             a.closest('.awaiting-mod')[ 0 === n ? 'addClass' : 'removeClass' ]('count-0');
  148.             updateCount( a, n );
  149.         });
  150.  
  151.         if ( ! commentPostId ) {
  152.             return;
  153.         }
  154.  
  155.         // cache selectors to not get dupes
  156.         pending = $( 'span.' + pendingClass, postSelector );
  157.         noPending = $( 'span.' + noClass, postSelector );
  158.  
  159.         pending.each(function() {
  160.             var a = $(this), n = getCount(a) + diff;
  161.             if ( n < 1 )
  162.                 n = 0;
  163.  
  164.             if ( 0 === n ) {
  165.                 a.parent().addClass( noParentClass );
  166.                 a.removeClass( pendingClass ).addClass( noClass );
  167.             } else {
  168.                 a.parent().removeClass( noParentClass );
  169.                 a.addClass( pendingClass ).removeClass( noClass );
  170.             }
  171.             updateCount( a, n );
  172.         });
  173.  
  174.         noPending.each(function() {
  175.             var a = $(this);
  176.             if ( diff > 0 ) {
  177.                 a.parent().removeClass( noParentClass );
  178.                 a.removeClass( noClass ).addClass( pendingClass );
  179.             } else {
  180.                 a.parent().addClass( noParentClass );
  181.                 a.addClass( noClass ).removeClass( pendingClass );
  182.             }
  183.             updateCount( a, diff );
  184.         });
  185.     };
  186.  
  187. setCommentsList = function() {
  188.     var totalInput, perPageInput, pageInput, dimAfter, delBefore, updateTotalCount, delAfter, refillTheExtraList, diff,
  189.         lastConfidentTime = 0;
  190.  
  191.     totalInput = $('input[name="_total"]', '#comments-form');
  192.     perPageInput = $('input[name="_per_page"]', '#comments-form');
  193.     pageInput = $('input[name="_page"]', '#comments-form');
  194.  
  195.     // Updates the current total (stored in the _total input)
  196.     updateTotalCount = function( total, time, setConfidentTime ) {
  197.         if ( time < lastConfidentTime )
  198.             return;
  199.  
  200.         if ( setConfidentTime )
  201.             lastConfidentTime = time;
  202.  
  203.         totalInput.val( total.toString() );
  204.     };
  205.  
  206.     // this fires when viewing "All"
  207.     dimAfter = function( r, settings ) {
  208.         var editRow, replyID, replyButton, response,
  209.             c = $( '#' + settings.element );
  210.  
  211.         if ( true !== settings.parsed ) {
  212.             response = settings.parsed.responses[0];
  213.         }
  214.  
  215.         editRow = $('#replyrow');
  216.         replyID = $('#comment_ID', editRow).val();
  217.         replyButton = $('#replybtn', editRow);
  218.  
  219.         if ( c.is('.unapproved') ) {
  220.             if ( settings.data.id == replyID )
  221.                 replyButton.text(adminCommentsL10n.replyApprove);
  222.  
  223.             c.find( '.row-actions span.view' ).addClass( 'hidden' ).end()
  224.                 .find( 'div.comment_status' ).html( '0' );
  225.  
  226.         } else {
  227.             if ( settings.data.id == replyID )
  228.                 replyButton.text(adminCommentsL10n.reply);
  229.  
  230.             c.find( '.row-actions span.view' ).removeClass( 'hidden' ).end()
  231.                 .find( 'div.comment_status' ).html( '1' );
  232.         }
  233.  
  234.         diff = $('#' + settings.element).is('.' + settings.dimClass) ? 1 : -1;
  235.         if ( response ) {
  236.             updateDashboardText( response.supplemental );
  237.             updatePending( diff, response.supplemental.postId );
  238.             updateApproved( -1 * diff, response.supplemental.postId );
  239.         } else {
  240.             updatePending( diff );
  241.             updateApproved( -1 * diff  );
  242.         }
  243.     };
  244.  
  245.     // Send current total, page, per_page and url
  246.     delBefore = function( settings, list ) {
  247.         var note, id, el, n, h, a, author,
  248.             action = false,
  249.             wpListsData = $( settings.target ).attr( 'data-wp-lists' );
  250.  
  251.         settings.data._total = totalInput.val() || 0;
  252.         settings.data._per_page = perPageInput.val() || 0;
  253.         settings.data._page = pageInput.val() || 0;
  254.         settings.data._url = document.location.href;
  255.         settings.data.comment_status = $('input[name="comment_status"]', '#comments-form').val();
  256.  
  257.         if ( wpListsData.indexOf(':trash=1') != -1 )
  258.             action = 'trash';
  259.         else if ( wpListsData.indexOf(':spam=1') != -1 )
  260.             action = 'spam';
  261.  
  262.         if ( action ) {
  263.             id = wpListsData.replace(/.*?comment-([0-9]+).*/, '$1');
  264.             el = $('#comment-' + id);
  265.             note = $('#' + action + '-undo-holder').html();
  266.  
  267.             el.find('.check-column :checkbox').prop('checked', false); // Uncheck the row so as not to be affected by Bulk Edits.
  268.  
  269.             if ( el.siblings('#replyrow').length && commentReply.cid == id )
  270.                 commentReply.close();
  271.  
  272.             if ( el.is('tr') ) {
  273.                 n = el.children(':visible').length;
  274.                 author = $('.author strong', el).text();
  275.                 h = $('<tr id="undo-' + id + '" class="undo un' + action + '" style="display:none;"><td colspan="' + n + '">' + note + '</td></tr>');
  276.             } else {
  277.                 author = $('.comment-author', el).text();
  278.                 h = $('<div id="undo-' + id + '" style="display:none;" class="undo un' + action + '">' + note + '</div>');
  279.             }
  280.  
  281.             el.before(h);
  282.  
  283.             $('strong', '#undo-' + id).text(author);
  284.             a = $('.undo a', '#undo-' + id);
  285.             a.attr('href', 'comment.php?action=un' + action + 'comment&c=' + id + '&_wpnonce=' + settings.data._ajax_nonce);
  286.             a.attr('data-wp-lists', 'delete:the-comment-list:comment-' + id + '::un' + action + '=1');
  287.             a.attr('class', 'vim-z vim-destructive');
  288.             $('.avatar', el).first().clone().prependTo('#undo-' + id + ' .' + action + '-undo-inside');
  289.  
  290.             a.click(function( e ){
  291.                 e.preventDefault();
  292.                 e.stopPropagation(); // ticket #35904
  293.                 list.wpList.del(this);
  294.                 $('#undo-' + id).css( {backgroundColor:'#ceb'} ).fadeOut(350, function(){
  295.                     $(this).remove();
  296.                     $('#comment-' + id).css('backgroundColor', '').fadeIn(300, function(){ $(this).show(); });
  297.                 });
  298.             });
  299.         }
  300.  
  301.         return settings;
  302.     };
  303.  
  304.     // In admin-ajax.php, we send back the unix time stamp instead of 1 on success
  305.     delAfter = function( r, settings ) {
  306.         var total_items_i18n, total, animated, animatedCallback,
  307.             response = true === settings.parsed ? {} : settings.parsed.responses[0],
  308.             commentStatus = true === settings.parsed ? '' : response.supplemental.status,
  309.             commentPostId = true === settings.parsed ? '' : response.supplemental.postId,
  310.             newTotal = true === settings.parsed ? '' : response.supplemental,
  311.  
  312.             targetParent = $( settings.target ).parent(),
  313.             commentRow = $('#' + settings.element),
  314.  
  315.             spamDiff, trashDiff, pendingDiff, approvedDiff,
  316.  
  317.             approved = commentRow.hasClass( 'approved' ),
  318.             unapproved = commentRow.hasClass( 'unapproved' ),
  319.             spammed = commentRow.hasClass( 'spam' ),
  320.             trashed = commentRow.hasClass( 'trash' ),
  321.             undoing = false; // ticket #35904
  322.  
  323.         updateDashboardText( newTotal );
  324.  
  325.         // the order of these checks is important
  326.         // .unspam can also have .approve or .unapprove
  327.         // .untrash can also have .approve or .unapprove
  328.  
  329.         if ( targetParent.is( 'span.undo' ) ) {
  330.             // the comment was spammed
  331.             if ( targetParent.hasClass( 'unspam' ) ) {
  332.                 spamDiff = -1;
  333.  
  334.                 if ( 'trash' === commentStatus ) {
  335.                     trashDiff = 1;
  336.                 } else if ( '1' === commentStatus ) {
  337.                     approvedDiff = 1;
  338.                 } else if ( '0' === commentStatus ) {
  339.                     pendingDiff = 1;
  340.                 }
  341.  
  342.             // the comment was trashed
  343.             } else if ( targetParent.hasClass( 'untrash' ) ) {
  344.                 trashDiff = -1;
  345.  
  346.                 if ( 'spam' === commentStatus ) {
  347.                     spamDiff = 1;
  348.                 } else if ( '1' === commentStatus ) {
  349.                     approvedDiff = 1;
  350.                 } else if ( '0' === commentStatus ) {
  351.                     pendingDiff = 1;
  352.                 }
  353.             }
  354.  
  355.             undoing = true;
  356.  
  357.         // user clicked "Spam"
  358.         } else if ( targetParent.is( 'span.spam' ) ) {
  359.             // the comment is currently approved
  360.             if ( approved ) {
  361.                 approvedDiff = -1;
  362.             // the comment is currently pending
  363.             } else if ( unapproved ) {
  364.                 pendingDiff = -1;
  365.             // the comment was in the trash
  366.             } else if ( trashed ) {
  367.                 trashDiff = -1;
  368.             }
  369.             // you can't spam an item on the spam screen
  370.             spamDiff = 1;
  371.  
  372.         // user clicked "Unspam"
  373.         } else if ( targetParent.is( 'span.unspam' ) ) {
  374.             if ( approved ) {
  375.                 pendingDiff = 1;
  376.             } else if ( unapproved ) {
  377.                 approvedDiff = 1;
  378.             } else if ( trashed ) {
  379.                 // the comment was previously approved
  380.                 if ( targetParent.hasClass( 'approve' ) ) {
  381.                     approvedDiff = 1;
  382.                 // the comment was previously pending
  383.                 } else if ( targetParent.hasClass( 'unapprove' ) ) {
  384.                     pendingDiff = 1;
  385.                 }
  386.             } else if ( spammed ) {
  387.                 if ( targetParent.hasClass( 'approve' ) ) {
  388.                     approvedDiff = 1;
  389.  
  390.                 } else if ( targetParent.hasClass( 'unapprove' ) ) {
  391.                     pendingDiff = 1;
  392.                 }
  393.             }
  394.             // you can Unspam an item on the spam screen
  395.             spamDiff = -1;
  396.  
  397.         // user clicked "Trash"
  398.         } else if ( targetParent.is( 'span.trash' ) ) {
  399.             if ( approved ) {
  400.                 approvedDiff = -1;
  401.             } else if ( unapproved ) {
  402.                 pendingDiff = -1;
  403.             // the comment was in the spam queue
  404.             } else if ( spammed ) {
  405.                 spamDiff = -1;
  406.             }
  407.             // you can't trash an item on the trash screen
  408.             trashDiff = 1;
  409.  
  410.         // user clicked "Restore"
  411.         } else if ( targetParent.is( 'span.untrash' ) ) {
  412.             if ( approved ) {
  413.                 pendingDiff = 1;
  414.             } else if ( unapproved ) {
  415.                 approvedDiff = 1;
  416.             } else if ( trashed ) {
  417.                 if ( targetParent.hasClass( 'approve' ) ) {
  418.                     approvedDiff = 1;
  419.                 } else if ( targetParent.hasClass( 'unapprove' ) ) {
  420.                     pendingDiff = 1;
  421.                 }
  422.             }
  423.             // you can't go from trash to spam
  424.             // you can untrash on the trash screen
  425.             trashDiff = -1;
  426.  
  427.         // User clicked "Approve"
  428.         } else if ( targetParent.is( 'span.approve:not(.unspam):not(.untrash)' ) ) {
  429.             approvedDiff = 1;
  430.             pendingDiff = -1;
  431.  
  432.         // User clicked "Unapprove"
  433.         } else if ( targetParent.is( 'span.unapprove:not(.unspam):not(.untrash)' ) ) {
  434.             approvedDiff = -1;
  435.             pendingDiff = 1;
  436.  
  437.         // User clicked "Delete Permanently"
  438.         } else if ( targetParent.is( 'span.delete' ) ) {
  439.             if ( spammed ) {
  440.                 spamDiff = -1;
  441.             } else if ( trashed ) {
  442.                 trashDiff = -1;
  443.             }
  444.         }
  445.  
  446.         if ( pendingDiff ) {
  447.             updatePending( pendingDiff, commentPostId );
  448.             updateCountText( 'span.all-count', pendingDiff );
  449.         }
  450.  
  451.         if ( approvedDiff ) {
  452.             updateApproved( approvedDiff, commentPostId );
  453.             updateCountText( 'span.all-count', approvedDiff );
  454.         }
  455.  
  456.         if ( spamDiff ) {
  457.             updateCountText( 'span.spam-count', spamDiff );
  458.         }
  459.  
  460.         if ( trashDiff ) {
  461.             updateCountText( 'span.trash-count', trashDiff );
  462.         }
  463.  
  464.         if (
  465.             ( ( 'trash' === settings.data.comment_status ) && !getCount( $( 'span.trash-count' ) ) ) ||
  466.             ( ( 'spam' === settings.data.comment_status ) && !getCount( $( 'span.spam-count' ) ) )
  467.         ) {
  468.             $( '#delete_all' ).hide();
  469.         }
  470.  
  471.         if ( ! isDashboard ) {
  472.             total = totalInput.val() ? parseInt( totalInput.val(), 10 ) : 0;
  473.             if ( $(settings.target).parent().is('span.undo') )
  474.                 total++;
  475.             else
  476.                 total--;
  477.  
  478.             if ( total < 0 )
  479.                 total = 0;
  480.  
  481.             if ( 'object' === typeof r ) {
  482.                 if ( response.supplemental.total_items_i18n && lastConfidentTime < response.supplemental.time ) {
  483.                     total_items_i18n = response.supplemental.total_items_i18n || '';
  484.                     if ( total_items_i18n ) {
  485.                         $('.displaying-num').text( total_items_i18n );
  486.                         $('.total-pages').text( response.supplemental.total_pages_i18n );
  487.                         $('.tablenav-pages').find('.next-page, .last-page').toggleClass('disabled', response.supplemental.total_pages == $('.current-page').val());
  488.                     }
  489.                     updateTotalCount( total, response.supplemental.time, true );
  490.                 } else if ( response.supplemental.time ) {
  491.                     updateTotalCount( total, response.supplemental.time, false );
  492.                 }
  493.             } else {
  494.                 updateTotalCount( total, r, false );
  495.             }
  496.         }
  497.  
  498.         if ( ! theExtraList || theExtraList.length === 0 || theExtraList.children().length === 0 || undoing ) {
  499.             return;
  500.         }
  501.  
  502.         theList.get(0).wpList.add( theExtraList.children( ':eq(0):not(.no-items)' ).remove().clone() );
  503.  
  504.         refillTheExtraList();
  505.  
  506.         animated = $( ':animated', '#the-comment-list' );
  507.         animatedCallback = function () {
  508.             if ( ! $( '#the-comment-list tr:visible' ).length ) {
  509.                 theList.get(0).wpList.add( theExtraList.find( '.no-items' ).clone() );
  510.             }
  511.         };
  512.  
  513.         if ( animated.length ) {
  514.             animated.promise().done( animatedCallback );
  515.         } else {
  516.             animatedCallback();
  517.         }
  518.     };
  519.  
  520.     refillTheExtraList = function(ev) {
  521.         var args = $.query.get(), total_pages = $('.total-pages').text(), per_page = $('input[name="_per_page"]', '#comments-form').val();
  522.  
  523.         if (! args.paged)
  524.             args.paged = 1;
  525.  
  526.         if (args.paged > total_pages) {
  527.             return;
  528.         }
  529.  
  530.         if (ev) {
  531.             theExtraList.empty();
  532.             args.number = Math.min(8, per_page); // see WP_Comments_List_Table::prepare_items() @ class-wp-comments-list-table.php
  533.         } else {
  534.             args.number = 1;
  535.             args.offset = Math.min(8, per_page) - 1; // fetch only the next item on the extra list
  536.         }
  537.  
  538.         args.no_placeholder = true;
  539.  
  540.         args.paged ++;
  541.  
  542.         // $.query.get() needs some correction to be sent into an ajax request
  543.         if ( true === args.comment_type )
  544.             args.comment_type = '';
  545.  
  546.         args = $.extend(args, {
  547.             'action': 'fetch-list',
  548.             'list_args': list_args,
  549.             '_ajax_fetch_list_nonce': $('#_ajax_fetch_list_nonce').val()
  550.         });
  551.  
  552.         $.ajax({
  553.             url: ajaxurl,
  554.             global: false,
  555.             dataType: 'json',
  556.             data: args,
  557.             success: function(response) {
  558.                 theExtraList.get(0).wpList.add( response.rows );
  559.             }
  560.         });
  561.     };
  562.  
  563.     theExtraList = $('#the-extra-comment-list').wpList( { alt: '', delColor: 'none', addColor: 'none' } );
  564.     theList = $('#the-comment-list').wpList( { alt: '', delBefore: delBefore, dimAfter: dimAfter, delAfter: delAfter, addColor: 'none' } )
  565.         .bind('wpListDelEnd', function(e, s){
  566.             var wpListsData = $(s.target).attr('data-wp-lists'), id = s.element.replace(/[^0-9]+/g, '');
  567.  
  568.             if ( wpListsData.indexOf(':trash=1') != -1 || wpListsData.indexOf(':spam=1') != -1 )
  569.                 $('#undo-' + id).fadeIn(300, function(){ $(this).show(); });
  570.         });
  571. };
  572.  
  573. commentReply = {
  574.     cid : '',
  575.     act : '',
  576.     originalContent : '',
  577.  
  578.     init : function() {
  579.         var row = $('#replyrow');
  580.  
  581.         $('a.cancel', row).click(function() { return commentReply.revert(); });
  582.         $('a.save', row).click(function() { return commentReply.send(); });
  583.         $( 'input#author-name, input#author-email, input#author-url', row ).keypress( function( e ) {
  584.             if ( e.which == 13 ) {
  585.                 commentReply.send();
  586.                 e.preventDefault();
  587.                 return false;
  588.             }
  589.         });
  590.  
  591.         // add events
  592.         $('#the-comment-list .column-comment > p').dblclick(function(){
  593.             commentReply.toggle($(this).parent());
  594.         });
  595.  
  596.         $('#doaction, #doaction2, #post-query-submit').click(function(){
  597.             if ( $('#the-comment-list #replyrow').length > 0 )
  598.                 commentReply.close();
  599.         });
  600.  
  601.         this.comments_listing = $('#comments-form > input[name="comment_status"]').val() || '';
  602.  
  603.         /* $(listTable).bind('beforeChangePage', function(){
  604.             commentReply.close();
  605.         }); */
  606.     },
  607.  
  608.     addEvents : function(r) {
  609.         r.each(function() {
  610.             $(this).find('.column-comment > p').dblclick(function(){
  611.                 commentReply.toggle($(this).parent());
  612.             });
  613.         });
  614.     },
  615.  
  616.     toggle : function(el) {
  617.         if ( 'none' !== $( el ).css( 'display' ) && ( $( '#replyrow' ).parent().is('#com-reply') || window.confirm( adminCommentsL10n.warnQuickEdit ) ) ) {
  618.             $( el ).find( 'a.vim-q' ).click();
  619.         }
  620.     },
  621.  
  622.     revert : function() {
  623.  
  624.         if ( $('#the-comment-list #replyrow').length < 1 )
  625.             return false;
  626.  
  627.         $('#replyrow').fadeOut('fast', function(){
  628.             commentReply.close();
  629.         });
  630.  
  631.         return false;
  632.     },
  633.  
  634.     close : function() {
  635.         var c, replyrow = $('#replyrow');
  636.  
  637.         // replyrow is not showing?
  638.         if ( replyrow.parent().is('#com-reply') )
  639.             return;
  640.  
  641.         if ( this.cid && this.act == 'edit-comment' ) {
  642.             c = $('#comment-' + this.cid);
  643.             c.fadeIn(300, function(){ c.show(); }).css('backgroundColor', '');
  644.         }
  645.  
  646.         // reset the Quicktags buttons
  647.         if ( typeof QTags != 'undefined' )
  648.             QTags.closeAllTags('replycontent');
  649.  
  650.         $('#add-new-comment').css('display', '');
  651.  
  652.         replyrow.hide();
  653.         $('#com-reply').append( replyrow );
  654.         $('#replycontent').css('height', '').val('');
  655.         $('#edithead input').val('');
  656.         $( '.notice-error', replyrow )
  657.             .addClass( 'hidden' )
  658.             .find( '.error' ).empty();
  659.         $( '.spinner', replyrow ).removeClass( 'is-active' );
  660.  
  661.         this.cid = '';
  662.         this.originalContent = '';
  663.     },
  664.  
  665.     open : function(comment_id, post_id, action) {
  666.         var editRow, rowData, act, replyButton, editHeight,
  667.             t = this,
  668.             c = $('#comment-' + comment_id),
  669.             h = c.height(),
  670.             colspanVal = 0;
  671.  
  672.         if ( ! this.discardCommentChanges() ) {
  673.             return false;
  674.         }
  675.  
  676.         t.close();
  677.         t.cid = comment_id;
  678.  
  679.         editRow = $('#replyrow');
  680.         rowData = $('#inline-'+comment_id);
  681.         action = action || 'replyto';
  682.         act = 'edit' == action ? 'edit' : 'replyto';
  683.         act = t.act = act + '-comment';
  684.         t.originalContent = $('textarea.comment', rowData).val();
  685.         colspanVal = $( '> th:visible, > td:visible', c ).length;
  686.  
  687.         // Make sure it's actually a table and there's a `colspan` value to apply.
  688.         if ( editRow.hasClass( 'inline-edit-row' ) && 0 !== colspanVal ) {
  689.             $( 'td', editRow ).attr( 'colspan', colspanVal );
  690.         }
  691.  
  692.         $('#action', editRow).val(act);
  693.         $('#comment_post_ID', editRow).val(post_id);
  694.         $('#comment_ID', editRow).val(comment_id);
  695.  
  696.         if ( action == 'edit' ) {
  697.             $( '#author-name', editRow ).val( $( 'div.author', rowData ).text() );
  698.             $('#author-email', editRow).val( $('div.author-email', rowData).text() );
  699.             $('#author-url', editRow).val( $('div.author-url', rowData).text() );
  700.             $('#status', editRow).val( $('div.comment_status', rowData).text() );
  701.             $('#replycontent', editRow).val( $('textarea.comment', rowData).val() );
  702.             $( '#edithead, #editlegend, #savebtn', editRow ).show();
  703.             $('#replyhead, #replybtn, #addhead, #addbtn', editRow).hide();
  704.  
  705.             if ( h > 120 ) {
  706.                 // Limit the maximum height when editing very long comments to make it more manageable.
  707.                 // The textarea is resizable in most browsers, so the user can adjust it if needed.
  708.                 editHeight = h > 500 ? 500 : h;
  709.                 $('#replycontent', editRow).css('height', editHeight + 'px');
  710.             }
  711.  
  712.             c.after( editRow ).fadeOut('fast', function(){
  713.                 $('#replyrow').fadeIn(300, function(){ $(this).show(); });
  714.             });
  715.         } else if ( action == 'add' ) {
  716.             $('#addhead, #addbtn', editRow).show();
  717.             $( '#replyhead, #replybtn, #edithead, #editlegend, #savebtn', editRow ) .hide();
  718.             $('#the-comment-list').prepend(editRow);
  719.             $('#replyrow').fadeIn(300);
  720.         } else {
  721.             replyButton = $('#replybtn', editRow);
  722.             $( '#edithead, #editlegend, #savebtn, #addhead, #addbtn', editRow ).hide();
  723.             $('#replyhead, #replybtn', editRow).show();
  724.             c.after(editRow);
  725.  
  726.             if ( c.hasClass('unapproved') ) {
  727.                 replyButton.text(adminCommentsL10n.replyApprove);
  728.             } else {
  729.                 replyButton.text(adminCommentsL10n.reply);
  730.             }
  731.  
  732.             $('#replyrow').fadeIn(300, function(){ $(this).show(); });
  733.         }
  734.  
  735.         setTimeout(function() {
  736.             var rtop, rbottom, scrollTop, vp, scrollBottom;
  737.  
  738.             rtop = $('#replyrow').offset().top;
  739.             rbottom = rtop + $('#replyrow').height();
  740.             scrollTop = window.pageYOffset || document.documentElement.scrollTop;
  741.             vp = document.documentElement.clientHeight || window.innerHeight || 0;
  742.             scrollBottom = scrollTop + vp;
  743.  
  744.             if ( scrollBottom - 20 < rbottom )
  745.                 window.scroll(0, rbottom - vp + 35);
  746.             else if ( rtop - 20 < scrollTop )
  747.                 window.scroll(0, rtop - 35);
  748.  
  749.             $('#replycontent').focus().keyup(function(e){
  750.                 if ( e.which == 27 )
  751.                     commentReply.revert(); // close on Escape
  752.             });
  753.         }, 600);
  754.  
  755.         return false;
  756.     },
  757.  
  758.     send : function() {
  759.         var post = {},
  760.             $errorNotice = $( '#replysubmit .error-notice' );
  761.  
  762.         $errorNotice.addClass( 'hidden' );
  763.         $( '#replysubmit .spinner' ).addClass( 'is-active' );
  764.  
  765.         $('#replyrow input').not(':button').each(function() {
  766.             var t = $(this);
  767.             post[ t.attr('name') ] = t.val();
  768.         });
  769.  
  770.         post.content = $('#replycontent').val();
  771.         post.id = post.comment_post_ID;
  772.         post.comments_listing = this.comments_listing;
  773.         post.p = $('[name="p"]').val();
  774.  
  775.         if ( $('#comment-' + $('#comment_ID').val()).hasClass('unapproved') )
  776.             post.approve_parent = 1;
  777.  
  778.         $.ajax({
  779.             type : 'POST',
  780.             url : ajaxurl,
  781.             data : post,
  782.             success : function(x) { commentReply.show(x); },
  783.             error : function(r) { commentReply.error(r); }
  784.         });
  785.  
  786.         return false;
  787.     },
  788.  
  789.     show : function(xml) {
  790.         var t = this, r, c, id, bg, pid;
  791.  
  792.         if ( typeof(xml) == 'string' ) {
  793.             t.error({'responseText': xml});
  794.             return false;
  795.         }
  796.  
  797.         r = wpAjax.parseAjaxResponse(xml);
  798.         if ( r.errors ) {
  799.             t.error({'responseText': wpAjax.broken});
  800.             return false;
  801.         }
  802.  
  803.         t.revert();
  804.  
  805.         r = r.responses[0];
  806.         id = '#comment-' + r.id;
  807.  
  808.         if ( 'edit-comment' == t.act )
  809.             $(id).remove();
  810.  
  811.         if ( r.supplemental.parent_approved ) {
  812.             pid = $('#comment-' + r.supplemental.parent_approved);
  813.             updatePending( -1, r.supplemental.parent_post_id );
  814.  
  815.             if ( this.comments_listing == 'moderated' ) {
  816.                 pid.animate( { 'backgroundColor':'#CCEEBB' }, 400, function(){
  817.                     pid.fadeOut();
  818.                 });
  819.                 return;
  820.             }
  821.         }
  822.  
  823.         if ( r.supplemental.i18n_comments_text ) {
  824.             if ( isDashboard ) {
  825.                 updateDashboardText( r.supplemental );
  826.             } else {
  827.                 updateApproved( 1, r.supplemental.parent_post_id );
  828.                 updateCountText( 'span.all-count', 1 );
  829.             }
  830.         }
  831.  
  832.         c = $.trim(r.data); // Trim leading whitespaces
  833.         $(c).hide();
  834.         $('#replyrow').after(c);
  835.  
  836.         id = $(id);
  837.         t.addEvents(id);
  838.         bg = id.hasClass('unapproved') ? '#FFFFE0' : id.closest('.widefat, .postbox').css('backgroundColor');
  839.  
  840.         id.animate( { 'backgroundColor':'#CCEEBB' }, 300 )
  841.             .animate( { 'backgroundColor': bg }, 300, function() {
  842.                 if ( pid && pid.length ) {
  843.                     pid.animate( { 'backgroundColor':'#CCEEBB' }, 300 )
  844.                         .animate( { 'backgroundColor': bg }, 300 )
  845.                         .removeClass('unapproved').addClass('approved')
  846.                         .find('div.comment_status').html('1');
  847.                 }
  848.             });
  849.  
  850.     },
  851.  
  852.     error : function(r) {
  853.         var er = r.statusText,
  854.             $errorNotice = $( '#replysubmit .notice-error' ),
  855.             $error = $errorNotice.find( '.error' );
  856.  
  857.         $( '#replysubmit .spinner' ).removeClass( 'is-active' );
  858.  
  859.         if ( r.responseText )
  860.             er = r.responseText.replace( /<.[^<>]*?>/g, '' );
  861.  
  862.         if ( er ) {
  863.             $errorNotice.removeClass( 'hidden' );
  864.             $error.html( er );
  865.         }
  866.     },
  867.  
  868.     addcomment: function(post_id) {
  869.         var t = this;
  870.  
  871.         $('#add-new-comment').fadeOut(200, function(){
  872.             t.open(0, post_id, 'add');
  873.             $('table.comments-box').css('display', '');
  874.             $('#no-comments').remove();
  875.         });
  876.     },
  877.  
  878.     /**
  879.      * Alert the user if they have unsaved changes on a comment that will be
  880.      * lost if they proceed.
  881.      *
  882.      * @returns {boolean}
  883.      */
  884.     discardCommentChanges: function() {
  885.         var editRow = $( '#replyrow' );
  886.  
  887.         if  ( this.originalContent === $( '#replycontent', editRow ).val() ) {
  888.             return true;
  889.         }
  890.  
  891.         return window.confirm( adminCommentsL10n.warnCommentChanges );
  892.     }
  893. };
  894.  
  895. $(document).ready(function(){
  896.     var make_hotkeys_redirect, edit_comment, toggle_all, make_bulk;
  897.  
  898.     setCommentsList();
  899.     commentReply.init();
  900.  
  901.     $(document).on( 'click', 'span.delete a.delete', function( e ) {
  902.         e.preventDefault();
  903.     });
  904.  
  905.     if ( typeof $.table_hotkeys != 'undefined' ) {
  906.         make_hotkeys_redirect = function(which) {
  907.             return function() {
  908.                 var first_last, l;
  909.  
  910.                 first_last = 'next' == which? 'first' : 'last';
  911.                 l = $('.tablenav-pages .'+which+'-page:not(.disabled)');
  912.                 if (l.length)
  913.                     window.location = l[0].href.replace(/\&hotkeys_highlight_(first|last)=1/g, '')+'&hotkeys_highlight_'+first_last+'=1';
  914.             };
  915.         };
  916.  
  917.         edit_comment = function(event, current_row) {
  918.             window.location = $('span.edit a', current_row).attr('href');
  919.         };
  920.  
  921.         toggle_all = function() {
  922.             $('#cb-select-all-1').data( 'wp-toggle', 1 ).trigger( 'click' ).removeData( 'wp-toggle' );
  923.         };
  924.  
  925.         make_bulk = function(value) {
  926.             return function() {
  927.                 var scope = $('select[name="action"]');
  928.                 $('option[value="' + value + '"]', scope).prop('selected', true);
  929.                 $('#doaction').click();
  930.             };
  931.         };
  932.  
  933.         $.table_hotkeys(
  934.             $('table.widefat'),
  935.             [
  936.                 'a', 'u', 's', 'd', 'r', 'q', 'z',
  937.                 ['e', edit_comment],
  938.                 ['shift+x', toggle_all],
  939.                 ['shift+a', make_bulk('approve')],
  940.                 ['shift+s', make_bulk('spam')],
  941.                 ['shift+d', make_bulk('delete')],
  942.                 ['shift+t', make_bulk('trash')],
  943.                 ['shift+z', make_bulk('untrash')],
  944.                 ['shift+u', make_bulk('unapprove')]
  945.             ],
  946.             {
  947.                 highlight_first: adminCommentsL10n.hotkeys_highlight_first,
  948.                 highlight_last: adminCommentsL10n.hotkeys_highlight_last,
  949.                 prev_page_link_cb: make_hotkeys_redirect('prev'),
  950.                 next_page_link_cb: make_hotkeys_redirect('next'),
  951.                 hotkeys_opts: {
  952.                     disableInInput: true,
  953.                     type: 'keypress',
  954.                     noDisable: '.check-column input[type="checkbox"]'
  955.                 },
  956.                 cycle_expr: '#the-comment-list tr',
  957.                 start_row_index: 0
  958.             }
  959.         );
  960.     }
  961.  
  962.     // Quick Edit and Reply have an inline comment editor.
  963.     $( '#the-comment-list' ).on( 'click', '.comment-inline', function (e) {
  964.         e.preventDefault();
  965.         var $el = $( this ),
  966.             action = 'replyto';
  967.  
  968.         if ( 'undefined' !== typeof $el.data( 'action' ) ) {
  969.             action = $el.data( 'action' );
  970.         }
  971.  
  972.         commentReply.open( $el.data( 'commentId' ), $el.data( 'postId' ), action );
  973.     } );
  974. });
  975.  
  976. })(jQuery);
  977.