1 /* global wpColorPickerL10n */
2 ( function( $, undef ){
6 _before = '<a tabindex="0" class="wp-color-result" />',
7 _after = '<div class="wp-picker-holder" />',
8 _wrap = '<div class="wp-picker-container" />',
9 _button = '<input type="button" class="button button-small hidden" />';
11 // jQuery UI Widget constructor
21 // bail early for unsupported Iris.
22 if ( ! $.support.iris ) {
29 $.extend( self.options, el.data() );
31 // keep close bound so it can be attached to a body listener
32 self.close = $.proxy( self.close, self );
34 self.initialValue = el.val();
36 // Set up HTML structure, hide things
37 el.addClass( 'wp-color-picker' ).hide().wrap( _wrap );
38 self.wrap = el.parent();
39 self.toggler = $( _before ).insertBefore( el ).css( { backgroundColor: self.initialValue } ).attr( 'title', wpColorPickerL10n.pick ).attr( 'data-current', wpColorPickerL10n.current );
40 self.pickerContainer = $( _after ).insertAfter( el );
41 self.button = $( _button );
43 if ( self.options.defaultColor ) {
44 self.button.addClass( 'wp-picker-default' ).val( wpColorPickerL10n.defaultString );
46 self.button.addClass( 'wp-picker-clear' ).val( wpColorPickerL10n.clear );
49 el.wrap( '<span class="wp-picker-input-wrap" />' ).after(self.button);
52 target: self.pickerContainer,
56 palettes: self.options.palettes,
57 change: function( event, ui ) {
58 self.toggler.css( { backgroundColor: ui.color.toString() } );
59 // check for a custom cb
60 if ( $.isFunction( self.options.change ) ) {
61 self.options.change.call( this, event, ui );
66 el.val( self.initialValue );
68 if ( ! self.options.hide ) {
72 _addListeners: function() {
75 // prevent any clicks inside this widget from leaking to the top and closing it
76 self.wrap.on( 'click.wpcolorpicker', function( event ) {
77 event.stopPropagation();
80 self.toggler.click( function(){
81 if ( self.toggler.hasClass( 'wp-picker-open' ) ) {
88 self.element.change( function( event ) {
92 if ( val === '' || val === '#' ) {
93 self.toggler.css( 'backgroundColor', '' );
94 // fire clear callback if we have one
95 if ( $.isFunction( self.options.clear ) ) {
96 self.options.clear.call( this, event );
101 // open a keyboard-focused closed picker with space or enter
102 self.toggler.on( 'keyup', function( event ) {
103 if ( event.keyCode === 13 || event.keyCode === 32 ) {
104 event.preventDefault();
105 self.toggler.trigger( 'click' ).next().focus();
109 self.button.click( function( event ) {
111 if ( me.hasClass( 'wp-picker-clear' ) ) {
112 self.element.val( '' );
113 self.toggler.css( 'backgroundColor', '' );
114 if ( $.isFunction( self.options.clear ) ) {
115 self.options.clear.call( this, event );
117 } else if ( me.hasClass( 'wp-picker-default' ) ) {
118 self.element.val( self.options.defaultColor ).change();
123 this.element.show().iris( 'toggle' ).focus();
124 this.button.removeClass( 'hidden' );
125 this.toggler.addClass( 'wp-picker-open' );
126 $( 'body' ).trigger( 'click.wpcolorpicker' ).on( 'click.wpcolorpicker', this.close );
129 this.element.hide().iris( 'toggle' );
130 this.button.addClass( 'hidden' );
131 this.toggler.removeClass( 'wp-picker-open' );
132 $( 'body' ).off( 'click.wpcolorpicker', this.close );
134 // $("#input").wpColorPicker('color') returns the current color
135 // $("#input").wpColorPicker('color', '#bada55') to set
136 color: function( newColor ) {
137 if ( newColor === undef ) {
138 return this.element.iris( 'option', 'color' );
141 this.element.iris( 'option', 'color', newColor );
143 //$("#input").wpColorPicker('defaultColor') returns the current default color
144 //$("#input").wpColorPicker('defaultColor', newDefaultColor) to set
145 defaultColor: function( newDefaultColor ) {
146 if ( newDefaultColor === undef ) {
147 return this.options.defaultColor;
150 this.options.defaultColor = newDefaultColor;
154 $.widget( 'wp.wpColorPicker', ColorPicker );