Changeset 28239
- Timestamp:
- 05/02/2014 11:17:13 AM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/js/color-picker.js
r26074 r28239 1 /* global wpColorPickerL10n :true*/1 /* global wpColorPickerL10n */ 2 2 ( function( $, undef ){ 3 3 … … 20 20 _create: function() { 21 21 // bail early for unsupported Iris. 22 if ( ! $.support.iris ) 22 if ( ! $.support.iris ) { 23 23 return; 24 } 25 24 26 var self = this, 25 27 el = self.element; 26 28 27 29 $.extend( self.options, el.data() ); 30 31 // keep close bound so it can be attached to a body listener 32 self.close = $.proxy( self.close, self ); 28 33 29 34 self.initialValue = el.val(); … … 36 41 self.button = $( _button ); 37 42 38 if ( self.options.defaultColor ) 43 if ( self.options.defaultColor ) { 39 44 self.button.addClass( 'wp-picker-default' ).val( wpColorPickerL10n.defaultString ); 40 else45 } else { 41 46 self.button.addClass( 'wp-picker-clear' ).val( wpColorPickerL10n.clear ); 47 } 42 48 43 el.wrap( '<span class="wp-picker-input-wrap" />').after(self.button);49 el.wrap( '<span class="wp-picker-input-wrap" />' ).after(self.button); 44 50 45 51 el.iris( { … … 52 58 self.toggler.css( { backgroundColor: ui.color.toString() } ); 53 59 // check for a custom cb 54 if ( $.isFunction( self.options.change ) ) 60 if ( $.isFunction( self.options.change ) ) { 55 61 self.options.change.call( this, event, ui ); 62 } 56 63 } 57 64 } ); 65 58 66 el.val( self.initialValue ); 59 67 self._addListeners(); 60 if ( ! self.options.hide ) 68 if ( ! self.options.hide ) { 61 69 self.toggler.click(); 70 } 62 71 }, 63 72 _addListeners: function() { 64 73 var self = this; 65 74 66 self.toggler.click( function( event ){ 75 // prevent any clicks inside this widget from leaking to the top and closing it 76 self.wrap.on( 'click.wpcolorpicker', function( event ) { 67 77 event.stopPropagation(); 68 self.element.toggle().iris( 'toggle' );69 self.button.toggleClass('hidden');70 self.toggler.toggleClass( 'wp-picker-open' );71 72 // close picker when you click outside it73 if ( self.toggler.hasClass( 'wp-picker-open' ) )74 $( 'body' ).on( 'click', { wrap: self.wrap, toggler: self.toggler }, self._bodyListener );75 else76 $( 'body' ).off( 'click', self._bodyListener );77 78 }); 78 79 79 self.element.change(function( event ) { 80 var me = $(this), 80 self.toggler.click( function(){ 81 if ( self.toggler.hasClass( 'wp-picker-open' ) ) { 82 self.close(); 83 } else { 84 self.open(); 85 } 86 }); 87 88 self.element.change( function( event ) { 89 var me = $( this ), 81 90 val = me.val(); 82 91 // Empty = clear 83 92 if ( val === '' || val === '#' ) { 84 self.toggler.css( 'backgroundColor', '');93 self.toggler.css( 'backgroundColor', '' ); 85 94 // fire clear callback if we have one 86 if ( $.isFunction( self.options.clear ) ) 95 if ( $.isFunction( self.options.clear ) ) { 87 96 self.options.clear.call( this, event ); 97 } 88 98 } 89 99 }); 90 100 91 101 // open a keyboard-focused closed picker with space or enter 92 self.toggler.on( 'keyup', function( e) {93 if ( e .keyCode === 13 || e.keyCode === 32 ) {94 e .preventDefault();95 self.toggler.trigger( 'click').next().focus();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(); 96 106 } 97 107 }); 98 108 99 109 self.button.click( function( event ) { 100 var me = $( this);110 var me = $( this ); 101 111 if ( me.hasClass( 'wp-picker-clear' ) ) { 102 112 self.element.val( '' ); 103 self.toggler.css( 'backgroundColor', '');104 if ( $.isFunction( self.options.clear ) ) 113 self.toggler.css( 'backgroundColor', '' ); 114 if ( $.isFunction( self.options.clear ) ) { 105 115 self.options.clear.call( this, event ); 116 } 106 117 } else if ( me.hasClass( 'wp-picker-default' ) ) { 107 118 self.element.val( self.options.defaultColor ).change(); … … 109 120 }); 110 121 }, 111 _bodyListener: function( event ) { 112 if ( ! event.data.wrap.find( event.target ).length ) 113 event.data.toggler.click(); 122 open: function() { 123 this.element.show().iris( 'toggle' ); 124 this.button.removeClass( 'hidden' ); 125 this.toggler.addClass( 'wp-picker-open' ); 126 $( 'body' ).trigger( 'click.wpcolorpicker' ).on( 'click.wpcolorpicker', this.close ); 127 }, 128 close: function() { 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 ); 114 133 }, 115 134 // $("#input").wpColorPicker('color') returns the current color 116 135 // $("#input").wpColorPicker('color', '#bada55') to set 117 136 color: function( newColor ) { 118 if ( newColor === undef ) 137 if ( newColor === undef ) { 119 138 return this.element.iris( 'option', 'color' ); 139 } 120 140 121 141 this.element.iris( 'option', 'color', newColor ); … … 124 144 //$("#input").wpColorPicker('defaultColor', newDefaultColor) to set 125 145 defaultColor: function( newDefaultColor ) { 126 if ( newDefaultColor === undef ) 146 if ( newDefaultColor === undef ) { 127 147 return this.options.defaultColor; 148 } 128 149 129 150 this.options.defaultColor = newDefaultColor;
Note: See TracChangeset
for help on using the changeset viewer.