Total Complexity | 5 |
Complexity/F | 1.25 |
Lines of Code | 35 |
Function Count | 4 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | /* global kirkiSetSettingValue */ |
||
2 | wp.customize.controlConstructor['kirki-preset'] = wp.customize.kirkiDynamicControl.extend({ |
||
3 | |||
4 | initKirkiControl: function() { |
||
5 | |||
6 | var control = this, |
||
|
|||
7 | selectValue; |
||
8 | |||
9 | // Trigger a change |
||
10 | this.container.on( 'change', 'select', function() { |
||
11 | |||
12 | // Get the control's value |
||
13 | selectValue = jQuery( this ).val(); |
||
14 | |||
15 | // Update the value using the customizer API and trigger the "save" button |
||
16 | control.setting.set( selectValue ); |
||
17 | |||
18 | // We have to get the choices of this control |
||
19 | // and then start parsing them to see what we have to do for each of the choices. |
||
20 | jQuery.each( control.params.choices, function( key, value ) { |
||
21 | |||
22 | // If the current value of the control is the key of the choice, |
||
23 | // then we can continue processing, Otherwise there's no reason to do anything. |
||
24 | if ( selectValue === key ) { |
||
25 | |||
26 | // Each choice has an array of settings defined in it. |
||
27 | // We'll have to loop through them all and apply the changes needed to them. |
||
28 | jQuery.each( value.settings, function( presetSetting, presetSettingValue ) { |
||
29 | kirkiSetSettingValue.set( presetSetting, presetSettingValue ); |
||
30 | }); |
||
31 | } |
||
32 | }); |
||
33 | wp.customize.previewer.refresh(); |
||
34 | }); |
||
35 | } |
||
36 | }); |
||
37 |
Since ECMAScript 6, you can create block-scoped vars or constants with the keywords
let
orconst
. These variables/constants are only valid in the code block where they have been declared.Consider the following two pieces of code:
and
The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.
To know more about this ECMA6 feature, look at the MDN pages on let and const.