/** * customizer.js * * author Denis Franchi * package Atomy * version 1.0.5 */ /** Sortable Repeater Custom Control */ jQuery( document ).ready(function($) { "use strict"; // Update the values for all our input fields and initialise the sortable repeater $('.sortable_repeater_control').each(function() { // If there is an existing customizer value, populate our rows var defaultValuesArray = $(this).find('.customize-control-sortable-repeater').val().split(','); var numRepeaterItems = defaultValuesArray.length; if(numRepeaterItems > 0) { // Add the first item to our existing input field $(this).find('.repeater-input').val(defaultValuesArray[0]); // Create a new row for each new value if(numRepeaterItems > 1) { var i; for (i = 1; i < numRepeaterItems; ++i) { skyrocketAppendRow($(this), defaultValuesArray[i]); } } } }); // Make our Repeater fields sortable $(this).find('.sortable').sortable({ update: function(event, ui) { skyrocketGetAllInputs($(this).parent()); } }); // Remove item starting from it's parent element $('.sortable').on('click', '.customize-control-sortable-repeater-delete', function(event) { event.preventDefault(); var numItems = $(this).parent().parent().find('.repeater').length; if(numItems > 1) { $(this).parent().slideUp('fast', function() { var parentContainer = $(this).parent().parent(); $(this).remove(); skyrocketGetAllInputs(parentContainer); }) } else { $(this).parent().find('.repeater-input').val(''); skyrocketGetAllInputs($(this).parent().parent().parent()); } }); // Add new item $('.customize-control-sortable-repeater-add').click(function(event) { event.preventDefault(); skyrocketAppendRow($(this).parent()); skyrocketGetAllInputs($(this).parent()); }); // Refresh our hidden field if any fields change $('.sortable').change(function() { skyrocketGetAllInputs($(this).parent()); }) // Add https:// to the start of the URL if it doesn't have it $('.sortable').on('blur', '.repeater-input', function() { var url = $(this); var val = url.val(); if(val && !val.match(/^.+:\/\/.*/)) { // Important! Make sure to trigger change event so Customizer knows it has to save the field url.val('https://' + val).trigger('change'); } }); // Append a new row to our list of elements function skyrocketAppendRow($element, defaultValue = '') { var newRow = '
'; $element.find('.sortable').append(newRow); $element.find('.sortable').find('.repeater:last').slideDown('slow', function(){ $(this).find('input').focus(); }); } // Get the values from the repeater input fields and add to our hidden field function skyrocketGetAllInputs($element) { var inputValues = $element.find('.repeater-input').map(function() { return $(this).val(); }).toArray(); // Add all the values from our repeater fields to the hidden field (which is the one that actually gets saved) $element.find('.customize-control-sortable-repeater').val(inputValues); // Important! Make sure to trigger change event so Customizer knows it has to save the field $element.find('.customize-control-sortable-repeater').trigger('change'); } /** Slider Custom Control */ // Set our slider defaults and initialise the slider $('.slider-custom-control').each(function(){ var sliderValue = $(this).find('.customize-control-slider-value').val(); var newSlider = $(this).find('.slider'); var sliderMinValue = parseFloat(newSlider.attr('slider-min-value')); var sliderMaxValue = parseFloat(newSlider.attr('slider-max-value')); var sliderStepValue = parseFloat(newSlider.attr('slider-step-value')); newSlider.slider({ value: sliderValue, min: sliderMinValue, max: sliderMaxValue, step: sliderStepValue, change: function(e,ui){ // Important! When slider stops moving make sure to trigger change event so Customizer knows it has to save the field $(this).parent().find('.customize-control-slider-value').trigger('change'); } }); }); // Change the value of the input field as the slider is moved $('.slider').on('slide', function(event, ui) { $(this).parent().find('.customize-control-slider-value').val(ui.value); }); // Reset slider and input field back to the default value $('.slider-reset').on('click', function() { var resetValue = $(this).attr('slider-reset-value'); $(this).parent().find('.customize-control-slider-value').val(resetValue); $(this).parent().find('.slider').slider('value', resetValue); }); // Update slider if the input field loses focus as it's most likely changed $('.customize-control-slider-value').blur(function() { var resetValue = $(this).val(); var slider = $(this).parent().find('.slider'); var sliderMinValue = parseInt(slider.attr('slider-min-value')); var sliderMaxValue = parseInt(slider.attr('slider-max-value')); // Make sure our manual input value doesn't exceed the minimum & maxmium values if(resetValue < sliderMinValue) { resetValue = sliderMinValue; $(this).val(resetValue); } if(resetValue > sliderMaxValue) { resetValue = sliderMaxValue; $(this).val(resetValue); } $(this).parent().find('.slider').slider('value', resetValue); }); /** Single Accordion Custom Control */ $('.single-accordion-toggle').click(function() { var $accordionToggle = $(this); $(this).parent().find('.single-accordion').slideToggle('slow', function() { $accordionToggle.toggleClass('single-accordion-toggle-rotate', $(this).is(':visible')); }); }); /** Image Check Box Custom Control */ $('.multi-image-checkbox').on('change', function () { getAllCheckboxes($(this).parent().parent()); }); // Get the values from the checkboxes and add to our hidden field function getAllCheckboxes($element) { var inputValues = $element.find('.multi-image-checkbox').map(function() { if( $(this).is(':checked') ) { return $(this).val(); // } else { // return ''; } }).toArray(); // Important! Make sure to trigger change event so Customizer knows it has to save the field $element.find('.customize-control-multi-image-checkbox').val(inputValues).trigger('change'); } /** Alpha Color Picker Custom Control */ // Loop over each control and transform it into our color picker. $( '.alpha-color-control' ).each( function() { // Scope the vars. var $control, startingColor, paletteInput, showOpacity, defaultColor, palette, colorPickerOptions, $container, $alphaSlider, alphaVal, sliderOptions; // Store the control instance. $control = $( this ); // Get a clean starting value for the option. startingColor = $control.val().replace( /\s+/g, '' ); // Get some data off the control. paletteInput = $control.attr( 'data-palette' ); showOpacity = $control.attr( 'data-show-opacity' ); defaultColor = $control.attr( 'data-default-color' ); // Process the palette. if ( paletteInput.indexOf( '|' ) !== -1 ) { palette = paletteInput.split( '|' ); } else if ( 'false' == paletteInput ) { palette = false; } else { palette = true; } // Set up the options that we'll pass to wpColorPicker(). colorPickerOptions = { change: function( event, ui ) { var key, value, alpha, $transparency; key = $control.attr( 'data-customize-setting-link' ); value = $control.wpColorPicker( 'color' ); // Set the opacity value on the slider handle when the default color button is clicked. if ( defaultColor == value ) { alpha = acp_get_alpha_value_from_color( value ); $alphaSlider.find( '.ui-slider-handle' ).text( alpha ); } // Send ajax request to wp.customize to trigger the Save action. wp.customize( key, function( obj ) { obj.set( value ); }); $transparency = $container.find( '.transparency' ); // Always show the background color of the opacity slider at 100% opacity. $transparency.css( 'background-color', ui.color.toString( 'no-alpha' ) ); }, palettes: palette // Use the passed in palette. }; // Create the colorpicker. $control.wpColorPicker( colorPickerOptions ); $container = $control.parents( '.wp-picker-container:first' ); // Insert our opacity slider. $( '