wp/wp-admin/js/inline-edit-tax.js
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
     2  * This file is used on the term overview page to power quick-editing terms.
     2  * This file is used on the term overview page to power quick-editing terms.
     3  *
     3  *
     4  * @output wp-admin/js/inline-edit-tax.js
     4  * @output wp-admin/js/inline-edit-tax.js
     5  */
     5  */
     6 
     6 
     7 /* global inlineEditL10n, ajaxurl, inlineEditTax */
     7 /* global ajaxurl, inlineEditTax */
     8 
     8 
     9 window.wp = window.wp || {};
     9 window.wp = window.wp || {};
    10 
    10 
    11 /**
    11 /**
    12  * Consists of functions relevant to the inline taxonomy editor.
    12  * Consists of functions relevant to the inline taxonomy editor.
    27 	 *
    27 	 *
    28 	 * @since 2.7.0
    28 	 * @since 2.7.0
    29 	 *
    29 	 *
    30 	 * @this inlineEditTax
    30 	 * @this inlineEditTax
    31 	 * @memberof inlineEditTax
    31 	 * @memberof inlineEditTax
    32 	 * @returns {void}
    32 	 * @return {void}
    33 	 */
    33 	 */
    34 	init : function() {
    34 	init : function() {
    35 		var t = this, row = $('#inline-edit');
    35 		var t = this, row = $('#inline-edit');
    36 
    36 
    37 		t.type = $('#the-list').attr('data-wp-lists').substr(5);
    37 		t.type = $('#the-list').attr('data-wp-lists').substr(5);
    41 			$( this ).attr( 'aria-expanded', 'true' );
    41 			$( this ).attr( 'aria-expanded', 'true' );
    42 			inlineEditTax.edit( this );
    42 			inlineEditTax.edit( this );
    43 		});
    43 		});
    44 
    44 
    45 		/**
    45 		/**
    46 		 * Cancels inline editing when pressing escape inside the inline editor.
    46 		 * Cancels inline editing when pressing Escape inside the inline editor.
    47 		 *
    47 		 *
    48 		 * @param {Object} e The keyup event that has been triggered.
    48 		 * @param {Object} e The keyup event that has been triggered.
    49 		 */
    49 		 */
    50 		row.keyup( function( e ) {
    50 		row.keyup( function( e ) {
    51 			// 27 = [escape]
    51 			// 27 = [Escape].
    52 			if ( e.which === 27 ) {
    52 			if ( e.which === 27 ) {
    53 				return inlineEditTax.revert();
    53 				return inlineEditTax.revert();
    54 			}
    54 			}
    55 		});
    55 		});
    56 
    56 
    67 		$( '.save', row ).click( function() {
    67 		$( '.save', row ).click( function() {
    68 			return inlineEditTax.save(this);
    68 			return inlineEditTax.save(this);
    69 		});
    69 		});
    70 
    70 
    71 		/**
    71 		/**
    72 		 * Saves the inline edits when pressing enter inside the inline editor.
    72 		 * Saves the inline edits when pressing Enter inside the inline editor.
    73 		 */
    73 		 */
    74 		$( 'input, select', row ).keydown( function( e ) {
    74 		$( 'input, select', row ).keydown( function( e ) {
    75 			// 13 = [enter]
    75 			// 13 = [Enter].
    76 			if ( e.which === 13 ) {
    76 			if ( e.which === 13 ) {
    77 				return inlineEditTax.save( this );
    77 				return inlineEditTax.save( this );
    78 			}
    78 			}
    79 		});
    79 		});
    80 
    80 
    94 	 * @this inlineEditTax
    94 	 * @this inlineEditTax
    95 	 * @memberof inlineEditTax
    95 	 * @memberof inlineEditTax
    96 	 *
    96 	 *
    97 	 * @param {HTMLElement} el An element within the table row or the table row
    97 	 * @param {HTMLElement} el An element within the table row or the table row
    98 	 *                         itself that we want to quick edit.
    98 	 *                         itself that we want to quick edit.
    99 	 * @returns {void}
    99 	 * @return {void}
   100 	 */
   100 	 */
   101 	toggle : function(el) {
   101 	toggle : function(el) {
   102 		var t = this;
   102 		var t = this;
   103 
   103 
   104 		$(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);
   113 	 * @memberof inlineEditTax
   113 	 * @memberof inlineEditTax
   114 	 *
   114 	 *
   115 	 * @param {string|HTMLElement} id The ID of the term we want to quick edit or an
   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
   116 	 *                                element within the table row or the
   117 	 * table row itself.
   117 	 * table row itself.
   118 	 * @returns {boolean} Always returns false.
   118 	 * @return {boolean} Always returns false.
   119 	 */
   119 	 */
   120 	edit : function(id) {
   120 	edit : function(id) {
   121 		var editRow, rowData, val,
   121 		var editRow, rowData, val,
   122 			t = this;
   122 			t = this;
   123 		t.revert();
   123 		t.revert();
   160 	 * @memberof inlineEditTax
   160 	 * @memberof inlineEditTax
   161 	 *
   161 	 *
   162 	 * @param {string|HTMLElement} id The ID of the term we want to quick edit or an
   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
   163 	 *                                element within the table row or the
   164 	 * table row itself.
   164 	 * table row itself.
   165 	 * @returns {boolean} Always returns false.
   165 	 * @return {boolean} Always returns false.
   166 	 */
   166 	 */
   167 	save : function(id) {
   167 	save : function(id) {
   168 		var params, fields, tax = $('input[name="taxonomy"]').val() || '';
   168 		var params, fields, tax = $('input[name="taxonomy"]').val() || '';
   169 
   169 
   170 		// Makes sure we can pass an HTMLElement as the ID.
   170 		// Makes sure we can pass an HTMLElement as the ID.
   182 		};
   182 		};
   183 
   183 
   184 		fields = $('#edit-'+id).find(':input').serialize();
   184 		fields = $('#edit-'+id).find(':input').serialize();
   185 		params = fields + '&' + $.param(params);
   185 		params = fields + '&' + $.param(params);
   186 
   186 
   187 		// Do the ajax request to save the data to the server.
   187 		// Do the Ajax request to save the data to the server.
   188 		$.post( ajaxurl, params,
   188 		$.post( ajaxurl, params,
   189 			/**
   189 			/**
   190 			 * Handles the response from the server
   190 			 * Handles the response from the server
   191 			 *
   191 			 *
   192 			 * Handles the response from the server, replaces the table row with the response
   192 			 * Handles the response from the server, replaces the table row with the response
   222 						row.hide().fadeIn( 400, function() {
   222 						row.hide().fadeIn( 400, function() {
   223 							// Move focus back to the Quick Edit button.
   223 							// Move focus back to the Quick Edit button.
   224 							row.find( '.editinline' )
   224 							row.find( '.editinline' )
   225 								.attr( 'aria-expanded', 'false' )
   225 								.attr( 'aria-expanded', 'false' )
   226 								.focus();
   226 								.focus();
   227 							wp.a11y.speak( inlineEditL10n.saved );
   227 							wp.a11y.speak( wp.i18n.__( 'Changes saved.' ) );
   228 						});
   228 						});
   229 
   229 
   230 					} else {
   230 					} else {
   231 						$errorNotice.removeClass( 'hidden' );
   231 						$errorNotice.removeClass( 'hidden' );
   232 						$error.html( r );
   232 						$error.html( r );
   236 						 */
   236 						 */
   237 						wp.a11y.speak( $error.text() );
   237 						wp.a11y.speak( $error.text() );
   238 					}
   238 					}
   239 				} else {
   239 				} else {
   240 					$errorNotice.removeClass( 'hidden' );
   240 					$errorNotice.removeClass( 'hidden' );
   241 					$error.html( inlineEditL10n.error );
   241 					$error.text( wp.i18n.__( 'Error while saving the changes.' ) );
   242 					wp.a11y.speak( inlineEditL10n.error );
   242 					wp.a11y.speak( wp.i18n.__( 'Error while saving the changes.' ) );
   243 				}
   243 				}
   244 			}
   244 			}
   245 		);
   245 		);
   246 
   246 
   247 		// Prevent submitting the form when pressing Enter on a focused field.
   247 		// Prevent submitting the form when pressing Enter on a focused field.
   253 	 *
   253 	 *
   254 	 * @since 2.7.0
   254 	 * @since 2.7.0
   255 	 *
   255 	 *
   256 	 * @this inlineEditTax
   256 	 * @this inlineEditTax
   257 	 * @memberof inlineEditTax
   257 	 * @memberof inlineEditTax
   258 	 * @returns {void}
   258 	 * @return {void}
   259 	 */
   259 	 */
   260 	revert : function() {
   260 	revert : function() {
   261 		var id = $('table.widefat tr.inline-editor').attr('id');
   261 		var id = $('table.widefat tr.inline-editor').attr('id');
   262 
   262 
   263 		if ( id ) {
   263 		if ( id ) {
   278 	 * @since 2.7.0
   278 	 * @since 2.7.0
   279 	 *
   279 	 *
   280 	 * @memberof inlineEditTax
   280 	 * @memberof inlineEditTax
   281 	 *
   281 	 *
   282 	 * @param {HTMLElement} o An element within the table row or the table row itself.
   282 	 * @param {HTMLElement} o An element within the table row or the table row itself.
   283 	 * @returns {string} The ID of the term based on the element.
   283 	 * @return {string} The ID of the term based on the element.
   284 	 */
   284 	 */
   285 	getId : function(o) {
   285 	getId : function(o) {
   286 		var id = o.tagName === 'TR' ? o.id : $(o).parents('tr').attr('id'), parts = id.split('-');
   286 		var id = o.tagName === 'TR' ? o.id : $(o).parents('tr').attr('id'), parts = id.split('-');
   287 
   287 
   288 		return parts[parts.length - 1];
   288 		return parts[parts.length - 1];