2012-09-27 03:57:38 +02:00
|
|
|
( function( $, undef ){
|
|
|
|
|
|
|
|
// html stuff
|
2012-11-06 00:00:15 +01:00
|
|
|
var _before = '<a tabindex="0" class="wp-color-result" />',
|
|
|
|
_after = '<div class="wp-picker-holder" />',
|
|
|
|
_wrap = '<div class="wp-picker-container" />',
|
|
|
|
_button = '<input type="button" class="button button-small hidden" />';
|
2012-09-27 03:57:38 +02:00
|
|
|
|
|
|
|
// jQuery UI Widget constructor
|
|
|
|
var ColorPicker = {
|
|
|
|
options: {
|
|
|
|
defaultColor: false,
|
|
|
|
change: false,
|
|
|
|
clear: false,
|
2012-11-08 00:32:03 +01:00
|
|
|
hide: true,
|
|
|
|
palettes: true
|
2012-09-27 03:57:38 +02:00
|
|
|
},
|
|
|
|
_create: function() {
|
2013-02-16 18:44:41 +01:00
|
|
|
// bail early for unsupported Iris.
|
|
|
|
if ( ! $.support.iris )
|
2012-09-27 03:57:38 +02:00
|
|
|
return;
|
|
|
|
var self = this;
|
|
|
|
var el = self.element;
|
2013-02-16 18:44:41 +01:00
|
|
|
|
2012-09-27 03:57:38 +02:00
|
|
|
$.extend( self.options, el.data() );
|
|
|
|
|
|
|
|
self.initialValue = el.val();
|
|
|
|
|
|
|
|
// Set up HTML structure, hide things
|
|
|
|
el.addClass( 'wp-color-picker' ).hide().wrap( _wrap );
|
|
|
|
self.wrap = el.parent();
|
|
|
|
self.toggler = $( _before ).insertBefore( el ).css( { backgroundColor: self.initialValue } ).attr( "title", wpColorPickerL10n.pick ).attr( "data-current", wpColorPickerL10n.current );
|
|
|
|
self.pickerContainer = $( _after ).insertAfter( el );
|
|
|
|
self.button = $( _button );
|
|
|
|
|
|
|
|
if ( self.options.defaultColor )
|
|
|
|
self.button.addClass( 'wp-picker-default' ).val( wpColorPickerL10n.defaultString );
|
|
|
|
else
|
|
|
|
self.button.addClass( 'wp-picker-clear' ).val( wpColorPickerL10n.clear );
|
|
|
|
|
2012-11-06 00:00:15 +01:00
|
|
|
el.wrap('<span class="wp-picker-input-wrap" />').after(self.button);
|
2012-09-27 03:57:38 +02:00
|
|
|
|
|
|
|
el.iris( {
|
|
|
|
target: self.pickerContainer,
|
|
|
|
hide: true,
|
2012-11-06 00:00:15 +01:00
|
|
|
width: 255,
|
|
|
|
mode: 'hsv',
|
2012-11-08 00:32:03 +01:00
|
|
|
palettes: self.options.palettes,
|
2012-09-27 03:57:38 +02:00
|
|
|
change: function( event, ui ) {
|
|
|
|
self.toggler.css( { backgroundColor: ui.color.toString() } );
|
|
|
|
// check for a custom cb
|
|
|
|
if ( $.isFunction( self.options.change ) )
|
|
|
|
self.options.change.call( this, event, ui );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
el.val( self.initialValue );
|
|
|
|
self._addListeners();
|
|
|
|
if ( ! self.options.hide )
|
|
|
|
self.toggler.click();
|
|
|
|
},
|
|
|
|
_addListeners: function() {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
self.toggler.click( function( event ){
|
|
|
|
event.stopPropagation();
|
|
|
|
self.element.toggle().iris( 'toggle' );
|
|
|
|
self.button.toggleClass('hidden');
|
|
|
|
self.toggler.toggleClass( 'wp-picker-open' );
|
|
|
|
|
|
|
|
// close picker when you click outside it
|
|
|
|
if ( self.toggler.hasClass( 'wp-picker-open' ) )
|
|
|
|
$( "body" ).on( 'click', { wrap: self.wrap, toggler: self.toggler }, self._bodyListener );
|
|
|
|
else
|
|
|
|
$( "body" ).off( 'click', self._bodyListener );
|
|
|
|
});
|
|
|
|
|
|
|
|
self.element.change(function( event ) {
|
|
|
|
var me = $(this),
|
|
|
|
val = me.val();
|
|
|
|
// Empty = clear
|
|
|
|
if ( val === '' || val === '#' ) {
|
|
|
|
self.toggler.css('backgroundColor', '');
|
|
|
|
// fire clear callback if we have one
|
|
|
|
if ( $.isFunction( self.options.clear ) )
|
|
|
|
self.options.clear.call( this, event );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// open a keyboard-focused closed picker with space or enter
|
2012-11-06 00:00:15 +01:00
|
|
|
self.toggler.on('keyup', function( e ) {
|
|
|
|
if ( e.keyCode === 13 || e.keyCode === 32 ) {
|
2012-09-27 03:57:38 +02:00
|
|
|
e.preventDefault();
|
|
|
|
self.toggler.trigger('click').next().focus();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
self.button.click( function( event ) {
|
|
|
|
var me = $(this);
|
|
|
|
if ( me.hasClass( 'wp-picker-clear' ) ) {
|
|
|
|
self.element.val( '' );
|
|
|
|
self.toggler.css('backgroundColor', '');
|
|
|
|
if ( $.isFunction( self.options.clear ) )
|
|
|
|
self.options.clear.call( this, event );
|
|
|
|
} else if ( me.hasClass( 'wp-picker-default' ) ) {
|
|
|
|
self.element.val( self.options.defaultColor ).change();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
_bodyListener: function( event ) {
|
|
|
|
if ( ! event.data.wrap.find( event.target ).length )
|
|
|
|
event.data.toggler.click();
|
|
|
|
},
|
|
|
|
// $("#input").wpColorPicker('color') returns the current color
|
|
|
|
// $("#input").wpColorPicker('color', '#bada55') to set
|
|
|
|
color: function( newColor ) {
|
|
|
|
if ( newColor === undef )
|
|
|
|
return this.element.iris( "option", "color" );
|
|
|
|
|
|
|
|
this.element.iris( "option", "color", newColor );
|
2012-11-06 00:00:15 +01:00
|
|
|
},
|
2012-11-08 00:32:03 +01:00
|
|
|
//$("#input").wpColorPicker('defaultColor') returns the current default color
|
|
|
|
//$("#input").wpColorPicker('defaultColor', newDefaultColor) to set
|
|
|
|
defaultColor: function( newDefaultColor ) {
|
2012-11-06 00:00:15 +01:00
|
|
|
if ( newDefaultColor === undef )
|
|
|
|
return this.options.defaultColor;
|
|
|
|
|
|
|
|
this.options.defaultColor = newDefaultColor;
|
2012-09-27 03:57:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$.widget( 'wp.wpColorPicker', ColorPicker );
|
|
|
|
}( jQuery ) );
|