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