1
|
|
|
/* global dimensionskirkiL10n */ |
2
|
|
|
wp.customize.controlConstructor['kirki-dimensions'] = wp.customize.kirkiDynamicControl.extend({ |
3
|
|
|
|
4
|
|
|
initKirkiControl: function() { |
5
|
|
|
|
6
|
|
|
var control = this, |
|
|
|
|
7
|
|
|
subControls = control.params.choices.controls, |
8
|
|
|
value = {}, |
9
|
|
|
subsArray = [], |
10
|
|
|
i; |
11
|
|
|
|
12
|
|
|
_.each( subControls, function( v, i ) { |
13
|
|
|
if ( true === v ) { |
14
|
|
|
subsArray.push( i ); |
15
|
|
|
} |
16
|
|
|
} ); |
17
|
|
|
|
18
|
|
|
for ( i = 0; i < subsArray.length; i++ ) { |
19
|
|
|
value[ subsArray[ i ] ] = control.setting._value[ subsArray[ i ] ]; |
20
|
|
|
control.updateDimensionsValue( subsArray[ i ], value ); |
21
|
|
|
} |
22
|
|
|
}, |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Updates the value. |
26
|
|
|
*/ |
27
|
|
|
updateDimensionsValue: function( context, value ) { |
28
|
|
|
|
29
|
|
|
var control = this; |
|
|
|
|
30
|
|
|
|
31
|
|
|
control.container.on( 'change keyup paste', '.' + context + ' input', function() { |
32
|
|
|
value[ context ] = jQuery( this ).val(); |
33
|
|
|
|
34
|
|
|
// Notifications. |
35
|
|
|
control.kirkiNotifications(); |
36
|
|
|
|
37
|
|
|
// Save the value |
38
|
|
|
control.saveValue( value ); |
39
|
|
|
}); |
40
|
|
|
}, |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Saves the value. |
44
|
|
|
*/ |
45
|
|
|
saveValue: function( value ) { |
46
|
|
|
|
47
|
|
|
var control = this, |
|
|
|
|
48
|
|
|
newValue = {}; |
49
|
|
|
|
50
|
|
|
_.each( value, function( newSubValue, i ) { |
51
|
|
|
newValue[ i ] = newSubValue; |
52
|
|
|
}); |
53
|
|
|
|
54
|
|
|
control.setting.set( newValue ); |
55
|
|
|
}, |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Handles notifications. |
59
|
|
|
*/ |
60
|
|
|
kirkiNotifications: function() { |
61
|
|
|
|
62
|
|
|
var control = this; |
|
|
|
|
63
|
|
|
|
64
|
|
|
wp.customize( control.id, function( setting ) { |
65
|
|
|
setting.bind( function( value ) { |
66
|
|
|
var code = 'long_title', |
|
|
|
|
67
|
|
|
subs = {}, |
68
|
|
|
message; |
69
|
|
|
|
70
|
|
|
setting.notifications.remove( code ); |
71
|
|
|
|
72
|
|
|
_.each( value, function( val, direction ) { |
73
|
|
|
if ( false === control.kirkiValidateCSSValue( val ) ) { |
74
|
|
|
subs[ direction ] = val; |
75
|
|
|
} else { |
76
|
|
|
delete subs[ direction ]; |
77
|
|
|
} |
78
|
|
|
} ); |
79
|
|
|
|
80
|
|
|
if ( ! _.isEmpty( subs ) ) { |
81
|
|
|
message = dimensionskirkiL10n['invalid-value'] + ' (' + _.values( subs ).toString() + ') '; |
82
|
|
|
setting.notifications.add( code, new wp.customize.Notification( code, { |
83
|
|
|
type: 'warning', |
84
|
|
|
message: message |
85
|
|
|
} ) ); |
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
setting.notifications.remove( code ); |
89
|
|
|
} ); |
90
|
|
|
} ); |
91
|
|
|
} |
92
|
|
|
}); |
93
|
|
|
|
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.