| Total Complexity | 11 |
| Complexity/F | 1.38 |
| Lines of Code | 66 |
| Function Count | 8 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 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 |
Since ECMAScript 6, you can create block-scoped vars or constants with the keywords
letorconst. 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.