Changeset 32012
- Timestamp:
- 04/04/2015 04:33:24 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/js/customize-widgets.js
r31985 r32012 928 928 929 929 /** 930 * Get the property that represents the state of an input.931 * 932 * @param {jQuery| DOMElement} input933 * @returns {string }930 * Get the state for an input depending on its type. 931 * 932 * @param {jQuery|Element} input 933 * @returns {string|boolean|array|*} 934 934 * @private 935 935 */ 936 _getInputStatePropertyName: function( input ) { 937 var $input = $( input ); 938 939 if ( $input.is( ':radio, :checkbox' ) ) { 940 return 'checked'; 936 _getInputState: function( input ) { 937 input = $( input ); 938 if ( input.is( ':radio, :checkbox' ) ) { 939 return input.prop( 'checked' ); 940 } else if ( input.is( 'select[multiple]' ) ) { 941 return input.find( 'option:selected' ).map( function () { 942 return $( this ).val(); 943 } ).get(); 941 944 } else { 942 return 'value'; 945 return input.val(); 946 } 947 }, 948 949 /** 950 * Update an input's state based on its type. 951 * 952 * @param {jQuery|Element} input 953 * @param {string|boolean|array|*} state 954 * @private 955 */ 956 _setInputState: function ( input, state ) { 957 input = $( input ); 958 if ( input.is( ':radio, :checkbox' ) ) { 959 input.prop( 'checked', state ); 960 } else if ( input.is( 'select[multiple]' ) ) { 961 if ( ! $.isArray( state ) ) { 962 state = []; 963 } else { 964 // Make sure all state items are strings since the DOM value is a string 965 state = _.map( state, function ( value ) { 966 return String( value ); 967 } ); 968 } 969 input.find( 'option' ).each( function () { 970 $( this ).prop( 'selected', -1 !== _.indexOf( state, String( this.value ) ) ); 971 } ); 972 } else { 973 input.val( state ); 943 974 } 944 975 }, … … 1017 1048 // then we do not need to touch the UI and mess up the user's ongoing editing. 1018 1049 $inputs.each( function() { 1019 var input = $( this ), 1020 property = self._getInputStatePropertyName( this ); 1021 input.data( 'state' + updateNumber, input.prop( property ) ); 1050 $( this ).data( 'state' + updateNumber, self._getInputState( this ) ); 1022 1051 } ); 1023 1052 … … 1072 1101 var $input = $( this ), 1073 1102 $sanitizedInput = $( $sanitizedInputs[i] ), 1074 property = self._getInputStatePropertyName( this ),1075 1103 submittedState, sanitizedState, canUpdateState; 1076 1104 1077 1105 submittedState = $input.data( 'state' + updateNumber ); 1078 sanitizedState = $sanitizedInput.prop( property);1106 sanitizedState = self._getInputState( $sanitizedInput ); 1079 1107 $input.data( 'sanitized', sanitizedState ); 1080 1108 1081 canUpdateState = ( submittedState !== sanitizedState && ( args.ignoreActiveElement || ! $input.is( document.activeElement ) ));1109 canUpdateState = ( ! _.isEqual( submittedState, sanitizedState ) && ( args.ignoreActiveElement || ! $input.is( document.activeElement ) ) ); 1082 1110 if ( canUpdateState ) { 1083 $input.prop( property, sanitizedState );1111 self._setInputState( $input, sanitizedState ); 1084 1112 } 1085 1113 } );
Note: See TracChangeset
for help on using the changeset viewer.