+ // Fill queue initially.
+ if ( section.screenshotQueue === null ) {
+ section.screenshotQueue = section.controls();
+ }
+
+ // Are all screenshots rendered?
+ if ( ! section.screenshotQueue.length ) {
+ return;
+ }
+
+ section.screenshotQueue = _.filter( section.screenshotQueue, function( control ) {
+ var $imageWrapper = control.container.find( '.theme-screenshot' ),
+ $image = $imageWrapper.find( 'img' );
+
+ if ( ! $image.length ) {
+ return false;
+ }
+
+ if ( $image.is( ':hidden' ) ) {
+ return true;
+ }
+
+ // Based on unveil.js.
+ var wt = section.$window.scrollTop(),
+ wb = wt + section.$window.height(),
+ et = $image.offset().top,
+ ih = $imageWrapper.height(),
+ eb = et + ih,
+ threshold = ih * 3,
+ inView = eb >= wt - threshold && et <= wb + threshold;
+
+ if ( inView ) {
+ control.container.trigger( 'render-screenshot' );
+ }
+
+ // If the image is in view return false so it's cleared from the queue.
+ return ! inView;
+ } );
+ },
+
+ /**
+ * Advance the modal to the next theme.
+ *
+ * @since 4.2.0
+ */
+ nextTheme: function () {
+ var section = this;
+ if ( section.getNextTheme() ) {
+ section.showDetails( section.getNextTheme(), function() {
+ section.overlay.find( '.right' ).focus();
+ } );
+ }
+ },
+
+ /**
+ * Get the next theme model.
+ *
+ * @since 4.2.0
+ */
+ getNextTheme: function () {
+ var control, next;
+ control = api.control( 'theme_' + this.currentTheme );
+ next = control.container.next( 'li.customize-control-theme' );
+ if ( ! next.length ) {
+ return false;
+ }
+ next = next[0].id.replace( 'customize-control-', '' );
+ control = api.control( next );
+
+ return control.params.theme;
+ },
+
+ /**
+ * Advance the modal to the previous theme.
+ *
+ * @since 4.2.0
+ */
+ previousTheme: function () {
+ var section = this;
+ if ( section.getPreviousTheme() ) {
+ section.showDetails( section.getPreviousTheme(), function() {
+ section.overlay.find( '.left' ).focus();
+ } );
+ }
+ },
+
+ /**
+ * Get the previous theme model.
+ *
+ * @since 4.2.0
+ */
+ getPreviousTheme: function () {
+ var control, previous;
+ control = api.control( 'theme_' + this.currentTheme );
+ previous = control.container.prev( 'li.customize-control-theme' );
+ if ( ! previous.length ) {
+ return false;
+ }
+ previous = previous[0].id.replace( 'customize-control-', '' );
+ control = api.control( previous );
+
+ return control.params.theme;
+ },
+
+ /**
+ * Disable buttons when we're viewing the first or last theme.
+ *
+ * @since 4.2.0
+ */
+ updateLimits: function () {
+ if ( ! this.getNextTheme() ) {
+ this.overlay.find( '.right' ).addClass( 'disabled' );
+ }
+ if ( ! this.getPreviousTheme() ) {
+ this.overlay.find( '.left' ).addClass( 'disabled' );
+ }
+ },
+
+ /**
+ * Render & show the theme details for a given theme model.
+ *
+ * @since 4.2.0
+ *
+ * @param {Object} theme
+ */
+ showDetails: function ( theme, callback ) {
+ var section = this;
+ callback = callback || function(){};
+ section.currentTheme = theme.id;
+ section.overlay.html( section.template( theme ) )
+ .fadeIn( 'fast' )
+ .focus();
+ $( 'body' ).addClass( 'modal-open' );
+ section.containFocus( section.overlay );
+ section.updateLimits();
+ callback();
+ },
+
+ /**
+ * Close the theme details modal.
+ *
+ * @since 4.2.0
+ */
+ closeDetails: function () {
+ $( 'body' ).removeClass( 'modal-open' );
+ this.overlay.fadeOut( 'fast' );
+ api.control( 'theme_' + this.currentTheme ).focus();
+ },
+
+ /**
+ * Keep tab focus within the theme details modal.
+ *
+ * @since 4.2.0
+ */
+ containFocus: function( el ) {
+ var tabbables;
+
+ el.on( 'keydown', function( event ) {
+
+ // Return if it's not the tab key
+ // When navigating with prev/next focus is already handled
+ if ( 9 !== event.keyCode ) {
+ return;
+ }
+
+ // uses jQuery UI to get the tabbable elements
+ tabbables = $( ':tabbable', el );
+
+ // Keep focus within the overlay
+ if ( tabbables.last()[0] === event.target && ! event.shiftKey ) {
+ tabbables.first().focus();
+ return false;
+ } else if ( tabbables.first()[0] === event.target && event.shiftKey ) {
+ tabbables.last().focus();
+ return false;
+ }
+ });
+ }
+ });
+
+ /**
+ * @since 4.1.0
+ *
+ * @class
+ * @augments wp.customize.Class
+ */
+ api.Panel = Container.extend({
+ containerType: 'panel',
+
+ /**
+ * @since 4.1.0
+ *
+ * @param {string} id - The ID for the panel.
+ * @param {object} options - Object containing one property: params.
+ * @param {object} options.params - Object containing the following properties.
+ * @param {string} options.params.title - Title shown when panel is collapsed and expanded.
+ * @param {string=} [options.params.description] - Description shown at the top of the panel.
+ * @param {number=100} [options.params.priority] - The sort priority for the panel.
+ * @param {string=default} [options.params.type] - The type of the panel. See wp.customize.panelConstructor.
+ * @param {string=} [options.params.content] - The markup to be used for the panel container. If empty, a JS template is used.
+ * @param {boolean=true} [options.params.active] - Whether the panel is active or not.
+ */
+ initialize: function ( id, options ) {
+ var panel = this;
+ Container.prototype.initialize.call( panel, id, options );
+ panel.embed();
+ panel.deferred.embedded.done( function () {
+ panel.ready();
+ });
+ },
+
+ /**
+ * Embed the container in the DOM when any parent panel is ready.
+ *
+ * @since 4.1.0
+ */
+ embed: function () {
+ var panel = this,
+ parentContainer = $( '#customize-theme-controls > ul' ); // @todo This should be defined elsewhere, and to be configurable
+
+ if ( ! panel.container.parent().is( parentContainer ) ) {
+ parentContainer.append( panel.container );
+ panel.renderContent();
+ }
+
+ api.bind( 'pane-contents-reflowed', _.debounce( function() {
+ panel._recalculateTopMargin();
+ }, 100 ) );
+
+ panel.deferred.embedded.resolve();
+ },
+
+ /**
+ * @since 4.1.0
+ */
+ attachEvents: function () {
+ var meta, panel = this;
+
+ // Expand/Collapse accordion sections on click.
+ panel.container.find( '.accordion-section-title' ).on( 'click keydown', function( event ) {
+ if ( api.utils.isKeydownButNotEnterEvent( event ) ) {
+ return;
+ }
+ event.preventDefault(); // Keep this AFTER the key filter above
+
+ if ( ! panel.expanded() ) {
+ panel.expand();
+ }
+ });
+
+ // Close panel.
+ panel.container.find( '.customize-panel-back' ).on( 'click keydown', function( event ) {
+ if ( api.utils.isKeydownButNotEnterEvent( event ) ) {
+ return;
+ }
+ event.preventDefault(); // Keep this AFTER the key filter above
+
+ if ( panel.expanded() ) {
+ panel.collapse();
+ }
+ });
+
+ meta = panel.container.find( '.panel-meta:first' );
+
+ meta.find( '> .accordion-section-title .customize-help-toggle' ).on( 'click keydown', function( event ) {
+ if ( api.utils.isKeydownButNotEnterEvent( event ) ) {
+ return;
+ }
+ event.preventDefault(); // Keep this AFTER the key filter above
+
+ meta = panel.container.find( '.panel-meta' );
+ if ( meta.hasClass( 'cannot-expand' ) ) {
+ return;
+ }
+
+ var content = meta.find( '.customize-panel-description:first' );
+ if ( meta.hasClass( 'open' ) ) {
+ meta.toggleClass( 'open' );
+ content.slideUp( panel.defaultExpandedArguments.duration );
+ $( this ).attr( 'aria-expanded', false );
+ } else {
+ content.slideDown( panel.defaultExpandedArguments.duration );
+ meta.toggleClass( 'open' );
+ $( this ).attr( 'aria-expanded', true );
+ }
+ });
+
+ },
+
+ /**
+ * Get the sections that are associated with this panel, sorted by their priority Value.
+ *
+ * @since 4.1.0
+ *
+ * @returns {Array}
+ */
+ sections: function () {
+ return this._children( 'panel', 'section' );
+ },
+
+ /**
+ * Return whether this panel has any active sections.
+ *
+ * @since 4.1.0
+ *
+ * @returns {boolean}
+ */
+ isContextuallyActive: function () {
+ var panel = this,
+ sections = panel.sections(),
+ activeCount = 0;
+ _( sections ).each( function ( section ) {
+ if ( section.active() && section.isContextuallyActive() ) {
+ activeCount += 1;
+ }
+ } );
+ return ( activeCount !== 0 );
+ },
+
+ /**
+ * Update UI to reflect expanded state
+ *
+ * @since 4.1.0
+ *
+ * @param {Boolean} expanded
+ * @param {Object} args
+ * @param {Boolean} args.unchanged
+ * @param {Function} args.completeCallback
+ */
+ onChangeExpanded: function ( expanded, args ) {
+
+ // Immediately call the complete callback if there were no changes
+ if ( args.unchanged ) {
+ if ( args.completeCallback ) {
+ args.completeCallback();
+ }
+ return;
+ }
+
+ // Note: there is a second argument 'args' passed
+ var position, scroll,
+ panel = this,
+ accordionSection = panel.container.closest( '.accordion-section' ),
+ overlay = accordionSection.closest( '.wp-full-overlay' ),
+ container = accordionSection.closest( '.wp-full-overlay-sidebar-content' ),
+ siblings = container.find( '.open' ),
+ topPanel = overlay.find( '#customize-theme-controls > ul > .accordion-section > .accordion-section-title' ),
+ backBtn = accordionSection.find( '.customize-panel-back' ),
+ panelTitle = accordionSection.find( '.accordion-section-title' ).first(),
+ content = accordionSection.find( '.control-panel-content' ),
+ headerActionsHeight = $( '#customize-header-actions' ).height();
+
+ if ( expanded ) {
+
+ // Collapse any sibling sections/panels
+ api.section.each( function ( section ) {
+ if ( panel.id !== section.panel() ) {
+ section.collapse( { duration: 0 } );
+ }
+ });
+ api.panel.each( function ( otherPanel ) {
+ if ( panel !== otherPanel ) {
+ otherPanel.collapse( { duration: 0 } );
+ }
+ });
+
+ content.show( 0, function() {
+ content.parent().show();
+ position = content.offset().top;
+ scroll = container.scrollTop();
+ content.css( 'margin-top', ( headerActionsHeight - position - scroll ) );
+ accordionSection.addClass( 'current-panel' );
+ overlay.addClass( 'in-sub-panel' );
+ container.scrollTop( 0 );
+ if ( args.completeCallback ) {
+ args.completeCallback();
+ }
+ } );
+ topPanel.attr( 'tabindex', '-1' );
+ backBtn.attr( 'tabindex', '0' );
+ backBtn.focus();
+ panel._recalculateTopMargin();
+ } else {
+ siblings.removeClass( 'open' );
+ accordionSection.removeClass( 'current-panel' );
+ overlay.removeClass( 'in-sub-panel' );
+ content.delay( 180 ).hide( 0, function() {
+ content.css( 'margin-top', 'inherit' ); // Reset
+ if ( args.completeCallback ) {
+ args.completeCallback();
+ }
+ } );
+ topPanel.attr( 'tabindex', '0' );
+ backBtn.attr( 'tabindex', '-1' );
+ panelTitle.focus();
+ container.scrollTop( 0 );
+ }
+ },
+
+ /**
+ * Recalculate the top margin.
+ *
+ * @since 4.4.0
+ * @private
+ */
+ _recalculateTopMargin: function() {
+ var panel = this, headerActionsHeight, content, accordionSection;
+ headerActionsHeight = $( '#customize-header-actions' ).height();
+ accordionSection = panel.container.closest( '.accordion-section' );
+ content = accordionSection.find( '.control-panel-content' );
+ content.css( 'margin-top', ( parseInt( content.css( 'margin-top' ), 10 ) - ( content.offset().top - headerActionsHeight ) ) );
+ },
+
+ /**
+ * Render the panel from its JS template, if it exists.
+ *
+ * The panel's container must already exist in the DOM.
+ *
+ * @since 4.3.0
+ */
+ renderContent: function () {
+ var template,
+ panel = this;
+
+ // Add the content to the container.
+ if ( 0 !== $( '#tmpl-' + panel.templateSelector + '-content' ).length ) {
+ template = wp.template( panel.templateSelector + '-content' );
+ } else {
+ template = wp.template( 'customize-panel-default-content' );
+ }
+ if ( template && panel.container ) {
+ panel.container.find( '.accordion-sub-container' ).html( template( panel.params ) );
+ }
+ }
+ });
+
+ /**
+ * A Customizer Control.
+ *
+ * A control provides a UI element that allows a user to modify a Customizer Setting.
+ *
+ * @see PHP class WP_Customize_Control.
+ *
+ * @class
+ * @augments wp.customize.Class
+ *
+ * @param {string} id Unique identifier for the control instance.
+ * @param {object} options Options hash for the control instance.
+ * @param {object} options.params
+ * @param {object} options.params.type Type of control (e.g. text, radio, dropdown-pages, etc.)
+ * @param {string} options.params.content The HTML content for the control.
+ * @param {string} options.params.priority Order of priority to show the control within the section.
+ * @param {string} options.params.active
+ * @param {string} options.params.section The ID of the section the control belongs to.
+ * @param {string} options.params.settings.default The ID of the setting the control relates to.
+ * @param {string} options.params.settings.data
+ * @param {string} options.params.label
+ * @param {string} options.params.description
+ * @param {string} options.params.instanceNumber Order in which this instance was created in relation to other instances.
+ */
+ api.Control = api.Class.extend({
+ defaultActiveArguments: { duration: 'fast', completeCallback: $.noop },
+
+ initialize: function( id, options ) {
+ var control = this,
+ nodes, radios, settings;
+
+ control.params = {};
+ $.extend( control, options || {} );
+ control.id = id;
+ control.selector = '#customize-control-' + id.replace( /\]/g, '' ).replace( /\[/g, '-' );