1 /* global inlineEditL10n, ajaxurl */
3 * This file is used on the term overview page to power quick-editing terms.
6 window.wp = window.wp || {};
9 * Consists of functions relevant to the inline taxonomy editor.
11 * @namespace inlineEditTax
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
24 * @summary Initializes the inline taxonomy editor.
26 * Adds event handlers to be able to quick edit.
31 * @memberof inlineEditTax
35 var t = this, row = $('#inline-edit');
37 t.type = $('#the-list').attr('data-wp-lists').substr(5);
38 t.what = '#'+t.type+'-';
40 $('#the-list').on('click', 'a.editinline', function(){
41 inlineEditTax.edit(this);
46 * @summary Cancels inline editing when pressing escape inside the inline editor.
48 * @param {Object} e The keyup event that has been triggered.
50 row.keyup( function( e ) {
52 if ( e.which === 27 ) {
53 return inlineEditTax.revert();
58 * @summary Cancels inline editing when clicking the cancel button.
60 $( '.cancel', row ).click( function() {
61 return inlineEditTax.revert();
65 * @summary Saves the inline edits when clicking the save button.
67 $( '.save', row ).click( function() {
68 return inlineEditTax.save(this);
72 * @summary Saves the inline edits when pressing enter inside the inline editor.
74 $( 'input, select', row ).keydown( function( e ) {
76 if ( e.which === 13 ) {
77 return inlineEditTax.save( this );
82 * @summary Saves the inline edits on submitting the inline edit form.
84 $( '#posts-filter input[type="submit"]' ).mousedown( function() {
90 * Toggles the quick edit based on if it is currently shown or hidden.
95 * @memberof inlineEditTax
97 * @param {HTMLElement} el An element within the table row or the table row
98 * itself that we want to quick edit.
101 toggle : function(el) {
104 $(t.what+t.getId(el)).css('display') === 'none' ? t.revert() : t.edit(el);
108 * Shows the quick editor
112 * @this inlineEditTax
113 * @memberof inlineEditTax
115 * @param {string|HTMLElement} id The ID of the term we want to quick edit or an
116 * element within the table row or the
118 * @returns {boolean} Always returns false.
120 edit : function(id) {
121 var editRow, rowData, val,
125 // Makes sure we can pass an HTMLElement as the ID.
126 if ( typeof(id) === 'object' ) {
130 editRow = $('#inline-edit').clone(true), rowData = $('#inline_'+id);
131 $( 'td', editRow ).attr( 'colspan', $( 'th:visible, td:visible', '.wp-list-table.widefat:first thead' ).length );
133 $(t.what+id).hide().after(editRow).after('<tr class="hidden"></tr>');
135 val = $('.name', rowData);
136 val.find( 'img' ).replaceWith( function() { return this.alt; } );
138 $(':input[name="name"]', editRow).val( val );
140 val = $('.slug', rowData);
141 val.find( 'img' ).replaceWith( function() { return this.alt; } );
143 $(':input[name="slug"]', editRow).val( val );
145 $(editRow).attr('id', 'edit-'+id).addClass('inline-editor').show();
146 $('.ptitle', editRow).eq(0).focus();
152 * @summary Saves the quick edit data.
154 * Saves the quick edit data to the server and replaces the table row with the
155 * HTML retrieved from the server.
159 * @this inlineEditTax
160 * @memberof inlineEditTax
162 * @param {string|HTMLElement} id The ID of the term we want to quick edit or an
163 * element within the table row or the
165 * @returns {boolean} Always returns false.
167 save : function(id) {
168 var params, fields, tax = $('input[name="taxonomy"]').val() || '';
170 // Makes sure we can pass an HTMLElement as the ID.
171 if( typeof(id) === 'object' ) {
175 $( 'table.widefat .spinner' ).addClass( 'is-active' );
178 action: 'inline-save-tax',
184 fields = $('#edit-'+id).find(':input').serialize();
185 params = fields + '&' + $.param(params);
187 // Do the ajax request to save the data to the server.
188 $.post( ajaxurl, params,
190 * @summary Handles the response from the server.
192 * Handles the response from the server, replaces the table row with the response
195 * @param {string} r The string with which to replace the table row.
198 var row, new_id, option_value,
199 $errorSpan = $( '#edit-' + id + ' .inline-edit-save .error' );
201 $( 'table.widefat .spinner' ).removeClass( 'is-active' );
204 if ( -1 !== r.indexOf( '<tr' ) ) {
205 $(inlineEditTax.what+id).siblings('tr.hidden').addBack().remove();
206 new_id = $(r).attr('id');
208 $('#edit-'+id).before(r).remove();
211 option_value = new_id.replace( inlineEditTax.type + '-', '' );
212 row = $( '#' + new_id );
215 row = $( inlineEditTax.what + id );
218 // Update the value in the Parent dropdown.
219 $( '#parent' ).find( 'option[value=' + option_value + ']' ).text( row.find( '.row-title' ).text() );
221 row.hide().fadeIn( 400, function() {
222 // Move focus back to the Quick Edit link.
223 row.find( '.editinline' ).focus();
224 wp.a11y.speak( inlineEditL10n.saved );
228 $errorSpan.html( r ).show();
230 * Some error strings may contain HTML entities (e.g. `“`), let's use
231 * the HTML element's text.
233 wp.a11y.speak( $errorSpan.text() );
236 $errorSpan.html( inlineEditL10n.error ).show();
237 wp.a11y.speak( inlineEditL10n.error );
242 // Prevent submitting the form when pressing Enter on a focused field.
247 * Closes the quick edit form.
251 * @this inlineEditTax
252 * @memberof inlineEditTax
255 revert : function() {
256 var id = $('table.widefat tr.inline-editor').attr('id');
259 $( 'table.widefat .spinner' ).removeClass( 'is-active' );
260 $('#'+id).siblings('tr.hidden').addBack().remove();
261 id = id.substr( id.lastIndexOf('-') + 1 );
263 // Show the taxonomy row and move focus back to the Quick Edit link.
264 $( this.what + id ).show().find( '.editinline' ).focus();
269 * Retrieves the ID of the term of the element inside the table row.
273 * @memberof inlineEditTax
275 * @param {HTMLElement} o An element within the table row or the table row itself.
276 * @returns {string} The ID of the term based on the element.
278 getId : function(o) {
279 var id = o.tagName === 'TR' ? o.id : $(o).parents('tr').attr('id'), parts = id.split('-');
281 return parts[parts.length - 1];
285 $(document).ready(function(){inlineEditTax.init();});
287 })( jQuery, window.wp );