Test Setup Failed
Push — master ( c73065...3e6d69 )
by Aristeides
02:06
created

controls/js/src/dimensions.js   A

Complexity

Total Complexity 14
Complexity/F 1.4

Size

Lines of Code 91
Function Count 10

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 0
nc 1
dl 0
loc 91
rs 10
c 0
b 0
f 0
wmc 14
mnd 1
bc 15
fnc 10
bpm 1.5
cpm 1.4
noi 5
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