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