/wp-admin/js/edit-comments.js

https://bitbucket.org/devbctph/futura_wp · JavaScript · 976 lines · 757 code · 171 blank · 48 comment · 210 complexity · a975fc736cdecddbfb9aed80699fb3af MD5 · raw file

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