Completed
Pull Request — master (#1710)
by Aristeides
18:06 queued 08:13
created

controls/js/src/slider.js   A

Complexity

Total Complexity 6
Complexity/F 1.2

Size

Lines of Code 44
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 0
nc 2
dl 0
loc 44
rs 10
c 0
b 0
f 0
wmc 6
mnd 1
bc 5
fnc 5
bpm 1
cpm 1.2
noi 1
1
wp.customize.controlConstructor['kirki-slider'] = wp.customize.kirkiDynamicControl.extend({
2
3
	initKirkiControl: function() {
4
		var control      = this,
5
		    changeAction = ( 'postMessage' === control.setting.transport ) ? 'mousemove change' : 'change',
6
			rangeInput   = control.container.find( 'input[type="range"]' ),
7
			textInput    = control.container.find( 'input[type="text"]' ),
8
		    value        = control.setting._value;
9
10
		// Set the initial value in the text input.
11
		textInput.attr( 'value', value );
12
13
		// If the range input value changes copy the value to the text input.
14
		rangeInput.on( 'mousemove change', function() {
15
			textInput.attr( 'value', rangeInput.val() );
16
		} );
17
18
		// Save the value when the range input value changes.
19
		// This is separate from the above because of the postMessage differences.
20
		// If the control refreshes the preview pane,
21
		// we don't want a refresh for every change
22
		// but 1 final refresh when the value is changed.
23
		rangeInput.on( changeAction, function() {
24
			control.setting.set( rangeInput.val() );
25
		} );
26
27
		// If the text input value changes,
28
		// copy the value to the range input
29
		// and then save.
30
		textInput.on( 'input paste change', function() {
31
			rangeInput.attr( 'value', textInput.val() );
32
			control.setting.set( textInput.val() );
33
		} );
34
35
		// If the reset button is clicked,
36
		// set slider and text input values to default
37
		// and hen save.
38
		control.container.find( '.slider-reset' ).on( 'click', function() {
39
			textInput.attr( 'value', control.params['default'] );
40
			rangeInput.attr( 'value', control.params['default'] );
41
			control.setting.set( textInput.val() );
42
		} );
43
	}
44
});
45