You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

288 regels
7.3 KiB

  1. /* global inlineEditL10n, ajaxurl */
  2. /**
  3. * This file is used on the term overview page to power quick-editing terms.
  4. */
  5. window.wp = window.wp || {};
  6. /**
  7. * Consists of functions relevant to the inline taxonomy editor.
  8. *
  9. * @namespace inlineEditTax
  10. *
  11. * @property {string} type The type of inline edit we are currently on.
  12. * @property {string} what The type property with a hash prefixed and a dash
  13. * suffixed.
  14. */
  15. var inlineEditTax;
  16. ( function( $, wp ) {
  17. inlineEditTax = {
  18. /**
  19. * @summary Initializes the inline taxonomy editor.
  20. *
  21. * Adds event handlers to be able to quick edit.
  22. *
  23. * @since 2.7.0
  24. *
  25. * @this inlineEditTax
  26. * @memberof inlineEditTax
  27. * @returns {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', 'a.editinline', function(){
  34. inlineEditTax.edit(this);
  35. return false;
  36. });
  37. /*
  38. * @summary 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.keyup( function( e ) {
  43. // 27 = [escape]
  44. if ( e.which === 27 ) {
  45. return inlineEditTax.revert();
  46. }
  47. });
  48. /**
  49. * @summary Cancels inline editing when clicking the cancel button.
  50. */
  51. $( '.cancel', row ).click( function() {
  52. return inlineEditTax.revert();
  53. });
  54. /**
  55. * @summary Saves the inline edits when clicking the save button.
  56. */
  57. $( '.save', row ).click( function() {
  58. return inlineEditTax.save(this);
  59. });
  60. /**
  61. * @summary Saves the inline edits when pressing enter inside the inline editor.
  62. */
  63. $( 'input, select', row ).keydown( function( e ) {
  64. // 13 = [enter]
  65. if ( e.which === 13 ) {
  66. return inlineEditTax.save( this );
  67. }
  68. });
  69. /**
  70. * @summary Saves the inline edits on submitting the inline edit form.
  71. */
  72. $( '#posts-filter input[type="submit"]' ).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. * @returns {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. * @returns {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).focus();
  126. return false;
  127. },
  128. /**
  129. * @summary 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. * @returns {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. * @summary 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. $errorSpan = $( '#edit-' + id + ' .inline-edit-save .error' );
  172. $( 'table.widefat .spinner' ).removeClass( 'is-active' );
  173. if (r) {
  174. if ( -1 !== r.indexOf( '<tr' ) ) {
  175. $(inlineEditTax.what+id).siblings('tr.hidden').addBack().remove();
  176. new_id = $(r).attr('id');
  177. $('#edit-'+id).before(r).remove();
  178. if ( new_id ) {
  179. option_value = new_id.replace( inlineEditTax.type + '-', '' );
  180. row = $( '#' + new_id );
  181. } else {
  182. option_value = id;
  183. row = $( inlineEditTax.what + id );
  184. }
  185. // Update the value in the Parent dropdown.
  186. $( '#parent' ).find( 'option[value=' + option_value + ']' ).text( row.find( '.row-title' ).text() );
  187. row.hide().fadeIn( 400, function() {
  188. // Move focus back to the Quick Edit link.
  189. row.find( '.editinline' ).focus();
  190. wp.a11y.speak( inlineEditL10n.saved );
  191. });
  192. } else {
  193. $errorSpan.html( r ).show();
  194. /*
  195. * Some error strings may contain HTML entities (e.g. `&#8220`), let's use
  196. * the HTML element's text.
  197. */
  198. wp.a11y.speak( $errorSpan.text() );
  199. }
  200. } else {
  201. $errorSpan.html( inlineEditL10n.error ).show();
  202. wp.a11y.speak( inlineEditL10n.error );
  203. }
  204. }
  205. );
  206. // Prevent submitting the form when pressing Enter on a focused field.
  207. return false;
  208. },
  209. /**
  210. * Closes the quick edit form.
  211. *
  212. * @since 2.7.0
  213. *
  214. * @this inlineEditTax
  215. * @memberof inlineEditTax
  216. * @returns {void}
  217. */
  218. revert : function() {
  219. var id = $('table.widefat tr.inline-editor').attr('id');
  220. if ( id ) {
  221. $( 'table.widefat .spinner' ).removeClass( 'is-active' );
  222. $('#'+id).siblings('tr.hidden').addBack().remove();
  223. id = id.substr( id.lastIndexOf('-') + 1 );
  224. // Show the taxonomy row and move focus back to the Quick Edit link.
  225. $( this.what + id ).show().find( '.editinline' ).focus();
  226. }
  227. },
  228. /**
  229. * Retrieves the ID of the term of the element inside the table row.
  230. *
  231. * @since 2.7.0
  232. *
  233. * @memberof inlineEditTax
  234. *
  235. * @param {HTMLElement} o An element within the table row or the table row itself.
  236. * @returns {string} The ID of the term based on the element.
  237. */
  238. getId : function(o) {
  239. var id = o.tagName === 'TR' ? o.id : $(o).parents('tr').attr('id'), parts = id.split('-');
  240. return parts[parts.length - 1];
  241. }
  242. };
  243. $(document).ready(function(){inlineEditTax.init();});
  244. })( jQuery, window.wp );