wp.customize.controlConstructor['kirki-code'] = wp.customize.Control.extend({ // When we're finished loading continue processing ready: function() { 'use strict'; var control = this; // Add to the queue. control.kirkiLoader(); }, // Add control to a queue and load when the time is right. kirkiLoader: function( forceLoad ) { var control = this, waitTime = 100, i; if ( _.isUndefined( window.kirkiControlsLoader ) ) { window.kirkiControlsLoader = { queue: [], done: [], busy: false }; } // No need to proceed if this control has already been initialized. if ( -1 !== window.kirkiControlsLoader.done.indexOf( control.id ) ) { return; } // Add this control to the queue if it's not already there. if ( -1 === window.kirkiControlsLoader.queue.indexOf( control.id ) ) { window.kirkiControlsLoader.queue.push( control.id ); } // If we're busy check back again later. if ( true === window.kirkiControlsLoader.busy ) { setTimeout( function() { control.kirkiLoader(); }, waitTime ); return; } // Run if force-loading and not busy. if ( true === forceLoad || false === window.kirkiControlsLoader.busy ) { // Set to busy. window.kirkiControlsLoader.busy = true; // Init the control JS. control.initKirkiControl(); jQuery( control.container.find( '.kirki-controls-loading-spinner' ) ).hide(); // Mark as done and remove from queue. window.kirkiControlsLoader.done.push( control.id ); i = window.kirkiControlsLoader.queue.indexOf( control.id ); window.kirkiControlsLoader.queue.splice( i, 1 ); // Set busy to false after waitTime has passed. setTimeout( function() { window.kirkiControlsLoader.busy = false; }, waitTime ); return; } if ( control.id === window.kirkiControlsLoader.queue[0] ) { control.kirkiLoader( true ); } }, initKirkiControl: function() { 'use strict'; var control = this, element = control.container.find( '.kirki-codemirror-editor' ), language = ( 'html' === control.params.choices.language ) ? { name: 'htmlmixed' } : control.params.choices.language, editor, container, height; control.container.find( '.kirki-controls-loading-spinner' ).hide(); editor = CodeMirror.fromTextArea( element[0], { value: control.setting._value, mode: language, lineNumbers: true, lineWrapping: true, theme: control.params.choices.theme }); if ( ! _.isUndefined( control.params.choices.height ) ) { height = Number( control.params.choices.height ); if ( ! isNaN( height ) ) { container = control.container.find( '.codemirror-kirki-wrapper' ); jQuery( container ).css( 'max-height', function() { return control.params.choices.height; } ); editor.setSize( null, control.params.choices.height ); } } // On change make sure we infor the Customizer API editor.on( 'change', function() { control.setting.set( editor.getValue() ); }); // Hack to refresh the editor when we open a section element.parents( '.accordion-section' ).on( 'click', function() { editor.refresh(); }); } });