You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

190 lines
5.3 KiB

  1. /* global wpColorPickerL10n */
  2. ( function( $, undef ){
  3. var ColorPicker,
  4. // html stuff
  5. _before = '<a tabindex="0" class="wp-color-result" />',
  6. _after = '<div class="wp-picker-holder" />',
  7. _wrap = '<div class="wp-picker-container" />',
  8. _button = '<input type="button" class="button button-small hidden" />';
  9. // jQuery UI Widget constructor
  10. ColorPicker = {
  11. options: {
  12. defaultColor: false,
  13. change: false,
  14. clear: false,
  15. hide: true,
  16. palettes: true,
  17. width: 255,
  18. mode: 'hsv',
  19. type: 'full',
  20. slider: 'horizontal'
  21. },
  22. _createHueOnly: function() {
  23. var self = this,
  24. el = self.element,
  25. color;
  26. // hide input
  27. el.hide();
  28. // max saturated color for hue to be obvious
  29. color = 'hsl(' + el.val() + ', 100, 50)';
  30. el.iris( {
  31. mode: 'hsl',
  32. type: 'hue',
  33. hide: false,
  34. color: color,
  35. change: function( event, ui ) {
  36. if ( $.isFunction( self.options.change ) ) {
  37. self.options.change.call( this, event, ui );
  38. }
  39. },
  40. width: self.options.width,
  41. slider: self.options.slider
  42. } );
  43. },
  44. _create: function() {
  45. // bail early for unsupported Iris.
  46. if ( ! $.support.iris ) {
  47. return;
  48. }
  49. var self = this,
  50. el = self.element;
  51. $.extend( self.options, el.data() );
  52. // hue-only gets created differently
  53. if ( self.options.type === 'hue' ) {
  54. return self._createHueOnly();
  55. }
  56. // keep close bound so it can be attached to a body listener
  57. self.close = $.proxy( self.close, self );
  58. self.initialValue = el.val();
  59. // Set up HTML structure, hide things
  60. el.addClass( 'wp-color-picker' ).hide().wrap( _wrap );
  61. self.wrap = el.parent();
  62. self.toggler = $( _before ).insertBefore( el ).css( { backgroundColor: self.initialValue } ).attr( 'title', wpColorPickerL10n.pick ).attr( 'data-current', wpColorPickerL10n.current );
  63. self.pickerContainer = $( _after ).insertAfter( el );
  64. self.button = $( _button );
  65. if ( self.options.defaultColor ) {
  66. self.button.addClass( 'wp-picker-default' ).val( wpColorPickerL10n.defaultString );
  67. } else {
  68. self.button.addClass( 'wp-picker-clear' ).val( wpColorPickerL10n.clear );
  69. }
  70. el.wrap( '<span class="wp-picker-input-wrap" />' ).after(self.button);
  71. el.iris( {
  72. target: self.pickerContainer,
  73. hide: self.options.hide,
  74. width: self.options.width,
  75. mode: self.options.mode,
  76. palettes: self.options.palettes,
  77. change: function( event, ui ) {
  78. self.toggler.css( { backgroundColor: ui.color.toString() } );
  79. // check for a custom cb
  80. if ( $.isFunction( self.options.change ) ) {
  81. self.options.change.call( this, event, ui );
  82. }
  83. }
  84. } );
  85. el.val( self.initialValue );
  86. self._addListeners();
  87. if ( ! self.options.hide ) {
  88. self.toggler.click();
  89. }
  90. },
  91. _addListeners: function() {
  92. var self = this;
  93. // prevent any clicks inside this widget from leaking to the top and closing it
  94. self.wrap.on( 'click.wpcolorpicker', function( event ) {
  95. event.stopPropagation();
  96. });
  97. self.toggler.click( function(){
  98. if ( self.toggler.hasClass( 'wp-picker-open' ) ) {
  99. self.close();
  100. } else {
  101. self.open();
  102. }
  103. });
  104. self.element.change( function( event ) {
  105. var me = $( this ),
  106. val = me.val();
  107. // Empty = clear
  108. if ( val === '' || val === '#' ) {
  109. self.toggler.css( 'backgroundColor', '' );
  110. // fire clear callback if we have one
  111. if ( $.isFunction( self.options.clear ) ) {
  112. self.options.clear.call( this, event );
  113. }
  114. }
  115. });
  116. // open a keyboard-focused closed picker with space or enter
  117. self.toggler.on( 'keyup', function( event ) {
  118. if ( event.keyCode === 13 || event.keyCode === 32 ) {
  119. event.preventDefault();
  120. self.toggler.trigger( 'click' ).next().focus();
  121. }
  122. });
  123. self.button.click( function( event ) {
  124. var me = $( this );
  125. if ( me.hasClass( 'wp-picker-clear' ) ) {
  126. self.element.val( '' );
  127. self.toggler.css( 'backgroundColor', '' );
  128. if ( $.isFunction( self.options.clear ) ) {
  129. self.options.clear.call( this, event );
  130. }
  131. } else if ( me.hasClass( 'wp-picker-default' ) ) {
  132. self.element.val( self.options.defaultColor ).change();
  133. }
  134. });
  135. },
  136. open: function() {
  137. this.element.show().iris( 'toggle' ).focus();
  138. this.button.removeClass( 'hidden' );
  139. this.wrap.addClass( 'wp-picker-active' );
  140. this.toggler.addClass( 'wp-picker-open' );
  141. $( 'body' ).trigger( 'click.wpcolorpicker' ).on( 'click.wpcolorpicker', this.close );
  142. },
  143. close: function() {
  144. this.element.hide().iris( 'toggle' );
  145. this.button.addClass( 'hidden' );
  146. this.wrap.removeClass( 'wp-picker-active' );
  147. this.toggler.removeClass( 'wp-picker-open' );
  148. $( 'body' ).off( 'click.wpcolorpicker', this.close );
  149. },
  150. // $("#input").wpColorPicker('color') returns the current color
  151. // $("#input").wpColorPicker('color', '#bada55') to set
  152. color: function( newColor ) {
  153. if ( newColor === undef ) {
  154. return this.element.iris( 'option', 'color' );
  155. }
  156. this.element.iris( 'option', 'color', newColor );
  157. },
  158. //$("#input").wpColorPicker('defaultColor') returns the current default color
  159. //$("#input").wpColorPicker('defaultColor', newDefaultColor) to set
  160. defaultColor: function( newDefaultColor ) {
  161. if ( newDefaultColor === undef ) {
  162. return this.options.defaultColor;
  163. }
  164. this.options.defaultColor = newDefaultColor;
  165. }
  166. };
  167. $.widget( 'wp.wpColorPicker', ColorPicker );
  168. }( jQuery ) );