var kirki = {
/**
* An object containing definitions for controls.
*
* @since 3.0.16
*/
control: {
/**
* 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.
* @returns {void}
*/
template: function( control ) {
control.container.html( kirki.input.color.getTemplate( {
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: control.setting._value
} ) );
}
}
},
/**
* An object containing definitions for input fields.
*
* @since 3.0.16
*/
input: {
/**
* Color input fields.
*
* @since 3.0.16
*/
color: {
/**
* Get the HTML for color inputs.
*
* @since 3.0.16
* @param {object} [data] The arguments.
* @returns {string}
*/
getTemplate: function( data ) {
var html = '';
data = _.defaults( data, {
label: '',
description: '',
mode: 'full',
inputAttrs: '',
'data-palette': data['data-palette'] ? data['data-palette'] : true,
'data-default-color': data['data-default-color'] ? data['data-default-color'] : '',
'data-alpha': data['data-alpha'] ? data['data-alpha'] : false,
value: '',
'data-id': ''
} );
html += '';
html += '';
return '
' + html + '
';
},
/**
* Init the control.
*
* @since 3.0.16
* @param {object} [control] The control object.
* @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() {
control.setting.set( '' );
});
}
}, 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 );
}
});
}
}
},
/**
* An object containing definitions for settings.
*
* @since 3.0.16
*/
setting: {
/**
* Gets the value of a setting.
*
* This is a helper function that allows us to get the value of
* control[key1][key2] for example, when the setting used in the
* customizer API is "control".
*
* @since 3.0.16
* @param {string} [setting] The setting for which we're getting the value.
* @returns {mixed} Depends on the value.
*/
get: function( setting ) {
var parts = setting.split( '[' ),
foundSetting = '',
foundInStep = 0,
currentVal = '';
_.each( parts, function( part, i ) {
part = part.replace( ']', '' );
if ( 0 === i ) {
foundSetting = part;
} else {
foundSetting += '[' + part + ']';
}
if ( ! _.isUndefined( wp.customize.instance( foundSetting ) ) ) {
currentVal = wp.customize.instance( foundSetting ).get();
foundInStep = i;
}
if ( foundInStep < i ) {
if ( _.isObject( currentVal ) && ! _.isUndefined( currentVal[ part ] ) ) {
currentVal = currentVal[ part ];
}
}
});
return currentVal;
},
/**
* Sets the value of a setting.
*
* This function is a bit complicated because there any many scenarios to consider.
* Example: We want to save the value for my_setting[something][3][something-else].
* The control's setting is my_setting[something].
* So we need to find that first, then figure out the remaining parts,
* merge the values recursively to avoid destroying my_setting[something][2]
* and also take into account any defined "key" arguments which take this even deeper.
*
* @since 3.0.16
* @param {object|string} [element] The DOM element whose value has changed,
* or an ID.
* @param {mixed} [value] Depends on the control-type.
* @param {string} [key] If we only want to save an item in an object
* we can define the key here.
* @returns {void}
*/
set: function( element, value, key ) {
var setting,
parts,
currentNode = '',
foundNode = '',
subSettingObj = {},
currentVal,
subSetting,
subSettingParts;
// Get the setting from the element.
setting = element;
if ( _.isObject( element ) ) {
if ( jQuery( element ).attr( 'data-id' ) ) {
setting = element.attr( 'data-id' );
} else {
setting = element.parents( '[data-id]' ).attr( 'data-id' );
}
}
parts = setting.split( '[' ),
// Find the setting we're using in the control using the customizer API.
_.each( parts, function( part, i ) {
part = part.replace( ']', '' );
// The current part of the setting.
currentNode = ( 0 === i ) ? part : '[' + part + ']';
// When we find the node, get the value from it.
// In case of an object we'll need to merge with current values.
if ( ! _.isUndefined( wp.customize.instance( currentNode ) ) ) {
foundNode = currentNode;
currentVal = wp.customize.instance( foundNode ).get();
}
} );
// Get the remaining part of the setting that was unused.
subSetting = setting.replace( foundNode, '' );
// If subSetting is not empty, then we're dealing with an object
// and we need to dig deeper and recursively merge the values.
if ( '' !== subSetting ) {
if ( ! _.isObject( currentVal ) ) {
currentVal = {};
}
if ( '[' === subSetting.charAt( 0 ) ) {
subSetting = subSetting.replace( '[', '' );
}
subSettingParts = subSetting.split( '[' );
_.each( subSettingParts, function( subSettingPart, i ) {
subSettingParts[ i ] = subSettingPart.replace( ']', '' );
} );
// If using a key, we need to go 1 level deeper.
if ( key ) {
subSettingParts.push( key );
}
// Converting to a JSON string and then parsing that to an object
// may seem a bit hacky and crude but it's efficient and works.
subSettingObj = '{"' + subSettingParts.join( '":{"' ) + '":"' + value + '"' + '}'.repeat( subSettingParts.length );
subSettingObj = JSON.parse( subSettingObj );
// Recursively merge with current value.
jQuery.extend( true, currentVal, subSettingObj );
value = currentVal;
} else {
if ( key ) {
currentVal = ( ! _.isObject( currentVal ) ) ? {} : currentVal;
currentVal[ key ] = value;
value = currentVal;
}
}
wp.customize.control( foundNode ).setting.set( value );
}
}
};