edit-comments.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  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. $('.error', replyrow).empty().hide();
  545. $( '.spinner', replyrow ).removeClass( 'is-active' );
  546. this.cid = '';
  547. this.originalContent = '';
  548. },
  549. open : function(comment_id, post_id, action) {
  550. var editRow, rowData, act, replyButton, editHeight,
  551. t = this,
  552. c = $('#comment-' + comment_id),
  553. h = c.height(),
  554. colspanVal = 0;
  555. if ( ! this.discardCommentChanges() ) {
  556. return false;
  557. }
  558. t.close();
  559. t.cid = comment_id;
  560. editRow = $('#replyrow');
  561. rowData = $('#inline-'+comment_id);
  562. action = action || 'replyto';
  563. act = 'edit' == action ? 'edit' : 'replyto';
  564. act = t.act = act + '-comment';
  565. t.originalContent = $('textarea.comment', rowData).val();
  566. colspanVal = $( '> th:visible, > td:visible', c ).length;
  567. // Make sure it's actually a table and there's a `colspan` value to apply.
  568. if ( editRow.hasClass( 'inline-edit-row' ) && 0 !== colspanVal ) {
  569. $( 'td', editRow ).attr( 'colspan', colspanVal );
  570. }
  571. $('#action', editRow).val(act);
  572. $('#comment_post_ID', editRow).val(post_id);
  573. $('#comment_ID', editRow).val(comment_id);
  574. if ( action == 'edit' ) {
  575. $( '#author-name', editRow ).val( $( 'div.author', rowData ).text() );
  576. $('#author-email', editRow).val( $('div.author-email', rowData).text() );
  577. $('#author-url', editRow).val( $('div.author-url', rowData).text() );
  578. $('#status', editRow).val( $('div.comment_status', rowData).text() );
  579. $('#replycontent', editRow).val( $('textarea.comment', rowData).val() );
  580. $( '#edithead, #editlegend, #savebtn', editRow ).show();
  581. $('#replyhead, #replybtn, #addhead, #addbtn', editRow).hide();
  582. if ( h > 120 ) {
  583. // Limit the maximum height when editing very long comments to make it more manageable.
  584. // The textarea is resizable in most browsers, so the user can adjust it if needed.
  585. editHeight = h > 500 ? 500 : h;
  586. $('#replycontent', editRow).css('height', editHeight + 'px');
  587. }
  588. c.after( editRow ).fadeOut('fast', function(){
  589. $('#replyrow').fadeIn(300, function(){ $(this).show(); });
  590. });
  591. } else if ( action == 'add' ) {
  592. $('#addhead, #addbtn', editRow).show();
  593. $( '#replyhead, #replybtn, #edithead, #editlegend, #savebtn', editRow ) .hide();
  594. $('#the-comment-list').prepend(editRow);
  595. $('#replyrow').fadeIn(300);
  596. } else {
  597. replyButton = $('#replybtn', editRow);
  598. $( '#edithead, #editlegend, #savebtn, #addhead, #addbtn', editRow ).hide();
  599. $('#replyhead, #replybtn', editRow).show();
  600. c.after(editRow);
  601. if ( c.hasClass('unapproved') ) {
  602. replyButton.text(adminCommentsL10n.replyApprove);
  603. } else {
  604. replyButton.text(adminCommentsL10n.reply);
  605. }
  606. $('#replyrow').fadeIn(300, function(){ $(this).show(); });
  607. }
  608. setTimeout(function() {
  609. var rtop, rbottom, scrollTop, vp, scrollBottom;
  610. rtop = $('#replyrow').offset().top;
  611. rbottom = rtop + $('#replyrow').height();
  612. scrollTop = window.pageYOffset || document.documentElement.scrollTop;
  613. vp = document.documentElement.clientHeight || window.innerHeight || 0;
  614. scrollBottom = scrollTop + vp;
  615. if ( scrollBottom - 20 < rbottom )
  616. window.scroll(0, rbottom - vp + 35);
  617. else if ( rtop - 20 < scrollTop )
  618. window.scroll(0, rtop - 35);
  619. $('#replycontent').focus().keyup(function(e){
  620. if ( e.which == 27 )
  621. commentReply.revert(); // close on Escape
  622. });
  623. }, 600);
  624. return false;
  625. },
  626. send : function() {
  627. var post = {};
  628. $('#replysubmit .error').hide();
  629. $( '#replysubmit .spinner' ).addClass( 'is-active' );
  630. $('#replyrow input').not(':button').each(function() {
  631. var t = $(this);
  632. post[ t.attr('name') ] = t.val();
  633. });
  634. post.content = $('#replycontent').val();
  635. post.id = post.comment_post_ID;
  636. post.comments_listing = this.comments_listing;
  637. post.p = $('[name="p"]').val();
  638. if ( $('#comment-' + $('#comment_ID').val()).hasClass('unapproved') )
  639. post.approve_parent = 1;
  640. $.ajax({
  641. type : 'POST',
  642. url : ajaxurl,
  643. data : post,
  644. success : function(x) { commentReply.show(x); },
  645. error : function(r) { commentReply.error(r); }
  646. });
  647. return false;
  648. },
  649. show : function(xml) {
  650. var t = this, r, c, id, bg, pid;
  651. if ( typeof(xml) == 'string' ) {
  652. t.error({'responseText': xml});
  653. return false;
  654. }
  655. r = wpAjax.parseAjaxResponse(xml);
  656. if ( r.errors ) {
  657. t.error({'responseText': wpAjax.broken});
  658. return false;
  659. }
  660. t.revert();
  661. r = r.responses[0];
  662. id = '#comment-' + r.id;
  663. if ( 'edit-comment' == t.act )
  664. $(id).remove();
  665. if ( r.supplemental.parent_approved ) {
  666. pid = $('#comment-' + r.supplemental.parent_approved);
  667. updatePending( -1, r.supplemental.parent_post_id );
  668. if ( this.comments_listing == 'moderated' ) {
  669. pid.animate( { 'backgroundColor':'#CCEEBB' }, 400, function(){
  670. pid.fadeOut();
  671. });
  672. return;
  673. }
  674. }
  675. if ( r.supplemental.i18n_comments_text ) {
  676. if ( isDashboard ) {
  677. updateDashboardText( r.supplemental );
  678. } else {
  679. updateApproved( 1, r.supplemental.parent_post_id );
  680. updateCountText( 'span.all-count', 1 );
  681. }
  682. }
  683. c = $.trim(r.data); // Trim leading whitespaces
  684. $(c).hide();
  685. $('#replyrow').after(c);
  686. id = $(id);
  687. t.addEvents(id);
  688. bg = id.hasClass('unapproved') ? '#FFFFE0' : id.closest('.widefat, .postbox').css('backgroundColor');
  689. id.animate( { 'backgroundColor':'#CCEEBB' }, 300 )
  690. .animate( { 'backgroundColor': bg }, 300, function() {
  691. if ( pid && pid.length ) {
  692. pid.animate( { 'backgroundColor':'#CCEEBB' }, 300 )
  693. .animate( { 'backgroundColor': bg }, 300 )
  694. .removeClass('unapproved').addClass('approved')
  695. .find('div.comment_status').html('1');
  696. }
  697. });
  698. },
  699. error : function(r) {
  700. var er = r.statusText;
  701. $( '#replysubmit .spinner' ).removeClass( 'is-active' );
  702. if ( r.responseText )
  703. er = r.responseText.replace( /<.[^<>]*?>/g, '' );
  704. if ( er )
  705. $('#replysubmit .error').html(er).show();
  706. },
  707. addcomment: function(post_id) {
  708. var t = this;
  709. $('#add-new-comment').fadeOut(200, function(){
  710. t.open(0, post_id, 'add');
  711. $('table.comments-box').css('display', '');
  712. $('#no-comments').remove();
  713. });
  714. },
  715. /**
  716. * Alert the user if they have unsaved changes on a comment that will be
  717. * lost if they proceed.
  718. *
  719. * @returns {boolean}
  720. */
  721. discardCommentChanges: function() {
  722. var editRow = $( '#replyrow' );
  723. if ( this.originalContent === $( '#replycontent', editRow ).val() ) {
  724. return true;
  725. }
  726. return window.confirm( adminCommentsL10n.warnCommentChanges );
  727. }
  728. };
  729. $(document).ready(function(){
  730. var make_hotkeys_redirect, edit_comment, toggle_all, make_bulk;
  731. setCommentsList();
  732. commentReply.init();
  733. $(document).on( 'click', 'span.delete a.delete', function( e ) {
  734. e.preventDefault();
  735. });
  736. if ( typeof $.table_hotkeys != 'undefined' ) {
  737. make_hotkeys_redirect = function(which) {
  738. return function() {
  739. var first_last, l;
  740. first_last = 'next' == which? 'first' : 'last';
  741. l = $('.tablenav-pages .'+which+'-page:not(.disabled)');
  742. if (l.length)
  743. window.location = l[0].href.replace(/\&hotkeys_highlight_(first|last)=1/g, '')+'&hotkeys_highlight_'+first_last+'=1';
  744. };
  745. };
  746. edit_comment = function(event, current_row) {
  747. window.location = $('span.edit a', current_row).attr('href');
  748. };
  749. toggle_all = function() {
  750. $('#cb-select-all-1').data( 'wp-toggle', 1 ).trigger( 'click' ).removeData( 'wp-toggle' );
  751. };
  752. make_bulk = function(value) {
  753. return function() {
  754. var scope = $('select[name="action"]');
  755. $('option[value="' + value + '"]', scope).prop('selected', true);
  756. $('#doaction').click();
  757. };
  758. };
  759. $.table_hotkeys(
  760. $('table.widefat'),
  761. [
  762. 'a', 'u', 's', 'd', 'r', 'q', 'z',
  763. ['e', edit_comment],
  764. ['shift+x', toggle_all],
  765. ['shift+a', make_bulk('approve')],
  766. ['shift+s', make_bulk('spam')],
  767. ['shift+d', make_bulk('delete')],
  768. ['shift+t', make_bulk('trash')],
  769. ['shift+z', make_bulk('untrash')],
  770. ['shift+u', make_bulk('unapprove')]
  771. ],
  772. {
  773. highlight_first: adminCommentsL10n.hotkeys_highlight_first,
  774. highlight_last: adminCommentsL10n.hotkeys_highlight_last,
  775. prev_page_link_cb: make_hotkeys_redirect('prev'),
  776. next_page_link_cb: make_hotkeys_redirect('next'),
  777. hotkeys_opts: {
  778. disableInInput: true,
  779. type: 'keypress',
  780. noDisable: '.check-column input[type="checkbox"]'
  781. },
  782. cycle_expr: '#the-comment-list tr',
  783. start_row_index: 0
  784. }
  785. );
  786. }
  787. // Quick Edit and Reply have an inline comment editor.
  788. $( '#the-comment-list' ).on( 'click', '.comment-inline', function (e) {
  789. e.preventDefault();
  790. var $el = $( this ),
  791. action = 'replyto';
  792. if ( 'undefined' !== typeof $el.data( 'action' ) ) {
  793. action = $el.data( 'action' );
  794. }
  795. commentReply.open( $el.data( 'commentId' ), $el.data( 'postId' ), action );
  796. } );
  797. });
  798. })(jQuery);