/* global ajaxurl, kirkiL10n */ var kirki = { initialized: false, /** * Initialize the object. * * @since 3.0.17 * @returns {void} */ initialize: function() { var self = this; // We only need to initialize once. if ( self.initialized ) { return; } self.util.webfonts.google.initialize(); // Mark as initialized. self.initialized = true; }, /** * An object containing definitions for controls. * * @since 3.0.16 */ control: { /** * The radio control. * * @since 3.0.17 */ 'kirki-radio': { /** * Init the control. * * @since 3.0.17 * @param {Object} control - The customizer control object. * @returns {void} */ init: function( control ) { var self = this; // Render the template. self.template( control ); // Init the control. kirki.input.radio.init( control ); }, /** * Render the template. * * @since 3.0.17 * @param {Object} control - The customizer control object. * @param {Object} control.params - The control parameters. * @param {string} control.params.label - The control label. * @param {string} control.params.description - The control description. * @param {string} control.params.inputAttrs - extra input arguments. * @param {string} control.params.default - The default value. * @param {Object} control.params.choices - Any extra choices we may need. * @param {string} control.id - The setting. * @returns {void} */ template: function( control ) { var template = wp.template( 'kirki-input-radio' ); control.container.html( template( { label: control.params.label, description: control.params.description, 'data-id': control.id, inputAttrs: control.params.inputAttrs, 'default': control.params['default'], value: kirki.setting.get( control.id ), choices: control.params.choices } ) ); } }, /** * The color control. * * @since 3.0.16 */ 'kirki-color': { /** * Init the control. * * @since 3.0.16 * @param {Object} control - The customizer control object. * @returns {void} */ init: function( control ) { var self = this; // Render the template. self.template( control ); // Init the control. kirki.input.color.init( control ); }, /** * Render the template. * * @since 3.0.16 * @param {Object} control - The customizer control object. * @param {Object} control.params - The control parameters. * @param {string} control.params.label - The control label. * @param {string} control.params.description - The control description. * @param {string} control.params.mode - The colorpicker mode. Can be 'full' or 'hue'. * @param {bool|array} control.params.palette - false if we don't want a palette, * true to use the default palette, * array of custom hex colors if we want a custom palette. * @param {string} control.params.inputAttrs - extra input arguments. * @param {string} control.params.default - The default value. * @param {Object} control.params.choices - Any extra choices we may need. * @param {boolean} control.params.choices.alpha - should we add an alpha channel? * @param {string} control.id - The setting. * @returns {void} */ template: function( control ) { var template = wp.template( 'kirki-input-color' ); control.container.html( template( { label: control.params.label, description: control.params.description, 'data-id': control.id, mode: control.params.mode, inputAttrs: control.params.inputAttrs, 'data-palette': control.params.palette, 'data-default-color': control.params['default'], 'data-alpha': control.params.choices.alpha, value: kirki.setting.get( control.id ) } ) ); } }, /** * The generic control. * * @since 3.0.16 */ 'kirki-generic': { /** * Init the control. * * @since 3.0.17 * @param {Object} control - The customizer control object. * @param {Object} control.params - Control parameters. * @param {Object} control.params.choices - Define the specifics for this input. * @param {string} control.params.choices.element - The HTML element we want to use ('input', 'div', 'span' etc). * @returns {void} */ init: function( control ) { var self = this; // Render the template. self.template( control ); // Init the control. if ( ! _.isUndefined( control.params ) && ! _.isUndefined( control.params.choices ) && ! _.isUndefined( control.params.choices.element ) && 'textarea' === control.params.choices.element ) { kirki.input.textarea.init( control ); return; } kirki.input.genericInput.init( control ); }, /** * Render the template. * * @since 3.0.17 * @param {Object} control - The customizer control object. * @param {Object} control.params - The control parameters. * @param {string} control.params.label - The control label. * @param {string} control.params.description - The control description. * @param {string} control.params.inputAttrs - extra input arguments. * @param {string} control.params.default - The default value. * @param {Object} control.params.choices - Any extra choices we may need. * @param {boolean} control.params.choices.alpha - should we add an alpha channel? * @param {string} control.id - The setting. * @returns {void} */ template: function( control ) { var args = { label: control.params.label, description: control.params.description, 'data-id': control.id, inputAttrs: control.params.inputAttrs, choices: control.params.choices, value: kirki.setting.get( control.id ) }, template; if ( ! _.isUndefined( control.params ) && ! _.isUndefined( control.params.choices ) && ! _.isUndefined( control.params.choices.element ) && 'textarea' === control.params.choices.element ) { template = wp.template( 'kirki-input-textarea' ); control.container.html( template( args ) ); return; } template = wp.template( 'kirki-input-generic' ); control.container.html( template( args ) ); } }, 'kirki-select': { /** * Init the control. * * @since 3.0.17 * @param {Object} control - The customizer control object. * @returns {void} */ init: function( control ) { var self = this; // Render the template. self.template( control ); // Init the control. kirki.input.select.init( control ); }, /** * Render the template. * * @since 3.0.17 * @param {Object} control - The customizer control object. * @param {Object} control.params - The control parameters. * @param {string} control.params.label - The control label. * @param {string} control.params.description - The control description. * @param {string} control.params.inputAttrs - extra input arguments. * @param {Object} control.params.default - The default value. * @param {Object} control.params.choices - The choices for the select dropdown. * @param {string} control.id - The setting. * @returns {void} */ template: function( control ) { var template = wp.template( 'kirki-input-select' ); control.container.html( template( { label: control.params.label, description: control.params.description, 'data-id': control.id, inputAttrs: control.params.inputAttrs, choices: control.params.choices, value: kirki.setting.get( control.id ), multiple: control.params.multiple || 1 } ) ); } } }, /** * An object containing definitions for input fields. * * @since 3.0.16 */ input: { /** * Radio input fields. * * @since 3.0.17 */ radio: { /** * Init the control. * * @since 3.0.17 * @param {Object} control - The control object. * @param {Object} control.id - The setting. * @returns {void} */ init: function( control ) { var input = jQuery( 'input[data-id="' + control.id + '"]' ); // Save the value input.on( 'change keyup paste click', function() { kirki.setting.set( control.id, jQuery( this ).val() ); }); } }, /** * Color input fields. * * @since 3.0.16 */ color: { /** * Init the control. * * @since 3.0.16 * @param {Object} control - The control object. * @param {Object} control.id - The setting. * @param {Object} control.choices - Additional options for the colorpickers. * @param {Object} control.params - Control parameters. * @param {Object} control.params.choices - alias for control.choices. * @returns {void} */ init: function( control ) { var picker = jQuery( '.kirki-color-control[data-id="' + control.id + '"]' ), clear; control.choices = control.choices || {}; if ( _.isEmpty( control.choices ) && control.params.choices ) { control.choices = control.params.choices; } // If we have defined any extra choices, make sure they are passed-on to Iris. if ( ! _.isEmpty( control.choices ) ) { picker.wpColorPicker( control.choices ); } // Tweaks to make the "clear" buttons work. setTimeout( function() { clear = jQuery( '.kirki-input-container[data-id="' + control.id + '"] .wp-picker-clear' ); if ( clear.length ) { clear.click( function() { kirki.setting.set( control.id, '' ); }); } }, 200 ); // Saves our settings to the WP API picker.wpColorPicker({ change: function() { // Small hack: the picker needs a small delay setTimeout( function() { kirki.setting.set( control.id, picker.val() ); }, 20 ); } }); } }, /** * Generic input fields. * * @since 3.0.17 */ genericInput: { /** * Init the control. * * @since 3.0.17 * @param {Object} control - The control object. * @param {Object} control.id - The setting. * @returns {void} */ init: function( control ) { var input = jQuery( 'input[data-id="' + control.id + '"]' ); // Save the value input.on( 'change keyup paste click', function() { kirki.setting.set( control.id, jQuery( this ).val() ); }); } }, /** * Generic input fields. * * @since 3.0.17 */ textarea: { /** * Init the control. * * @since 3.0.17 * @param {Object} control - The control object. * @param {Object} control.id - The setting. * @returns {void} */ init: function( control ) { var textarea = jQuery( 'textarea[data-id="' + control.id + '"]' ); // Save the value textarea.on( 'change keyup paste click', function() { kirki.setting.set( control.id, jQuery( this ).val() ); }); } }, select: { /** * Init the control. * * @since 3.0.17 * @param {Object} control - The control object. * @param {Object} control.id - The setting. * @returns {void} */ init: function( control ) { var element = jQuery( 'select[data-id="' + control.id + '"]' ), multiple = parseInt( element.data( 'multiple' ), 10 ), selectValue, selectWooOptions = { escapeMarkup: function( markup ) { return markup; } }; if ( 1 < multiple ) { selectWooOptions.maximumSelectionLength = multiple; } jQuery( element ).selectWoo( selectWooOptions ).on( 'change', function() { selectValue = jQuery( this ).val(); selectValue = ( null === selectValue && 1 < multiple ) ? [] : selectValue; kirki.setting.set( control.id, selectValue ); }); } }, image: { /** * Get the HTML for image inputs. * * @since 3.0.17 * @param {Object} data - The arguments. * @returns {string} */ getTemplate: function( data ) { var html = '', saveAs = 'url', url; data = _.defaults( data, { label: '', description: '', inputAttrs: '', 'data-id': '', choices: {}, value: '' } ); if ( ! _.isUndefined( data.choices ) && ! _.isUndefined( data.choices.save_as ) ) { saveAs = data.choices.save_as; } url = data.value; if ( _.isObject( data.value ) && ! _.isUndefined( data.value.url ) ) { url = data.value.url; } html += ''; html += '
'; return '