Make WordPress Core

Ticket #37571: document-inline-edit-tax.diff

File document-inline-edit-tax.diff, 6.2 KB (added by atimmer, 9 years ago)
  • src/wp-admin/js/inline-edit-tax.js

    diff --git a/src/wp-admin/js/inline-edit-tax.js b/src/wp-admin/js/inline-edit-tax.js
    index 3bbf4fb..daad73f 100644
    a b  
    11/* global inlineEditL10n, ajaxurl */
     2/**
     3 * This file is used on the term overview page to power quick-editing terms.
     4 */
     5
    26window.wp = window.wp || {};
    37
     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 */
    417var inlineEditTax;
     18
    519( function( $, wp ) {
     20
    621inlineEditTax = {
    722
     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         */
    834        init : function() {
    935                var t = this, row = $('#inline-edit');
    1036
    inlineEditTax = { 
    1642                        return false;
    1743                });
    1844
    19                 // 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                 */
    2050                row.keyup( function( e ) {
     51                        // 27 = [escape]
    2152                        if ( e.which === 27 ) {
    2253                                return inlineEditTax.revert();
    2354                        }
    2455                });
    2556
     57                /**
     58                 * @summary Cancels inline editing when clicking the cancel button.
     59                 */
    2660                $( '.cancel', row ).click( function() {
    2761                        return inlineEditTax.revert();
    2862                });
     63
     64                /**
     65                 * @summary Saves the inline edits when clicking the save button.
     66                 */
    2967                $( '.save', row ).click( function() {
    3068                        return inlineEditTax.save(this);
    3169                });
     70
     71                /**
     72                 * @summary Saves the inline edits when pressing enter inside the inline editor.
     73                 */
    3274                $( 'input, select', row ).keydown( function( e ) {
     75                        // 13 = [enter]
    3376                        if ( e.which === 13 ) {
    3477                                return inlineEditTax.save( this );
    3578                        }
    3679                });
    3780
     81                /**
     82                 * @summary Saves the inline edits on submitting the inline edit form.
     83                 */
    3884                $( '#posts-filter input[type="submit"]' ).mousedown( function() {
    3985                        t.revert();
    4086                });
    4187        },
    4288
     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         */
    43101        toggle : function(el) {
    44102                var t = this;
     103
    45104                $(t.what+t.getId(el)).css('display') === 'none' ? t.revert() : t.edit(el);
    46105        },
    47106
     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         */
    48120        edit : function(id) {
    49121                var editRow, rowData, val,
    50122                        t = this;
    51123                t.revert();
    52124
     125                // Makes sure we can pass an HTMLElement as the ID.
    53126                if ( typeof(id) === 'object' ) {
    54127                        id = t.getId(id);
    55128                }
    inlineEditTax = { 
    75148                return false;
    76149        },
    77150
     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         */
    78167        save : function(id) {
    79168                var params, fields, tax = $('input[name="taxonomy"]').val() || '';
    80169
     170                // Makes sure we can pass an HTMLElement as the ID.
    81171                if( typeof(id) === 'object' ) {
    82172                        id = this.getId(id);
    83173                }
    inlineEditTax = { 
    94184                fields = $('#edit-'+id).find(':input').serialize();
    95185                params = fields + '&' + $.param(params);
    96186
    97                 // make ajax request
     187                // Do the ajax request to save the data to the server.
    98188                $.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                         */
    99197                        function(r) {
    100198                                var row, new_id, option_value,
    101199                                        $errorSpan = $( '#edit-' + id + ' .inline-edit-save .error' );
    inlineEditTax = { 
    128226
    129227                                        } else {
    130228                                                $errorSpan.html( r ).show();
    131                                                 // Some error strings may contain HTML entities (e.g. `&#8220`), let's use the HTML element's text.
     229                                                /*
     230                                                 * Some error strings may contain HTML entities (e.g. `&#8220`), let's use
     231                                                 * the HTML element's text.
     232                                                 */
    132233                                                wp.a11y.speak( $errorSpan.text() );
    133234                                        }
    134235                                } else {
    inlineEditTax = { 
    137238                                }
    138239                        }
    139240                );
     241
    140242                // Prevent submitting the form when pressing Enter on a focused field.
    141243                return false;
    142244        },
    143245
     246        /**
     247         * Closes the quick edit form.
     248         *
     249         * @since 2.7.0
     250         *
     251         * @this inlineEditTax
     252         * @memberof inlineEditTax
     253         * @returns {void}
     254         */
    144255        revert : function() {
    145256                var id = $('table.widefat tr.inline-editor').attr('id');
    146257
    inlineEditTax = { 
    148259                        $( 'table.widefat .spinner' ).removeClass( 'is-active' );
    149260                        $('#'+id).siblings('tr.hidden').addBack().remove();
    150261                        id = id.substr( id.lastIndexOf('-') + 1 );
     262
    151263                        // Show the taxonomy row and move focus back to the Quick Edit link.
    152264                        $( this.what + id ).show().find( '.editinline' ).focus();
    153265                }
    154266        },
    155267
     268        /**
     269         * Retrieves the ID of the term of the element inside the table row.
     270         *
     271         * @since 2.7.0
     272         *
     273         * @memberof inlineEditTax
     274         *
     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.
     277         */
    156278        getId : function(o) {
    157279                var id = o.tagName === 'TR' ? o.id : $(o).parents('tr').attr('id'), parts = id.split('-');
     280
    158281                return parts[parts.length - 1];
    159282        }
    160283};
    161284
    162285$(document).ready(function(){inlineEditTax.init();});
     286
    163287})( jQuery, window.wp );