Completed
Pull Request — master (#1710)
by Aristeides
04:22 queued 02:20
created

controls/js/src/sortable.js   A

Complexity

Total Complexity 11
Complexity/F 1.38

Size

Lines of Code 66
Function Count 8

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 0
nc 2
dl 0
loc 66
rs 10
c 0
b 0
f 0
wmc 11
mnd 1
bc 11
fnc 8
bpm 1.375
cpm 1.375
noi 3
1
/* global kirkiControlLoader */
2
wp.customize.controlConstructor['kirki-sortable'] = wp.customize.Control.extend({
3
4
	// When we're finished loading continue processing
5
	ready: function() {
6
7
		'use strict';
8
9
		var control = this;
10
11
		// Init the control.
12
		if ( ! _.isUndefined( window.kirkiControlLoader ) && _.isFunction( kirkiControlLoader ) ) {
13
			kirkiControlLoader( control );
14
		} else {
15
			control.initKirkiControl();
16
		}
17
	},
18
19
	initKirkiControl: function() {
20
21
		'use strict';
22
23
		var control = this;
24
25
		control.container.find( '.kirki-controls-loading-spinner' ).hide();
26
27
		// Set the sortable container.
28
		control.sortableContainer = control.container.find( 'ul.sortable' ).first();
29
30
		// Init sortable.
31
		control.sortableContainer.sortable({
32
33
			// Update value when we stop sorting.
34
			stop: function() {
35
				control.updateValue();
36
			}
37
		}).disableSelection().find( 'li' ).each( function() {
38
39
			// Enable/disable options when we click on the eye of Thundera.
40
			jQuery( this ).find( 'i.visibility' ).click( function() {
41
				jQuery( this ).toggleClass( 'dashicons-visibility-faint' ).parents( 'li:eq(0)' ).toggleClass( 'invisible' );
42
			});
43
		}).click( function() {
44
45
			// Update value on click.
46
			control.updateValue();
47
		});
48
	},
49
50
	/**
51
	 * Updates the sorting list
52
	 */
53
	updateValue: function() {
54
55
		'use strict';
56
57
		var control = this,
58
		    newValue = [];
59
60
		this.sortableContainer.find( 'li' ).each( function() {
61
			if ( ! jQuery( this ).is( '.invisible' ) ) {
62
				newValue.push( jQuery( this ).data( 'value' ) );
63
			}
64
		});
65
		control.setting.set( newValue );
66
	}
67
});
68