2018-06-28 04:30:15 +02:00
|
|
|
/**
|
|
|
|
* @output wp-admin/js/color-picker.js
|
|
|
|
*/
|
|
|
|
|
2017-09-03 18:02:46 +02:00
|
|
|
( function( $, undef ) {
|
2012-09-27 03:57:38 +02:00
|
|
|
|
2013-11-10 03:43:10 +01:00
|
|
|
var ColorPicker,
|
2017-09-03 18:02:46 +02:00
|
|
|
_before = '<button type="button" class="button wp-color-result" aria-expanded="false"><span class="wp-color-result-text"></span></button>',
|
2012-11-06 00:00:15 +01:00
|
|
|
_after = '<div class="wp-picker-holder" />',
|
|
|
|
_wrap = '<div class="wp-picker-container" />',
|
2017-09-03 18:02:46 +02:00
|
|
|
_button = '<input type="button" class="button button-small" />',
|
|
|
|
_wrappingLabel = '<label></label>',
|
2020-07-07 18:10:05 +02:00
|
|
|
_wrappingLabelText = '<span class="screen-reader-text"></span>',
|
|
|
|
__ = wp.i18n.__;
|
2013-11-10 03:43:10 +01:00
|
|
|
|
2017-08-18 15:10:44 +02:00
|
|
|
/**
|
|
|
|
* Creates a jQuery UI color picker that is used in the theme customizer.
|
|
|
|
*
|
2017-12-15 14:42:46 +01:00
|
|
|
* @class $.widget.wp.wpColorPicker
|
|
|
|
*
|
2017-08-18 15:10:44 +02:00
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
2017-12-15 14:42:46 +01:00
|
|
|
ColorPicker = /** @lends $.widget.wp.wpColorPicker.prototype */{
|
2012-09-27 03:57:38 +02:00
|
|
|
options: {
|
|
|
|
defaultColor: false,
|
|
|
|
change: false,
|
|
|
|
clear: false,
|
2012-11-08 00:32:03 +01:00
|
|
|
hide: true,
|
2014-11-13 13:34:23 +01:00
|
|
|
palettes: true,
|
|
|
|
width: 255,
|
2016-10-26 00:33:30 +02:00
|
|
|
mode: 'hsv',
|
|
|
|
type: 'full',
|
|
|
|
slider: 'horizontal'
|
|
|
|
},
|
2017-08-18 15:10:44 +02:00
|
|
|
/**
|
2017-12-18 15:14:53 +01:00
|
|
|
* Creates a color picker that only allows you to adjust the hue.
|
2017-08-18 15:10:44 +02:00
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
* @access private
|
|
|
|
*
|
2019-11-29 19:01:03 +01:00
|
|
|
* @return {void}
|
2017-08-18 15:10:44 +02:00
|
|
|
*/
|
2016-10-26 00:33:30 +02:00
|
|
|
_createHueOnly: function() {
|
|
|
|
var self = this,
|
|
|
|
el = self.element,
|
|
|
|
color;
|
|
|
|
|
|
|
|
el.hide();
|
2017-08-18 15:10:44 +02:00
|
|
|
|
|
|
|
// Set the saturation to the maximum level.
|
2016-10-26 00:33:30 +02:00
|
|
|
color = 'hsl(' + el.val() + ', 100, 50)';
|
|
|
|
|
2017-08-18 15:10:44 +02:00
|
|
|
// Create an instance of the color picker, using the hsl mode.
|
2016-10-26 00:33:30 +02:00
|
|
|
el.iris( {
|
|
|
|
mode: 'hsl',
|
|
|
|
type: 'hue',
|
|
|
|
hide: false,
|
|
|
|
color: color,
|
2017-08-18 15:10:44 +02:00
|
|
|
/**
|
2017-12-18 15:14:53 +01:00
|
|
|
* Handles the onChange event if one has been defined in the options.
|
2017-08-18 15:10:44 +02:00
|
|
|
*
|
2017-12-15 14:42:46 +01:00
|
|
|
* @ignore
|
|
|
|
*
|
2017-08-18 15:10:44 +02:00
|
|
|
* @param {Event} event The event that's being called.
|
|
|
|
* @param {HTMLElement} ui The HTMLElement containing the color picker.
|
|
|
|
*
|
2019-11-29 19:01:03 +01:00
|
|
|
* @return {void}
|
2017-08-18 15:10:44 +02:00
|
|
|
*/
|
2016-10-26 00:33:30 +02:00
|
|
|
change: function( event, ui ) {
|
2021-02-23 20:45:04 +01:00
|
|
|
if ( typeof self.options.change === 'function' ) {
|
2016-10-26 00:33:30 +02:00
|
|
|
self.options.change.call( this, event, ui );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
width: self.options.width,
|
|
|
|
slider: self.options.slider
|
|
|
|
} );
|
2012-09-27 03:57:38 +02:00
|
|
|
},
|
2017-08-18 15:10:44 +02:00
|
|
|
/**
|
|
|
|
* Creates the color picker, sets default values, css classes and wraps it all in HTML.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
* @access private
|
|
|
|
*
|
2019-11-29 19:01:03 +01:00
|
|
|
* @return {void}
|
2017-08-18 15:10:44 +02:00
|
|
|
*/
|
2012-09-27 03:57:38 +02:00
|
|
|
_create: function() {
|
2017-08-18 15:10:44 +02:00
|
|
|
// Return early if Iris support is missing.
|
2014-05-02 13:18:15 +02:00
|
|
|
if ( ! $.support.iris ) {
|
2012-09-27 03:57:38 +02:00
|
|
|
return;
|
2014-05-02 13:18:15 +02:00
|
|
|
}
|
|
|
|
|
2013-11-09 22:29:09 +01:00
|
|
|
var self = this,
|
2013-11-10 03:43:10 +01:00
|
|
|
el = self.element;
|
2013-02-16 18:44:41 +01:00
|
|
|
|
2017-08-18 15:10:44 +02:00
|
|
|
// Override default options with options bound to the element.
|
2012-09-27 03:57:38 +02:00
|
|
|
$.extend( self.options, el.data() );
|
|
|
|
|
2017-08-18 15:10:44 +02:00
|
|
|
// Create a color picker which only allows adjustments to the hue.
|
2016-10-26 00:33:30 +02:00
|
|
|
if ( self.options.type === 'hue' ) {
|
|
|
|
return self._createHueOnly();
|
|
|
|
}
|
|
|
|
|
2017-08-18 15:10:44 +02:00
|
|
|
// Bind the close event.
|
2014-05-02 13:18:15 +02:00
|
|
|
self.close = $.proxy( self.close, self );
|
|
|
|
|
2012-09-27 03:57:38 +02:00
|
|
|
self.initialValue = el.val();
|
|
|
|
|
2017-09-03 18:02:46 +02:00
|
|
|
// Add a CSS class to the input field.
|
|
|
|
el.addClass( 'wp-color-picker' );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if there's already a wrapping label, e.g. in the Customizer.
|
|
|
|
* If there's no label, add a default one to match the Customizer template.
|
|
|
|
*/
|
|
|
|
if ( ! el.parent( 'label' ).length ) {
|
|
|
|
// Wrap the input field in the default label.
|
|
|
|
el.wrap( _wrappingLabel );
|
|
|
|
// Insert the default label text.
|
|
|
|
self.wrappingLabelText = $( _wrappingLabelText )
|
|
|
|
.insertBefore( el )
|
2020-07-07 18:10:05 +02:00
|
|
|
.text( __( 'Color value' ) );
|
2017-09-03 18:02:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* At this point, either it's the standalone version or the Customizer
|
|
|
|
* one, we have a wrapping label to use as hook in the DOM, let's store it.
|
|
|
|
*/
|
|
|
|
self.wrappingLabel = el.parent();
|
|
|
|
|
|
|
|
// Wrap the label in the main wrapper.
|
|
|
|
self.wrappingLabel.wrap( _wrap );
|
|
|
|
// Store a reference to the main wrapper.
|
|
|
|
self.wrap = self.wrappingLabel.parent();
|
|
|
|
// Set up the toggle button and insert it before the wrapping label.
|
|
|
|
self.toggler = $( _before )
|
|
|
|
.insertBefore( self.wrappingLabel )
|
|
|
|
.css( { backgroundColor: self.initialValue } );
|
|
|
|
// Set the toggle button span element text.
|
2020-07-07 18:10:05 +02:00
|
|
|
self.toggler.find( '.wp-color-result-text' ).text( __( 'Select Color' ) );
|
2017-09-03 18:02:46 +02:00
|
|
|
// Set up the Iris container and insert it after the wrapping label.
|
|
|
|
self.pickerContainer = $( _after ).insertAfter( self.wrappingLabel );
|
|
|
|
// Store a reference to the Clear/Default button.
|
2012-09-27 03:57:38 +02:00
|
|
|
self.button = $( _button );
|
|
|
|
|
2017-09-03 18:02:46 +02:00
|
|
|
// Set up the Clear/Default button.
|
2014-05-02 13:18:15 +02:00
|
|
|
if ( self.options.defaultColor ) {
|
2017-09-03 18:02:46 +02:00
|
|
|
self.button
|
|
|
|
.addClass( 'wp-picker-default' )
|
2020-07-07 18:10:05 +02:00
|
|
|
.val( __( 'Default' ) )
|
|
|
|
.attr( 'aria-label', __( 'Select default color' ) );
|
2014-05-02 13:18:15 +02:00
|
|
|
} else {
|
2017-09-03 18:02:46 +02:00
|
|
|
self.button
|
|
|
|
.addClass( 'wp-picker-clear' )
|
2020-07-07 18:10:05 +02:00
|
|
|
.val( __( 'Clear' ) )
|
|
|
|
.attr( 'aria-label', __( 'Clear color' ) );
|
2014-05-02 13:18:15 +02:00
|
|
|
}
|
2012-09-27 03:57:38 +02:00
|
|
|
|
2017-09-03 18:02:46 +02:00
|
|
|
// Wrap the wrapping label in its wrapper and append the Clear/Default button.
|
|
|
|
self.wrappingLabel
|
|
|
|
.wrap( '<span class="wp-picker-input-wrap hidden" />' )
|
|
|
|
.after( self.button );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The input wrapper now contains the label+input+Clear/Default button.
|
|
|
|
* Store a reference to the input wrapper: we'll use this to toggle
|
|
|
|
* the controls visibility.
|
|
|
|
*/
|
|
|
|
self.inputWrapper = el.closest( '.wp-picker-input-wrap' );
|
2012-09-27 03:57:38 +02:00
|
|
|
|
|
|
|
el.iris( {
|
|
|
|
target: self.pickerContainer,
|
2014-10-12 02:51:18 +02:00
|
|
|
hide: self.options.hide,
|
2014-11-13 13:34:23 +01:00
|
|
|
width: self.options.width,
|
|
|
|
mode: self.options.mode,
|
2012-11-08 00:32:03 +01:00
|
|
|
palettes: self.options.palettes,
|
2017-08-18 15:10:44 +02:00
|
|
|
/**
|
|
|
|
* Handles the onChange event if one has been defined in the options and additionally
|
|
|
|
* sets the background color for the toggler element.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
2017-12-15 14:42:46 +01:00
|
|
|
* @ignore
|
|
|
|
*
|
2017-08-18 15:10:44 +02:00
|
|
|
* @param {Event} event The event that's being called.
|
|
|
|
* @param {HTMLElement} ui The HTMLElement containing the color picker.
|
|
|
|
*
|
2019-11-29 19:01:03 +01:00
|
|
|
* @return {void}
|
2017-08-18 15:10:44 +02:00
|
|
|
*/
|
2012-09-27 03:57:38 +02:00
|
|
|
change: function( event, ui ) {
|
|
|
|
self.toggler.css( { backgroundColor: ui.color.toString() } );
|
2017-08-18 15:10:44 +02:00
|
|
|
|
2021-02-23 20:45:04 +01:00
|
|
|
if ( typeof self.options.change === 'function' ) {
|
2012-09-27 03:57:38 +02:00
|
|
|
self.options.change.call( this, event, ui );
|
2014-05-02 13:18:15 +02:00
|
|
|
}
|
2012-09-27 03:57:38 +02:00
|
|
|
}
|
|
|
|
} );
|
2014-05-02 13:18:15 +02:00
|
|
|
|
2012-09-27 03:57:38 +02:00
|
|
|
el.val( self.initialValue );
|
|
|
|
self._addListeners();
|
2017-08-18 15:10:44 +02:00
|
|
|
|
|
|
|
// Force the color picker to always be closed on initial load.
|
2014-05-02 13:18:15 +02:00
|
|
|
if ( ! self.options.hide ) {
|
2012-09-27 03:57:38 +02:00
|
|
|
self.toggler.click();
|
2014-05-02 13:18:15 +02:00
|
|
|
}
|
2012-09-27 03:57:38 +02:00
|
|
|
},
|
2017-08-18 15:10:44 +02:00
|
|
|
/**
|
2017-12-18 15:14:53 +01:00
|
|
|
* Binds event listeners to the color picker.
|
2017-08-18 15:10:44 +02:00
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
* @access private
|
|
|
|
*
|
2019-11-29 19:01:03 +01:00
|
|
|
* @return {void}
|
2017-08-18 15:10:44 +02:00
|
|
|
*/
|
2012-09-27 03:57:38 +02:00
|
|
|
_addListeners: function() {
|
|
|
|
var self = this;
|
|
|
|
|
2017-08-18 15:10:44 +02:00
|
|
|
/**
|
2017-12-18 15:14:53 +01:00
|
|
|
* Prevent any clicks inside this widget from leaking to the top and closing it.
|
2017-08-18 15:10:44 +02:00
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
|
|
|
* @param {Event} event The event that's being called.
|
|
|
|
*
|
2019-11-29 19:01:03 +01:00
|
|
|
* @return {void}
|
2017-08-18 15:10:44 +02:00
|
|
|
*/
|
2014-05-02 13:18:15 +02:00
|
|
|
self.wrap.on( 'click.wpcolorpicker', function( event ) {
|
2012-09-27 03:57:38 +02:00
|
|
|
event.stopPropagation();
|
|
|
|
});
|
|
|
|
|
2017-08-18 15:10:44 +02:00
|
|
|
/**
|
2017-12-18 15:14:53 +01:00
|
|
|
* Open or close the color picker depending on the class.
|
2017-08-18 15:10:44 +02:00
|
|
|
*
|
2019-11-29 19:01:03 +01:00
|
|
|
* @since 3.5.0
|
2017-08-18 15:10:44 +02:00
|
|
|
*/
|
2021-01-22 13:32:03 +01:00
|
|
|
self.toggler.on( 'click', function(){
|
2014-05-02 13:18:15 +02:00
|
|
|
if ( self.toggler.hasClass( 'wp-picker-open' ) ) {
|
|
|
|
self.close();
|
|
|
|
} else {
|
|
|
|
self.open();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-08-18 15:10:44 +02:00
|
|
|
/**
|
|
|
|
* Checks if value is empty when changing the color in the color picker.
|
|
|
|
* If so, the background color is cleared.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
|
|
|
* @param {Event} event The event that's being called.
|
|
|
|
*
|
2019-11-29 19:01:03 +01:00
|
|
|
* @return {void}
|
2017-08-18 15:10:44 +02:00
|
|
|
*/
|
2021-01-22 13:32:03 +01:00
|
|
|
self.element.on( 'change', function( event ) {
|
2014-05-02 13:18:15 +02:00
|
|
|
var me = $( this ),
|
2012-09-27 03:57:38 +02:00
|
|
|
val = me.val();
|
2017-08-18 15:10:44 +02:00
|
|
|
|
2012-09-27 03:57:38 +02:00
|
|
|
if ( val === '' || val === '#' ) {
|
2014-05-02 13:18:15 +02:00
|
|
|
self.toggler.css( 'backgroundColor', '' );
|
2017-08-18 15:10:44 +02:00
|
|
|
// Fire clear callback if we have one.
|
2021-02-23 20:45:04 +01:00
|
|
|
if ( typeof self.options.clear === 'function' ) {
|
2012-09-27 03:57:38 +02:00
|
|
|
self.options.clear.call( this, event );
|
2014-05-02 13:18:15 +02:00
|
|
|
}
|
2012-09-27 03:57:38 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-08-18 15:10:44 +02:00
|
|
|
/**
|
|
|
|
* Enables the user to either clear the color in the color picker or revert back to the default color.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
|
|
|
* @param {Event} event The event that's being called.
|
|
|
|
*
|
2019-11-29 19:01:03 +01:00
|
|
|
* @return {void}
|
2017-08-18 15:10:44 +02:00
|
|
|
*/
|
2021-01-22 13:32:03 +01:00
|
|
|
self.button.on( 'click', function( event ) {
|
2014-05-02 13:18:15 +02:00
|
|
|
var me = $( this );
|
2012-09-27 03:57:38 +02:00
|
|
|
if ( me.hasClass( 'wp-picker-clear' ) ) {
|
|
|
|
self.element.val( '' );
|
2014-05-02 13:18:15 +02:00
|
|
|
self.toggler.css( 'backgroundColor', '' );
|
|
|
|
if ( $.isFunction( self.options.clear ) ) {
|
2012-09-27 03:57:38 +02:00
|
|
|
self.options.clear.call( this, event );
|
2014-05-02 13:18:15 +02:00
|
|
|
}
|
2012-09-27 03:57:38 +02:00
|
|
|
} else if ( me.hasClass( 'wp-picker-default' ) ) {
|
|
|
|
self.element.val( self.options.defaultColor ).change();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2017-08-18 15:10:44 +02:00
|
|
|
/**
|
2017-12-18 15:14:53 +01:00
|
|
|
* Opens the color picker dialog.
|
2017-08-18 15:10:44 +02:00
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
2019-11-29 19:01:03 +01:00
|
|
|
* @return {void}
|
2017-08-18 15:10:44 +02:00
|
|
|
*/
|
2014-05-02 13:18:15 +02:00
|
|
|
open: function() {
|
2017-09-03 18:02:46 +02:00
|
|
|
this.element.iris( 'toggle' );
|
|
|
|
this.inputWrapper.removeClass( 'hidden' );
|
2015-10-13 03:08:25 +02:00
|
|
|
this.wrap.addClass( 'wp-picker-active' );
|
2017-09-03 18:02:46 +02:00
|
|
|
this.toggler
|
|
|
|
.addClass( 'wp-picker-open' )
|
|
|
|
.attr( 'aria-expanded', 'true' );
|
2014-05-02 13:18:15 +02:00
|
|
|
$( 'body' ).trigger( 'click.wpcolorpicker' ).on( 'click.wpcolorpicker', this.close );
|
|
|
|
},
|
2017-08-18 15:10:44 +02:00
|
|
|
/**
|
2017-12-18 15:14:53 +01:00
|
|
|
* Closes the color picker dialog.
|
2017-08-18 15:10:44 +02:00
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
2019-11-29 19:01:03 +01:00
|
|
|
* @return {void}
|
2017-08-18 15:10:44 +02:00
|
|
|
*/
|
2014-05-02 13:18:15 +02:00
|
|
|
close: function() {
|
2017-09-03 18:02:46 +02:00
|
|
|
this.element.iris( 'toggle' );
|
|
|
|
this.inputWrapper.addClass( 'hidden' );
|
2015-10-13 03:08:25 +02:00
|
|
|
this.wrap.removeClass( 'wp-picker-active' );
|
2017-09-03 18:02:46 +02:00
|
|
|
this.toggler
|
|
|
|
.removeClass( 'wp-picker-open' )
|
|
|
|
.attr( 'aria-expanded', 'false' );
|
2014-05-02 13:18:15 +02:00
|
|
|
$( 'body' ).off( 'click.wpcolorpicker', this.close );
|
2012-09-27 03:57:38 +02:00
|
|
|
},
|
2017-08-18 15:10:44 +02:00
|
|
|
/**
|
|
|
|
* Returns the iris object if no new color is provided. If a new color is provided, it sets the new color.
|
|
|
|
*
|
|
|
|
* @param newColor {string|*} The new color to use. Can be undefined.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
2019-11-29 19:01:03 +01:00
|
|
|
* @return {string} The element's color.
|
2017-08-18 15:10:44 +02:00
|
|
|
*/
|
2012-09-27 03:57:38 +02:00
|
|
|
color: function( newColor ) {
|
2014-05-02 13:18:15 +02:00
|
|
|
if ( newColor === undef ) {
|
2013-11-09 22:29:09 +01:00
|
|
|
return this.element.iris( 'option', 'color' );
|
2014-05-02 13:18:15 +02:00
|
|
|
}
|
2013-11-09 22:29:09 +01:00
|
|
|
this.element.iris( 'option', 'color', newColor );
|
2012-11-06 00:00:15 +01:00
|
|
|
},
|
2017-08-18 15:10:44 +02:00
|
|
|
/**
|
|
|
|
* Returns the iris object if no new default color is provided.
|
|
|
|
* If a new default color is provided, it sets the new default color.
|
|
|
|
*
|
|
|
|
* @param newDefaultColor {string|*} The new default color to use. Can be undefined.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
2019-11-29 19:01:03 +01:00
|
|
|
* @return {boolean|string} The element's color.
|
2017-08-18 15:10:44 +02:00
|
|
|
*/
|
2012-11-08 00:32:03 +01:00
|
|
|
defaultColor: function( newDefaultColor ) {
|
2014-05-02 13:18:15 +02:00
|
|
|
if ( newDefaultColor === undef ) {
|
2012-11-06 00:00:15 +01:00
|
|
|
return this.options.defaultColor;
|
2014-05-02 13:18:15 +02:00
|
|
|
}
|
2012-11-06 00:00:15 +01:00
|
|
|
|
|
|
|
this.options.defaultColor = newDefaultColor;
|
2012-09-27 03:57:38 +02:00
|
|
|
}
|
2013-11-09 22:29:09 +01:00
|
|
|
};
|
2012-09-27 03:57:38 +02:00
|
|
|
|
2017-08-18 15:10:44 +02:00
|
|
|
// Register the color picker as a widget.
|
2012-09-27 03:57:38 +02:00
|
|
|
$.widget( 'wp.wpColorPicker', ColorPicker );
|
2013-11-09 22:29:09 +01:00
|
|
|
}( jQuery ) );
|