Make WordPress Core

Changeset 41264


Ignore:
Timestamp:
08/18/2017 01:09:45 PM (9 years ago)
Author:
adamsilverstein
Message:

Docs: Improve JavaScript documentation in color-picker.js.

Add and improve JSDOC blocks.

Props carolinegeven, jjcomack, jipmoors.
Fixes #41063.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/js/color-picker.js

    r38931 r41264  
    33
    44        var ColorPicker,
    5                 // html stuff
    65                _before = '<a tabindex="0" class="wp-color-result" />',
    76                _after = '<div class="wp-picker-holder" />',
     
    98                _button = '<input type="button" class="button button-small hidden" />';
    109
    11         // jQuery UI Widget constructor
     10        /**
     11         * @summary Creates a jQuery UI color picker.
     12         *
     13         * Creates a jQuery UI color picker that is used in the theme customizer.
     14         *
     15         * @since 3.5.0
     16         */
    1217        ColorPicker = {
    1318                options: {
     
    2227                        slider: 'horizontal'
    2328                },
     29                /**
     30                 * @summary Creates a color picker that only allows you to adjust the hue.
     31                 *
     32                 * @since 3.5.0
     33                 *
     34                 * @access private
     35                 *
     36                 * @returns {void}
     37                 */
    2438                _createHueOnly: function() {
    2539                        var self = this,
     
    2741                                color;
    2842
    29                         // hide input
    3043                        el.hide();
    31                         // max saturated color for hue to be obvious
     44
     45                        // Set the saturation to the maximum level.
    3246                        color = 'hsl(' + el.val() + ', 100, 50)';
    3347
     48                        // Create an instance of the color picker, using the hsl mode.
    3449                        el.iris( {
    3550                                mode: 'hsl',
     
    3752                                hide: false,
    3853                                color: color,
     54                                /**
     55                                 * @summary Handles the onChange event if one has been defined in the options.
     56                                 *
     57                                 * @param {Event} event    The event that's being called.
     58                                 * @param {HTMLElement} ui The HTMLElement containing the color picker.
     59                                 *
     60                                 * @returns {void}
     61                                 */
    3962                                change: function( event, ui ) {
    4063                                        if ( $.isFunction( self.options.change ) ) {
     
    4669                        } );
    4770                },
     71                /**
     72                 * @summary Creates the color picker.
     73                 *
     74                 * Creates the color picker, sets default values, css classes and wraps it all in HTML.
     75                 *
     76                 * @since 3.5.0
     77                 *
     78                 * @access private
     79                 *
     80                 * @returns {void}
     81                 */
    4882                _create: function() {
    49                         // bail early for unsupported Iris.
     83                        // Return early if Iris support is missing.
    5084                        if ( ! $.support.iris ) {
    5185                                return;
     
    5589                                el = self.element;
    5690
     91                        // Override default options with options bound to the element.
    5792                        $.extend( self.options, el.data() );
    5893
    59                         // hue-only gets created differently
     94                        // Create a color picker which only allows adjustments to the hue.
    6095                        if ( self.options.type === 'hue' ) {
    6196                                return self._createHueOnly();
    6297                        }
    6398
    64                         // keep close bound so it can be attached to a body listener
     99                        // Bind the close event.
    65100                        self.close = $.proxy( self.close, self );
    66101
    67102                        self.initialValue = el.val();
    68103
    69                         // Set up HTML structure, hide things
     104                        // Set up HTML structure and hide the color picker.
    70105                        el.addClass( 'wp-color-picker' ).hide().wrap( _wrap );
    71106                        self.wrap = el.parent();
     
    80115                        }
    81116
    82                         el.wrap( '<span class="wp-picker-input-wrap" />' ).after(self.button);
     117                        el.wrap( '<span class="wp-picker-input-wrap" />' ).after( self.button );
    83118
    84119                        el.iris( {
     
    88123                                mode: self.options.mode,
    89124                                palettes: self.options.palettes,
     125                                /**
     126                                 * @summary Handles the onChange event if one has been defined in the options.
     127                                 *
     128                                 * Handles the onChange event if one has been defined in the options and additionally
     129                                 * sets the background color for the toggler element.
     130                                 *
     131                                 * @since 3.5.0
     132                                 *
     133                                 * @param {Event} event    The event that's being called.
     134                                 * @param {HTMLElement} ui The HTMLElement containing the color picker.
     135                                 *
     136                                 * @returns {void}
     137                                 */
    90138                                change: function( event, ui ) {
    91139                                        self.toggler.css( { backgroundColor: ui.color.toString() } );
    92                                         // check for a custom cb
     140
    93141                                        if ( $.isFunction( self.options.change ) ) {
    94142                                                self.options.change.call( this, event, ui );
     
    99147                        el.val( self.initialValue );
    100148                        self._addListeners();
     149
     150                        // Force the color picker to always be closed on initial load.
    101151                        if ( ! self.options.hide ) {
    102152                                self.toggler.click();
    103153                        }
    104154                },
     155                /**
     156                 * @summary Binds event listeners to the color picker.
     157                 *
     158                 * @since 3.5.0
     159                 *
     160                 * @access private
     161                 *
     162                 * @returns {void}
     163                 */
    105164                _addListeners: function() {
    106165                        var self = this;
    107166
    108                         // prevent any clicks inside this widget from leaking to the top and closing it
     167                        /**
     168                         * @summary Prevent any clicks inside this widget from leaking to the top and closing it.
     169                         *
     170                         * @since 3.5.0
     171                         *
     172                         * @param {Event} event The event that's being called.
     173                         *
     174                         * @returs {void}
     175                         */
    109176                        self.wrap.on( 'click.wpcolorpicker', function( event ) {
    110177                                event.stopPropagation();
    111178                        });
    112179
     180                        /**
     181                         * @summary Open or close the color picker depending on the class.
     182                         *
     183                         * @since 3.5
     184                         */
    113185                        self.toggler.click( function(){
    114186                                if ( self.toggler.hasClass( 'wp-picker-open' ) ) {
     
    119191                        });
    120192
     193                        /**
     194                         * @summary Checks if value is empty when changing the color in the color picker.
     195                         *
     196                         * Checks if value is empty when changing the color in the color picker.
     197                         * If so, the background color is cleared.
     198                         *
     199                         * @since 3.5.0
     200                         *
     201                         * @param {Event} event The event that's being called.
     202                         *
     203                         * @returns {void}
     204                         */
    121205                        self.element.change( function( event ) {
    122206                                var me = $( this ),
    123207                                        val = me.val();
    124                                 // Empty = clear
     208
    125209                                if ( val === '' || val === '#' ) {
    126210                                        self.toggler.css( 'backgroundColor', '' );
    127                                         // fire clear callback if we have one
     211                                        // Fire clear callback if we have one.
    128212                                        if ( $.isFunction( self.options.clear ) ) {
    129213                                                self.options.clear.call( this, event );
     
    132216                        });
    133217
    134                         // open a keyboard-focused closed picker with space or enter
     218                        /**
     219                         * @summary Enables the user to open the color picker with their keyboard.
     220                         *
     221                         * Enables the user to open the color picker with their keyboard.
     222                         * This is possible by using the space or enter key.
     223                         *
     224                         * @since 3.5.0
     225                         *
     226                         * @param {Event} event The event that's being called.
     227                         *
     228                         * @returns {void}
     229                         */
    135230                        self.toggler.on( 'keyup', function( event ) {
    136231                                if ( event.keyCode === 13 || event.keyCode === 32 ) {
     
    140235                        });
    141236
     237                        /**
     238                         * @summary Enables the user to clear or revert the color in the color picker.
     239                         *
     240                         * Enables the user to either clear the color in the color picker or revert back to the default color.
     241                         *
     242                         * @since 3.5.0
     243                         *
     244                         * @param {Event} event The event that's being called.
     245                         *
     246                         * @returns {void}
     247                         */
    142248                        self.button.click( function( event ) {
    143249                                var me = $( this );
     
    153259                        });
    154260                },
     261                /**
     262                 * @summary Opens the color picker dialog.
     263                 *
     264                 * @since 3.5.0
     265                 *
     266                 * @returns {void}
     267                 */
    155268                open: function() {
    156269                        this.element.show().iris( 'toggle' ).focus();
     
    160273                        $( 'body' ).trigger( 'click.wpcolorpicker' ).on( 'click.wpcolorpicker', this.close );
    161274                },
     275                /**
     276                 * @summary Closes the color picker dialog.
     277                 *
     278                 * @since 3.5.0
     279                 *
     280                 * @returns {void}
     281                 */
    162282                close: function() {
    163283                        this.element.hide().iris( 'toggle' );
     
    167287                        $( 'body' ).off( 'click.wpcolorpicker', this.close );
    168288                },
    169                 // $("#input").wpColorPicker('color') returns the current color
    170                 // $("#input").wpColorPicker('color', '#bada55') to set
     289                /**
     290                 * @summary Returns iris object or sets new color.
     291                 *
     292                 * Returns the iris object if no new color is provided. If a new color is provided, it sets the new color.
     293                 *
     294                 * @param newColor {string|*} The new color to use. Can be undefined.
     295                 *
     296                 * @since 3.5.0
     297                 *
     298                 * @returns {string} The element's color
     299                 */
    171300                color: function( newColor ) {
    172301                        if ( newColor === undef ) {
     
    175304                        this.element.iris( 'option', 'color', newColor );
    176305                },
    177                 //$("#input").wpColorPicker('defaultColor') returns the current default color
    178                 //$("#input").wpColorPicker('defaultColor', newDefaultColor) to set
     306                /**
     307                 * @summary Returns iris object or sets new default color.
     308                 *
     309                 * Returns the iris object if no new default color is provided.
     310                 * If a new default color is provided, it sets the new default color.
     311                 *
     312                 * @param newDefaultColor {string|*} The new default color to use. Can be undefined.
     313                 *
     314                 * @since 3.5.0
     315                 *
     316                 * @returns {boolean|string} The element's color.
     317                 */
    179318                defaultColor: function( newDefaultColor ) {
    180319                        if ( newDefaultColor === undef ) {
     
    186325        };
    187326
     327        // Register the color picker as a widget.
    188328        $.widget( 'wp.wpColorPicker', ColorPicker );
    189329}( jQuery ) );
Note: See TracChangeset for help on using the changeset viewer.