sortable.js (2016B)
1 wp.customize.controlConstructor['kirki-sortable'] = wp.customize.Control.extend({ 2 3 ready: function() { 4 5 'use strict'; 6 7 var control = this; 8 9 // The hidden field that keeps the data saved 10 this.settingField = this.container.find( '[data-customize-setting-link]' ).first(); 11 12 // The sortable container 13 this.sortableContainer = this.container.find( 'ul.sortable' ).first(); 14 15 // Set the field value for the first time 16 this.setValue( this.setting.get(), false ); 17 18 // Init the sortable container 19 this.sortableContainer.sortable() 20 .disableSelection() 21 .on( 'sortstop', function( event, ui ) { 22 control.sort(); 23 }) 24 .find( 'li' ).each(function() { 25 jQuery( this ).find( 'i.visibility' ).click( function() { 26 jQuery( this ).toggleClass( 'dashicons-visibility-faint' ).parents( 'li:eq(0)' ).toggleClass( 'invisible' ); 27 }); 28 }) 29 .click( function() { 30 control.sort(); 31 }); 32 }, 33 34 /** 35 * Updates the sorting list 36 */ 37 sort: function() { 38 39 'use strict'; 40 41 var newValue = []; 42 this.sortableContainer.find( 'li' ).each( function() { 43 var $this = jQuery( this ); 44 if ( ! $this.is( '.invisible' ) ) { 45 newValue.push( $this.data( 'value' ) ); 46 } 47 }); 48 49 this.setValue( newValue, true ); 50 51 }, 52 53 /** 54 * Get the current value of the setting 55 * 56 * @return Object 57 */ 58 getValue: function() { 59 60 'use strict'; 61 62 // The setting is saved in PHP serialized format 63 return unserialize( this.setting.get() ); 64 65 }, 66 67 /** 68 * Set a new value for the setting 69 * 70 * @param newValue Object 71 * @param refresh If we want to refresh the previewer or not 72 */ 73 setValue: function( newValue, refresh ) { 74 75 'use strict'; 76 77 var newValueSerialized = newValue; serialize( newValue ); 78 79 this.setting.set( newValueSerialized ); 80 81 // Update the hidden field 82 this.settingField.val( newValueSerialized ); 83 84 if ( refresh ) { 85 86 // Trigger the change event on the hidden field so 87 // previewer refresh the website on Customizer 88 this.settingField.trigger( 'change' ); 89 90 } 91 92 } 93 94 });