Passed
Push — develop ( 3892e2...b1663d )
by Aristeides
03:47
created

modules/css-vars/script.js (1 issue)

1
/* global kirkiCssVarFields */
2
var kirkiCssVars = {
3
4
	/**
5
	 * Get styles.
6
	 *
7
	 * @since 3.0.28
8
	 * @returns {Object}
9
	 */
10
	getStyles: function() {
11
		var style     = jQuery( '#kirki-css-vars' ),
12
			styles    = style.html().replace( ':root{', '' ).replace( '}', '' ).split( ';' ),
13
			stylesObj = {};
14
15
		// Format styles as a object we can then tweak.
16
		_.each( styles, function( style ) {
17
			style = style.split( ':' );
18
			if ( style[0] && style[1] ) {
19
				stylesObj[ style[0] ] = style[1];
20
			}
21
		} );
22
		return stylesObj;
23
	},
24
25
	/**
26
	 * Builds the styles from an object.
27
	 *
28
	 * @since 3.0.28
29
	 * @param {Object} vars - The vars.
30
	 * @returns {string}
31
	 */
32
	buildStyle: function( vars ) {
33
		var style = '';
34
35
		_.each( vars, function( val, name ) {
36
			style += name + ':' + val + ';';
37
		} );
38
		return ':root{' + style + '}';
39
	}
40
};
41
42
jQuery( document ).ready( function() {
43
	_.each( kirkiCssVarFields, function( field ) {
44
		wp.customize( field.settings, function( value ) {
45
			value.bind( function( newVal ) {
46
				var val = newVal;
47
				styles = kirkiCssVars.getStyles();
48
49
				_.each( field.css_vars, function( cssVar ) {
50
					if ( cssVar[2] && _.isObject( value ) && value[ cssVar[2] ] ) {
51
						newVal = value[ cssVar[2] ];
0 ignored issues
show
Comprehensibility Best Practice introduced by
This re-assigns to the parameter newVal. Re-assigning to parameters often makes code less readable, consider introducing a new variable instead.
Loading history...
52
					}
53
					styles[ cssVar[0] ] = cssVar[1].replace( '$', newVal );
54
				} );
55
				jQuery( '#kirki-css-vars' ).html( kirkiCssVars.buildStyle( styles ) )				;
56
			} );
57
		} );
58
	} );
59
} );
60