wp/wp-admin/js/color-picker.js
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
       
     1 /**
       
     2  * @output wp-admin/js/color-picker.js
       
     3  */
       
     4 
     1 /* global wpColorPickerL10n */
     5 /* global wpColorPickerL10n */
     2 ( function( $, undef ) {
     6 ( function( $, undef ) {
     3 
     7 
     4 	var ColorPicker,
     8 	var ColorPicker,
     5 		_before = '<button type="button" class="button wp-color-result" aria-expanded="false"><span class="wp-color-result-text"></span></button>',
     9 		_before = '<button type="button" class="button wp-color-result" aria-expanded="false"><span class="wp-color-result-text"></span></button>',
     8 		_button = '<input type="button" class="button button-small" />',
    12 		_button = '<input type="button" class="button button-small" />',
     9 		_wrappingLabel = '<label></label>',
    13 		_wrappingLabel = '<label></label>',
    10 		_wrappingLabelText = '<span class="screen-reader-text"></span>';
    14 		_wrappingLabelText = '<span class="screen-reader-text"></span>';
    11 
    15 
    12 	/**
    16 	/**
    13 	 * @summary Creates a jQuery UI color picker.
    17 	 * Creates a jQuery UI color picker that is used in the theme customizer.
    14 	 *
    18 	 *
    15 	 * Creates a jQuery UI color picker that is used in the theme customizer.
    19 	 * @class $.widget.wp.wpColorPicker
    16 	 *
    20 	 *
    17 	 * @since 3.5.0
    21 	 * @since 3.5.0
    18 	 */
    22 	 */
    19 	ColorPicker = {
    23 	ColorPicker = /** @lends $.widget.wp.wpColorPicker.prototype */{
    20 		options: {
    24 		options: {
    21 			defaultColor: false,
    25 			defaultColor: false,
    22 			change: false,
    26 			change: false,
    23 			clear: false,
    27 			clear: false,
    24 			hide: true,
    28 			hide: true,
    27 			mode: 'hsv',
    31 			mode: 'hsv',
    28 			type: 'full',
    32 			type: 'full',
    29 			slider: 'horizontal'
    33 			slider: 'horizontal'
    30 		},
    34 		},
    31 		/**
    35 		/**
    32 		 * @summary Creates a color picker that only allows you to adjust the hue.
    36 		 * Creates a color picker that only allows you to adjust the hue.
    33 		 *
    37 		 *
    34 		 * @since 3.5.0
    38 		 * @since 3.5.0
    35 		 *
    39 		 *
    36 		 * @access private
    40 		 * @access private
    37 		 *
    41 		 *
    52 				mode: 'hsl',
    56 				mode: 'hsl',
    53 				type: 'hue',
    57 				type: 'hue',
    54 				hide: false,
    58 				hide: false,
    55 				color: color,
    59 				color: color,
    56 				/**
    60 				/**
    57 				 * @summary Handles the onChange event if one has been defined in the options.
    61 				 * Handles the onChange event if one has been defined in the options.
       
    62 				 *
       
    63 				 * @ignore
    58 				 *
    64 				 *
    59 				 * @param {Event} event    The event that's being called.
    65 				 * @param {Event} event    The event that's being called.
    60 				 * @param {HTMLElement} ui The HTMLElement containing the color picker.
    66 				 * @param {HTMLElement} ui The HTMLElement containing the color picker.
    61 				 *
    67 				 *
    62 				 * @returns {void}
    68 				 * @returns {void}
    69 				width: self.options.width,
    75 				width: self.options.width,
    70 				slider: self.options.slider
    76 				slider: self.options.slider
    71 			} );
    77 			} );
    72 		},
    78 		},
    73 		/**
    79 		/**
    74 		 * @summary Creates the color picker.
       
    75 		 *
       
    76 		 * Creates the color picker, sets default values, css classes and wraps it all in HTML.
    80 		 * Creates the color picker, sets default values, css classes and wraps it all in HTML.
    77 		 *
    81 		 *
    78 		 * @since 3.5.0
    82 		 * @since 3.5.0
    79 		 *
    83 		 *
    80 		 * @access private
    84 		 * @access private
   170 				hide: self.options.hide,
   174 				hide: self.options.hide,
   171 				width: self.options.width,
   175 				width: self.options.width,
   172 				mode: self.options.mode,
   176 				mode: self.options.mode,
   173 				palettes: self.options.palettes,
   177 				palettes: self.options.palettes,
   174 				/**
   178 				/**
   175 				 * @summary Handles the onChange event if one has been defined in the options.
       
   176 				 *
       
   177 				 * Handles the onChange event if one has been defined in the options and additionally
   179 				 * Handles the onChange event if one has been defined in the options and additionally
   178 				 * sets the background color for the toggler element.
   180 				 * sets the background color for the toggler element.
   179 				 *
   181 				 *
   180 				 * @since 3.5.0
   182 				 * @since 3.5.0
       
   183 				 *
       
   184 				 * @ignore
   181 				 *
   185 				 *
   182 				 * @param {Event} event    The event that's being called.
   186 				 * @param {Event} event    The event that's being called.
   183 				 * @param {HTMLElement} ui The HTMLElement containing the color picker.
   187 				 * @param {HTMLElement} ui The HTMLElement containing the color picker.
   184 				 *
   188 				 *
   185 				 * @returns {void}
   189 				 * @returns {void}
   200 			if ( ! self.options.hide ) {
   204 			if ( ! self.options.hide ) {
   201 				self.toggler.click();
   205 				self.toggler.click();
   202 			}
   206 			}
   203 		},
   207 		},
   204 		/**
   208 		/**
   205 		 * @summary Binds event listeners to the color picker.
   209 		 * Binds event listeners to the color picker.
   206 		 *
   210 		 *
   207 		 * @since 3.5.0
   211 		 * @since 3.5.0
   208 		 *
   212 		 *
   209 		 * @access private
   213 		 * @access private
   210 		 *
   214 		 *
   212 		 */
   216 		 */
   213 		_addListeners: function() {
   217 		_addListeners: function() {
   214 			var self = this;
   218 			var self = this;
   215 
   219 
   216 			/**
   220 			/**
   217 			 * @summary Prevent any clicks inside this widget from leaking to the top and closing it.
   221 			 * Prevent any clicks inside this widget from leaking to the top and closing it.
   218 			 *
   222 			 *
   219 			 * @since 3.5.0
   223 			 * @since 3.5.0
   220 			 *
   224 			 *
   221 			 * @param {Event} event The event that's being called.
   225 			 * @param {Event} event The event that's being called.
   222 			 *
   226 			 *
   225 			self.wrap.on( 'click.wpcolorpicker', function( event ) {
   229 			self.wrap.on( 'click.wpcolorpicker', function( event ) {
   226 				event.stopPropagation();
   230 				event.stopPropagation();
   227 			});
   231 			});
   228 
   232 
   229 			/**
   233 			/**
   230 			 * @summary Open or close the color picker depending on the class.
   234 			 * Open or close the color picker depending on the class.
   231 			 *
   235 			 *
   232 			 * @since 3.5
   236 			 * @since 3.5
   233 			 */
   237 			 */
   234 			self.toggler.click( function(){
   238 			self.toggler.click( function(){
   235 				if ( self.toggler.hasClass( 'wp-picker-open' ) ) {
   239 				if ( self.toggler.hasClass( 'wp-picker-open' ) ) {
   238 					self.open();
   242 					self.open();
   239 				}
   243 				}
   240 			});
   244 			});
   241 
   245 
   242 			/**
   246 			/**
   243 			 * @summary Checks if value is empty when changing the color in the color picker.
       
   244 			 *
       
   245 			 * Checks if value is empty when changing the color in the color picker.
   247 			 * Checks if value is empty when changing the color in the color picker.
   246 			 * If so, the background color is cleared.
   248 			 * If so, the background color is cleared.
   247 			 *
   249 			 *
   248 			 * @since 3.5.0
   250 			 * @since 3.5.0
   249 			 *
   251 			 *
   263 					}
   265 					}
   264 				}
   266 				}
   265 			});
   267 			});
   266 
   268 
   267 			/**
   269 			/**
   268 			 * @summary Enables the user to clear or revert the color in the color picker.
       
   269 			 *
       
   270 			 * Enables the user to either clear the color in the color picker or revert back to the default color.
   270 			 * Enables the user to either clear the color in the color picker or revert back to the default color.
   271 			 *
   271 			 *
   272 			 * @since 3.5.0
   272 			 * @since 3.5.0
   273 			 *
   273 			 *
   274 			 * @param {Event} event The event that's being called.
   274 			 * @param {Event} event The event that's being called.
   287 					self.element.val( self.options.defaultColor ).change();
   287 					self.element.val( self.options.defaultColor ).change();
   288 				}
   288 				}
   289 			});
   289 			});
   290 		},
   290 		},
   291 		/**
   291 		/**
   292 		 * @summary Opens the color picker dialog.
   292 		 * Opens the color picker dialog.
   293 		 *
   293 		 *
   294 		 * @since 3.5.0
   294 		 * @since 3.5.0
   295 		 *
   295 		 *
   296 		 * @returns {void}
   296 		 * @returns {void}
   297 		 */
   297 		 */
   303 				.addClass( 'wp-picker-open' )
   303 				.addClass( 'wp-picker-open' )
   304 				.attr( 'aria-expanded', 'true' );
   304 				.attr( 'aria-expanded', 'true' );
   305 			$( 'body' ).trigger( 'click.wpcolorpicker' ).on( 'click.wpcolorpicker', this.close );
   305 			$( 'body' ).trigger( 'click.wpcolorpicker' ).on( 'click.wpcolorpicker', this.close );
   306 		},
   306 		},
   307 		/**
   307 		/**
   308 		 * @summary Closes the color picker dialog.
   308 		 * Closes the color picker dialog.
   309 		 *
   309 		 *
   310 		 * @since 3.5.0
   310 		 * @since 3.5.0
   311 		 *
   311 		 *
   312 		 * @returns {void}
   312 		 * @returns {void}
   313 		 */
   313 		 */
   319 				.removeClass( 'wp-picker-open' )
   319 				.removeClass( 'wp-picker-open' )
   320 				.attr( 'aria-expanded', 'false' );
   320 				.attr( 'aria-expanded', 'false' );
   321 			$( 'body' ).off( 'click.wpcolorpicker', this.close );
   321 			$( 'body' ).off( 'click.wpcolorpicker', this.close );
   322 		},
   322 		},
   323 		/**
   323 		/**
   324 		 * @summary Returns iris object or sets new color.
       
   325 		 *
       
   326 		 * Returns the iris object if no new color is provided. If a new color is provided, it sets the new color.
   324 		 * Returns the iris object if no new color is provided. If a new color is provided, it sets the new color.
   327 		 *
   325 		 *
   328 		 * @param newColor {string|*} The new color to use. Can be undefined.
   326 		 * @param newColor {string|*} The new color to use. Can be undefined.
   329 		 *
   327 		 *
   330 		 * @since 3.5.0
   328 		 * @since 3.5.0
   336 				return this.element.iris( 'option', 'color' );
   334 				return this.element.iris( 'option', 'color' );
   337 			}
   335 			}
   338 			this.element.iris( 'option', 'color', newColor );
   336 			this.element.iris( 'option', 'color', newColor );
   339 		},
   337 		},
   340 		/**
   338 		/**
   341 		 * @summary Returns iris object or sets new default color.
       
   342 		 *
       
   343 		 * Returns the iris object if no new default color is provided.
   339 		 * Returns the iris object if no new default color is provided.
   344 		 * If a new default color is provided, it sets the new default color.
   340 		 * If a new default color is provided, it sets the new default color.
   345 		 *
   341 		 *
   346 		 * @param newDefaultColor {string|*} The new default color to use. Can be undefined.
   342 		 * @param newDefaultColor {string|*} The new default color to use. Can be undefined.
   347 		 *
   343 		 *