Make WordPress Core

Changeset 38644


Ignore:
Timestamp:
09/22/2016 06:49:26 PM (9 years ago)
Author:
SergeyBiryukov
Message:

Docs: Add documentation for wp-admin/js/inline-edit-tax.js.

Props atimmer, boblinthorst.
Fixes #37571.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/js/inline-edit-tax.js

    r37439 r38644  
    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');
     
    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();
     
    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 );
     
    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();
     
    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,
     
    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);
     
    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);
     
    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,
     
    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                    }
     
    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');
     
    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();
     
    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    }
     
    161284
    162285$(document).ready(function(){inlineEditTax.init();});
     286
    163287})( jQuery, window.wp );
Note: See TracChangeset for help on using the changeset viewer.