/* jshint -W079 */ if ( _.isUndefined( window.hooSetSettingValue ) ) { var hooSetSettingValue = { // jscs:ignore requireVarDeclFirst /** * Set the value of the control. * * @param string setting The setting-ID. * @param mixed value The value. */ set: function( setting, value ) { /** * Get the control of the sub-setting. * This will be used to get properties we need from that control, * and determine if we need to do any further work based on those. */ var $this = this, subControl = wp.customize.settings.controls[ setting ], valueJSON; // If the control doesn't exist then return. if ( _.isUndefined( subControl ) ) { return true; } // First set the value in the wp object. The control type doesn't matter here. $this.setValue( setting, value ); // Process visually changing the value based on the control type. switch ( subControl.type ) { case 'hoo-background': if ( ! _.isUndefined( value['background-color'] ) ) { $this.setColorPicker( $this.findElement( setting, '.hoo-color-control' ), value['background-color'] ); } $this.findElement( setting, '.placeholder, .thumbnail' ).removeClass().addClass( 'placeholder' ).html( 'No file selected' ); _.each( ['background-repeat', 'background-position'], function( subVal ) { if ( ! _.isUndefined( value[ subVal ] ) ) { $this.setSelect2( $this.findElement( setting, '.' + subVal + ' select' ), value[ subVal ] ); } }); _.each( ['background-size', 'background-attachment'], function( subVal ) { jQuery( $this.findElement( setting, '.' + subVal + ' input[value="' + value + '"]' ) ).prop( 'checked', true ); }); valueJSON = JSON.stringify( value ).replace( /'/g, ''' ); jQuery( $this.findElement( setting, '.background-hidden-value' ).attr( 'value', valueJSON ) ).trigger( 'change' ); break; case 'hoo-code': jQuery( $this.findElement( setting, '.CodeMirror' ) )[0].CodeMirror.setValue( value ); break; case 'checkbox': case 'hoo-switch': case 'hoo-toggle': value = ( 1 === value || '1' === value || true === value ) ? true : false; jQuery( $this.findElement( setting, 'input' ) ).prop( 'checked', value ); wp.customize.instance( setting ).set( value ); break; case 'hoo-select': case 'hoo-preset': case 'hoo-fontawesome': $this.setSelect2( $this.findElement( setting, 'select' ), value ); break; case 'hoo-slider': jQuery( $this.findElement( setting, 'input' ) ).prop( 'value', value ); jQuery( $this.findElement( setting, '.hoo_range_value .value' ) ).html( value ); break; case 'hoo-generic': if ( _.isUndefined( subControl.choices ) || _.isUndefined( subControl.choices.element ) ) { subControl.choices.element = 'input'; } jQuery( $this.findElement( setting, subControl.choices.element ) ).prop( 'value', value ); break; case 'hoo-color': $this.setColorPicker( $this.findElement( setting, '.hoo-color-control' ), value ); break; case 'hoo-multicheck': $this.findElement( setting, 'input' ).each( function() { jQuery( this ).prop( 'checked', false ); }); _.each( value, function( subValue, i ) { jQuery( $this.findElement( setting, 'input[value="' + value[ i ] + '"]' ) ).prop( 'checked', true ); }); break; case 'hoo-multicolor': _.each( value, function( subVal, index ) { $this.setColorPicker( $this.findElement( setting, '.multicolor-index-' + index ), subVal ); }); break; case 'hoo-radio-buttonset': case 'hoo-radio-image': case 'hoo-radio': case 'hoo-dashicons': case 'hoo-color-palette': case 'hoo-palette': jQuery( $this.findElement( setting, 'input[value="' + value + '"]' ) ).prop( 'checked', true ); break; case 'hoo-typography': _.each( ['font-family', 'variant', 'subsets'], function( subVal ) { if ( ! _.isUndefined( value[ subVal ] ) ) { $this.setSelect2( $this.findElement( setting, '.' + subVal + ' select' ), value[ subVal ] ); } }); _.each( ['font-size', 'line-height', 'letter-spacing', 'word-spacing'], function( subVal ) { if ( ! _.isUndefined( value[ subVal ] ) ) { jQuery( $this.findElement( setting, '.' + subVal + ' input' ) ).prop( 'value', value[ subVal ] ); } }); if ( ! _.isUndefined( value.color ) ) { $this.setColorPicker( $this.findElement( setting, '.hoo-color-control' ), value.color ); } valueJSON = JSON.stringify( value ).replace( /'/g, ''' ); jQuery( $this.findElement( setting, '.typography-hidden-value' ).attr( 'value', valueJSON ) ).trigger( 'change' ); break; case 'hoo-dimensions': _.each( value, function( subValue, id ) { jQuery( $this.findElement( setting, '.' + id + ' input' ) ).prop( 'value', subValue ); }); break; case 'hoo-repeater': // Not yet implemented. break; case 'hoo-custom': // Do nothing. break; default: jQuery( $this.findElement( setting, 'input' ) ).prop( 'value', value ); } }, /** * Set the value for colorpickers. * CAUTION: This only sets the value visually, it does not change it in th wp object. * * @param object selector jQuery object for this element. * @param string value The value we want to set. */ setColorPicker: function( selector, value ) { selector.attr( 'data-default-color', value ).data( 'default-color', value ).wpColorPicker( 'color', value ); }, /** * Sets the value in a select2 element. * CAUTION: This only sets the value visually, it does not change it in th wp object. * * @param string selector The CSS identifier for this select2. * @param string value The value we want to set. */ setSelect2: function( selector, value ) { jQuery( selector ).select2().val( value ).trigger( 'change' ); }, /** * Sets the value in textarea elements. * CAUTION: This only sets the value visually, it does not change it in th wp object. * * @param string selector The CSS identifier for this textarea. * @param string value The value we want to set. */ setTextarea: function( selector, value ) { jQuery( selector ).prop( 'value', value ); }, /** * Finds an element inside this control. * * @param string setting The setting ID. * @param string element The CSS identifier. */ findElement: function( setting, element ) { return wp.customize.control( setting ).container.find( element ); }, /** * Updates the value in the wp.customize object. * * @param string setting The setting-ID. * @param mixed value The value. */ setValue: function( setting, value, timeout ) { timeout = ( _.isUndefined( timeout ) ) ? 100 : parseInt( timeout, 10 ); wp.customize.instance( setting ).set({}); setTimeout( function() { wp.customize.instance( setting ).set( value ); }, timeout ); } }; }