+ },
+
+ /**
+ * Find controls associated with this setting.
+ *
+ * @since 4.6.0
+ * @returns {wp.customize.Control[]} Controls associated with setting.
+ */
+ findControls: function() {
+ var setting = this, controls = [];
+ api.control.each( function( control ) {
+ _.each( control.settings, function( controlSetting ) {
+ if ( controlSetting.id === setting.id ) {
+ controls.push( control );
+ }
+ } );
+ } );
+ return controls;
+ }
+ });
+
+ /**
+ * Utility function namespace
+ */
+ api.utils = {};
+
+ /**
+ * Watch all changes to Value properties, and bubble changes to parent Values instance
+ *
+ * @since 4.1.0
+ *
+ * @param {wp.customize.Class} instance
+ * @param {Array} properties The names of the Value instances to watch.
+ */
+ api.utils.bubbleChildValueChanges = function ( instance, properties ) {
+ $.each( properties, function ( i, key ) {
+ instance[ key ].bind( function ( to, from ) {
+ if ( instance.parent && to !== from ) {
+ instance.parent.trigger( 'change', instance );
+ }
+ } );
+ } );
+ };
+
+ /**
+ * Expand a panel, section, or control and focus on the first focusable element.
+ *
+ * @since 4.1.0
+ *
+ * @param {Object} [params]
+ * @param {Function} [params.completeCallback]
+ */
+ focus = function ( params ) {
+ var construct, completeCallback, focus, focusElement;
+ construct = this;
+ params = params || {};
+ focus = function () {
+ var focusContainer;
+ if ( construct.extended( api.Panel ) && construct.expanded && construct.expanded() ) {
+ focusContainer = construct.container.find( 'ul.control-panel-content' );
+ } else if ( construct.extended( api.Section ) && construct.expanded && construct.expanded() ) {
+ focusContainer = construct.container.find( 'ul.accordion-section-content' );
+ } else {
+ focusContainer = construct.container;
+ }
+
+ focusElement = focusContainer.find( '.control-focus:first' );
+ if ( 0 === focusElement.length ) {
+ // Note that we can't use :focusable due to a jQuery UI issue. See: https://github.com/jquery/jquery-ui/pull/1583
+ focusElement = focusContainer.find( 'input, select, textarea, button, object, a[href], [tabindex]' ).filter( ':visible' ).first();
+ }
+ focusElement.focus();
+ };
+ if ( params.completeCallback ) {
+ completeCallback = params.completeCallback;
+ params.completeCallback = function () {
+ focus();
+ completeCallback();
+ };
+ } else {
+ params.completeCallback = focus;
+ }
+ if ( construct.expand ) {
+ construct.expand( params );
+ } else {
+ params.completeCallback();
+ }
+ };
+
+ /**
+ * Stable sort for Panels, Sections, and Controls.
+ *
+ * If a.priority() === b.priority(), then sort by their respective params.instanceNumber.
+ *
+ * @since 4.1.0
+ *
+ * @param {(wp.customize.Panel|wp.customize.Section|wp.customize.Control)} a
+ * @param {(wp.customize.Panel|wp.customize.Section|wp.customize.Control)} b
+ * @returns {Number}
+ */
+ api.utils.prioritySort = function ( a, b ) {
+ if ( a.priority() === b.priority() && typeof a.params.instanceNumber === 'number' && typeof b.params.instanceNumber === 'number' ) {
+ return a.params.instanceNumber - b.params.instanceNumber;
+ } else {
+ return a.priority() - b.priority();
+ }
+ };
+
+ /**
+ * Return whether the supplied Event object is for a keydown event but not the Enter key.
+ *
+ * @since 4.1.0
+ *
+ * @param {jQuery.Event} event
+ * @returns {boolean}
+ */
+ api.utils.isKeydownButNotEnterEvent = function ( event ) {
+ return ( 'keydown' === event.type && 13 !== event.which );
+ };
+
+ /**
+ * Return whether the two lists of elements are the same and are in the same order.
+ *
+ * @since 4.1.0
+ *
+ * @param {Array|jQuery} listA
+ * @param {Array|jQuery} listB
+ * @returns {boolean}
+ */
+ api.utils.areElementListsEqual = function ( listA, listB ) {
+ var equal = (
+ listA.length === listB.length && // if lists are different lengths, then naturally they are not equal
+ -1 === _.indexOf( _.map( // are there any false values in the list returned by map?
+ _.zip( listA, listB ), // pair up each element between the two lists
+ function ( pair ) {
+ return $( pair[0] ).is( pair[1] ); // compare to see if each pair are equal
+ }
+ ), false ) // check for presence of false in map's return value
+ );
+ return equal;
+ };
+
+ /**
+ * Base class for Panel and Section.
+ *
+ * @since 4.1.0
+ *
+ * @class
+ * @augments wp.customize.Class
+ */
+ Container = api.Class.extend({
+ defaultActiveArguments: { duration: 'fast', completeCallback: $.noop },
+ defaultExpandedArguments: { duration: 'fast', completeCallback: $.noop },
+ containerType: 'container',
+ defaults: {
+ title: '',
+ description: '',
+ priority: 100,
+ type: 'default',
+ content: null,
+ active: true,
+ instanceNumber: null
+ },
+
+ /**
+ * @since 4.1.0
+ *
+ * @param {string} id - The ID for the container.
+ * @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 container = this;
+ container.id = id;
+ options = options || {};
+
+ options.params = _.defaults(
+ options.params || {},
+ container.defaults
+ );
+
+ $.extend( container, options );
+ container.templateSelector = 'customize-' + container.containerType + '-' + container.params.type;
+ container.container = $( container.params.content );
+ if ( 0 === container.container.length ) {
+ container.container = $( container.getContainer() );
+ }
+
+ container.deferred = {
+ embedded: new $.Deferred()
+ };
+ container.priority = new api.Value();
+ container.active = new api.Value();
+ container.activeArgumentsQueue = [];
+ container.expanded = new api.Value();
+ container.expandedArgumentsQueue = [];
+
+ container.active.bind( function ( active ) {
+ var args = container.activeArgumentsQueue.shift();
+ args = $.extend( {}, container.defaultActiveArguments, args );
+ active = ( active && container.isContextuallyActive() );
+ container.onChangeActive( active, args );
+ });
+ container.expanded.bind( function ( expanded ) {
+ var args = container.expandedArgumentsQueue.shift();
+ args = $.extend( {}, container.defaultExpandedArguments, args );
+ container.onChangeExpanded( expanded, args );
+ });
+
+ container.deferred.embedded.done( function () {
+ container.attachEvents();
+ });
+
+ api.utils.bubbleChildValueChanges( container, [ 'priority', 'active' ] );
+
+ container.priority.set( container.params.priority );
+ container.active.set( container.params.active );
+ container.expanded.set( false );
+ },
+
+ /**
+ * @since 4.1.0
+ *
+ * @abstract
+ */
+ ready: function() {},
+
+ /**
+ * Get the child models associated with this parent, sorting them by their priority Value.
+ *
+ * @since 4.1.0
+ *
+ * @param {String} parentType
+ * @param {String} childType
+ * @returns {Array}
+ */
+ _children: function ( parentType, childType ) {
+ var parent = this,
+ children = [];
+ api[ childType ].each( function ( child ) {
+ if ( child[ parentType ].get() === parent.id ) {
+ children.push( child );
+ }
+ } );
+ children.sort( api.utils.prioritySort );
+ return children;
+ },
+
+ /**
+ * To override by subclass, to return whether the container has active children.
+ *
+ * @since 4.1.0
+ *
+ * @abstract
+ */
+ isContextuallyActive: function () {
+ throw new Error( 'Container.isContextuallyActive() must be overridden in a subclass.' );
+ },
+
+ /**
+ * Active state change handler.
+ *
+ * Shows the container if it is active, hides it if not.
+ *
+ * To override by subclass, update the container's UI to reflect the provided active state.
+ *
+ * @since 4.1.0
+ *
+ * @param {Boolean} active
+ * @param {Object} args
+ * @param {Object} args.duration
+ * @param {Object} args.completeCallback
+ */
+ onChangeActive: function( active, args ) {
+ var duration, construct = this, expandedOtherPanel;
+ if ( args.unchanged ) {
+ if ( args.completeCallback ) {
+ args.completeCallback();
+ }
+ return;
+ }
+
+ duration = ( 'resolved' === api.previewer.deferred.active.state() ? args.duration : 0 );
+
+ if ( construct.extended( api.Panel ) ) {
+ // If this is a panel is not currently expanded but another panel is expanded, do not animate.
+ api.panel.each(function ( panel ) {
+ if ( panel !== construct && panel.expanded() ) {
+ expandedOtherPanel = panel;
+ duration = 0;
+ }
+ });
+
+ // Collapse any expanded sections inside of this panel first before deactivating.
+ if ( ! active ) {
+ _.each( construct.sections(), function( section ) {
+ section.collapse( { duration: 0 } );
+ } );
+ }
+ }
+
+ if ( ! $.contains( document, construct.container[0] ) ) {
+ // jQuery.fn.slideUp is not hiding an element if it is not in the DOM
+ construct.container.toggle( active );
+ if ( args.completeCallback ) {
+ args.completeCallback();
+ }
+ } else if ( active ) {
+ construct.container.stop( true, true ).slideDown( duration, args.completeCallback );
+ } else {
+ if ( construct.expanded() ) {
+ construct.collapse({
+ duration: duration,
+ completeCallback: function() {
+ construct.container.stop( true, true ).slideUp( duration, args.completeCallback );
+ }
+ });
+ } else {
+ construct.container.stop( true, true ).slideUp( duration, args.completeCallback );
+ }
+ }
+
+ // Recalculate the margin-top immediately, not waiting for debounced reflow, to prevent momentary (100ms) vertical jiggle.
+ if ( expandedOtherPanel ) {
+ expandedOtherPanel._recalculateTopMargin();
+ }
+ },
+
+ /**
+ * @since 4.1.0
+ *
+ * @params {Boolean} active
+ * @param {Object} [params]
+ * @returns {Boolean} false if state already applied
+ */
+ _toggleActive: function ( active, params ) {
+ var self = this;
+ params = params || {};
+ if ( ( active && this.active.get() ) || ( ! active && ! this.active.get() ) ) {
+ params.unchanged = true;
+ self.onChangeActive( self.active.get(), params );
+ return false;
+ } else {
+ params.unchanged = false;
+ this.activeArgumentsQueue.push( params );
+ this.active.set( active );
+ return true;
+ }
+ },
+
+ /**
+ * @param {Object} [params]
+ * @returns {Boolean} false if already active
+ */
+ activate: function ( params ) {
+ return this._toggleActive( true, params );
+ },
+
+ /**
+ * @param {Object} [params]
+ * @returns {Boolean} false if already inactive
+ */
+ deactivate: function ( params ) {
+ return this._toggleActive( false, params );
+ },
+
+ /**
+ * To override by subclass, update the container's UI to reflect the provided active state.
+ * @abstract
+ */
+ onChangeExpanded: function () {
+ throw new Error( 'Must override with subclass.' );
+ },
+
+ /**
+ * Handle the toggle logic for expand/collapse.
+ *
+ * @param {Boolean} expanded - The new state to apply.
+ * @param {Object} [params] - Object containing options for expand/collapse.
+ * @param {Function} [params.completeCallback] - Function to call when expansion/collapse is complete.
+ * @returns {Boolean} false if state already applied or active state is false
+ */
+ _toggleExpanded: function( expanded, params ) {
+ var instance = this, previousCompleteCallback;
+ params = params || {};
+ previousCompleteCallback = params.completeCallback;
+
+ // Short-circuit expand() if the instance is not active.
+ if ( expanded && ! instance.active() ) {
+ return false;
+ }
+
+ params.completeCallback = function() {
+ if ( previousCompleteCallback ) {
+ previousCompleteCallback.apply( instance, arguments );
+ }
+ if ( expanded ) {
+ instance.container.trigger( 'expanded' );
+ } else {
+ instance.container.trigger( 'collapsed' );
+ }
+ };
+ if ( ( expanded && instance.expanded.get() ) || ( ! expanded && ! instance.expanded.get() ) ) {
+ params.unchanged = true;
+ instance.onChangeExpanded( instance.expanded.get(), params );
+ return false;
+ } else {
+ params.unchanged = false;
+ instance.expandedArgumentsQueue.push( params );
+ instance.expanded.set( expanded );
+ return true;
+ }
+ },
+
+ /**
+ * @param {Object} [params]
+ * @returns {Boolean} false if already expanded or if inactive.
+ */
+ expand: function ( params ) {
+ return this._toggleExpanded( true, params );
+ },
+
+ /**
+ * @param {Object} [params]
+ * @returns {Boolean} false if already collapsed.
+ */
+ collapse: function ( params ) {
+ return this._toggleExpanded( false, params );
+ },
+
+ /**
+ * Bring the container into view and then expand this and bring it into view
+ * @param {Object} [params]
+ */
+ focus: focus,
+
+ /**
+ * Return the container html, generated from its JS template, if it exists.
+ *
+ * @since 4.3.0
+ */
+ getContainer: function () {
+ var template,
+ container = this;
+
+ if ( 0 !== $( '#tmpl-' + container.templateSelector ).length ) {
+ template = wp.template( container.templateSelector );
+ } else {
+ template = wp.template( 'customize-' + container.containerType + '-default' );
+ }
+ if ( template && container.container ) {
+ return $.trim( template( container.params ) );
+ }
+
+ return '<li></li>';
+ }
+ });
+
+ /**
+ * @since 4.1.0
+ *
+ * @class
+ * @augments wp.customize.Class
+ */
+ api.Section = Container.extend({
+ containerType: 'section',
+ defaults: {
+ title: '',
+ description: '',
+ priority: 100,
+ type: 'default',
+ content: null,
+ active: true,
+ instanceNumber: null,
+ panel: null,
+ customizeAction: ''
+ },
+
+ /**
+ * @since 4.1.0
+ *
+ * @param {string} id - The ID for the section.
+ * @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 section is collapsed and expanded.
+ * @param {string=} [options.params.description] - Description shown at the top of the section.
+ * @param {number=100} [options.params.priority] - The sort priority for the section.
+ * @param {string=default} [options.params.type] - The type of the section. See wp.customize.sectionConstructor.
+ * @param {string=} [options.params.content] - The markup to be used for the section container. If empty, a JS template is used.
+ * @param {boolean=true} [options.params.active] - Whether the section is active or not.
+ * @param {string} options.params.panel - The ID for the panel this section is associated with.
+ * @param {string=} [options.params.customizeAction] - Additional context information shown before the section title when expanded.
+ */
+ initialize: function ( id, options ) {
+ var section = this;
+ Container.prototype.initialize.call( section, id, options );
+
+ section.id = id;
+ section.panel = new api.Value();
+ section.panel.bind( function ( id ) {
+ $( section.container ).toggleClass( 'control-subsection', !! id );
+ });
+ section.panel.set( section.params.panel || '' );
+ api.utils.bubbleChildValueChanges( section, [ 'panel' ] );
+
+ section.embed();
+ section.deferred.embedded.done( function () {
+ section.ready();
+ });
+ },
+
+ /**
+ * Embed the container in the DOM when any parent panel is ready.
+ *
+ * @since 4.1.0
+ */
+ embed: function () {
+ var section = this, inject;
+
+ // Watch for changes to the panel state
+ inject = function ( panelId ) {
+ var parentContainer;
+ if ( panelId ) {
+ // The panel has been supplied, so wait until the panel object is registered
+ api.panel( panelId, function ( panel ) {
+ // The panel has been registered, wait for it to become ready/initialized
+ panel.deferred.embedded.done( function () {
+ parentContainer = panel.container.find( 'ul:first' );
+ if ( ! section.container.parent().is( parentContainer ) ) {
+ parentContainer.append( section.container );
+ }
+ section.deferred.embedded.resolve();
+ });
+ } );
+ } else {
+ // There is no panel, so embed the section in the root of the customizer
+ parentContainer = $( '#customize-theme-controls' ).children( 'ul' ); // @todo This should be defined elsewhere, and to be configurable
+ if ( ! section.container.parent().is( parentContainer ) ) {
+ parentContainer.append( section.container );
+ }
+ section.deferred.embedded.resolve();
+ }
+ };
+ section.panel.bind( inject );
+ inject( section.panel.get() ); // Since a section may never get a panel, assume that it won't ever get one
+
+ section.deferred.embedded.done(function() {
+ // Fix the top margin after reflow.
+ api.bind( 'pane-contents-reflowed', _.debounce( function() {
+ section._recalculateTopMargin();
+ }, 100 ) );
+ });
+ },
+
+ /**
+ * Add behaviors for the accordion section.
+ *
+ * @since 4.1.0
+ */
+ attachEvents: function () {
+ var section = this;
+
+ // Expand/Collapse accordion sections on click.
+ section.container.find( '.accordion-section-title, .customize-section-back' ).on( 'click keydown', function( event ) {
+ if ( api.utils.isKeydownButNotEnterEvent( event ) ) {
+ return;
+ }
+ event.preventDefault(); // Keep this AFTER the key filter above
+
+ if ( section.expanded() ) {
+ section.collapse();
+ } else {
+ section.expand();
+ }
+ });
+ },
+
+ /**
+ * Return whether this section has any active controls.
+ *
+ * @since 4.1.0
+ *
+ * @returns {Boolean}
+ */
+ isContextuallyActive: function () {
+ var section = this,
+ controls = section.controls(),
+ activeCount = 0;
+ _( controls ).each( function ( control ) {
+ if ( control.active() ) {
+ activeCount += 1;
+ }
+ } );
+ return ( activeCount !== 0 );
+ },
+
+ /**
+ * Get the controls that are associated with this section, sorted by their priority Value.
+ *
+ * @since 4.1.0
+ *
+ * @returns {Array}
+ */
+ controls: function () {
+ return this._children( 'section', 'control' );
+ },
+
+ /**
+ * Update UI to reflect expanded state.
+ *
+ * @since 4.1.0
+ *
+ * @param {Boolean} expanded
+ * @param {Object} args
+ */
+ onChangeExpanded: function ( expanded, args ) {
+ var section = this,
+ container = section.container.closest( '.wp-full-overlay-sidebar-content' ),
+ content = section.container.find( '.accordion-section-content' ),
+ overlay = section.container.closest( '.wp-full-overlay' ),
+ backBtn = section.container.find( '.customize-section-back' ),
+ sectionTitle = section.container.find( '.accordion-section-title' ).first(),
+ headerActionsHeight = $( '#customize-header-actions' ).height(),
+ resizeContentHeight, expand, position, scroll;
+
+ if ( expanded && ! section.container.hasClass( 'open' ) ) {
+
+ if ( args.unchanged ) {
+ expand = args.completeCallback;
+ } else {
+ container.scrollTop( 0 );
+ resizeContentHeight = function() {
+ var matchMedia, offset;
+ matchMedia = window.matchMedia || window.msMatchMedia;
+ offset = 90; // 45px for customize header actions + 45px for footer actions.
+
+ // No footer on small screens.
+ if ( matchMedia && matchMedia( '(max-width: 640px)' ).matches ) {
+ offset = 45;
+ }
+ content.css( 'height', ( window.innerHeight - offset ) );
+ };
+ expand = function() {
+ section.container.addClass( 'open' );
+ overlay.addClass( 'section-open' );
+ position = content.offset().top;
+ scroll = container.scrollTop();
+ content.css( 'margin-top', ( headerActionsHeight - position - scroll ) );
+ resizeContentHeight();
+ sectionTitle.attr( 'tabindex', '-1' );
+ backBtn.attr( 'tabindex', '0' );
+ backBtn.focus();
+ if ( args.completeCallback ) {
+ args.completeCallback();
+ }
+
+ // Fix the height after browser resize.
+ $( window ).on( 'resize.customizer-section', _.debounce( resizeContentHeight, 100 ) );
+
+ setTimeout( _.bind( section._recalculateTopMargin, section ), 0 );
+ };
+ }
+
+ if ( ! args.allowMultiple ) {
+ api.section.each( function ( otherSection ) {
+ if ( otherSection !== section ) {
+ otherSection.collapse( { duration: args.duration } );
+ }
+ });
+ }
+
+ if ( section.panel() ) {
+ api.panel( section.panel() ).expand({
+ duration: args.duration,
+ completeCallback: expand
+ });
+ } else {
+ api.panel.each( function( panel ) {
+ panel.collapse();
+ });
+ expand();
+ }
+
+ } else if ( ! expanded && section.container.hasClass( 'open' ) ) {
+ section.container.removeClass( 'open' );
+ overlay.removeClass( 'section-open' );
+ content.css( 'margin-top', '' );
+ container.scrollTop( 0 );
+ backBtn.attr( 'tabindex', '-1' );
+ sectionTitle.attr( 'tabindex', '0' );
+ sectionTitle.focus();
+ if ( args.completeCallback ) {
+ args.completeCallback();
+ }
+ $( window ).off( 'resize.customizer-section' );
+ } else {
+ if ( args.completeCallback ) {
+ args.completeCallback();
+ }
+ }
+ },
+
+ /**
+ * Recalculate the top margin.
+ *
+ * @since 4.4.0
+ * @private
+ */
+ _recalculateTopMargin: function() {
+ var section = this, content, offset, headerActionsHeight;
+ content = section.container.find( '.accordion-section-content' );
+ if ( 0 === content.length ) {
+ return;
+ }
+ headerActionsHeight = $( '#customize-header-actions' ).height();
+ offset = ( content.offset().top - headerActionsHeight );
+ if ( 0 < offset ) {
+ content.css( 'margin-top', ( parseInt( content.css( 'margin-top' ), 10 ) - offset ) );
+ }