Unyson icon indicating copy to clipboard operation
Unyson copied to clipboard

switcher choices live preview

Open spaxter opened this issue 10 years ago • 4 comments

Hi, i want to display the value from switcher choices in customizer live preview but it didn't work...

this is in my theme-customizer.js. the "Toolbar Height" part works, "Toolbar Padding Custom" not...

( function( $ ) {

    // Toolbar Height
    wp.customize( 'fw_options[toolbar_height]', function( value ) {
        value.bind( function( newval ) {
            newval = JSON.parse(newval);
            $( '.toolbar' ).css( 'height', newval[0].value );
        } );
    } );

    // Toolbar Padding Custom
    wp.customize( 'fw_options[toolbar_padding][custom][toolbar_padding_custom]', function( value ) {
        value.bind( function( newval ) {
            newval = JSON.parse(newval);
            $( '.toolbar' ).text( newval[0].value );
        } );
    } );

} )( jQuery );`

my options in customizer.php from unyson...

// Toolbar Height
                    'toolbar_height' => array(
                        'label' => __( 'Toolbar Height', 'fw' ),
                        'type'  => 'short-text',
                        'value' => '100px',
                        'desc'    => __( '' ),
                        'help'    => __( 'Set height for Toolbar in px', 'fw' ),
                    ),

                    // Toolbar Padding
                    'toolbar_padding' => array(
                        'type'         => 'multi-picker',
                        'label'        => false,
                        'desc'         => false,
                        'picker'       => array(
                            'gadget' => array(
                                'label'        => __( 'Toolbar Padding', 'fw' ),
                                'type'         => 'switch',
                                'right-choice' => array(
                                    'value' => 'custom',
                                    'label' => __( 'Custom', 'fw' )
                                ),
                                'left-choice'  => array(
                                    'value' => 'default',
                                    'label' => __( 'Default', 'fw' )
                                ),
                                'value'        => 'yes',
                                'desc'         => __( '' ),
                                'help'  => __( 'Set default or custom padding for Toolbar in px or %', 'fw' ),
                            )
                        ),
                        'choices'      => array(

                            'default'  => array(

                                // Toolbar Padding Default
                                'toolbar_padding_default' => array(
                                    'label' => __( 'Toolbar Padding Default', 'fw' ),
                                    'type'  => 'select',
                                    'value' => '',
                                    'desc'  => __( '' ),
                                    'choices' => array(
                                        ''                  => __( 'Bootstrap standard', 'fw' ),
                                        'padding_top'       => __( 'Padding Top', 'fw' ),
                                        'padding_bottom'    => __( 'Padding Bottom', 'fw' ),
                                        'padding_remove'    => __( 'Padding Remove', 'fw' ),
                                        'padding_custom'    => __( 'Padding Custom', 'fw' ),
                                        ),                                  
                                    'help'  => __( 'Set default padding for Toolbar', 'fw' ),
                                )
                            ),

                            'custom'  => array(

                                // Toolbar Padding Custom
                                'toolbar_padding_custom' => array(
                                    'label' => __( 'Toolbar Padding Custom', 'fw' ),
                                    'type'  => 'text',
                                    'value' => '0px 0px 0px 0px',
                                    'desc'  => __( '' ),
                                    'help'  => __( 'Set custom padding for Toolbar in px', 'fw' ),
                                )
                            )

                        ),
                        'show_borders' => true,
                    ),

and finally my customizer.php from theme

/**
 * Add postMessage support for site title and description for the Theme Customizer.
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function jd_customize_register( $wp_customize ) {
    $wp_customize->get_setting( 'blogname' )->transport         = 'postMessage';
    $wp_customize->get_setting( 'blogdescription' )->transport  = 'postMessage';
    $wp_customize->get_setting( 'header_textcolor' )->transport = 'postMessage';
    if ($wp_customize->get_setting('fw_options[toolbar_padding][custom][toolbar_padding_custom]')) {
            $wp_customize->get_setting('fw_options[toolbar_padding][custom][toolbar_padding_custom]')->transport = 'postMessage';
        }
}
add_action( 'customize_register', 'jd_customize_register' );


/**
 * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
 */
function jd_customize_preview_js() {
    wp_enqueue_script( 'jd_customizer', get_template_directory_uri() . '/assets/js/customizer.js', array( 'customize-preview' ), '20130508', true );
    wp_enqueue_script('jd_customizer_unyson', get_template_directory_uri() .'/assets/js/theme-customizer.js', array( 'jquery','customize-preview' ), fw()->theme->manifest->get_version(), true );
}
add_action( 'customize_preview_init', 'jd_customize_preview_js' );

I have tried different things, but can't find a solution... hope someine can help, thanks a lot...

spaxter avatar Feb 02 '16 20:02 spaxter

nobody can help? I search for a long time now and had tried different things, but i don't found a solution. the only i want to know is, how can i get the values from sub arrays in customizer preview. e.g. fw_options[toolbar_padding][custom][toolbar_padding_custom] can anyone give me an example or a snippet please...

spaxter avatar Feb 12 '16 10:02 spaxter

Hello,

Sorry for late reply.

Customizer options are only one level, you can't hook in sub options.

test_option: value
fw_options[some_option]: value // value structure is unknown

So the solution is

  1. Change in add_action( 'customize_register', ... );

    $wp_customize->get_setting('fw_options[toolbar_padding]')->transport = 'postMessage';
    
  2. Change in customizer.js

    // Toolbar Padding Custom
    wp.customize( 'fw_options[toolbar_padding]', function( value ) {
        value.bind( function( newval ) {
            var inputs = {};
            // Transform [ {name:'x', value:'y'}, {...} ] to { 'x':'y', ... }
            $.each(JSON.parse(newval), function(x,i){ inputs[i.name] = i.value; });
    
            console.log(inputs); // debug
    
            if (JSON.parse(inputs['fw_options[toolbar_padding][gadget]']) == 'custom') {
                $( '.toolbar' ).css('padding', inputs['fw_options[toolbar_padding][custom][toolbar_padding_custom]'] );
            } else {
                $( '.toolbar' ).css('padding', '');
            }
        } );
    } );
    

ghost avatar Feb 12 '16 11:02 ghost

thanks for feedback,

i have tested your example with a other control (background-image), but i get an error in console...

// Toolbar Background Image 2
wp.customize( 'fw_options[toolbar_bg_image2]', function( value ) {
    value.bind( function( newval ) {
        var inputs = {};
        // Transform [ {name:'x', value:'y'}, {...} ] to { 'x':'y', ... }
        $.each(JSON.parse(newval), function(x,i){ inputs[i.name] = i.value; });

        console.log(inputs); // debug

        if (JSON.parse(inputs['fw_options[toolbar_bg_image2][type]']) == 'predefined') {
            $( '.toolbar' ).css('background-image', inputs['fw_options[toolbar_bg_image2][predefined]'] );
        } else {
            $( '.toolbar' ).css('background-image', 'none');
        }
    } );
} );

the console log

unbenannt-2

spaxter avatar Feb 12 '16 12:02 spaxter

You have all the details in console, just click on one of those links and it will redirect you exactly to the code with the problem.

I think the problem is in

$( '.toolbar' ).css('background-image', inputs['fw_options[toolbar_bg_image2][predefined]'] );

it must be

$( '.toolbar' ).css('background-image', 'url('+ inputs['fw_options[toolbar_bg_image2][predefined]'] +')' );

ghost avatar Feb 12 '16 12:02 ghost