+ overlay = $( '.wp-full-overlay' );
+ overlay.addClass( 'customize-loading' );
+
+ onceProcessingComplete = function() {
+ var request;
+ if ( api.state( 'processing' ).get() > 0 ) {
+ return;
+ }
+
+ api.state( 'processing' ).unbind( onceProcessingComplete );
+
+ request = api.requestChangesetUpdate();
+ request.done( function() {
+ $( window ).off( 'beforeunload.customize-confirm' );
+ top.location.href = urlParser.href;
+ deferred.resolve();
+ } );
+ request.fail( function() {
+ overlay.removeClass( 'customize-loading' );
+ deferred.reject();
+ } );
+ };
+
+ if ( 0 === api.state( 'processing' ).get() ) {
+ onceProcessingComplete();
+ } else {
+ api.state( 'processing' ).bind( onceProcessingComplete );
+ }
+
+ return deferred.promise();
+ },
+
+ /**
+ * Render & show the theme details for a given theme model.
+ *
+ * @since 4.2.0
+ *
+ * @param {Object} theme
+ */
+ showDetails: function ( theme, callback ) {
+ var section = this, link;
+ 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();
+
+ link = section.overlay.find( '.inactive-theme > a' );
+
+ link.on( 'click', function( event ) {
+ event.preventDefault();
+
+ // Short-circuit if request is currently being made.
+ if ( link.hasClass( 'disabled' ) ) {
+ return;
+ }
+ link.addClass( 'disabled' );
+
+ section.loadThemePreview( theme.id ).fail( function() {
+ link.removeClass( 'disabled' );
+ } );
+ } );
+ 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,
+ container = $( '#customize-theme-controls' ),
+ parentContainer = $( '.customize-pane-parent' ); // @todo This should be defined elsewhere, and to be configurable
+
+ if ( ! panel.headContainer.parent().is( parentContainer ) ) {
+ parentContainer.append( panel.headContainer );
+ }
+ if ( ! panel.contentContainer.parent().is( panel.headContainer ) ) {
+ container.append( panel.contentContainer );
+ panel.renderContent();
+ }
+
+ panel.deferred.embedded.resolve();
+ },
+
+ /**
+ * @since 4.1.0
+ */
+ attachEvents: function () {
+ var meta, panel = this;
+
+ // Expand/Collapse accordion sections on click.
+ panel.headContainer.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
+
+ 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 panel = this,
+ accordionSection = panel.contentContainer,
+ overlay = accordionSection.closest( '.wp-full-overlay' ),
+ container = accordionSection.closest( '.wp-full-overlay-sidebar-content' ),
+ topPanel = panel.headContainer.find( '.accordion-section-title' ),
+ backBtn = accordionSection.find( '.customize-panel-back' );
+
+ if ( expanded && ! accordionSection.hasClass( 'current-panel' ) ) {
+ // 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 } );
+ }
+ });
+
+ panel._animateChangeExpanded( function() {
+ topPanel.attr( 'tabindex', '-1' );
+ backBtn.attr( 'tabindex', '0' );
+
+ backBtn.focus();
+ accordionSection.css( 'top', '' );
+ container.scrollTop( 0 );
+
+ if ( args.completeCallback ) {
+ args.completeCallback();
+ }
+ } );
+
+ overlay.addClass( 'in-sub-panel' );
+ accordionSection.addClass( 'current-panel' );
+ api.state( 'expandedPanel' ).set( panel );
+
+ } else if ( ! expanded && accordionSection.hasClass( 'current-panel' ) ) {
+ panel._animateChangeExpanded( function() {
+ topPanel.attr( 'tabindex', '0' );
+ backBtn.attr( 'tabindex', '-1' );
+
+ topPanel.focus();
+ accordionSection.css( 'top', '' );
+
+ if ( args.completeCallback ) {
+ args.completeCallback();
+ }
+ } );
+
+ overlay.removeClass( 'in-sub-panel' );
+ accordionSection.removeClass( 'current-panel' );
+ if ( panel === api.state( 'expandedPanel' ).get() ) {
+ api.state( 'expandedPanel' ).set( false );
+ }
+ }
+ },
+
+ /**
+ * 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.headContainer ) {
+ panel.contentContainer.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, '-' );
+ control.templateSelector = 'customize-control-' + control.params.type + '-content';
+ control.container = control.params.content ? $( control.params.content ) : $( control.selector );
+
+ control.deferred = {
+ embedded: new $.Deferred()
+ };
+ control.section = new api.Value();
+ control.priority = new api.Value();
+ control.active = new api.Value();
+ control.activeArgumentsQueue = [];
+ control.notifications = new api.Values({ defaultConstructor: api.Notification });
+
+ control.elements = [];
+
+ nodes = control.container.find('[data-customize-setting-link]');
+ radios = {};
+
+ nodes.each( function() {
+ var node = $( this ),
+ name;
+
+ if ( node.is( ':radio' ) ) {
+ name = node.prop( 'name' );
+ if ( radios[ name ] ) {
+ return;
+ }
+
+ radios[ name ] = true;
+ node = nodes.filter( '[name="' + name + '"]' );
+ }
+
+ api( node.data( 'customizeSettingLink' ), function( setting ) {
+ var element = new api.Element( node );
+ control.elements.push( element );
+ element.sync( setting );
+ element.set( setting() );
+ });
+ });
+
+ control.active.bind( function ( active ) {
+ var args = control.activeArgumentsQueue.shift();
+ args = $.extend( {}, control.defaultActiveArguments, args );
+ control.onChangeActive( active, args );
+ } );
+
+ control.section.set( control.params.section );
+ control.priority.set( isNaN( control.params.priority ) ? 10 : control.params.priority );
+ control.active.set( control.params.active );
+
+ api.utils.bubbleChildValueChanges( control, [ 'section', 'priority', 'active' ] );
+
+ /*
+ * After all settings related to the control are available,
+ * make them available on the control and embed the control into the page.
+ */
+ settings = $.map( control.params.settings, function( value ) {
+ return value;
+ });
+
+ if ( 0 === settings.length ) {
+ control.setting = null;
+ control.settings = {};
+ control.embed();
+ } else {
+ api.apply( api, settings.concat( function() {
+ var key;
+
+ control.settings = {};
+ for ( key in control.params.settings ) {
+ control.settings[ key ] = api( control.params.settings[ key ] );
+ }
+
+ control.setting = control.settings['default'] || null;
+
+ // Add setting notifications to the control notification.
+ _.each( control.settings, function( setting ) {
+ setting.notifications.bind( 'add', function( settingNotification ) {
+ var controlNotification, code, params;
+ code = setting.id + ':' + settingNotification.code;
+ params = _.extend(
+ {},
+ settingNotification,
+ {
+ setting: setting.id
+ }
+ );
+ controlNotification = new api.Notification( code, params );
+ control.notifications.add( controlNotification.code, controlNotification );
+ } );
+ setting.notifications.bind( 'remove', function( settingNotification ) {
+ control.notifications.remove( setting.id + ':' + settingNotification.code );
+ } );
+ } );
+
+ control.embed();
+ }) );
+ }
+
+ // After the control is embedded on the page, invoke the "ready" method.
+ control.deferred.embedded.done( function () {
+ /*
+ * Note that this debounced/deferred rendering is needed for two reasons:
+ * 1) The 'remove' event is triggered just _before_ the notification is actually removed.
+ * 2) Improve performance when adding/removing multiple notifications at a time.
+ */
+ var debouncedRenderNotifications = _.debounce( function renderNotifications() {
+ control.renderNotifications();
+ } );
+ control.notifications.bind( 'add', function( notification ) {
+ wp.a11y.speak( notification.message, 'assertive' );
+ debouncedRenderNotifications();
+ } );
+ control.notifications.bind( 'remove', debouncedRenderNotifications );
+ control.renderNotifications();
+
+ control.ready();
+ });
+ },
+
+ /**
+ * Embed the control into the page.
+ */
+ embed: function () {
+ var control = this,
+ inject;
+
+ // Watch for changes to the section state
+ inject = function ( sectionId ) {
+ var parentContainer;
+ if ( ! sectionId ) { // @todo allow a control to be embedded without a section, for instance a control embedded in the front end.
+ return;
+ }
+ // Wait for the section to be registered
+ api.section( sectionId, function ( section ) {
+ // Wait for the section to be ready/initialized
+ section.deferred.embedded.done( function () {
+ parentContainer = ( section.contentContainer.is( 'ul' ) ) ? section.contentContainer : section.contentContainer.find( 'ul:first' );
+ if ( ! control.container.parent().is( parentContainer ) ) {
+ parentContainer.append( control.container );
+ control.renderContent();
+ }
+ control.deferred.embedded.resolve();
+ });
+ });
+ };
+ control.section.bind( inject );
+ inject( control.section.get() );
+ },
+
+ /**
+ * Triggered when the control's markup has been injected into the DOM.
+ *
+ * @returns {void}
+ */
+ ready: function() {
+ var control = this, newItem;
+ if ( 'dropdown-pages' === control.params.type && control.params.allow_addition ) {
+ newItem = control.container.find( '.new-content-item' );
+ newItem.hide(); // Hide in JS to preserve flex display when showing.
+ control.container.on( 'click', '.add-new-toggle', function( e ) {
+ $( e.currentTarget ).slideUp( 180 );
+ newItem.slideDown( 180 );
+ newItem.find( '.create-item-input' ).focus();
+ });
+ control.container.on( 'click', '.add-content', function() {
+ control.addNewPage();
+ });
+ control.container.on( 'keyup', '.create-item-input', function( e ) {
+ if ( 13 === e.which ) { // Enter
+ control.addNewPage();
+ }
+ });
+ }
+ },
+
+ /**
+ * Get the element inside of a control's container that contains the validation error message.
+ *
+ * Control subclasses may override this to return the proper container to render notifications into.
+ * Injects the notification container for existing controls that lack the necessary container,
+ * including special handling for nav menu items and widgets.
+ *
+ * @since 4.6.0
+ * @returns {jQuery} Setting validation message element.
+ * @this {wp.customize.Control}
+ */
+ getNotificationsContainerElement: function() {
+ var control = this, controlTitle, notificationsContainer;
+
+ notificationsContainer = control.container.find( '.customize-control-notifications-container:first' );
+ if ( notificationsContainer.length ) {
+ return notificationsContainer;
+ }
+
+ notificationsContainer = $( '<div class="customize-control-notifications-container"></div>' );
+
+ if ( control.container.hasClass( 'customize-control-nav_menu_item' ) ) {
+ control.container.find( '.menu-item-settings:first' ).prepend( notificationsContainer );
+ } else if ( control.container.hasClass( 'customize-control-widget_form' ) ) {
+ control.container.find( '.widget-inside:first' ).prepend( notificationsContainer );
+ } else {
+ controlTitle = control.container.find( '.customize-control-title' );
+ if ( controlTitle.length ) {
+ controlTitle.after( notificationsContainer );
+ } else {
+ control.container.prepend( notificationsContainer );
+ }
+ }
+ return notificationsContainer;
+ },
+
+ /**
+ * Render notifications.
+ *
+ * Renders the `control.notifications` into the control's container.
+ * Control subclasses may override this method to do their own handling
+ * of rendering notifications.
+ *
+ * @since 4.6.0
+ * @this {wp.customize.Control}
+ */
+ renderNotifications: function() {
+ var control = this, container, notifications, hasError = false;
+ container = control.getNotificationsContainerElement();
+ if ( ! container || ! container.length ) {
+ return;
+ }
+ notifications = [];
+ control.notifications.each( function( notification ) {
+ notifications.push( notification );
+ if ( 'error' === notification.type ) {
+ hasError = true;
+ }
+ } );
+
+ if ( 0 === notifications.length ) {
+ container.stop().slideUp( 'fast' );
+ } else {
+ container.stop().slideDown( 'fast', null, function() {
+ $( this ).css( 'height', 'auto' );
+ } );
+ }
+
+ if ( ! control.notificationsTemplate ) {
+ control.notificationsTemplate = wp.template( 'customize-control-notifications' );
+ }
+
+ control.container.toggleClass( 'has-notifications', 0 !== notifications.length );
+ control.container.toggleClass( 'has-error', hasError );
+ container.empty().append( $.trim(
+ control.notificationsTemplate( { notifications: notifications, altNotice: Boolean( control.altNotice ) } )
+ ) );
+ },
+
+ /**
+ * Normal controls do not expand, so just expand its parent
+ *
+ * @param {Object} [params]
+ */
+ expand: function ( params ) {
+ api.section( this.section() ).expand( params );
+ },
+
+ /**
+ * Bring the containing section and panel into view and then
+ * this control into view, focusing on the first input.
+ */
+ focus: focus,
+
+ /**
+ * Update UI in response to a change in the control's active state.
+ * This does not change the active state, it merely handles the behavior
+ * for when it does change.
+ *
+ * @since 4.1.0
+ *
+ * @param {Boolean} active
+ * @param {Object} args
+ * @param {Number} args.duration
+ * @param {Callback} args.completeCallback
+ */
+ onChangeActive: function ( active, args ) {
+ if ( args.unchanged ) {
+ if ( args.completeCallback ) {
+ args.completeCallback();
+ }
+ return;
+ }
+
+ if ( ! $.contains( document, this.container[0] ) ) {
+ // jQuery.fn.slideUp is not hiding an element if it is not in the DOM
+ this.container.toggle( active );
+ if ( args.completeCallback ) {
+ args.completeCallback();
+ }
+ } else if ( active ) {
+ this.container.slideDown( args.duration, args.completeCallback );
+ } else {
+ this.container.slideUp( args.duration, args.completeCallback );
+ }
+ },
+
+ /**
+ * @deprecated 4.1.0 Use this.onChangeActive() instead.
+ */
+ toggle: function ( active ) {
+ return this.onChangeActive( active, this.defaultActiveArguments );
+ },
+
+ /**
+ * Shorthand way to enable the active state.
+ *
+ * @since 4.1.0
+ *
+ * @param {Object} [params]
+ * @returns {Boolean} false if already active
+ */
+ activate: Container.prototype.activate,
+
+ /**