wp/wp-admin/js/color-picker.js
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
     1 /**
     1 /**
     2  * @output wp-admin/js/color-picker.js
     2  * @output wp-admin/js/color-picker.js
     3  */
     3  */
     4 
     4 
     5 /* global wpColorPickerL10n */
       
     6 ( function( $, undef ) {
     5 ( function( $, undef ) {
     7 
     6 
     8 	var ColorPicker,
     7 	var ColorPicker,
     9 		_before = '<button type="button" class="button wp-color-result" aria-expanded="false"><span class="wp-color-result-text"></span></button>',
     8 		_before = '<button type="button" class="button wp-color-result" aria-expanded="false"><span class="wp-color-result-text"></span></button>',
    10 		_after = '<div class="wp-picker-holder" />',
     9 		_after = '<div class="wp-picker-holder" />',
    11 		_wrap = '<div class="wp-picker-container" />',
    10 		_wrap = '<div class="wp-picker-container" />',
    12 		_button = '<input type="button" class="button button-small" />',
    11 		_button = '<input type="button" class="button button-small" />',
    13 		_wrappingLabel = '<label></label>',
    12 		_wrappingLabel = '<label></label>',
    14 		_wrappingLabelText = '<span class="screen-reader-text"></span>';
    13 		_wrappingLabelText = '<span class="screen-reader-text"></span>',
       
    14 		__ = wp.i18n.__;
    15 
    15 
    16 	/**
    16 	/**
    17 	 * Creates a jQuery UI color picker that is used in the theme customizer.
    17 	 * Creates a jQuery UI color picker that is used in the theme customizer.
    18 	 *
    18 	 *
    19 	 * @class $.widget.wp.wpColorPicker
    19 	 * @class $.widget.wp.wpColorPicker
    34 		},
    34 		},
    35 		/**
    35 		/**
    36 		 * 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.
    37 		 *
    37 		 *
    38 		 * @since 3.5.0
    38 		 * @since 3.5.0
    39 		 *
       
    40 		 * @access private
    39 		 * @access private
    41 		 *
    40 		 *
    42 		 * @returns {void}
    41 		 * @return {void}
    43 		 */
    42 		 */
    44 		_createHueOnly: function() {
    43 		_createHueOnly: function() {
    45 			var self = this,
    44 			var self = this,
    46 				el = self.element,
    45 				el = self.element,
    47 				color;
    46 				color;
    63 				 * @ignore
    62 				 * @ignore
    64 				 *
    63 				 *
    65 				 * @param {Event} event    The event that's being called.
    64 				 * @param {Event} event    The event that's being called.
    66 				 * @param {HTMLElement} ui The HTMLElement containing the color picker.
    65 				 * @param {HTMLElement} ui The HTMLElement containing the color picker.
    67 				 *
    66 				 *
    68 				 * @returns {void}
    67 				 * @return {void}
    69 				 */
    68 				 */
    70 				change: function( event, ui ) {
    69 				change: function( event, ui ) {
    71 					if ( $.isFunction( self.options.change ) ) {
    70 					if ( $.isFunction( self.options.change ) ) {
    72 						self.options.change.call( this, event, ui );
    71 						self.options.change.call( this, event, ui );
    73 					}
    72 					}
    78 		},
    77 		},
    79 		/**
    78 		/**
    80 		 * Creates the color picker, sets default values, css classes and wraps it all in HTML.
    79 		 * Creates the color picker, sets default values, css classes and wraps it all in HTML.
    81 		 *
    80 		 *
    82 		 * @since 3.5.0
    81 		 * @since 3.5.0
    83 		 *
       
    84 		 * @access private
    82 		 * @access private
    85 		 *
    83 		 *
    86 		 * @returns {void}
    84 		 * @return {void}
    87 		 */
    85 		 */
    88 		_create: function() {
    86 		_create: function() {
    89 			// Return early if Iris support is missing.
    87 			// Return early if Iris support is missing.
    90 			if ( ! $.support.iris ) {
    88 			if ( ! $.support.iris ) {
    91 				return;
    89 				return;
   118 				// Wrap the input field in the default label.
   116 				// Wrap the input field in the default label.
   119 				el.wrap( _wrappingLabel );
   117 				el.wrap( _wrappingLabel );
   120 				// Insert the default label text.
   118 				// Insert the default label text.
   121 				self.wrappingLabelText = $( _wrappingLabelText )
   119 				self.wrappingLabelText = $( _wrappingLabelText )
   122 					.insertBefore( el )
   120 					.insertBefore( el )
   123 					.text( wpColorPickerL10n.defaultLabel );
   121 					.text( __( 'Color value' ) );
   124 			}
   122 			}
   125 
   123 
   126 			/*
   124 			/*
   127 			 * At this point, either it's the standalone version or the Customizer
   125 			 * At this point, either it's the standalone version or the Customizer
   128 			 * one, we have a wrapping label to use as hook in the DOM, let's store it.
   126 			 * one, we have a wrapping label to use as hook in the DOM, let's store it.
   136 			// Set up the toggle button and insert it before the wrapping label.
   134 			// Set up the toggle button and insert it before the wrapping label.
   137 			self.toggler = $( _before )
   135 			self.toggler = $( _before )
   138 				.insertBefore( self.wrappingLabel )
   136 				.insertBefore( self.wrappingLabel )
   139 				.css( { backgroundColor: self.initialValue } );
   137 				.css( { backgroundColor: self.initialValue } );
   140 			// Set the toggle button span element text.
   138 			// Set the toggle button span element text.
   141 			self.toggler.find( '.wp-color-result-text' ).text( wpColorPickerL10n.pick );
   139 			self.toggler.find( '.wp-color-result-text' ).text( __( 'Select Color' ) );
   142 			// Set up the Iris container and insert it after the wrapping label.
   140 			// Set up the Iris container and insert it after the wrapping label.
   143 			self.pickerContainer = $( _after ).insertAfter( self.wrappingLabel );
   141 			self.pickerContainer = $( _after ).insertAfter( self.wrappingLabel );
   144 			// Store a reference to the Clear/Default button.
   142 			// Store a reference to the Clear/Default button.
   145 			self.button = $( _button );
   143 			self.button = $( _button );
   146 
   144 
   147 			// Set up the Clear/Default button.
   145 			// Set up the Clear/Default button.
   148 			if ( self.options.defaultColor ) {
   146 			if ( self.options.defaultColor ) {
   149 				self.button
   147 				self.button
   150 					.addClass( 'wp-picker-default' )
   148 					.addClass( 'wp-picker-default' )
   151 					.val( wpColorPickerL10n.defaultString )
   149 					.val( __( 'Default' ) )
   152 					.attr( 'aria-label', wpColorPickerL10n.defaultAriaLabel );
   150 					.attr( 'aria-label', __( 'Select default color' ) );
   153 			} else {
   151 			} else {
   154 				self.button
   152 				self.button
   155 					.addClass( 'wp-picker-clear' )
   153 					.addClass( 'wp-picker-clear' )
   156 					.val( wpColorPickerL10n.clear )
   154 					.val( __( 'Clear' ) )
   157 					.attr( 'aria-label', wpColorPickerL10n.clearAriaLabel );
   155 					.attr( 'aria-label', __( 'Clear color' ) );
   158 			}
   156 			}
   159 
   157 
   160 			// Wrap the wrapping label in its wrapper and append the Clear/Default button.
   158 			// Wrap the wrapping label in its wrapper and append the Clear/Default button.
   161 			self.wrappingLabel
   159 			self.wrappingLabel
   162 				.wrap( '<span class="wp-picker-input-wrap hidden" />' )
   160 				.wrap( '<span class="wp-picker-input-wrap hidden" />' )
   184 				 * @ignore
   182 				 * @ignore
   185 				 *
   183 				 *
   186 				 * @param {Event} event    The event that's being called.
   184 				 * @param {Event} event    The event that's being called.
   187 				 * @param {HTMLElement} ui The HTMLElement containing the color picker.
   185 				 * @param {HTMLElement} ui The HTMLElement containing the color picker.
   188 				 *
   186 				 *
   189 				 * @returns {void}
   187 				 * @return {void}
   190 				 */
   188 				 */
   191 				change: function( event, ui ) {
   189 				change: function( event, ui ) {
   192 					self.toggler.css( { backgroundColor: ui.color.toString() } );
   190 					self.toggler.css( { backgroundColor: ui.color.toString() } );
   193 
   191 
   194 					if ( $.isFunction( self.options.change ) ) {
   192 					if ( $.isFunction( self.options.change ) ) {
   207 		},
   205 		},
   208 		/**
   206 		/**
   209 		 * Binds event listeners to the color picker.
   207 		 * Binds event listeners to the color picker.
   210 		 *
   208 		 *
   211 		 * @since 3.5.0
   209 		 * @since 3.5.0
   212 		 *
       
   213 		 * @access private
   210 		 * @access private
   214 		 *
   211 		 *
   215 		 * @returns {void}
   212 		 * @return {void}
   216 		 */
   213 		 */
   217 		_addListeners: function() {
   214 		_addListeners: function() {
   218 			var self = this;
   215 			var self = this;
   219 
   216 
   220 			/**
   217 			/**
   222 			 *
   219 			 *
   223 			 * @since 3.5.0
   220 			 * @since 3.5.0
   224 			 *
   221 			 *
   225 			 * @param {Event} event The event that's being called.
   222 			 * @param {Event} event The event that's being called.
   226 			 *
   223 			 *
   227 			 * @returs {void}
   224 			 * @return {void}
   228 			 */
   225 			 */
   229 			self.wrap.on( 'click.wpcolorpicker', function( event ) {
   226 			self.wrap.on( 'click.wpcolorpicker', function( event ) {
   230 				event.stopPropagation();
   227 				event.stopPropagation();
   231 			});
   228 			});
   232 
   229 
   233 			/**
   230 			/**
   234 			 * Open or close the color picker depending on the class.
   231 			 * Open or close the color picker depending on the class.
   235 			 *
   232 			 *
   236 			 * @since 3.5
   233 			 * @since 3.5.0
   237 			 */
   234 			 */
   238 			self.toggler.click( function(){
   235 			self.toggler.click( function(){
   239 				if ( self.toggler.hasClass( 'wp-picker-open' ) ) {
   236 				if ( self.toggler.hasClass( 'wp-picker-open' ) ) {
   240 					self.close();
   237 					self.close();
   241 				} else {
   238 				} else {
   249 			 *
   246 			 *
   250 			 * @since 3.5.0
   247 			 * @since 3.5.0
   251 			 *
   248 			 *
   252 			 * @param {Event} event The event that's being called.
   249 			 * @param {Event} event The event that's being called.
   253 			 *
   250 			 *
   254 			 * @returns {void}
   251 			 * @return {void}
   255 			 */
   252 			 */
   256 			self.element.change( function( event ) {
   253 			self.element.change( function( event ) {
   257 				var me = $( this ),
   254 				var me = $( this ),
   258 					val = me.val();
   255 					val = me.val();
   259 
   256 
   271 			 *
   268 			 *
   272 			 * @since 3.5.0
   269 			 * @since 3.5.0
   273 			 *
   270 			 *
   274 			 * @param {Event} event The event that's being called.
   271 			 * @param {Event} event The event that's being called.
   275 			 *
   272 			 *
   276 			 * @returns {void}
   273 			 * @return {void}
   277 			 */
   274 			 */
   278 			self.button.click( function( event ) {
   275 			self.button.click( function( event ) {
   279 				var me = $( this );
   276 				var me = $( this );
   280 				if ( me.hasClass( 'wp-picker-clear' ) ) {
   277 				if ( me.hasClass( 'wp-picker-clear' ) ) {
   281 					self.element.val( '' );
   278 					self.element.val( '' );
   291 		/**
   288 		/**
   292 		 * Opens the color picker dialog.
   289 		 * Opens the color picker dialog.
   293 		 *
   290 		 *
   294 		 * @since 3.5.0
   291 		 * @since 3.5.0
   295 		 *
   292 		 *
   296 		 * @returns {void}
   293 		 * @return {void}
   297 		 */
   294 		 */
   298 		open: function() {
   295 		open: function() {
   299 			this.element.iris( 'toggle' );
   296 			this.element.iris( 'toggle' );
   300 			this.inputWrapper.removeClass( 'hidden' );
   297 			this.inputWrapper.removeClass( 'hidden' );
   301 			this.wrap.addClass( 'wp-picker-active' );
   298 			this.wrap.addClass( 'wp-picker-active' );
   307 		/**
   304 		/**
   308 		 * Closes the color picker dialog.
   305 		 * Closes the color picker dialog.
   309 		 *
   306 		 *
   310 		 * @since 3.5.0
   307 		 * @since 3.5.0
   311 		 *
   308 		 *
   312 		 * @returns {void}
   309 		 * @return {void}
   313 		 */
   310 		 */
   314 		close: function() {
   311 		close: function() {
   315 			this.element.iris( 'toggle' );
   312 			this.element.iris( 'toggle' );
   316 			this.inputWrapper.addClass( 'hidden' );
   313 			this.inputWrapper.addClass( 'hidden' );
   317 			this.wrap.removeClass( 'wp-picker-active' );
   314 			this.wrap.removeClass( 'wp-picker-active' );
   325 		 *
   322 		 *
   326 		 * @param newColor {string|*} The new color to use. Can be undefined.
   323 		 * @param newColor {string|*} The new color to use. Can be undefined.
   327 		 *
   324 		 *
   328 		 * @since 3.5.0
   325 		 * @since 3.5.0
   329 		 *
   326 		 *
   330 		 * @returns {string} The element's color
   327 		 * @return {string} The element's color.
   331 		 */
   328 		 */
   332 		color: function( newColor ) {
   329 		color: function( newColor ) {
   333 			if ( newColor === undef ) {
   330 			if ( newColor === undef ) {
   334 				return this.element.iris( 'option', 'color' );
   331 				return this.element.iris( 'option', 'color' );
   335 			}
   332 			}
   341 		 *
   338 		 *
   342 		 * @param newDefaultColor {string|*} The new default color to use. Can be undefined.
   339 		 * @param newDefaultColor {string|*} The new default color to use. Can be undefined.
   343 		 *
   340 		 *
   344 		 * @since 3.5.0
   341 		 * @since 3.5.0
   345 		 *
   342 		 *
   346 		 * @returns {boolean|string} The element's color.
   343 		 * @return {boolean|string} The element's color.
   347 		 */
   344 		 */
   348 		defaultColor: function( newDefaultColor ) {
   345 		defaultColor: function( newDefaultColor ) {
   349 			if ( newDefaultColor === undef ) {
   346 			if ( newDefaultColor === undef ) {
   350 				return this.options.defaultColor;
   347 				return this.options.defaultColor;
   351 			}
   348 			}