inline-edit-post.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /* global inlineEditL10n, ajaxurl, typenow */
  2. /**
  3. * This file contains the functions needed for the inline editing of posts.
  4. *
  5. * @since 2.7.0
  6. */
  7. window.wp = window.wp || {};
  8. /**
  9. * Manages the quick edit and bulk edit windows for editing posts or pages.
  10. *
  11. * @namespace
  12. *
  13. * @since 2.7.0
  14. * @access public
  15. *
  16. * @type {Object}
  17. *
  18. * @property {string} type The type of inline editor.
  19. * @property {string} what The prefix before the post id.
  20. *
  21. */
  22. var inlineEditPost;
  23. ( function( $, wp ) {
  24. inlineEditPost = {
  25. /**
  26. * @summary Initializes the inline and bulk post editor.
  27. *
  28. * Binds event handlers to the escape key to close the inline editor
  29. * and to the save and close buttons. Changes DOM to be ready for inline
  30. * editing. Adds event handler to bulk edit.
  31. *
  32. * @memberof inlineEditPost
  33. * @since 2.7.0
  34. *
  35. * @returns {void}
  36. */
  37. init : function(){
  38. var t = this, qeRow = $('#inline-edit'), bulkRow = $('#bulk-edit');
  39. t.type = $('table.widefat').hasClass('pages') ? 'page' : 'post';
  40. // Post id prefix.
  41. t.what = '#post-';
  42. /**
  43. * @summary Bind escape key to revert the changes and close the quick editor.
  44. *
  45. * @returns {boolean} The result of revert.
  46. */
  47. qeRow.keyup(function(e){
  48. // Revert changes if escape key is pressed.
  49. if ( e.which === 27 ) {
  50. return inlineEditPost.revert();
  51. }
  52. });
  53. /**
  54. * @summary Bind escape key to revert the changes and close the bulk editor.
  55. *
  56. * @returns {boolean} The result of revert.
  57. */
  58. bulkRow.keyup(function(e){
  59. // Revert changes if escape key is pressed.
  60. if ( e.which === 27 ) {
  61. return inlineEditPost.revert();
  62. }
  63. });
  64. /**
  65. * @summary Revert changes and close the quick editor if the cancel button is clicked.
  66. *
  67. * @returns {boolean} The result of revert.
  68. */
  69. $( '.cancel', qeRow ).click( function() {
  70. return inlineEditPost.revert();
  71. });
  72. /**
  73. * @summary Save changes in the quick editor if the save(named: update) button is clicked.
  74. *
  75. * @returns {boolean} The result of save.
  76. */
  77. $( '.save', qeRow ).click( function() {
  78. return inlineEditPost.save(this);
  79. });
  80. /**
  81. * @summary If enter is pressed, and the target is not the cancel button, save the post.
  82. *
  83. * @returns {boolean} The result of save.
  84. */
  85. $('td', qeRow).keydown(function(e){
  86. if ( e.which === 13 && ! $( e.target ).hasClass( 'cancel' ) ) {
  87. return inlineEditPost.save(this);
  88. }
  89. });
  90. /**
  91. * @summary Revert changes and close the bulk editor if the cancel button is clicked.
  92. *
  93. * @returns {boolean} The result of revert.
  94. */
  95. $( '.cancel', bulkRow ).click( function() {
  96. return inlineEditPost.revert();
  97. });
  98. /**
  99. * @summary Disables the password input field when the private post checkbox is checked.
  100. */
  101. $('#inline-edit .inline-edit-private input[value="private"]').click( function(){
  102. var pw = $('input.inline-edit-password-input');
  103. if ( $(this).prop('checked') ) {
  104. pw.val('').prop('disabled', true);
  105. } else {
  106. pw.prop('disabled', false);
  107. }
  108. });
  109. /**
  110. * @summary Bind click event to the .editinline link which opens the quick editor.
  111. */
  112. $('#the-list').on( 'click', 'a.editinline', function( e ) {
  113. e.preventDefault();
  114. inlineEditPost.edit(this);
  115. });
  116. $('#bulk-edit').find('fieldset:first').after(
  117. $('#inline-edit fieldset.inline-edit-categories').clone()
  118. ).siblings( 'fieldset:last' ).prepend(
  119. $('#inline-edit label.inline-edit-tags').clone()
  120. );
  121. $('select[name="_status"] option[value="future"]', bulkRow).remove();
  122. /**
  123. * @summary Adds onclick events to the apply buttons.
  124. */
  125. $('#doaction, #doaction2').click(function(e){
  126. var n;
  127. t.whichBulkButtonId = $( this ).attr( 'id' );
  128. n = t.whichBulkButtonId.substr( 2 );
  129. if ( 'edit' === $( 'select[name="' + n + '"]' ).val() ) {
  130. e.preventDefault();
  131. t.setBulk();
  132. } else if ( $('form#posts-filter tr.inline-editor').length > 0 ) {
  133. t.revert();
  134. }
  135. });
  136. },
  137. /**
  138. * @summary Toggles the quick edit window.
  139. *
  140. * Hides the window when it's active and shows the window when inactive.
  141. *
  142. * @memberof inlineEditPost
  143. * @since 2.7.0
  144. *
  145. * @param {Object} el Element within a post table row.
  146. */
  147. toggle : function(el){
  148. var t = this;
  149. $( t.what + t.getId( el ) ).css( 'display' ) === 'none' ? t.revert() : t.edit( el );
  150. },
  151. /**
  152. * @summary Creates the bulk editor row to edit multiple posts at once.
  153. *
  154. * @memberof inlineEditPost
  155. * @since 2.7.0
  156. */
  157. setBulk : function(){
  158. var te = '', type = this.type, c = true;
  159. this.revert();
  160. $( '#bulk-edit td' ).attr( 'colspan', $( 'th:visible, td:visible', '.widefat:first thead' ).length );
  161. // Insert the editor at the top of the table with an empty row above to maintain zebra striping.
  162. $('table.widefat tbody').prepend( $('#bulk-edit') ).prepend('<tr class="hidden"></tr>');
  163. $('#bulk-edit').addClass('inline-editor').show();
  164. /**
  165. * @summary Create a HTML div with the title and a delete link(cross-icon) for each selected post.
  166. *
  167. * Get the selected posts based on the checked checkboxes in the post table.
  168. * Create a HTML div with the title and a link(delete-icon) for each selected post.
  169. */
  170. $( 'tbody th.check-column input[type="checkbox"]' ).each( function() {
  171. // If the checkbox for a post is selected, add the post to the edit list.
  172. if ( $(this).prop('checked') ) {
  173. c = false;
  174. var id = $(this).val(), theTitle;
  175. theTitle = $('#inline_'+id+' .post_title').html() || inlineEditL10n.notitle;
  176. te += '<div id="ttle'+id+'"><a id="_'+id+'" class="ntdelbutton" title="'+inlineEditL10n.ntdeltitle+'">X</a>'+theTitle+'</div>';
  177. }
  178. });
  179. // If no checkboxes where checked, just hide the quick/bulk edit rows.
  180. if ( c ) {
  181. return this.revert();
  182. }
  183. // Add onclick events to the delete-icons in the bulk editors the post title list.
  184. $('#bulk-titles').html(te);
  185. /**
  186. * @summary Binds on click events to the checkboxes before the posts in the table.
  187. *
  188. * @listens click
  189. */
  190. $('#bulk-titles a').click(function(){
  191. var id = $(this).attr('id').substr(1);
  192. $('table.widefat input[value="' + id + '"]').prop('checked', false);
  193. $('#ttle'+id).remove();
  194. });
  195. // Enable auto-complete for tags when editing posts.
  196. if ( 'post' === type ) {
  197. $( 'tr.inline-editor textarea[data-wp-taxonomy]' ).each( function ( i, element ) {
  198. /*
  199. * While Quick Edit clones the form each time, Bulk Edit always re-uses
  200. * the same form. Let's check if an autocomplete instance already exists.
  201. */
  202. if ( $( element ).autocomplete( 'instance' ) ) {
  203. // jQuery equivalent of `continue` within an `each()` loop.
  204. return;
  205. }
  206. $( element ).wpTagsSuggest();
  207. } );
  208. }
  209. // Scrolls to the top of the table where the editor is rendered.
  210. $('html, body').animate( { scrollTop: 0 }, 'fast' );
  211. },
  212. /**
  213. * @summary Creates a quick edit window for the post that has been clicked.
  214. *
  215. * @memberof inlineEditPost
  216. * @since 2.7.0
  217. *
  218. * @param {number|Object} id The id of the clicked post or an element within a post
  219. * table row.
  220. * @returns {boolean} Always returns false at the end of execution.
  221. */
  222. edit : function(id) {
  223. var t = this, fields, editRow, rowData, status, pageOpt, pageLevel, nextPage, pageLoop = true, nextLevel, f, val, pw;
  224. t.revert();
  225. if ( typeof(id) === 'object' ) {
  226. id = t.getId(id);
  227. }
  228. fields = ['post_title', 'post_name', 'post_author', '_status', 'jj', 'mm', 'aa', 'hh', 'mn', 'ss', 'post_password', 'post_format', 'menu_order', 'page_template'];
  229. if ( t.type === 'page' ) {
  230. fields.push('post_parent');
  231. }
  232. // Add the new edit row with an extra blank row underneath to maintain zebra striping.
  233. editRow = $('#inline-edit').clone(true);
  234. $( 'td', editRow ).attr( 'colspan', $( 'th:visible, td:visible', '.widefat:first thead' ).length );
  235. $(t.what+id).removeClass('is-expanded').hide().after(editRow).after('<tr class="hidden"></tr>');
  236. // Populate fields in the quick edit window.
  237. rowData = $('#inline_'+id);
  238. if ( !$(':input[name="post_author"] option[value="' + $('.post_author', rowData).text() + '"]', editRow).val() ) {
  239. // The post author no longer has edit capabilities, so we need to add them to the list of authors.
  240. $(':input[name="post_author"]', editRow).prepend('<option value="' + $('.post_author', rowData).text() + '">' + $('#' + t.type + '-' + id + ' .author').text() + '</option>');
  241. }
  242. if ( $( ':input[name="post_author"] option', editRow ).length === 1 ) {
  243. $('label.inline-edit-author', editRow).hide();
  244. }
  245. for ( f = 0; f < fields.length; f++ ) {
  246. val = $('.'+fields[f], rowData);
  247. /**
  248. * @summary Replaces the image for a Twemoji(Twitter emoji) with it's alternate text.
  249. *
  250. * @returns Alternate text from the image.
  251. */
  252. val.find( 'img' ).replaceWith( function() { return this.alt; } );
  253. val = val.text();
  254. $(':input[name="' + fields[f] + '"]', editRow).val( val );
  255. }
  256. if ( $( '.comment_status', rowData ).text() === 'open' ) {
  257. $( 'input[name="comment_status"]', editRow ).prop( 'checked', true );
  258. }
  259. if ( $( '.ping_status', rowData ).text() === 'open' ) {
  260. $( 'input[name="ping_status"]', editRow ).prop( 'checked', true );
  261. }
  262. if ( $( '.sticky', rowData ).text() === 'sticky' ) {
  263. $( 'input[name="sticky"]', editRow ).prop( 'checked', true );
  264. }
  265. /**
  266. * @summary Creates the select boxes for the categories.
  267. */
  268. $('.post_category', rowData).each(function(){
  269. var taxname,
  270. term_ids = $(this).text();
  271. if ( term_ids ) {
  272. taxname = $(this).attr('id').replace('_'+id, '');
  273. $('ul.'+taxname+'-checklist :checkbox', editRow).val(term_ids.split(','));
  274. }
  275. });
  276. /**
  277. * @summary Gets all the taxonomies for live auto-fill suggestions.
  278. * When typing the name of a tag.
  279. */
  280. $('.tags_input', rowData).each(function(){
  281. var terms = $(this),
  282. taxname = $(this).attr('id').replace('_' + id, ''),
  283. textarea = $('textarea.tax_input_' + taxname, editRow),
  284. comma = inlineEditL10n.comma;
  285. terms.find( 'img' ).replaceWith( function() { return this.alt; } );
  286. terms = terms.text();
  287. if ( terms ) {
  288. if ( ',' !== comma ) {
  289. terms = terms.replace(/,/g, comma);
  290. }
  291. textarea.val(terms);
  292. }
  293. textarea.wpTagsSuggest();
  294. });
  295. // Handle the post status.
  296. status = $('._status', rowData).text();
  297. if ( 'future' !== status ) {
  298. $('select[name="_status"] option[value="future"]', editRow).remove();
  299. }
  300. pw = $( '.inline-edit-password-input' ).prop( 'disabled', false );
  301. if ( 'private' === status ) {
  302. $('input[name="keep_private"]', editRow).prop('checked', true);
  303. pw.val( '' ).prop( 'disabled', true );
  304. }
  305. // Remove the current page and children from the parent dropdown.
  306. pageOpt = $('select[name="post_parent"] option[value="' + id + '"]', editRow);
  307. if ( pageOpt.length > 0 ) {
  308. pageLevel = pageOpt[0].className.split('-')[1];
  309. nextPage = pageOpt;
  310. while ( pageLoop ) {
  311. nextPage = nextPage.next('option');
  312. if ( nextPage.length === 0 ) {
  313. break;
  314. }
  315. nextLevel = nextPage[0].className.split('-')[1];
  316. if ( nextLevel <= pageLevel ) {
  317. pageLoop = false;
  318. } else {
  319. nextPage.remove();
  320. nextPage = pageOpt;
  321. }
  322. }
  323. pageOpt.remove();
  324. }
  325. $(editRow).attr('id', 'edit-'+id).addClass('inline-editor').show();
  326. $('.ptitle', editRow).focus();
  327. return false;
  328. },
  329. /**
  330. * @summary Saves the changes made in the quick edit window to the post.
  331. * AJAX saving is only for Quick Edit and not for bulk edit.
  332. *
  333. * @since 2.7.0
  334. *
  335. * @param {int} id The id for the post that has been changed.
  336. * @returns {boolean} false, so the form does not submit when pressing
  337. * Enter on a focused field.
  338. */
  339. save : function(id) {
  340. var params, fields, page = $('.post_status_page').val() || '';
  341. if ( typeof(id) === 'object' ) {
  342. id = this.getId(id);
  343. }
  344. $( 'table.widefat .spinner' ).addClass( 'is-active' );
  345. params = {
  346. action: 'inline-save',
  347. post_type: typenow,
  348. post_ID: id,
  349. edit_date: 'true',
  350. post_status: page
  351. };
  352. fields = $('#edit-'+id).find(':input').serialize();
  353. params = fields + '&' + $.param(params);
  354. // Make ajax request.
  355. $.post( ajaxurl, params,
  356. function(r) {
  357. var $errorSpan = $( '#edit-' + id + ' .inline-edit-save .error' );
  358. $( 'table.widefat .spinner' ).removeClass( 'is-active' );
  359. $( '.ac_results' ).hide();
  360. if (r) {
  361. if ( -1 !== r.indexOf( '<tr' ) ) {
  362. $(inlineEditPost.what+id).siblings('tr.hidden').addBack().remove();
  363. $('#edit-'+id).before(r).remove();
  364. $( inlineEditPost.what + id ).hide().fadeIn( 400, function() {
  365. // Move focus back to the Quick Edit link. $( this ) is the row being animated.
  366. $( this ).find( '.editinline' ).focus();
  367. wp.a11y.speak( inlineEditL10n.saved );
  368. });
  369. } else {
  370. r = r.replace( /<.[^<>]*?>/g, '' );
  371. $errorSpan.html( r ).show();
  372. wp.a11y.speak( $errorSpan.text() );
  373. }
  374. } else {
  375. $errorSpan.html( inlineEditL10n.error ).show();
  376. wp.a11y.speak( inlineEditL10n.error );
  377. }
  378. },
  379. 'html');
  380. // Prevent submitting the form when pressing Enter on a focused field.
  381. return false;
  382. },
  383. /**
  384. * @summary Hides and empties the Quick Edit and/or Bulk Edit windows.
  385. *
  386. * @memberof inlineEditPost
  387. * @since 2.7.0
  388. *
  389. * @returns {boolean} Always returns false.
  390. */
  391. revert : function(){
  392. var $tableWideFat = $( '.widefat' ),
  393. id = $( '.inline-editor', $tableWideFat ).attr( 'id' );
  394. if ( id ) {
  395. $( '.spinner', $tableWideFat ).removeClass( 'is-active' );
  396. $( '.ac_results' ).hide();
  397. if ( 'bulk-edit' === id ) {
  398. // Hide the bulk editor.
  399. $( '#bulk-edit', $tableWideFat ).removeClass( 'inline-editor' ).hide().siblings( '.hidden' ).remove();
  400. $('#bulk-titles').empty();
  401. // Store the empty bulk editor in a hidden element.
  402. $('#inlineedit').append( $('#bulk-edit') );
  403. // Move focus back to the Bulk Action button that was activated.
  404. $( '#' + inlineEditPost.whichBulkButtonId ).focus();
  405. } else {
  406. // Remove both the inline-editor and its hidden tr siblings.
  407. $('#'+id).siblings('tr.hidden').addBack().remove();
  408. id = id.substr( id.lastIndexOf('-') + 1 );
  409. // Show the post row and move focus back to the Quick Edit link.
  410. $( this.what + id ).show().find( '.editinline' ).focus();
  411. }
  412. }
  413. return false;
  414. },
  415. /**
  416. * @summary Gets the id for a the post that you want to quick edit from the row
  417. * in the quick edit table.
  418. *
  419. * @memberof inlineEditPost
  420. * @since 2.7.0
  421. *
  422. * @param {Object} o DOM row object to get the id for.
  423. * @returns {string} The post id extracted from the table row in the object.
  424. */
  425. getId : function(o) {
  426. var id = $(o).closest('tr').attr('id'),
  427. parts = id.split('-');
  428. return parts[parts.length - 1];
  429. }
  430. };
  431. $( document ).ready( function(){ inlineEditPost.init(); } );
  432. // Show/hide locks on posts.
  433. $( document ).on( 'heartbeat-tick.wp-check-locked-posts', function( e, data ) {
  434. var locked = data['wp-check-locked-posts'] || {};
  435. $('#the-list tr').each( function(i, el) {
  436. var key = el.id, row = $(el), lock_data, avatar;
  437. if ( locked.hasOwnProperty( key ) ) {
  438. if ( ! row.hasClass('wp-locked') ) {
  439. lock_data = locked[key];
  440. row.find('.column-title .locked-text').text( lock_data.text );
  441. row.find('.check-column checkbox').prop('checked', false);
  442. if ( lock_data.avatar_src ) {
  443. avatar = $( '<img class="avatar avatar-18 photo" width="18" height="18" alt="" />' ).attr( 'src', lock_data.avatar_src.replace( /&amp;/g, '&' ) );
  444. row.find('.column-title .locked-avatar').empty().append( avatar );
  445. }
  446. row.addClass('wp-locked');
  447. }
  448. } else if ( row.hasClass('wp-locked') ) {
  449. // Make room for the CSS animation
  450. row.removeClass('wp-locked').delay(1000).find('.locked-info span').empty();
  451. }
  452. });
  453. }).on( 'heartbeat-send.wp-check-locked-posts', function( e, data ) {
  454. var check = [];
  455. $('#the-list tr').each( function(i, el) {
  456. if ( el.id ) {
  457. check.push( el.id );
  458. }
  459. });
  460. if ( check.length ) {
  461. data['wp-check-locked-posts'] = check;
  462. }
  463. }).ready( function() {
  464. // Set the heartbeat interval to 15 sec.
  465. if ( typeof wp !== 'undefined' && wp.heartbeat ) {
  466. wp.heartbeat.interval( 15 );
  467. }
  468. });
  469. })( jQuery, window.wp );