inline-edit-tax.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /**
  2. * This file is used on the term overview page to power quick-editing terms.
  3. *
  4. * @output wp-admin/js/inline-edit-tax.js
  5. */
  6. /* global ajaxurl, inlineEditTax */
  7. window.wp = window.wp || {};
  8. /**
  9. * Consists of functions relevant to the inline taxonomy editor.
  10. *
  11. * @namespace inlineEditTax
  12. *
  13. * @property {string} type The type of inline edit we are currently on.
  14. * @property {string} what The type property with a hash prefixed and a dash
  15. * suffixed.
  16. */
  17. ( function( $, wp ) {
  18. window.inlineEditTax = {
  19. /**
  20. * Initializes the inline taxonomy editor by adding event handlers to be able to
  21. * quick edit.
  22. *
  23. * @since 2.7.0
  24. *
  25. * @this inlineEditTax
  26. * @memberof inlineEditTax
  27. * @return {void}
  28. */
  29. init : function() {
  30. var t = this, row = $('#inline-edit');
  31. t.type = $('#the-list').attr('data-wp-lists').substr(5);
  32. t.what = '#'+t.type+'-';
  33. $( '#the-list' ).on( 'click', '.editinline', function() {
  34. $( this ).attr( 'aria-expanded', 'true' );
  35. inlineEditTax.edit( this );
  36. });
  37. /**
  38. * Cancels inline editing when pressing Escape inside the inline editor.
  39. *
  40. * @param {Object} e The keyup event that has been triggered.
  41. */
  42. row.on( 'keyup', function( e ) {
  43. // 27 = [Escape].
  44. if ( e.which === 27 ) {
  45. return inlineEditTax.revert();
  46. }
  47. });
  48. /**
  49. * Cancels inline editing when clicking the cancel button.
  50. */
  51. $( '.cancel', row ).on( 'click', function() {
  52. return inlineEditTax.revert();
  53. });
  54. /**
  55. * Saves the inline edits when clicking the save button.
  56. */
  57. $( '.save', row ).on( 'click', function() {
  58. return inlineEditTax.save(this);
  59. });
  60. /**
  61. * Saves the inline edits when pressing Enter inside the inline editor.
  62. */
  63. $( 'input, select', row ).on( 'keydown', function( e ) {
  64. // 13 = [Enter].
  65. if ( e.which === 13 ) {
  66. return inlineEditTax.save( this );
  67. }
  68. });
  69. /**
  70. * Saves the inline edits on submitting the inline edit form.
  71. */
  72. $( '#posts-filter input[type="submit"]' ).on( 'mousedown', function() {
  73. t.revert();
  74. });
  75. },
  76. /**
  77. * Toggles the quick edit based on if it is currently shown or hidden.
  78. *
  79. * @since 2.7.0
  80. *
  81. * @this inlineEditTax
  82. * @memberof inlineEditTax
  83. *
  84. * @param {HTMLElement} el An element within the table row or the table row
  85. * itself that we want to quick edit.
  86. * @return {void}
  87. */
  88. toggle : function(el) {
  89. var t = this;
  90. $(t.what+t.getId(el)).css('display') === 'none' ? t.revert() : t.edit(el);
  91. },
  92. /**
  93. * Shows the quick editor
  94. *
  95. * @since 2.7.0
  96. *
  97. * @this inlineEditTax
  98. * @memberof inlineEditTax
  99. *
  100. * @param {string|HTMLElement} id The ID of the term we want to quick edit or an
  101. * element within the table row or the
  102. * table row itself.
  103. * @return {boolean} Always returns false.
  104. */
  105. edit : function(id) {
  106. var editRow, rowData, val,
  107. t = this;
  108. t.revert();
  109. // Makes sure we can pass an HTMLElement as the ID.
  110. if ( typeof(id) === 'object' ) {
  111. id = t.getId(id);
  112. }
  113. editRow = $('#inline-edit').clone(true), rowData = $('#inline_'+id);
  114. $( 'td', editRow ).attr( 'colspan', $( 'th:visible, td:visible', '.wp-list-table.widefat:first thead' ).length );
  115. $(t.what+id).hide().after(editRow).after('<tr class="hidden"></tr>');
  116. val = $('.name', rowData);
  117. val.find( 'img' ).replaceWith( function() { return this.alt; } );
  118. val = val.text();
  119. $(':input[name="name"]', editRow).val( val );
  120. val = $('.slug', rowData);
  121. val.find( 'img' ).replaceWith( function() { return this.alt; } );
  122. val = val.text();
  123. $(':input[name="slug"]', editRow).val( val );
  124. $(editRow).attr('id', 'edit-'+id).addClass('inline-editor').show();
  125. $('.ptitle', editRow).eq(0).trigger( 'focus' );
  126. return false;
  127. },
  128. /**
  129. * Saves the quick edit data.
  130. *
  131. * Saves the quick edit data to the server and replaces the table row with the
  132. * HTML retrieved from the server.
  133. *
  134. * @since 2.7.0
  135. *
  136. * @this inlineEditTax
  137. * @memberof inlineEditTax
  138. *
  139. * @param {string|HTMLElement} id The ID of the term we want to quick edit or an
  140. * element within the table row or the
  141. * table row itself.
  142. * @return {boolean} Always returns false.
  143. */
  144. save : function(id) {
  145. var params, fields, tax = $('input[name="taxonomy"]').val() || '';
  146. // Makes sure we can pass an HTMLElement as the ID.
  147. if( typeof(id) === 'object' ) {
  148. id = this.getId(id);
  149. }
  150. $( 'table.widefat .spinner' ).addClass( 'is-active' );
  151. params = {
  152. action: 'inline-save-tax',
  153. tax_type: this.type,
  154. tax_ID: id,
  155. taxonomy: tax
  156. };
  157. fields = $('#edit-'+id).find(':input').serialize();
  158. params = fields + '&' + $.param(params);
  159. // Do the Ajax request to save the data to the server.
  160. $.post( ajaxurl, params,
  161. /**
  162. * Handles the response from the server
  163. *
  164. * Handles the response from the server, replaces the table row with the response
  165. * from the server.
  166. *
  167. * @param {string} r The string with which to replace the table row.
  168. */
  169. function(r) {
  170. var row, new_id, option_value,
  171. $errorNotice = $( '#edit-' + id + ' .inline-edit-save .notice-error' ),
  172. $error = $errorNotice.find( '.error' );
  173. $( 'table.widefat .spinner' ).removeClass( 'is-active' );
  174. if (r) {
  175. if ( -1 !== r.indexOf( '<tr' ) ) {
  176. $(inlineEditTax.what+id).siblings('tr.hidden').addBack().remove();
  177. new_id = $(r).attr('id');
  178. $('#edit-'+id).before(r).remove();
  179. if ( new_id ) {
  180. option_value = new_id.replace( inlineEditTax.type + '-', '' );
  181. row = $( '#' + new_id );
  182. } else {
  183. option_value = id;
  184. row = $( inlineEditTax.what + id );
  185. }
  186. // Update the value in the Parent dropdown.
  187. $( '#parent' ).find( 'option[value=' + option_value + ']' ).text( row.find( '.row-title' ).text() );
  188. row.hide().fadeIn( 400, function() {
  189. // Move focus back to the Quick Edit button.
  190. row.find( '.editinline' )
  191. .attr( 'aria-expanded', 'false' )
  192. .trigger( 'focus' );
  193. wp.a11y.speak( wp.i18n.__( 'Changes saved.' ) );
  194. });
  195. } else {
  196. $errorNotice.removeClass( 'hidden' );
  197. $error.html( r );
  198. /*
  199. * Some error strings may contain HTML entities (e.g. `&#8220`), let's use
  200. * the HTML element's text.
  201. */
  202. wp.a11y.speak( $error.text() );
  203. }
  204. } else {
  205. $errorNotice.removeClass( 'hidden' );
  206. $error.text( wp.i18n.__( 'Error while saving the changes.' ) );
  207. wp.a11y.speak( wp.i18n.__( 'Error while saving the changes.' ) );
  208. }
  209. }
  210. );
  211. // Prevent submitting the form when pressing Enter on a focused field.
  212. return false;
  213. },
  214. /**
  215. * Closes the quick edit form.
  216. *
  217. * @since 2.7.0
  218. *
  219. * @this inlineEditTax
  220. * @memberof inlineEditTax
  221. * @return {void}
  222. */
  223. revert : function() {
  224. var id = $('table.widefat tr.inline-editor').attr('id');
  225. if ( id ) {
  226. $( 'table.widefat .spinner' ).removeClass( 'is-active' );
  227. $('#'+id).siblings('tr.hidden').addBack().remove();
  228. id = id.substr( id.lastIndexOf('-') + 1 );
  229. // Show the taxonomy row and move focus back to the Quick Edit button.
  230. $( this.what + id ).show().find( '.editinline' )
  231. .attr( 'aria-expanded', 'false' )
  232. .trigger( 'focus' );
  233. }
  234. },
  235. /**
  236. * Retrieves the ID of the term of the element inside the table row.
  237. *
  238. * @since 2.7.0
  239. *
  240. * @memberof inlineEditTax
  241. *
  242. * @param {HTMLElement} o An element within the table row or the table row itself.
  243. * @return {string} The ID of the term based on the element.
  244. */
  245. getId : function(o) {
  246. var id = o.tagName === 'TR' ? o.id : $(o).parents('tr').attr('id'), parts = id.split('-');
  247. return parts[parts.length - 1];
  248. }
  249. };
  250. $( function() { inlineEditTax.init(); } );
  251. })( jQuery, window.wp );