Aucune description

inline-edit-post.js 16KB

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