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 |
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 } |
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 ) ) { |
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 { |
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 } |