Customize Widgets: Improve sync logic for select[multiple] inputs.

The current logic doesn't account for the special case of `select[multiple]` inputs which lack a single value to synchronize: The value to synchronize is an array of zero or more values.
This change replaces `_getInputStatePropertyName()` with `_getInputState()`, which returns the state for an input depending on its type, and `_setInputState()`, which updates an input's state based on its type.

props westonruter.
fixes #31885.
Built from https://develop.svn.wordpress.org/trunk@32012


git-svn-id: http://core.svn.wordpress.org/trunk@31991 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Dominik Schilling 2015-04-04 16:34:26 +00:00
parent 0306f2c011
commit 4f3fb2315d
3 changed files with 46 additions and 18 deletions

View File

@ -927,19 +927,50 @@
},
/**
* Get the property that represents the state of an input.
* Get the state for an input depending on its type.
*
* @param {jQuery|DOMElement} input
* @returns {string}
* @param {jQuery|Element} input
* @returns {string|boolean|array|*}
* @private
*/
_getInputStatePropertyName: function( input ) {
var $input = $( input );
if ( $input.is( ':radio, :checkbox' ) ) {
return 'checked';
_getInputState: function( input ) {
input = $( input );
if ( input.is( ':radio, :checkbox' ) ) {
return input.prop( 'checked' );
} else if ( input.is( 'select[multiple]' ) ) {
return input.find( 'option:selected' ).map( function () {
return $( this ).val();
} ).get();
} else {
return 'value';
return input.val();
}
},
/**
* Update an input's state based on its type.
*
* @param {jQuery|Element} input
* @param {string|boolean|array|*} state
* @private
*/
_setInputState: function ( input, state ) {
input = $( input );
if ( input.is( ':radio, :checkbox' ) ) {
input.prop( 'checked', state );
} else if ( input.is( 'select[multiple]' ) ) {
if ( ! $.isArray( state ) ) {
state = [];
} else {
// Make sure all state items are strings since the DOM value is a string
state = _.map( state, function ( value ) {
return String( value );
} );
}
input.find( 'option' ).each( function () {
$( this ).prop( 'selected', -1 !== _.indexOf( state, String( this.value ) ) );
} );
} else {
input.val( state );
}
},
@ -1016,9 +1047,7 @@
// we know if it got sanitized; if there is no difference in the sanitized value,
// then we do not need to touch the UI and mess up the user's ongoing editing.
$inputs.each( function() {
var input = $( this ),
property = self._getInputStatePropertyName( this );
input.data( 'state' + updateNumber, input.prop( property ) );
$( this ).data( 'state' + updateNumber, self._getInputState( this ) );
} );
if ( instanceOverride ) {
@ -1071,16 +1100,15 @@
$inputs.each( function( i ) {
var $input = $( this ),
$sanitizedInput = $( $sanitizedInputs[i] ),
property = self._getInputStatePropertyName( this ),
submittedState, sanitizedState, canUpdateState;
submittedState = $input.data( 'state' + updateNumber );
sanitizedState = $sanitizedInput.prop( property );
sanitizedState = self._getInputState( $sanitizedInput );
$input.data( 'sanitized', sanitizedState );
canUpdateState = ( submittedState !== sanitizedState && ( args.ignoreActiveElement || ! $input.is( document.activeElement ) ) );
canUpdateState = ( ! _.isEqual( submittedState, sanitizedState ) && ( args.ignoreActiveElement || ! $input.is( document.activeElement ) ) );
if ( canUpdateState ) {
$input.prop( property, sanitizedState );
self._setInputState( $input, sanitizedState );
}
} );

File diff suppressed because one or more lines are too long

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.2-beta4-32011';
$wp_version = '4.2-beta4-32012';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.