// Short-circuit if there are no changes to the notifications. if ( collection.container.is( collection.previousContainer ) && _.isEqual( notifications, collection.previousNotifications ) ) { return; }
// Make sure list is part of the container. listElement = collection.container.children( 'ul' ).first(); if ( ! listElement.length ) { listElement = $( '<ul></ul>' ); collection.container.append( listElement ); }
// Remove all notifications prior to re-rendering. listElement.find( '> [data-code]' ).remove();
/** * A Customizer Setting. * * A setting is WordPress data (theme mod, option, menu, etc.) that the user can * draft changes to in the Customizer. * * @see PHP class WP_Customize_Setting. * * @constructs wp.customize.Setting * @augments wp.customize.Value * * @since 3.4.0 * * @param {string} id - The setting ID. * @param {*} value - The initial value of the setting. * @param {Object} [options={}] - Options. * @param {string} [options.transport=refresh] - The transport to use for previewing. Supports 'refresh' and 'postMessage'. * @param {boolean} [options.dirty=false] - Whether the setting should be considered initially dirty. * @param {Object} [options.previewer] - The Previewer instance to sync with. Defaults to wp.customize.previewer. */ initialize: function( id, value, options ) { var setting = this, params; params = _.extend( { previewer: api.previewer }, setting.defaults, options || {} );
setting.id = id; setting._dirty = params.dirty; // The _dirty property is what the Customizer reads from. setting.notifications = new api.Notifications();
// Whenever the setting's value changes, refresh the preview. setting.bind( setting.preview ); },
/** * Refresh the preview, respective of the setting's refresh policy. * * If the preview hasn't sent a keep-alive message and is likely * disconnected by having navigated to a non-allowed URL, then the * refresh transport will be forced when postMessage is the transport. * Note that postMessage does not throw an error when the recipient window * fails to match the origin window, so using try/catch around the * previewer.send() call to then fallback to refresh will not work. * * @since 3.4.0 * @access public * * @return {void} */ preview: function() { var setting = this, transport; transport = setting.transport;
if ( 'postMessage' === transport && ! api.state( 'previewerAlive' ).get() ) { transport = 'refresh'; }
if ( 'postMessage' === transport ) { setting.previewer.send( 'setting', [ setting.id, setting() ] ); } else if ( 'refresh' === transport ) { setting.previewer.refresh(); } },
/** * Find controls associated with this setting. * * @since 4.6.0 * @return {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; } });
/* * Keep track of the revision associated with each updated setting so that * requestChangesetUpdate knows which dirty settings to include. Also, once * ready is triggered and all initial settings have been added, increment * revision for each newly-created initially-dirty setting so that it will * also be included in changeset update requests. */ api.bind( 'change', function incrementChangedSettingRevision( setting ) { api._latestRevision += 1; api._latestSettingRevisions[ setting.id ] = api._latestRevision; } ); api.bind( 'ready', function() { api.bind( 'add', function incrementCreatedSettingRevision( setting ) { if ( setting._dirty ) { api._latestRevision += 1; api._latestSettingRevisions[ setting.id ] = api._latestRevision; } } ); } );
/** * Get the dirty setting values. * * @alias wp.customize.dirtyValues * * @since 4.7.0 * @access public * * @param {Object} [options] Options. * @param {boolean} [options.unsaved=false] Whether only values not saved yet into a changeset will be returned (differential changes). * @return {Object} Dirty setting values. */ api.dirtyValues = function dirtyValues( options ) { var values = {}; api.each( function( setting ) { var settingRevision;
// Skip including settings that have already been included in the changeset, if only requesting unsaved. if ( api.state( 'changesetStatus' ).get() && ( options && options.unsaved ) && ( _.isUndefined( settingRevision ) || settingRevision <= api._lastSavedRevision ) ) { return; }
/** * Request updates to the changeset. * * @alias wp.customize.requestChangesetUpdate * * @since 4.7.0 * @access public * * @param {Object} [changes] - Mapping of setting IDs to setting params each normally including a value property, or mapping to null. * If not provided, then the changes will still be obtained from unsaved dirty settings. * @param {Object} [args] - Additional options for the save request. * @param {boolean} [args.autosave=false] - Whether changes will be stored in autosave revision if the changeset has been promoted from an auto-draft. * @param {boolean} [args.force=false] - Send request to update even when there are no changes to submit. This can be used to request the latest status of the changeset on the server. * @param {string} [args.title] - Title to update in the changeset. Optional. * @param {string} [args.date] - Date to update in the changeset. Optional. * @return {jQuery.Promise} Promise resolving with the response data. */ api.requestChangesetUpdate = function requestChangesetUpdate( changes, args ) { var deferred, request, submittedChanges = {}, data, submittedArgs; deferred = new $.Deferred();
// Prevent attempting changeset update while request is being made. if ( 0 !== api.state( 'processing' ).get() ) { deferred.reject( 'already_processing' ); return deferred.promise(); }
if ( changes ) { _.extend( submittedChanges, changes ); }
// Ensure all revised settings (changes pending save) are also included, but not if marked for deletion in changes. _.each( api.dirtyValues( { unsaved: true } ), function( dirtyValue, settingId ) { if ( ! changes || null !== changes[ settingId ] ) { submittedChanges[ settingId ] = _.extend( {}, submittedChanges[ settingId ] || {}, { value: dirtyValue } ); } } );
// Allow plugins to attach additional params to the settings. api.trigger( 'changeset-save', submittedChanges, submittedArgs );
// Short-circuit when there are no pending changes. if ( ! submittedArgs.force && _.isEmpty( submittedChanges ) && null === submittedArgs.title && null === submittedArgs.date ) { deferred.resolve( {} ); return deferred.promise(); }
// A status would cause a revision to be made, and for this wp.customize.previewer.save() should be used. // Status is also disallowed for revisions regardless. if ( submittedArgs.status ) { return deferred.reject( { code: 'illegal_status_in_changeset_update' } ).promise(); }
// Dates not beung allowed for revisions are is a technical limitation of post revisions. if ( submittedArgs.date && submittedArgs.autosave ) { return deferred.reject( { code: 'illegal_autosave_with_date_gmt' } ).promise(); }
// Make sure that publishing a changeset waits for all changeset update requests to complete. api.state( 'processing' ).set( api.state( 'processing' ).get() + 1 ); deferred.always( function() { api.state( 'processing' ).set( api.state( 'processing' ).get() - 1 ); } );
// Ensure that if any plugins add data to save requests by extending query() that they get included here. data = api.previewer.query( { excludeCustomizedSaved: true } ); delete data.customized; // Being sent in customize_changeset_data instead. _.extend( data, { nonce: api.settings.nonce.save, customize_theme: api.settings.theme.stylesheet, customize_changeset_data: JSON.stringify( submittedChanges ) } ); if ( null !== submittedArgs.title ) { data.customize_changeset_title = submittedArgs.title; } if ( null !== submittedArgs.date ) { data.customize_changeset_date = submittedArgs.date; } if ( false !== submittedArgs.autosave ) { data.customize_changeset_autosave = 'true'; }
// Allow plugins to modify the params included with the save request. api.trigger( 'save-request-params', data );
request = wp.ajax.post( 'customize_save', data );
request.done( function requestChangesetUpdateDone( data ) { var savedChangesetValues = {};
// Ensure that all settings updated subsequently will be included in the next changeset update request. api._lastSavedRevision = Math.max( api._latestRevision, api._lastSavedRevision );
api.previewer.send( 'changeset-saved', _.extend( {}, data, { saved_changeset_values: savedChangesetValues } ) ); } ); request.fail( function requestChangesetUpdateFail( data ) { deferred.reject( data ); api.trigger( 'changeset-error', data ); } ); request.always( function( data ) { if ( data.setting_validities ) { api._handleSettingValidities( { settingValidities: data.setting_validities } ); } } );
return deferred.promise(); };
/** * Watch all changes to Value properties, and bubble changes to parent Values instance * * @alias wp.customize.utils.bubbleChildValueChanges * * @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. * * @alias wp.customize~focus * * @since 4.1.0 * * @param {Object} [params] * @param {Function} [params.completeCallback] */ focus = function ( params ) { var construct, completeCallback, focus, focusElement, sections; construct = this; params = params || {}; focus = function () { // If a child section is currently expanded, collapse it. if ( construct.extended( api.Panel ) ) { sections = construct.sections(); if ( 1 < sections.length ) { sections.forEach( function ( section ) { if ( section.expanded() ) { section.collapse(); } } ); } }
/** * Stable sort for Panels, Sections, and Controls. * * If a.priority() === b.priority(), then sort by their respective params.instanceNumber. * * @alias wp.customize.utils.prioritySort * * @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 * @return {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. * * @alias wp.customize.utils.isKeydownButNotEnterEvent * * @since 4.1.0 * * @param {jQuery.Event} event * @return {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. * * @alias wp.customize.utils.areElementListsEqual * * @since 4.1.0 * * @param {Array|jQuery} listA * @param {Array|jQuery} listB * @return {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 is equal. } ), false ) // Check for presence of false in map's return value. ); return equal; };
/** * Highlight the existence of a button. * * This function reminds the user of a button represented by the specified * UI element, after an optional delay. If the user focuses the element * before the delay passes, the reminder is canceled. * * @alias wp.customize.utils.highlightButton * * @since 4.9.0 * * @param {jQuery} button - The element to highlight. * @param {Object} [options] - Options. * @param {number} [options.delay=0] - Delay in milliseconds. * @param {jQuery} [options.focusTarget] - A target for user focus that defaults to the highlighted element. * If the user focuses the target before the delay passes, the reminder * is canceled. This option exists to accommodate compound buttons * containing auxiliary UI, such as the Publish button augmented with a * Settings button. * @return {Function} An idempotent function that cancels the reminder. */ api.utils.highlightButton = function highlightButton( button, options ) { var animationClass = 'button-see-me', canceled = false, params;
if ( ! canceled ) { button.addClass( animationClass ); button.one( 'animationend', function() { /* * Remove animation class to avoid situations in Customizer where * DOM nodes are moved (re-inserted) and the animation repeats. */ button.removeClass( animationClass ); } ); } }, params.delay );
return cancelReminder; };
/** * Get current timestamp adjusted for server clock time. * * Same functionality as the `current_time( 'mysql', false )` function in PHP. * * @alias wp.customize.utils.getCurrentTimestamp * * @since 4.9.0 * * @return {number} Current timestamp. */ api.utils.getCurrentTimestamp = function getCurrentTimestamp() { var currentDate, currentClientTimestamp, timestampDifferential; currentClientTimestamp = _.now(); currentDate = new Date( api.settings.initialServerDate.replace( /-/g, '/' ) ); timestampDifferential = currentClientTimestamp - api.settings.initialClientTimestamp; timestampDifferential += api.settings.initialClientTimestamp - api.settings.initialServerTimestamp; currentDate.setTime( currentDate.getTime() + timestampDifferential ); return currentDate.getTime(); };
/** * Get remaining time of when the date is set. * * @alias wp.customize.utils.getRemainingTime * * @since 4.9.0 * * @param {string|number|Date} datetime - Date time or timestamp of the future date. * @return {number} remainingTime - Remaining time in milliseconds. */ api.utils.getRemainingTime = function getRemainingTime( datetime ) { var millisecondsDivider = 1000, remainingTime, timestamp; if ( datetime instanceof Date ) { timestamp = datetime.getTime(); } else if ( 'string' === typeof datetime ) { timestamp = ( new Date( datetime.replace( /-/g, '/' ) ) ).getTime(); } else { timestamp = datetime; }
/** * Base class for Panel and Section. * * @constructs wp.customize~Container * @augments wp.customize.Class * * @since 4.1.0 * * @borrows wp.customize~focus as focus * * @param {string} id - The ID for the container. * @param {Object} options - Object containing one property: params. * @param {string} options.title - Title shown when panel is collapsed and expanded. * @param {string} [options.description] - Description shown at the top of the panel. * @param {number} [options.priority=100] - The sort priority for the panel. * @param {string} [options.templateId] - Template selector for container. * @param {string} [options.type=default] - The type of the panel. See wp.customize.panelConstructor. * @param {string} [options.content] - The markup to be used for the panel container. If empty, a JS template is used. * @param {boolean} [options.active=true] - Whether the panel is active or not. * @param {Object} [options.params] - Deprecated wrapper for the above properties. */ initialize: function ( id, options ) { var container = this; container.id = id;
/** * Get the element that will contain the notifications. * * @since 4.9.0 * @return {jQuery} Notification container element. */ getNotificationsContainerElement: function() { var container = this; return container.contentContainer.find( '.customize-control-notifications-container:first' ); },
/** * Set up notifications. * * @since 4.9.0 * @return {void} */ setupNotifications: function() { var container = this, renderNotifications; container.notifications.container = container.getNotificationsContainerElement();
// Render notifications when they change and when the construct is expanded. renderNotifications = function() { if ( container.expanded.get() ) { container.notifications.render(); } }; container.expanded.bind( renderNotifications ); renderNotifications(); container.notifications.bind( 'change', _.debounce( renderNotifications ) ); },
/** * Get the child models associated with this parent, sorting them by their priority Value. * * @since 4.1.0 * * @param {string} parentType * @param {string} childType * @return {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 - The active state to transiution to. * @param {Object} [args] - Args. * @param {Object} [args.duration] - The duration for the slideUp/slideDown animation. * @param {boolean} [args.unchanged] - Whether the state is already known to not be changed, and so short-circuit with calling completeCallback early. * @param {Function} [args.completeCallback] - Function to call when the slideUp/slideDown has completed. */ onChangeActive: function( active, args ) { var construct = this, headContainer = construct.headContainer, duration, expandedOtherPanel;
if ( args.unchanged ) { if ( args.completeCallback ) { args.completeCallback(); } return; }
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, headContainer.get( 0 ) ) ) { // If the element is not in the DOM, then jQuery.fn.slideUp() does nothing. // In this case, a hard toggle is required instead. headContainer.toggle( active ); if ( args.completeCallback ) { args.completeCallback(); } } else if ( active ) { headContainer.slideDown( duration, args.completeCallback ); } else { if ( construct.expanded() ) { construct.collapse({ duration: duration, completeCallback: function() { headContainer.slideUp( duration, args.completeCallback ); } }); } else { headContainer.slideUp( duration, args.completeCallback ); } } },
/** * @since 4.1.0 * * @param {boolean} active * @param {Object} [params] * @return {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; } },
/** * 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. * @return {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; }
/** * Animate container state change if transitions are supported by the browser. * * @since 4.7.0 * @private * * @param {function} completeCallback Function to be called after transition is completed. * @return {void} */ _animateChangeExpanded: function( completeCallback ) { // Return if CSS transitions are not supported or if reduced motion is enabled. if ( ! normalizedTransitionendEventName || isReducedMotion ) { // Schedule the callback until the next tick to prevent focus loss. _.defer( function () { if ( completeCallback ) { completeCallback(); } } ); return; }
/* * is documented using @borrows in the constructor. */ focus: focus,
/** * Return the container html, generated from its JS template, if it exists. * * @since 4.3.0 */ getContainer: function () { var template, container = this;
/** * Find content element which is displayed when the section is expanded. * * After a construct is initialized, the return value will be available via the `contentContainer` property. * By default the element will be related it to the parent container with `aria-owns` and detached. * Custom panels and sections (such as the `NewMenuSection`) that do not have a sliding pane should * just return the content element without needing to add the `aria-owns` element or detach it from * the container. Such non-sliding pane custom sections also need to override the `onChangeExpanded` * method to handle animating the panel/section into and out of view. * * @since 4.7.0 * @access public * * @return {jQuery} Detached content element. */ getContent: function() { var construct = this, container = construct.container, content = container.find( '.accordion-section-content, .control-panel-content' ).first(), contentId = 'sub-' + container.attr( 'id' ), ownedElements = contentId, alreadyOwnedElements = container.attr( 'aria-owns' );
/** * @constructs wp.customize.Section * @augments wp.customize~Container * * @since 4.1.0 * * @param {string} id - The ID for the section. * @param {Object} options - Options. * @param {string} options.title - Title shown when section is collapsed and expanded. * @param {string} [options.description] - Description shown at the top of the section. * @param {number} [options.priority=100] - The sort priority for the section. * @param {string} [options.type=default] - The type of the section. See wp.customize.sectionConstructor. * @param {string} [options.content] - The markup to be used for the section container. If empty, a JS template is used. * @param {boolean} [options.active=true] - Whether the section is active or not. * @param {string} options.panel - The ID for the panel this section is associated with. * @param {string} [options.customizeAction] - Additional context information shown before the section title when expanded. * @param {Object} [options.params] - Deprecated wrapper for the above properties. */ initialize: function ( id, options ) { var section = this, params; params = options.params || options;
// Look up the type if one was not supplied. if ( ! params.type ) { _.find( api.sectionConstructor, function( Constructor, type ) { if ( Constructor === section.constructor ) { params.type = type; return true; } return false; } ); }
// 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.contentContainer; if ( ! section.headContainer.parent().is( parentContainer ) ) { parentContainer.append( section.headContainer ); } if ( ! section.contentContainer.parent().is( section.headContainer ) ) { section.containerParent.append( section.contentContainer ); } section.deferred.embedded.resolve(); }); } ); } else { // There is no panel, so embed the section in the root of the customizer. parentContainer = api.ensure( section.containerPaneParent ); if ( ! section.headContainer.parent().is( parentContainer ) ) { parentContainer.append( section.headContainer ); } if ( ! section.contentContainer.parent().is( section.headContainer ) ) { section.containerParent.append( section.contentContainer ); } 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. },
/** * Add behaviors for the accordion section. * * @since 4.1.0 */ attachEvents: function () { var meta, content, section = this;
if ( section.container.hasClass( 'cannot-expand' ) ) { return; }
// Expand/Collapse accordion sections on click. section.container.find( '.accordion-section-title button, .customize-section-back, .accordion-section-title[tabindex]' ).on( 'click keydown', function( event ) { if ( api.utils.isKeydownButNotEnterEvent( event ) ) { return; } event.preventDefault(); // Keep this AFTER the key filter above.
// This is very similar to what is found for api.Panel.attachEvents(). section.container.find( '.customize-section-title .customize-help-toggle' ).on( 'click', function() {
/** * Return whether this section has any active controls. * * @since 4.1.0 * * @return {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 * * @return {Array} */ controls: function () { return this._children( 'section', 'control' ); },
/** * wp.customize.ThemesSection * * Custom section for themes that loads themes by category, and also * handles the theme-details view rendering and navigation. * * @constructs wp.customize.ThemesSection * @augments wp.customize.Section * * @since 4.9.0 * * @param {string} id - ID. * @param {Object} options - Options. * @return {void} */ initialize: function( id, options ) { var section = this; section.headerContainer = $(); section.$window = $( window ); section.$body = $( document.body ); api.Section.prototype.initialize.call( section, id, options ); section.updateCountDebounced = _.debounce( section.updateCount, 500 ); },
/** * Embed the section in the DOM when the themes panel is ready. * * Insert the section before the themes container. Assume that a themes section is within a panel, but not necessarily the themes panel. * * @since 4.9.0 */ embed: function() { var inject, section = this;
// Watch for changes to the panel state. inject = function( panelId ) { var parentContainer; api.panel( panelId, function( panel ) {
// The panel has been registered, wait for it to become ready/initialized. panel.deferred.embedded.done( function() { parentContainer = panel.contentContainer; if ( ! section.headContainer.parent().is( parentContainer ) ) { parentContainer.find( '.customize-themes-full-container-container' ).before( section.headContainer ); } if ( ! section.contentContainer.parent().is( section.headContainer ) ) { section.containerParent.append( section.contentContainer ); } 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. },
// Pressing the right arrow key fires a theme:next event. if ( 39 === event.keyCode ) { section.nextTheme(); }
// Pressing the left arrow key fires a theme:previous event. if ( 37 === event.keyCode ) { section.previousTheme(); }
// Pressing the escape key fires a theme:collapse event. if ( 27 === event.keyCode ) { if ( section.$body.hasClass( 'modal-open' ) ) {
// Escape from the details modal. section.closeDetails(); } else {
// Escape from the infinite scroll list. section.headerContainer.find( '.customize-themes-section-title' ).focus(); } event.stopPropagation(); // Prevent section from being collapsed. } });
/** * Override Section.isContextuallyActive method. * * Ignore the active states' of the contained theme controls, and just * use the section's own active state instead. This prevents empty search * results for theme sections from causing the section to become inactive. * * @since 4.2.0 * * @return {boolean} */ isContextuallyActive: function () { return this.active(); },
// Expand section/panel. Only collapse when opening another section. section.headerContainer.on( 'click', '.customize-themes-section-title', function() {
// Note: there is a second argument 'args' passed. var section = this, container = section.contentContainer.closest( '.customize-themes-full-container' );
// Immediately call the complete callback if there were no changes. if ( args.unchanged ) { if ( args.completeCallback ) { args.completeCallback(); } return; }
function expand() {
// Try to load controls if none are loaded yet. if ( 0 === section.loaded ) { section.loadThemes(); }
// Collapse any sibling sections/panels. api.section.each( function ( otherSection ) { var searchTerm;
if ( otherSection !== section ) {
// Try to sync the current search term to the new section. if ( 'themes' === otherSection.params.type ) { searchTerm = otherSection.contentContainer.find( '.wp-filter-search' ).val(); section.contentContainer.find( '.wp-filter-search' ).val( searchTerm );
// Always hide, even if they don't exist or are already hidden. section.headerContainer.find( '.filter-details' ).slideUp( 180 );
container.off( 'scroll' );
if ( args.completeCallback ) { args.completeCallback(); } } },
/** * Return the section's content element without detaching from the parent. * * @since 4.9.0 * * @return {jQuery} */ getContent: function() { return this.container.find( '.control-section-content' ); },
/** * Load theme data via Ajax and add themes to the section as controls. * * @since 4.9.0 * * @return {void} */ loadThemes: function() { var section = this, params, page, request;
if ( section.loading ) { return; // We're already loading a batch of themes. }
// Parameters for every API query. Additional params are set in PHP. page = Math.ceil( section.loaded / 100 ) + 1; params = { 'nonce': api.settings.nonce.switch_themes, 'wp_customize': 'on', 'theme_action': section.params.action, 'customized_theme': api.settings.theme.stylesheet, 'page': page };
// Add fields for remote filtering. if ( 'remote' === section.params.filter_type ) { params.search = section.term; params.tags = section.tags; }
// Stop and try again if the term changed while loading. if ( '' !== section.nextTerm || '' !== section.nextTags ) { if ( section.nextTerm ) { section.term = section.nextTerm; } if ( section.nextTags ) { section.tags = section.nextTags; } section.nextTerm = ''; section.nextTags = ''; section.loading = false; section.loadThemes(); return; }
if ( 0 !== themes.length ) {
section.loadControls( themes, page );
if ( 1 === page ) {
// Pre-load the first 3 theme screenshots. _.each( section.controls().slice( 0, 3 ), function( control ) { var img, src = control.params.theme.screenshot[0]; if ( src ) { img = new Image(); img.src = src; } }); if ( 'local' !== section.params.filter_type ) { wp.a11y.speak( api.settings.l10n.themeSearchResults.replace( '%d', data.info.results ) ); } }
_.delay( section.renderScreenshots, 100 ); // Wait for the controls to become visible.
if ( 'local' === section.params.filter_type || 100 > themes.length ) { // If we have less than the requested 100 themes, it's the end of the list. section.fullyLoaded = true; } } else { if ( 0 === section.loaded ) { section.container.find( '.no-themes' ).show(); wp.a11y.speak( section.container.find( '.no-themes' ).text() ); } else { section.fullyLoaded = true; } } if ( 'local' === section.params.filter_type ) { section.updateCount(); // Count of visible theme controls. } else { section.updateCount( data.info.results ); // Total number of results including pages not yet loaded. } section.container.find( '.unexpected-error' ).hide(); // Hide error notice in case it was previously shown.
// This cannot run on request.always, as section.loading may turn false before the new controls load in the success case. section.headContainer.closest( '.wp-full-overlay' ).removeClass( 'loading' ); section.loading = false; }); request.fail(function( data ) { if ( 'undefined' === typeof data ) { section.container.find( '.unexpected-error' ).show(); wp.a11y.speak( section.container.find( '.unexpected-error' ).text() ); } else if ( 'undefined' !== typeof console && console.error ) { console.error( data ); }
// This cannot run on request.always, as section.loading may turn false before the new controls load in the success case. section.headContainer.closest( '.wp-full-overlay' ).removeClass( 'loading' ); section.loading = false; }); },
/** * Loads controls into the section from data received from loadThemes(). * * @since 4.9.0 * @param {Array} themes - Array of theme data to create controls with. * @param {number} page - Page of results being loaded. * @return {void} */ loadControls: function( themes, page ) { var newThemeControls = [], section = this;
// Add controls for each theme. _.each( themes, function( theme ) { var themeControl = new api.controlConstructor.theme( section.params.action + '_theme_' + theme.id, { type: 'theme', section: section.params.id, theme: theme, priority: section.loaded + 1 } );
if ( 1 !== page ) { Array.prototype.push.apply( section.screenshotQueue, newThemeControls ); // Add new themes to the screenshot queue. } },
/** * Determines whether more themes should be loaded, and loads them. * * @since 4.9.0 * @return {void} */ loadMore: function() { var section = this, container, bottom, threshold; if ( ! section.fullyLoaded && ! section.loading ) { container = section.container.closest( '.customize-themes-full-container' );
bottom = container.scrollTop() + container.height(); // Use a fixed distance to the bottom of loaded results to avoid unnecessarily // loading results sooner when using a percentage of scroll distance. threshold = container.prop( 'scrollHeight' ) - 3000;
/** * Event handler for search input that filters visible controls. * * @since 4.9.0 * * @param {string} term - The raw search input value. * @return {void} */ filterSearch: function( term ) { var count = 0, visible = false, section = this, noFilter = ( api.section.has( 'wporg_themes' ) && 'remote' !== section.params.filter_type ) ? '.no-themes-local' : '.no-themes', controls = section.controls(), terms;
if ( section.loading ) { return; }
// Standardize search term format and split into an array of individual words. terms = term.toLowerCase().trim().replace( /-/g, ' ' ).split( ' ' );
_.each( controls, function( control ) { visible = control.filter( terms ); // Shows/hides and sorts control based on the applicability of the search term. if ( visible ) { count = count + 1; } });
/** * Event handler for search input that determines if the terms have changed and loads new controls as needed. * * @since 4.9.0 * * @param {wp.customize.ThemesSection} section - The current theme section, passed through the debouncer. * @return {void} */ checkTerm: function( section ) { var newTerm; if ( 'remote' === section.params.filter_type ) { newTerm = section.contentContainer.find( '.wp-filter-search' ).val(); if ( section.term !== newTerm.trim() ) { section.initializeNewQuery( newTerm, section.tags ); } } },
/** * Check for filters checked in the feature filter list and initialize a new query. * * @since 4.9.0 * * @return {void} */ filtersChecked: function() { var section = this, items = section.container.find( '.filter-group' ).find( ':checkbox' ), tags = [];
// Check whether tags have changed, and either load or queue them. if ( ! _.isEqual( section.tags, tags ) ) { if ( section.loading ) { section.nextTags = tags; } else { if ( 'remote' === section.params.filter_type ) { section.initializeNewQuery( section.term, tags ); } else if ( 'local' === section.params.filter_type ) { section.filterSearch( tags.join( ' ' ) ); } } } },
/** * Reset the current query and load new results. * * @since 4.9.0 * * @param {string} newTerm - New term. * @param {Array} newTags - New tags. * @return {void} */ initializeNewQuery: function( newTerm, newTags ) { var section = this;
// Clear the controls in the section. _.each( section.controls(), function( control ) { control.container.remove(); api.control.remove( control.id ); }); section.loaded = 0; section.fullyLoaded = false; section.screenshotQueue = null;
// Run a new query, with loadThemes handling paging, etc. if ( ! section.loading ) { section.term = newTerm; section.tags = newTags; section.loadThemes(); } else { section.nextTerm = newTerm; // This will reload from loadThemes() with the newest term once the current batch is loaded. section.nextTags = newTags; // This will reload from loadThemes() with the newest tags once the current batch is loaded. } if ( ! section.expanded() ) { section.expand(); // Expand the section if it isn't expanded. } },
/** * Render control's screenshot if the control comes into view. * * @since 4.2.0 * * @return {void} */ renderScreenshots: function() { var section = this;
// Fill queue initially, or check for more if empty. if ( null === section.screenshotQueue || 0 === section.screenshotQueue.length ) {
// Add controls that haven't had their screenshots rendered. section.screenshotQueue = _.filter( section.controls(), function( control ) { return ! control.screenshotRendered; }); }
// Are all screenshots rendered (for now)? 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; } ); },
/** * Update the number of themes in the section. * * @since 4.9.0 * * @return {void} */ updateCount: function( count ) { var section = this, countEl, displayed;
/** * Render & show the theme details for a given theme model. * * @since 4.2.0 * * @param {Object} theme - Theme. * @param {Function} [callback] - Callback once the details have been shown. * @return {void} */ showDetails: function ( theme, callback ) { var section = this, panel = api.panel( 'themes' ); section.currentTheme = theme.id; section.overlay.html( section.template( theme ) ) .fadeIn( 'fast' ) .focus();
function disableSwitchButtons() { return ! panel.canSwitchTheme( theme.id ); }
// Temporary special function since supplying SFTP credentials does not work yet. See #42184. function disableInstallButtons() { return disableSwitchButtons() || false === api.settings.theme._canInstall || true === api.settings.theme._filesystemCredentialsNeeded; }
/** * Close the theme details modal. * * @since 4.2.0 * * @return {void} */ closeDetails: function () { var section = this; section.$body.removeClass( 'modal-open' ); section.overlay.fadeOut( 'fast' ); api.control( section.params.action + '_theme_' + section.currentTheme ).container.find( '.theme' ).focus(); },
/** * Keep tab focus within the theme details modal. * * @since 4.2.0 * * @param {jQuery} el - Element to contain focus. * @return {void} */ 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; } }); } });
/** * Class wp.customize.OuterSection. * * Creates section outside of the sidebar, there is no ui to trigger collapse/expand so * it would require custom handling. * * @constructs wp.customize.OuterSection * @augments wp.customize.Section * * @since 4.9.0 * * @return {void} */ initialize: function() { var section = this; section.containerParent = '#customize-outer-theme-controls'; section.containerPaneParent = '.customize-outer-pane-parent'; api.Section.prototype.initialize.apply( section, arguments ); },
/** * Overrides api.Section.prototype.onChangeExpanded to prevent collapse/expand effect * on other sections and panels. * * @since 4.9.0 * * @param {boolean} expanded - The expanded state to transition to. * @param {Object} [args] - Args. * @param {boolean} [args.unchanged] - Whether the state is already known to not be changed, and so short-circuit with calling completeCallback early. * @param {Function} [args.completeCallback] - Function to call when the slideUp/slideDown has completed. * @param {Object} [args.duration] - The duration for the animation. */ onChangeExpanded: function( expanded, args ) { var section = this, container = section.headContainer.closest( '.wp-full-overlay-sidebar-content' ), content = section.contentContainer, backBtn = content.find( '.customize-section-back' ), sectionTitle = section.headContainer.find( '.accordion-section-title button, .accordion-section-title[tabindex]' ).first(), body = $( document.body ), expand, panel;
/** * @constructs wp.customize.Panel * @augments wp.customize~Container * * @since 4.1.0 * * @param {string} id - The ID for the panel. * @param {Object} options - Object containing one property: params. * @param {string} options.title - Title shown when panel is collapsed and expanded. * @param {string} [options.description] - Description shown at the top of the panel. * @param {number} [options.priority=100] - The sort priority for the panel. * @param {string} [options.type=default] - The type of the panel. See wp.customize.panelConstructor. * @param {string} [options.content] - The markup to be used for the panel container. If empty, a JS template is used. * @param {boolean} [options.active=true] - Whether the panel is active or not. * @param {Object} [options.params] - Deprecated wrapper for the above properties. */ initialize: function ( id, options ) { var panel = this, params; params = options.params || options;
// Look up the type if one was not supplied. if ( ! params.type ) { _.find( api.panelConstructor, function( Constructor, type ) { if ( Constructor === panel.constructor ) { params.type = type; return true; } return false; } ); }
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.
/** * Get the sections that are associated with this panel, sorted by their priority Value. * * @since 4.1.0 * * @return {Array} */ sections: function () { return this._children( 'panel', 'section' ); },
/** * Return whether this panel has any active sections. * * @since 4.1.0 * * @return {boolean} Whether contextually active. */ isContextuallyActive: function () { var panel = this, sections = panel.sections(), activeCount = 0; _( sections ).each( function ( section ) { if ( section.active() && section.isContextuallyActive() ) { activeCount += 1; } } ); return ( activeCount !== 0 ); },
// 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 button, .accordion-section-title[tabindex]' ), backBtn = accordionSection.find( '.customize-panel-back' ), childSections = panel.sections(), skipTransition;
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 } ); } });
/** * 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;
// Temporary since supplying SFTP credentials does not work yet. See #42184. if ( api.settings.theme._canInstall && api.settings.theme._filesystemCredentialsNeeded ) { panel.notifications.add( new api.Notification( 'theme_install_unavailable', { message: api.l10n.themeInstallUnavailable, type: 'info', dismissible: true } ) ); }
// Update a theme. Theme cards have the class, the details modal has the id. panel.contentContainer.on( 'click', '.update-theme, #update-theme', function( event ) {
// #update-theme is a link. event.preventDefault(); event.stopPropagation();
// Immediately call the complete callback if there were no changes. if ( args.unchanged ) { if ( args.completeCallback ) { args.completeCallback(); } return; }
// Temporary since supplying SFTP credentials does not work yet. See #42184. if ( api.settings.theme._filesystemCredentialsNeeded ) { deferred.reject({ errorCode: 'theme_install_unavailable' }); return deferred.promise(); }
// Prevent loading a non-active theme preview when there is a drafted/scheduled changeset. if ( ! panel.canSwitchTheme( slug ) ) { deferred.reject({ errorCode: 'theme_switch_unavailable' }); return deferred.promise(); }
// Theme is already being installed. if ( _.contains( panel.installingThemes, slug ) ) { deferred.reject({ errorCode: 'theme_already_installing' }); return deferred.promise(); }
// Close the details modal if it's open to the installed theme. api.section.each( function( section ) { if ( 'themes' === section.params.type ) { if ( theme.id === section.currentTheme ) { // Don't close the modal if the user has navigated elsewhere. section.closeDetails(); } } }); } deferred.resolve( response ); };
panel.installingThemes.push( slug ); // Note: we don't remove elements from installingThemes, since they shouldn't be installed again. request = wp.updates.installTheme( { slug: slug } );
// Also preview the theme as the event is triggered on Install & Preview. if ( preview ) { api.notifications.add( new api.OverlayNotification( 'theme_installing', { message: api.l10n.themeDownloading, type: 'info', loading: true } ) ); }
// Prevent loading a non-active theme preview when there is a drafted/scheduled changeset. if ( ! panel.canSwitchTheme( themeId ) ) { deferred.reject({ errorCode: 'theme_switch_unavailable' }); return deferred.promise(); }
// Include autosaved param to load autosave revision without prompting user to restore it. if ( ! api.state( 'saved' ).get() ) { queryParams.customize_autosaved = 'on'; }
urlParser.search = $.param( queryParams );
// Update loading message. Everything else is handled by reloading the page. api.notifications.add( new api.OverlayNotification( 'theme_previewing', { message: api.l10n.themePreviewWait, type: 'info', loading: true } ) );
onceProcessingComplete = function() { var request; if ( api.state( 'processing' ).get() > 0 ) { return; }
/** * A Customizer Control. * * A control provides a UI element that allows a user to modify a Customizer Setting. * * @see PHP class WP_Customize_Control. * * @constructs wp.customize.Control * @augments wp.customize.Class * * @borrows wp.customize~focus as this#focus * @borrows wp.customize~Container#activate as this#activate * @borrows wp.customize~Container#deactivate as this#deactivate * @borrows wp.customize~Container#_toggleActive as this#_toggleActive * * @param {string} id - Unique identifier for the control instance. * @param {Object} options - Options hash for the control instance. * @param {Object} options.type - Type of control (e.g. text, radio, dropdown-pages, etc.) * @param {string} [options.content] - The HTML content for the control or at least its container. This should normally be left blank and instead supplying a templateId. * @param {string} [options.templateId] - Template ID for control's content. * @param {string} [options.priority=10] - Order of priority to show the control within the section. * @param {string} [options.active=true] - Whether the control is active. * @param {string} options.section - The ID of the section the control belongs to. * @param {mixed} [options.setting] - The ID of the main setting or an instance of this setting. * @param {mixed} options.settings - An object with keys (e.g. default) that maps to setting IDs or Setting/Value objects, or an array of setting IDs or Setting/Value objects. * @param {mixed} options.settings.default - The ID of the setting the control relates to. * @param {string} options.settings.data - @todo Is this used? * @param {string} options.label - Label. * @param {string} options.description - Description. * @param {number} [options.instanceNumber] - Order in which this instance was created in relation to other instances. * @param {Object} [options.params] - Deprecated wrapper for the above properties. * @return {void} */ initialize: function( id, options ) { var control = this, deferredSettingIds = [], settings, gatherSettings;
control.params = _.extend( {}, control.defaults, control.params || {}, // In case subclass already defines. options.params || options || {} // The options.params property is deprecated, but it is checked first for back-compat. );
// Look up the type if one was not supplied. if ( ! control.params.type ) { _.find( api.controlConstructor, function( Constructor, type ) { if ( Constructor === control.constructor ) { control.params.type = type; return true; } return false; } ); }
// Note: Settings can be an array or an object, with values being either setting IDs or Setting (or Value) objects. _.each( settings, function( value, key ) { var setting; if ( _.isObject( value ) && _.isFunction( value.extended ) && value.extended( api.Value ) ) { control.settings[ key ] = value; } else if ( _.isString( value ) ) { setting = api( value ); if ( setting ) { control.settings[ key ] = setting; } else { deferredSettingIds.push( value ); } } } );
// After the control is embedded on the page, invoke the "ready" method. control.deferred.embedded.done( function () { control.linkElements(); // Link any additional elements after template is rendered by renderContent(). control.setupNotifications(); control.ready(); }); },
/** * Link elements between settings and inputs. * * @since 4.7.0 * @access public * * @return {void} */ linkElements: function () { var control = this, nodes, radios, element;
// Let link by default refer to setting ID. If it doesn't exist, fallback to looking up by setting key. if ( node.data( 'customizeSettingLink' ) ) { setting = api( node.data( 'customizeSettingLink' ) ); } else if ( node.data( 'customizeSettingKeyLink' ) ) { setting = control.settings[ node.data( 'customizeSettingKeyLink' ) ]; }
if ( setting ) { element = new api.Element( node ); control.elements.push( element ); element.sync( setting ); element.set( setting() ); } } ); },
/** * 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. * * @return {void} */ ready: function() { var control = this, newItem; if ( 'dropdown-pages' === control.params.type && control.params.allow_addition ) { newItem = control.container.find( '.new-content-item-wrapper' ); 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( 'keydown', '.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 * @return {jQuery} Setting validation message element. */ getNotificationsContainerElement: function() { var control = this, controlTitle, 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. * * @deprecated in favor of `control.notifications.render()` * @since 4.6.0 * @this {wp.customize.Control} */ renderNotifications: function() { var control = this, container, notifications, hasError = false;
if ( 'undefined' !== typeof console && console.warn ) { console.warn( '[DEPRECATED] wp.customize.Control.prototype.renderNotifications() is deprecated in favor of instantiating a wp.customize.Notifications and calling its render() method.' ); }
/** * Normal controls do not expand, so just expand its parent * * @param {Object} [params] */ expand: function ( params ) { api.section( this.section() ).expand( params ); },
/* * Documented using @borrows in the constructor. */ 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 {Function} 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 ); },
/* * Documented using @borrows in the constructor */ activate: Container.prototype.activate,
/* * Documented using @borrows in the constructor */ deactivate: Container.prototype.deactivate,
/* * Documented using @borrows in the constructor */ _toggleActive: Container.prototype._toggleActive,
// @todo This function appears to be dead code and can be removed. dropdownInit: function() { var control = this, statuses = this.container.find('.dropdown-status'), params = this.params, toggleFreeze = false, update = function( to ) { if ( 'string' === typeof to && params.statuses && params.statuses[ to ] ) { statuses.html( params.statuses[ to ] ).show(); } else { statuses.hide(); } };
// Support the .dropdown class to open/close complex elements. this.container.on( 'click keydown', '.dropdown', function( event ) { if ( api.utils.isKeydownButNotEnterEvent( event ) ) { return; }
event.preventDefault();
if ( ! toggleFreeze ) { control.container.toggleClass( 'open' ); }
/** * Render the control from its JS template, if it exists. * * The control's container must already exist in the DOM. * * @since 4.1.0 */ renderContent: function () { var control = this, template, standardTypes, templateId, sectionId;
// Use default content template when a standard HTML type is used, // there isn't a more specific template existing, and the control container is empty. if ( templateId === 'customize-control-' + control.params.type + '-content' && _.contains( standardTypes, control.params.type ) && ! document.getElementById( 'tmpl-' + templateId ) && 0 === control.container.children().length ) { templateId = 'customize-control-default-content'; }
// Replace the container element's content with the control. if ( document.getElementById( 'tmpl-' + templateId ) ) { template = wp.template( templateId ); if ( template && control.container ) { control.container.html( template( control.params ) ); } }
// Re-render notifications after content has been re-rendered. control.notifications.container = control.getNotificationsContainerElement(); sectionId = control.section(); if ( ! sectionId || ( api.section.has( sectionId ) && api.section( sectionId ).expanded() ) ) { control.notifications.render(); } },
/** * Add a new page to a dropdown-pages control reusing menus code for this. * * @since 4.7.0 * @access private * * @return {void} */ addNewPage: function () { var control = this, promise, toggle, container, input, title, select;
// The menus functions add the page, publish when appropriate, // and also add the new page to the dropdown-pages controls. promise = api.Menus.insertAutoDraftPost( { post_title: title, post_type: 'page' } ); promise.done( function( data ) { var availableItem, $content, itemTemplate;
// Prepare the new page as an available menu item. // See api.Menus.submitNew(). availableItem = new api.Menus.AvailableItemModel( { 'id': 'post-' + data.post_id, // Used for available menu item Backbone models. 'title': title, 'type': 'post_type', 'type_label': api.Menus.data.l10n.page_label, 'object': 'page', 'object_id': data.post_id, 'url': data.url } );
// Add the new item to the list of available menu items. api.Menus.availableMenuItemsPanel.collection.add( availableItem ); $content = $( '#available-menu-items-post_type-page' ).find( '.available-menu-items-list' ); itemTemplate = wp.template( 'available-menu-item' ); $content.prepend( itemTemplate( availableItem.attributes ) );
// Focus the select control. select.focus(); control.setting.set( String( data.post_id ) ); // Triggers a preview refresh and updates the setting.
control.setting.bind( function ( value ) { // Bail if the update came from the control itself. if ( updating ) { return; } picker.val( value ); picker.wpColorPicker( 'color', value ); } );
// Collapse color picker when hitting Esc instead of collapsing the current section. control.container.on( 'keydown', function( event ) { var pickerContainer; if ( 27 !== event.which ) { // Esc. return; } pickerContainer = control.container.find( '.wp-picker-container' ); if ( pickerContainer.hasClass( 'wp-picker-active' ) ) { picker.wpColorPicker( 'close' ); control.container.find( '.wp-color-result' ).focus(); event.stopPropagation(); // Prevent section from being collapsed. } } ); } });
/** * A control that implements the media modal. * * @class wp.customize.MediaControl * @augments wp.customize.Control */ api.MediaControl = api.Control.extend(/** @lends wp.customize.MediaControl.prototype */{
/** * When the control's DOM structure is ready, * set up internal event bindings. */ ready: function() { var control = this; // Shortcut so that we don't have to use _.bind every time we add a callback. _.bindAll( control, 'restoreDefault', 'removeFile', 'openFrame', 'select', 'pausePlayer' );
// Resize the player controls when it becomes visible (ie when section is expanded). api.section( control.section() ).container .on( 'expanded', function() { if ( control.player ) { control.player.setControlsSize(); } }) .on( 'collapsed', function() { control.pausePlayer(); });
/** * Set attachment data and render content. * * Note that BackgroundImage.prototype.ready applies this ready method * to itself. Since BackgroundImage is an UploadControl, the value * is the attachment URL instead of the attachment ID. In this case * we skip fetching the attachment data because we have no ID available, * and it is the responsibility of the UploadControl to set the control's * attachmentData before calling the renderContent method. * * @param {number|string} value Attachment */ function setAttachmentDataAndRenderContent( value ) { var hasAttachmentData = $.Deferred();
if ( control.extended( api.UploadControl ) ) { hasAttachmentData.resolve(); } else { value = parseInt( value, 10 ); if ( _.isNaN( value ) || value <= 0 ) { delete control.params.attachment; hasAttachmentData.resolve(); } else if ( control.params.attachment && control.params.attachment.id === value ) { hasAttachmentData.resolve(); } }
// Fetch the attachment data. if ( 'pending' === hasAttachmentData.state() ) { wp.media.attachment( value ).fetch().done( function() { control.params.attachment = this.attributes; hasAttachmentData.resolve();
// Send attachment information to the preview for possible use in `postMessage` transport. wp.customize.previewer.send( control.setting.id + '-attachment-data', this.attributes ); } ); }
// Ensure attachment data is initially set (for dynamically-instantiated controls). setAttachmentDataAndRenderContent( control.setting() );
// Update the attachment data and re-render the control when the setting changes. control.setting.bind( setAttachmentDataAndRenderContent ); },
pausePlayer: function () { this.player && this.player.pause(); },
cleanupPlayer: function () { this.player && wp.media.mixin.removePlayer( this.player ); },
/** * Open the media modal. */ openFrame: function( event ) { if ( api.utils.isKeydownButNotEnterEvent( event ) ) { return; }
event.preventDefault();
if ( ! this.frame ) { this.initFrame(); }
this.frame.open(); },
/** * Create a media modal select frame, and store it so the instance can be reused when needed. */ initFrame: function() { this.frame = wp.media({ button: { text: this.params.button_labels.frame_button }, states: [ new wp.media.controller.Library({ title: this.params.button_labels.frame_title, library: wp.media.query({ type: this.params.mime_type }), multiple: false, date: false }) ] });
// When a file is selected, run a callback. this.frame.on( 'select', this.select ); },
/** * Callback handler for when an attachment is selected in the media modal. * Gets the selected image information, and sets it within the control. */ select: function() { // Get the attachment from the modal frame. var node, attachment = this.frame.state().get( 'selection' ).first().toJSON(), mejsSettings = window._wpmejsSettings || {};
this.params.attachment = attachment;
// Set the Customizer setting; the callback takes care of rendering. this.setting( attachment.id ); node = this.container.find( 'audio, video' ).get(0);
/** * Called when the "Remove" link is clicked. Empties the setting. * * @param {Object} event jQuery Event object */ removeFile: function( event ) { if ( api.utils.isKeydownButNotEnterEvent( event ) ) { return; } event.preventDefault();
this.params.attachment = {}; this.setting( '' ); this.renderContent(); // Not bound to setting change when emptying. } });
/** * An upload control, which utilizes the media modal. * * @class wp.customize.UploadControl * @augments wp.customize.MediaControl */ api.UploadControl = api.MediaControl.extend(/** @lends wp.customize.UploadControl.prototype */{
/** * Callback handler for when an attachment is selected in the media modal. * Gets the selected image information, and sets it within the control. */ select: function() { // Get the attachment from the modal frame. var node, attachment = this.frame.state().get( 'selection' ).first().toJSON(), mejsSettings = window._wpmejsSettings || {};
this.params.attachment = attachment;
// Set the Customizer setting; the callback takes care of rendering. this.setting( attachment.url ); node = this.container.find( 'audio, video' ).get(0);
/** * A control for uploading images. * * This control no longer needs to do anything more * than what the upload control does in JS. * * @class wp.customize.ImageControl * @augments wp.customize.UploadControl */ api.ImageControl = api.UploadControl.extend(/** @lends wp.customize.ImageControl.prototype */{ // @deprecated thumbnailSrc: function() {} });
/** * A control for uploading background images. * * @class wp.customize.BackgroundControl * @augments wp.customize.UploadControl */ api.BackgroundControl = api.UploadControl.extend(/** @lends wp.customize.BackgroundControl.prototype */{
/** * When the control's DOM structure is ready, * set up internal event bindings. */ ready: function() { api.UploadControl.prototype.ready.apply( this, arguments ); },
/** * Callback handler for when an attachment is selected in the media modal. * Does an additional Ajax request for setting the background context. */ select: function() { api.UploadControl.prototype.select.apply( this, arguments );
/** * A control for positioning a background image. * * @since 4.7.0 * * @class wp.customize.BackgroundPositionControl * @augments wp.customize.Control */ api.BackgroundPositionControl = api.Control.extend(/** @lends wp.customize.BackgroundPositionControl.prototype */{
/** * Set up control UI once embedded in DOM and settings are created. * * @since 4.7.0 * @access public */ ready: function() { var control = this, updateRadios;
control.container.on( 'change', 'input[name="background-position"]', function() { var position = $( this ).val().split( ' ' ); control.settings.x( position[0] ); control.settings.y( position[1] ); } );
updateRadios = _.debounce( function() { var x, y, radioInput, inputValue; x = control.settings.x.get(); y = control.settings.y.get(); inputValue = String( x ) + ' ' + String( y ); radioInput = control.container.find( 'input[name="background-position"][value="' + inputValue + '"]' ); radioInput.trigger( 'click' ); } ); control.settings.x.bind( updateRadios ); control.settings.y.bind( updateRadios );
updateRadios(); // Set initial UI. } } );
/** * A control for selecting and cropping an image. * * @class wp.customize.CroppedImageControl * @augments wp.customize.MediaControl */ api.CroppedImageControl = api.MediaControl.extend(/** @lends wp.customize.CroppedImageControl.prototype */{
/** * Open the media modal to the library state. */ openFrame: function( event ) { if ( api.utils.isKeydownButNotEnterEvent( event ) ) { return; }
/** * Create a media modal select frame, and store it so the instance can be reused when needed. */ initFrame: function() { var l10n = _wpMediaViewsL10n;
this.frame.on( 'select', this.onSelect, this ); this.frame.on( 'cropped', this.onCropped, this ); this.frame.on( 'skippedcrop', this.onSkippedCrop, this ); },
/** * After an image is selected in the media modal, switch to the cropper * state if the image isn't the right size. */ onSelect: function() { var attachment = this.frame.state().get( 'selection' ).first().toJSON();
/** * After the image has been cropped, apply the cropped image data to the setting. * * @param {Object} croppedImage Cropped attachment data. */ onCropped: function( croppedImage ) { this.setImageFromAttachment( croppedImage ); },
/** * Returns a set of options, computed from the attached image data and * control-specific data, to be fed to the imgAreaSelect plugin in * wp.media.view.Cropper. * * @param {wp.media.model.Attachment} attachment * @param {wp.media.controller.Cropper} controller * @return {Object} Options */ calculateImageSelectOptions: function( attachment, controller ) { var control = controller.get( 'control' ), flexWidth = !! parseInt( control.params.flex_width, 10 ), flexHeight = !! parseInt( control.params.flex_height, 10 ), realWidth = attachment.get( 'width' ), realHeight = attachment.get( 'height' ), xInit = parseInt( control.params.width, 10 ), yInit = parseInt( control.params.height, 10 ), requiredRatio = xInit / yInit, realRatio = realWidth / realHeight, xImg = xInit, yImg = yInit, x1, y1, imgSelectOptions;
/** * Check if the image's aspect ratio essentially matches the required aspect ratio. * * Floating point precision is low, so this allows a small tolerance. This * tolerance allows for images over 100,000 px on either side to still trigger * the cropping flow. * * @param {number} requiredRatio Required image ratio. * @param {number} realRatio Provided image ratio. * @return {boolean} Whether the image has the required aspect ratio. */ hasRequiredAspectRatio: function ( requiredRatio, realRatio ) { if ( Math.abs( requiredRatio - realRatio ) < 0.000001 ) { return true; }
return false; },
/** * If cropping was skipped, apply the image data directly to the setting. */ onSkippedCrop: function() { var attachment = this.frame.state().get( 'selection' ).first().toJSON(); this.setImageFromAttachment( attachment ); },
/** * Updates the setting and re-renders the control UI. * * @param {Object} attachment */ setImageFromAttachment: function( attachment ) { this.params.attachment = attachment;
// Set the Customizer setting; the callback takes care of rendering. this.setting( attachment.id ); } });
/** * A control for selecting and cropping Site Icons. * * @class wp.customize.SiteIconControl * @augments wp.customize.CroppedImageControl */ api.SiteIconControl = api.CroppedImageControl.extend(/** @lends wp.customize.SiteIconControl.prototype */{
/** * Create a media modal select frame, and store it so the instance can be reused when needed. */ initFrame: function() { var l10n = _wpMediaViewsL10n;
this.frame.on( 'select', this.onSelect, this ); this.frame.on( 'cropped', this.onCropped, this ); this.frame.on( 'skippedcrop', this.onSkippedCrop, this ); },
/** * After an image is selected in the media modal, switch to the cropper * state if the image isn't the right size. */ onSelect: function() { var attachment = this.frame.state().get( 'selection' ).first().toJSON(), controller = this;
/** * Sets up and opens the Media Manager in order to select an image. * Depending on both the size of the image and the properties of the * current theme, a cropping step after selection may be required or * skippable. * * @param {event} event */ openMedia: function(event) { var l10n = _wpMediaViewsL10n;
/** * After an image is selected in the media modal, * switch to the cropper state. */ onSelect: function() { this.frame.setState('cropper'); },
/** * After the image has been cropped, apply the cropped image data to the setting. * * @param {Object} croppedImage Cropped attachment data. */ onCropped: function(croppedImage) { var url = croppedImage.url, attachmentId = croppedImage.attachment_id, w = croppedImage.width, h = croppedImage.height; this.setImageFromURL(url, attachmentId, w, h); },
/** * If cropping was skipped, apply the image data directly to the setting. * * @param {Object} selection */ onSkippedCrop: function(selection) { var url = selection.get('url'), w = selection.get('width'), h = selection.get('height'); this.setImageFromURL(url, selection.id, w, h); },
/** * Creates a new wp.customize.HeaderTool.ImageModel from provided * header image data and inserts it into the user-uploaded headers * collection. * * @param {string} url * @param {number} attachmentId * @param {number} width * @param {number} height */ setImageFromURL: function(url, attachmentId, width, height) { var choice, data = {};
/** * Triggers the necessary events to deselect an image which was set as * the currently selected one. */ removeImage: function() { api.HeaderTool.currentHeader.trigger('hide'); api.HeaderTool.CombinedList.trigger('control:removeImage'); }
/** * @since 4.2.0 */ ready: function() { var control = this, panel = api.panel( 'themes' );
function disableSwitchButtons() { return ! panel.canSwitchTheme( control.params.theme.id ); }
// Temporary special function since supplying SFTP credentials does not work yet. See #42184. function disableInstallButtons() { return disableSwitchButtons() || false === api.settings.theme._canInstall || true === api.settings.theme._filesystemCredentialsNeeded; } function updateButtons() { control.container.find( 'button.preview, button.preview-theme' ).toggleClass( 'disabled', disableSwitchButtons() ); control.container.find( 'button.theme-install' ).toggleClass( 'disabled', disableInstallButtons() ); }
// Bail if the user scrolled on a touch device. if ( control.touchDrag === true ) { return control.touchDrag = false; }
// Prevent the modal from showing when the user clicks the action button. if ( $( event.target ).is( '.theme-actions .button, .update-theme' ) ) { return; }
event.preventDefault(); // Keep this AFTER the key filter above. section = api.section( control.section() ); section.showDetails( control.params.theme, function() {
// Temporary special function since supplying SFTP credentials does not work yet. See #42184. if ( api.settings.theme._filesystemCredentialsNeeded ) { section.overlay.find( '.theme-actions .delete-theme' ).remove(); } } ); });
control.container.on( 'render-screenshot', function() { var $screenshot = $( this ).find( 'img' ), source = $screenshot.data( 'src' );
/** * Show or hide the theme based on the presence of the term in the title, description, tags, and author. * * @since 4.2.0 * @param {Array} terms - An array of terms to search for. * @return {boolean} Whether a theme control was activated or not. */ filter: function( terms ) { var control = this, matchCount = 0, haystack = control.params.theme.name + ' ' + control.params.theme.description + ' ' + control.params.theme.tags + ' ' + control.params.theme.author + ' '; haystack = haystack.toLowerCase().replace( '-', ' ' );
// Back-compat for behavior in WordPress 4.2.0 to 4.8.X. if ( ! _.isArray( terms ) ) { terms = [ terms ]; }
// Always give exact name matches highest ranking. if ( control.params.theme.name.toLowerCase() === terms.join( ' ' ) ) { matchCount = 100; } else {
// Search for and weight (by 10) complete term matches. matchCount = matchCount + 10 * ( haystack.split( terms.join( ' ' ) ).length - 1 );
// Search for each term individually (as whole-word and partial match) and sum weighted match counts. _.each( terms, function( term ) { matchCount = matchCount + 2 * ( haystack.split( term + ' ' ).length - 1 ); // Whole-word, double-weighted. matchCount = matchCount + haystack.split( term ).length - 1; // Partial word, to minimize empty intermediate searches while typing. });
// Upper limit on match ranking. if ( matchCount > 99 ) { matchCount = 99; } }
/** * Initialize. * * @since 4.9.0 * @param {string} id - Unique identifier for the control instance. * @param {Object} options - Options hash for the control instance. * @return {void} */ initialize: function( id, options ) { var control = this; control.deferred = _.extend( control.deferred || {}, { codemirror: $.Deferred() } ); api.Control.prototype.initialize.call( control, id, options );
// Note that rendering is debounced so the props will be used when rendering happens after add event. control.notifications.bind( 'add', function( notification ) {
// Skip if control notification is not from setting csslint_error notification. if ( notification.code !== control.setting.id + ':csslint_error' ) { return; }
// Customize the template and behavior of csslint_error notifications. notification.templateId = 'customize-code-editor-lint-error-notification'; notification.render = (function( render ) { return function() { var li = render.call( this ); li.find( 'input[type=checkbox]' ).on( 'click', function() { control.setting.notifications.remove( 'csslint_error' ); } ); return li; }; })( notification.render ); } ); },
/** * Initialize the editor when the containing section is ready and expanded. * * @since 4.9.0 * @return {void} */ ready: function() { var control = this; if ( ! control.section() ) { control.initEditor(); return; }
// Wait to initialize editor until section is embedded and expanded. api.section( control.section(), function( section ) { section.deferred.embedded.done( function() { var onceExpanded; if ( section.expanded() ) { control.initEditor(); } else { onceExpanded = function( isExpanded ) { if ( isExpanded ) { control.initEditor(); section.expanded.unbind( onceExpanded ); } }; section.expanded.bind( onceExpanded ); } } ); } ); },
// Focus the editor when clicking on its label. control.container.find( 'label' ).on( 'click', function() { control.editor.codemirror.focus(); });
/* * When the CodeMirror instance changes, mirror to the textarea, * where we have our "true" change event handler bound. */ control.editor.codemirror.on( 'change', function( codemirror ) { suspendEditorUpdate = true; $textarea.val( codemirror.getValue() ).trigger( 'change' ); suspendEditorUpdate = false; });
// Update CodeMirror when the setting is changed by another plugin. control.setting.bind( function( value ) { if ( ! suspendEditorUpdate ) { control.editor.codemirror.setValue( value ); } });
// Prevent collapsing section when hitting Esc to tab out of editor. control.editor.codemirror.on( 'keydown', function onKeydown( codemirror, event ) { var escKeyCode = 27; if ( escKeyCode === event.keyCode ) { event.stopPropagation(); } });
$textarea.on( 'keydown', function onKeydown( event ) { var selectionStart, selectionEnd, value, tabKeyCode = 9, escKeyCode = 27;
if ( escKeyCode === event.keyCode ) { if ( ! $textarea.data( 'next-tab-blurs' ) ) { $textarea.data( 'next-tab-blurs', true ); event.stopPropagation(); // Prevent collapsing the section. } return; }
// Short-circuit if tab key is not being pressed or if a modifier key *is* being pressed. if ( tabKeyCode !== event.keyCode || event.ctrlKey || event.altKey || event.shiftKey ) { return; }
// Prevent capturing Tab characters if Esc was pressed. if ( $textarea.data( 'next-tab-blurs' ) ) { return; }
selectionStart = textarea.selectionStart; selectionEnd = textarea.selectionEnd; value = textarea.value;
if ( ! control.setting ) { throw new Error( 'Missing setting' ); }
control.container.find( '.date-input' ).each( function() { var input = $( this ), component, element; component = input.data( 'component' ); element = new api.Element( input ); control.inputElements[ component ] = element; control.elements.push( element );
// Add invalid date error once user changes (and has blurred the input). input.on( 'change', function() { if ( control.invalidDate ) { control.notifications.add( new api.Notification( 'invalid_date', { message: api.l10n.invalidDate } ) ); } } );
// Remove the error immediately after validity change. input.on( 'input', _.debounce( function() { if ( ! control.invalidDate ) { control.notifications.remove( 'invalid_date' ); } } ) );
/** * Validates if input components have valid date and time. * * @since 4.9.0 * @return {boolean} If date input fields has error. */ validateInputs: function validateInputs() { var control = this, components, validityInput;
/** * Updates number of days according to the month and year selected. * * @since 4.9.0 * @return {void} */ updateDaysForMonth: function updateDaysForMonth() { var control = this, daysInMonth, year, month, day;
month = parseInt( control.inputElements.month(), 10 ); year = parseInt( control.inputElements.year(), 10 ); day = parseInt( control.inputElements.day(), 10 );
if ( month && year ) { daysInMonth = new Date( year, month, 0 ).getDate(); control.inputElements.day.element.attr( 'max', daysInMonth );
if ( day > daysInMonth ) { control.inputElements.day( String( daysInMonth ) ); } } },
/** * Populate setting value from the inputs. * * @since 4.9.0 * @return {boolean} If setting updated. */ populateSetting: function populateSetting() { var control = this, date;
/** * Check if the date is in the future. * * @since 4.9.0 * @return {boolean} True if future date. */ isFutureDate: function isFutureDate() { var control = this; return 0 < api.utils.getRemainingTime( control.convertInputDateToString() ); },
/** * Convert hour in twelve hour format to twenty four hour format. * * @since 4.9.0 * @param {string} hourInTwelveHourFormat - Hour in twelve hour format. * @param {string} meridian - Either 'am' or 'pm'. * @return {string} Hour in twenty four hour format. */ convertHourToTwentyFourHourFormat: function convertHour( hourInTwelveHourFormat, meridian ) { var hourInTwentyFourHourFormat, hour, midDayHour = 12;
/** * Populates date inputs in date fields. * * @since 4.9.0 * @return {boolean} Whether the inputs were populated. */ populateDateInputs: function populateDateInputs() { var control = this, parsed;
_.each( control.inputElements, function( element, component ) { var value = parsed[ component ]; // This will be zero-padded string.
// Set month and meridian regardless of focused state since they are dropdowns. if ( 'month' === component || 'meridian' === component ) {
// Options in dropdowns are not zero-padded. value = value.replace( /^0/, '' );
element.set( value ); } else {
value = parseInt( value, 10 ); if ( ! element.element.is( document.activeElement ) ) {
// Populate element with zero-padded value if not focused. element.set( parsed[ component ] ); } else if ( value !== parseInt( element(), 10 ) ) {
// Forcibly update the value if its underlying value changed, regardless of zero-padding. element.set( String( value ) ); } } } );
return true; },
/** * Toggle future date notification for date control. * * @since 4.9.0 * @param {boolean} notify Add or remove the notification. * @return {wp.customize.DateTimeControl} */ toggleFutureDateNotification: function toggleFutureDateNotification( notify ) { var control = this, notificationCode, notification;
/** * Change objects contained within the main customize object to Settings. * * @alias wp.customize.defaultConstructor */ api.defaultConstructor = api.Setting;
/** * Collection of all registered controls. * * @alias wp.customize.control * * @since 3.4.0 * * @type {Function} * @param {...string} ids - One or more ids for controls to obtain. * @param {deferredControlsCallback} [callback] - Function called when all supplied controls exist. * @return {wp.customize.Control|undefined|jQuery.promise} Control instance or undefined (if function called with one id param), * or promise resolving to requested controls. * * @example <caption>Loop over all registered controls.</caption> * wp.customize.control.each( function( control ) { ... } ); * * @example <caption>Getting `background_color` control instance.</caption> * control = wp.customize.control( 'background_color' ); * * @example <caption>Check if control exists.</caption> * hasControl = wp.customize.control.has( 'background_color' ); * * @example <caption>Deferred getting of `background_color` control until it exists, using callback.</caption> * wp.customize.control( 'background_color', function( control ) { ... } ); * * @example <caption>Get title and tagline controls when they both exist, using promise (only available when multiple IDs are present).</caption> * promise = wp.customize.control( 'blogname', 'blogdescription' ); * promise.done( function( titleControl, taglineControl ) { ... } ); * * @example <caption>Get title and tagline controls when they both exist, using callback.</caption> * wp.customize.control( 'blogname', 'blogdescription', function( titleControl, taglineControl ) { ... } ); * * @example <caption>Getting setting value for `background_color` control.</caption> * value = wp.customize.control( 'background_color ').setting.get(); * value = wp.customize( 'background_color' ).get(); // Same as above, since setting ID and control ID are the same. * * @example <caption>Add new control for site title.</caption> * wp.customize.control.add( new wp.customize.Control( 'other_blogname', { * setting: 'blogname', * type: 'text', * label: 'Site title', * section: 'other_site_identify' * } ) ); * * @example <caption>Remove control.</caption> * wp.customize.control.remove( 'other_blogname' ); * * @example <caption>Listen for control being added.</caption> * wp.customize.control.bind( 'add', function( addedControl ) { ... } ) * * @example <caption>Listen for control being removed.</caption> * wp.customize.control.bind( 'removed', function( removedControl ) { ... } ) */ api.control = new api.Values({ defaultConstructor: api.Control });
/** * Collection of all registered sections. * * @alias wp.customize.section * * @since 3.4.0 * * @type {Function} * @param {...string} ids - One or more ids for sections to obtain. * @param {deferredSectionsCallback} [callback] - Function called when all supplied sections exist. * @return {wp.customize.Section|undefined|jQuery.promise} Section instance or undefined (if function called with one id param), * or promise resolving to requested sections. * * @example <caption>Loop over all registered sections.</caption> * wp.customize.section.each( function( section ) { ... } ) * * @example <caption>Getting `title_tagline` section instance.</caption> * section = wp.customize.section( 'title_tagline' ) * * @example <caption>Expand dynamically-created section when it exists.</caption> * wp.customize.section( 'dynamically_created', function( section ) { * section.expand(); * } ); * * @see {@link wp.customize.control} for further examples of how to interact with {@link wp.customize.Values} instances. */ api.section = new api.Values({ defaultConstructor: api.Section });
/** * Collection of all registered panels. * * @alias wp.customize.panel * * @since 4.0.0 * * @type {Function} * @param {...string} ids - One or more ids for panels to obtain. * @param {deferredPanelsCallback} [callback] - Function called when all supplied panels exist. * @return {wp.customize.Panel|undefined|jQuery.promise} Panel instance or undefined (if function called with one id param), * or promise resolving to requested panels. * * @example <caption>Loop over all registered panels.</caption> * wp.customize.panel.each( function( panel ) { ... } ) * * @example <caption>Getting nav_menus panel instance.</caption> * panel = wp.customize.panel( 'nav_menus' ); * * @example <caption>Expand dynamically-created panel when it exists.</caption> * wp.customize.panel( 'dynamically_created', function( panel ) { * panel.expand(); * } ); * * @see {@link wp.customize.control} for further examples of how to interact with {@link wp.customize.Values} instances. */ api.panel = new api.Values({ defaultConstructor: api.Panel });
/** * Collection of all global notifications. * * @alias wp.customize.notifications * * @since 4.9.0 * * @type {Function} * @param {...string} codes - One or more codes for notifications to obtain. * @param {deferredNotificationsCallback} [callback] - Function called when all supplied notifications exist. * @return {wp.customize.Notification|undefined|jQuery.promise} Notification instance or undefined (if function called with one code param), * or promise resolving to requested notifications. * * @example <caption>Check if existing notification</caption> * exists = wp.customize.notifications.has( 'a_new_day_arrived' ); * * @example <caption>Obtain existing notification</caption> * notification = wp.customize.notifications( 'a_new_day_arrived' ); * * @example <caption>Obtain notification that may not exist yet.</caption> * wp.customize.notifications( 'a_new_day_arrived', function( notification ) { ... } ); * * @example <caption>Add a warning notification.</caption> * wp.customize.notifications.add( new wp.customize.Notification( 'midnight_almost_here', { * type: 'warning', * message: 'Midnight has almost arrived!', * dismissible: true * } ) ); * * @example <caption>Remove a notification.</caption> * wp.customize.notifications.remove( 'a_new_day_arrived' ); * * @see {@link wp.customize.control} for further examples of how to interact with {@link wp.customize.Values} instances. */ api.notifications = new api.Notifications();
api.PreviewFrame = api.Messenger.extend(/** @lends wp.customize.PreviewFrame.prototype */{ sensitivity: null, // Will get set to api.settings.timeouts.previewFrameSensitivity.
/** * An object that fetches a preview in the background of the document, which * allows for seamless replacement of an existing preview. * * @constructs wp.customize.PreviewFrame * @augments wp.customize.Messenger * * @param {Object} params.container * @param {Object} params.previewUrl * @param {Object} params.query * @param {Object} options */ initialize: function( params, options ) { var deferred = $.Deferred();
/* * Make the instance of the PreviewFrame the promise object * so other objects can easily interact with it. */ deferred.promise( this );
/* * Submit customized data in POST request to preview frame window since * there are setting value changes not yet written to changeset. */ if ( hasPendingChangesetUpdate ) { form = $( '<form>', { action: urlParser.href, target: previewFrame.iframe.attr( 'name' ), method: 'post', hidden: 'hidden' } ); form.append( $( '<input>', { type: 'hidden', name: '_method', value: 'GET' } ) ); _.each( previewFrame.query, function( value, key ) { form.append( $( '<input>', { type: 'hidden', name: key, value: value } ) ); } ); previewFrame.container.append( form ); form.trigger( 'submit' ); form.remove(); // No need to keep the form around after submitted. }
(function(){ var id = 0; /** * Return an incremented ID for a preview messenger channel. * * This function is named "uuid" for historical reasons, but it is a * misnomer as it is not an actual UUID, and it is not universally unique. * This is not to be confused with `api.settings.changeset.uuid`. * * @return {string} */ api.PreviewFrame.uuid = function() { return 'preview-' + String( id++ ); }; }());
/** * Set the document title of the customizer. * * @alias wp.customize.setDocumentTitle * * @since 4.1.0 * * @param {string} documentTitle */ api.setDocumentTitle = function ( documentTitle ) { var tmpl, title; tmpl = api.settings.documentTitleTmpl; title = tmpl.replace( '%s', documentTitle ); document.title = title; api.trigger( 'title', title ); };
api.Previewer = api.Messenger.extend(/** @lends wp.customize.Previewer.prototype */{ refreshBuffer: null, // Will get set to api.settings.timeouts.windowRefresh.
/** * @constructs wp.customize.Previewer * @augments wp.customize.Messenger * * @param {Array} params.allowedUrls * @param {string} params.container A selector or jQuery element for the preview * frame to be placed. * @param {string} params.form * @param {string} params.previewUrl The URL to preview. * @param {Object} options */ initialize: function( params, options ) { var previewer = this, urlParser = document.createElement( 'a' );
/* * Limit the URL to internal, front-end links. * * If the front end and the admin are served from the same domain, load the * preview over ssl if the Customizer is being loaded over ssl. This avoids * insecure content warnings. This is not attempted if the admin and front end * are on different domains to avoid the case where the front end doesn't have * ssl certs. */
previewer.add( 'previewUrl', params.previewUrl ).setter( function( to ) { var result = null, urlParser, queryParams, parsedAllowedUrl, parsedCandidateUrls = []; urlParser = document.createElement( 'a' ); urlParser.href = to;
// Abort if URL is for admin or (static) files in wp-includes or wp-content. if ( /\/wp-(admin|includes|content)(\/|$)/.test( urlParser.pathname ) ) { return null; }
// Set the previewUrl without causing the url to set the iframe. if ( data.currentUrl ) { previewer.previewUrl.unbind( previewer.refresh ); previewer.previewUrl.set( data.currentUrl ); previewer.previewUrl.bind( previewer.refresh ); }
/* * Walk over all panels, sections, and controls and set their * respective active states to true if the preview explicitly * indicates as such. */ constructs = { panel: data.activePanels, section: data.activeSections, control: data.activeControls }; _( constructs ).each( function ( activeConstructs, type ) { api[ type ].each( function ( construct, id ) { var isDynamicallyCreated = _.isUndefined( api.settings[ type + 's' ][ id ] );
/* * If the construct was created statically in PHP (not dynamically in JS) * then consider a missing (undefined) value in the activeConstructs to * mean it should be deactivated (since it is gone). But if it is * dynamically created then only toggle activation if the value is defined, * as this means that the construct was also then correspondingly * created statically in PHP and the active callback is available. * Otherwise, dynamically-created constructs should normally have * their active states toggled in JS rather than from PHP. */ if ( ! isDynamicallyCreated || ! _.isUndefined( activeConstructs[ id ] ) ) { if ( activeConstructs[ id ] ) { construct.activate(); } else { construct.deactivate(); } } } ); } );
/** * Keep the preview alive by listening for ready and keep-alive messages. * * If a message is not received in the allotted time then the iframe will be set back to the last known valid URL. * * @since 4.7.0 * @access public * * @return {void} */ keepPreviewAlive: function keepPreviewAlive() { var previewer = this, keepAliveTick, timeoutId, handleMissingKeepAlive, scheduleKeepAliveCheck;
/** * Schedule a preview keep-alive check. * * Note that if a page load takes longer than keepAliveCheck milliseconds, * the keep-alive messages will still be getting sent from the previous * URL. */ scheduleKeepAliveCheck = function() { timeoutId = setTimeout( handleMissingKeepAlive, api.settings.timeouts.keepAliveCheck ); };
/** * Set the previewerAlive state to true when receiving a message from the preview. */ keepAliveTick = function() { api.state( 'previewerAlive' ).set( true ); clearTimeout( timeoutId ); scheduleKeepAliveCheck(); };
/** * Set the previewerAlive state to false if keepAliveCheck milliseconds have transpired without a message. * * This is most likely to happen in the case of a connectivity error, or if the theme causes the browser * to navigate to a non-allowed URL. Setting this state to false will force settings with a postMessage * transport to use refresh instead, causing the preview frame also to be replaced with the current * allowed preview URL. */ handleMissingKeepAlive = function() { api.state( 'previewerAlive' ).set( false ); }; scheduleKeepAliveCheck();
// This event will be received directly by the previewer in normal navigation; this is only needed for seamless refresh. previewer.trigger( 'ready', readyData ); });
/** * Handle setting_validities in an error response for the customize-save request. * * Add notifications to the settings and focus on the first control that has an invalid setting. * * @alias wp.customize._handleSettingValidities * * @since 4.6.0 * @private * * @param {Object} args * @param {Object} args.settingValidities * @param {boolean} [args.focusInvalidControl=false] * @return {void} */ api._handleSettingValidities = function handleSettingValidities( args ) { var invalidSettingControls, invalidSettings = [], wasFocused = false;
// Find the controls that correspond to each invalid setting. _.each( args.settingValidities, function( validity, settingId ) { var setting = api( settingId ); if ( setting ) {
// Add notifications for invalidities. if ( _.isObject( validity ) ) { _.each( validity, function( params, code ) { var notification, existingNotification, needsReplacement = false; notification = new api.Notification( code, _.extend( { fromServer: true }, params ) );
// Remove existing notification if already exists for code but differs in parameters. existingNotification = setting.notifications( notification.code ); if ( existingNotification ) { needsReplacement = notification.type !== existingNotification.type || notification.message !== existingNotification.message || ! _.isEqual( notification.data, existingNotification.data ); } if ( needsReplacement ) { setting.notifications.remove( code ); }
// Sort the sections within each panel. api.panel.each( function ( panel ) { if ( 'themes' === panel.id ) { return; // Don't reflow theme sections, as doing so moves them after the themes container. }
// Sort the controls within each section. api.section.each( function ( section ) { var controls = section.controls(), controlContainers = _.pluck( controls, 'container' ); if ( ! section.panel() ) { rootNodes.push( section ); } appendContainer = ( section.contentContainer.is( 'ul' ) ) ? section.contentContainer : section.contentContainer.find( 'ul:first' ); if ( ! api.utils.areElementListsEqual( controlContainers, appendContainer.children( '[id]' ) ) ) { _( controls ).each( function ( control ) { appendContainer.append( control.container ); } ); wasReflowed = true; } } );
// Sort the root panels and sections. rootNodes.sort( api.utils.prioritySort ); rootHeadContainers = _.pluck( rootNodes, 'headContainer' ); appendContainer = $( '#customize-theme-controls .customize-pane-parent' ); // @todo This should be defined elsewhere, and to be configurable. if ( ! api.utils.areElementListsEqual( rootHeadContainers, appendContainer.children() ) ) { _( rootNodes ).each( function ( rootNode ) { appendContainer.append( rootNode.headContainer ); } ); wasReflowed = true; }
// Now re-trigger the active Value callbacks so that the panels and sections can decide whether they can be rendered. api.panel.each( function ( panel ) { var value = panel.active(); panel.active.callbacks.fireWith( panel.active, [ value, value ] ); } ); api.section.each( function ( section ) { var value = section.active(); section.active.callbacks.fireWith( section.active, [ value, value ] ); } );
// Restore focus if there was a reflow and there was an active (focused) element. if ( wasReflowed && activeElement ) { activeElement.trigger( 'focus' ); } api.trigger( 'pane-contents-reflowed' ); }, api );
// Define state values. api.state = new api.Values(); _.each( [ 'saved', 'saving', 'trashing', 'activated', 'processing', 'paneVisible', 'expandedPanel', 'expandedSection', 'changesetDate', 'selectedChangesetDate', 'changesetStatus', 'selectedChangesetStatus', 'remainingTimeToPublish', 'previewerAlive', 'editShortcutVisibility', 'changesetLocked', 'previewedDevice' ], function( name ) { api.state.create( name ); });
// Add publish settings section in JS instead of PHP since the Customizer depends on it to function. api.bind( 'ready', function() { api.section.add( new api.OuterSection( 'publish_settings', { title: api.l10n.publishSettings, priority: 0, active: api.settings.theme.active } ) ); } );
// Set up publish settings section and its controls. api.section( 'publish_settings', function( section ) { var updateButtonsState, trashControl, updateSectionActive, isSectionActive, statusControl, dateControl, toggleDateControl, publishWhenTime, pollInterval, updateTimeArrivedPoller, cancelScheduleButtonReminder, timeArrivedPollingInterval = 1000;
/** * Return whether the publish settings section should be active. * * @return {boolean} Is section active. */ isSectionActive = function() { if ( ! api.state( 'activated' ).get() ) { return false; } if ( api.state( 'trashing' ).get() || 'trash' === api.state( 'changesetStatus' ).get() ) { return false; } if ( '' === api.state( 'changesetStatus' ).get() && api.state( 'saved' ).get() ) { return false; } return true; };
// Make sure publish settings are not available while the theme is not active and the customizer is in a published state. section.active.validate = isSectionActive; updateSectionActive = function() { section.active.set( isSectionActive() ); }; api.state( 'activated' ).bind( updateSectionActive ); api.state( 'trashing' ).bind( updateSectionActive ); api.state( 'saved' ).bind( updateSectionActive ); api.state( 'changesetStatus' ).bind( updateSectionActive ); updateSectionActive();
// Bind visibility of the publish settings button to whether the section is active. updateButtonsState = function() { publishSettingsBtn.toggle( section.active.get() ); saveBtn.toggleClass( 'has-next-sibling', section.active.get() ); }; updateButtonsState(); section.active.bind( updateButtonsState );
function highlightScheduleButton() { if ( ! cancelScheduleButtonReminder ) { cancelScheduleButtonReminder = api.utils.highlightButton( btnWrapper, { delay: 1000,
/* * Only abort the reminder when the save button is focused. * If the user clicks the settings button to toggle the * settings closed, we'll still remind them. */ focusTarget: saveBtn } ); } } function cancelHighlightScheduleButton() { if ( cancelScheduleButtonReminder ) { cancelScheduleButtonReminder(); cancelScheduleButtonReminder = null; } } api.state( 'selectedChangesetStatus' ).bind( cancelHighlightScheduleButton );
// Ensure dateControl only appears when selected status is future. dateControl.active.validate = function() { return 'future' === api.state( 'selectedChangesetStatus' ).get(); }; toggleDateControl = function( value ) { dateControl.active.set( 'future' === value ); }; toggleDateControl( api.state( 'selectedChangesetStatus' ).get() ); api.state( 'selectedChangesetStatus' ).bind( toggleDateControl );
// Show notification on date control when status is future but it isn't a future date. api.state( 'saving' ).bind( function( isSaving ) { if ( isSaving && 'future' === api.state( 'selectedChangesetStatus' ).get() ) { dateControl.toggleFutureDateNotification( ! dateControl.isFutureDate() ); } } ); } );
// Prevent the form from saving when enter is pressed on an input or select element. $('#customize-controls').on( 'keydown', function( e ) { var isEnter = ( 13 === e.which ), $el = $( e.target );
/** * Build the query to send along with the Preview request. * * @since 3.4.0 * @since 4.7.0 Added options param. * @access public * * @param {Object} [options] Options. * @param {boolean} [options.excludeCustomizedSaved=false] Exclude saved settings in customized response (values pending writing to changeset). * @return {Object} Query vars. */ query: function( options ) { var queryVars = { wp_customize: 'on', customize_theme: api.settings.theme.stylesheet, nonce: this.nonce.preview, customize_changeset_uuid: api.settings.changeset.uuid }; if ( api.settings.changeset.autosaved || ! api.state( 'saved' ).get() ) { queryVars.customize_autosaved = 'on'; }
/* * Exclude customized data if requested especially for calls to requestChangesetUpdate. * Changeset updates are differential and so it is a performance waste to send all of * the dirty settings with each update. */ queryVars.customized = JSON.stringify( api.dirtyValues( { unsaved: options && options.excludeCustomizedSaved } ) );
return queryVars; },
/** * Save (and publish) the customizer changeset. * * Updates to the changeset are transactional. If any of the settings * are invalid then none of them will be written into the changeset. * A revision will be made for the changeset post if revisions support * has been added to the post type. * * @since 3.4.0 * @since 4.7.0 Added args param and return value. * * @param {Object} [args] Args. * @param {string} [args.status=publish] Status. * @param {string} [args.date] Date, in local time in MySQL format. * @param {string} [args.title] Title * @return {jQuery.promise} Promise. */ save: function( args ) { var previewer = this, deferred = $.Deferred(), changesetStatus = api.state( 'selectedChangesetStatus' ).get(), selectedChangesetDate = api.state( 'selectedChangesetDate' ).get(), processing = api.state( 'processing' ), submitWhenDoneProcessing, submit, modifiedWhileSaving = {}, invalidSettings = [], invalidControls = [], invalidSettingLessControls = [];
/* * Note that excludeCustomizedSaved is intentionally false so that the entire * set of customized data will be included if bypassed changeset update. */ query = $.extend( previewer.query( { excludeCustomizedSaved: false } ), { nonce: previewer.nonce.save, customize_changeset_status: changesetStatus } );
// Allow plugins to modify the params included with the save request. api.trigger( 'save-request-params', query );
/* * Note that the dirty customized values will have already been set in the * changeset and so technically query.customized could be deleted. However, * it is remaining here to make sure that any settings that got updated * quietly which may have not triggered an update request will also get * included in the values that get saved to the changeset. This will ensure * that values that get injected via the saved event will be included in * the changeset. This also ensures that setting values that were invalid * will get re-validated, perhaps in the case of settings that are invalid * due to dependencies on other settings. */ request = wp.ajax.post( 'customize_save', query ); api.state( 'processing' ).set( api.state( 'processing' ).get() + 1 );
// Remove notifications that were added due to save failures. api.notifications.each( function( notification ) { if ( notification.saveFailure ) { api.notifications.remove( notification.code ); } });
if ( '0' === response ) { response = 'not_logged_in'; } else if ( '-1' === response ) { // Back-compat in case any other check_ajax_referer() call is dying. response = 'invalid_nonce'; }
// Mark all published as clean if they haven't been modified during the request. api.each( function( setting ) { /* * Note that the setting revision will be undefined in the case of setting * values that are marked as dirty when the customizer is loaded, such as * when applying starter content. All other dirty settings will have an * associated revision due to their modification triggering a change event. */ if ( setting._dirty && ( _.isUndefined( api._latestSettingRevisions[ setting.id ] ) || api._latestSettingRevisions[ setting.id ] <= latestRevision ) ) { setting._dirty = false; } } );
// Prevent subsequent requestChangesetUpdate() calls from including the settings that have been saved. api._lastSavedRevision = Math.max( latestRevision, api._lastSavedRevision );
// Restore the global dirty state if any settings were modified during save. if ( ! _.isEmpty( modifiedWhileSaving ) ) { api.state( 'saved' ).set( false ); } } ); };
/** * Trash the current changes. * * Revert the Customizer to its previously-published state. * * @since 4.9.0 * * @return {jQuery.promise} Promise. */ trash: function trash() { var request, success, fail;
// Ensure preview nonce is included with every customized request, to allow post data to be read. $.ajaxPrefilter( function injectPreviewNonce( options ) { if ( ! /wp_customize=on/.test( options.data ) ) { return; } options.data += '&' + $.param({ customize_preview_nonce: api.settings.nonce.preview }); });
// Refresh the nonces if the preview sends updated nonces over. api.previewer.bind( 'nonce', function( nonce ) { $.extend( this.nonce, nonce ); });
// Create Panels. $.each( api.settings.panels, function ( id, data ) { var Constructor = api.panelConstructor[ data.type ] || api.Panel, options; // Inclusion of params alias is for back-compat for custom panels that expect to augment this property. options = _.extend( { params: data }, data ); api.panel.add( new Constructor( id, options ) ); });
// Create Sections. $.each( api.settings.sections, function ( id, data ) { var Constructor = api.sectionConstructor[ data.type ] || api.Section, options; // Inclusion of params alias is for back-compat for custom sections that expect to augment this property. options = _.extend( { params: data }, data ); api.section.add( new Constructor( id, options ) ); });
// Create Controls. $.each( api.settings.controls, function( id, data ) { var Constructor = api.controlConstructor[ data.type ] || api.Control, options; // Inclusion of params alias is for back-compat for custom controls that expect to augment this property. options = _.extend( { params: data }, data ); api.control.add( new Constructor( id, options ) ); });
// Focus the autofocused element. _.each( [ 'panel', 'section', 'control' ], function( type ) { var id = api.settings.autofocus[ type ]; if ( ! id ) { return; }
/* * Defer focus until: * 1. The panel, section, or control exists (especially for dynamically-created ones). * 2. The instance is embedded in the document (and so is focusable). * 3. The preview has finished loading so that the active states have been set. */ api[ type ]( id, function( instance ) { instance.deferred.embedded.done( function() { api.previewer.deferred.active.done( function() { instance.focus(); }); }); }); });
// Set up global notifications area. api.bind( 'ready', function setUpGlobalNotificationsArea() { var sidebar, containerHeight, containerInitialTop; api.notifications.container = $( '#customize-notifications-area' );
/* * Save (publish) button should be enabled if saving is not currently happening, * and if the theme is not active or the changeset exists but is not published. */ canSave = ! saving() && ! trashing() && ! changesetLocked() && ( ! activated() || ! saved() || ( changesetStatus() !== selectedChangesetStatus() && '' !== changesetStatus() ) || ( 'future' === selectedChangesetStatus() && changesetDate.get() !== selectedChangesetDate.get() ) );
saveBtn.prop( 'disabled', ! canSave ); });
selectedChangesetStatus.validate = function( status ) { if ( '' === status || 'auto-draft' === status ) { return null; } return status; };
/** * Add notification regarding the availability of an autosave to restore. * * @return {void} */ function addAutosaveRestoreNotification() { var code = 'autosave_available', onStateChange;
// Since there is an autosave revision and the user hasn't loaded with autosaved, add notification to prompt to load autosaved version. api.notifications.add( new api.Notification( code, { message: api.l10n.autosaveNotice, type: 'warning', dismissible: true, render: function() { var li = api.Notification.prototype.render.call( this ), link;
// Check if preview url is valid and load the preview frame. if ( api.previewer.previewUrl() ) { api.previewer.refresh(); } else { api.previewer.previewUrl( api.settings.url.home ); }
/* * Abort if the event target is not the body (the default) and not inside of #customize-controls. * This ensures that ESC meant to collapse a modal dialog or a TinyMCE toolbar won't collapse something else. */ if ( ! $( event.target ).is( 'body' ) && ! $.contains( $( '#customize-controls' )[0], event.target ) ) { return; }
// Abort if we're inside of a block editor instance. if ( event.target.closest( '.block-editor-writing-flow' ) !== null || event.target.closest( '.block-editor-block-list__block-popover' ) !== null ) { return; }
// Check for expanded expandable controls (e.g. widgets and nav menus items), sections, and panels. api.control.each( function( control ) { if ( control.expanded && control.expanded() && _.isFunction( control.collapse ) ) { expandedControls.push( control ); } }); api.section.each( function( section ) { if ( section.expanded() ) { expandedSections.push( section ); } }); api.panel.each( function( panel ) { if ( panel.expanded() ) { expandedPanels.push( panel ); } });
// Skip collapsing expanded controls if there are no expanded sections. if ( expandedControls.length > 0 && 0 === expandedSections.length ) { expandedControls.length = 0; }
// Collapse the most granular expanded object. collapsedObject = expandedControls[0] || expandedSections[0] || expandedPanels[0]; if ( collapsedObject ) { if ( 'themes' === collapsedObject.params.type ) {
// Themes panel or section. if ( body.hasClass( 'modal-open' ) ) { collapsedObject.closeDetails(); } else if ( api.panel.has( 'themes' ) ) {
// If we're collapsing a section, collapse the panel also. api.panel( 'themes' ).collapse(); } return; } collapsedObject.collapse(); event.preventDefault(); } });
if ( isInView && ! isSticky ) { // Header is in the view but is not yet sticky. if ( headerTop >= scrollTop ) { // Header is fully visible. headerElement .addClass( 'is-sticky' ) .css( { top: parentContainer.css( 'top' ), width: headerParent.outerWidth() + 'px' } ); } } else if ( maybeSticky && ! isInView ) { // Header is out of the view. headerElement .addClass( 'is-in-view' ) .css( 'top', ( scrollTop - headerHeight ) + 'px' ); headerParent.css( 'padding-top', headerHeight + 'px' ); } }; }());
// Previewed device bindings. (The api.previewedDevice property // is how this Value was first introduced, but since it has moved to api.state.) api.previewedDevice = api.state( 'previewedDevice' );
// Bind site title display to the corresponding field. if ( title.length ) { api( 'blogname', function( setting ) { var updateTitle = function() { var blogTitle = setting() || ''; title.text( blogTitle.toString().trim() || api.l10n.untitledBlogName ); }; setting.bind( updateTitle ); updateTitle(); } ); }
/* * Create a postMessage connection with a parent frame, * in case the Customizer frame was opened with the Customize loader. * * @see wp.customize.Loader */ parent = new api.Messenger({ url: api.settings.url.parent, channel: 'loader' });
// Handle exiting of Customizer. (function() { var isInsideIframe = false;
function isCleanState() { var defaultChangesetStatus;
/* * Handle special case of previewing theme switch since some settings (for nav menus and widgets) * are pre-dirty and non-active themes can only ever be auto-drafts. */ if ( ! api.state( 'activated' ).get() ) { return 0 === api._latestRevision; }
// Dirty if the changeset status has been changed but not saved yet. defaultChangesetStatus = api.state( 'changesetStatus' ).get(); if ( '' === defaultChangesetStatus || 'auto-draft' === defaultChangesetStatus ) { defaultChangesetStatus = 'publish'; } if ( api.state( 'selectedChangesetStatus' ).get() !== defaultChangesetStatus ) { return false; }
// Dirty if scheduled but the changeset date hasn't been saved yet. if ( 'future' === api.state( 'selectedChangesetStatus' ).get() && api.state( 'selectedChangesetDate' ).get() !== api.state( 'changesetDate' ).get() ) { return false; }
/* * If we receive a 'back' event, we're inside an iframe. * Send any clicks to the 'Return' link to the parent page. */ parent.bind( 'back', function() { isInsideIframe = true; });
// @todo These should actually toggle the active state, // but without the preview overriding the state in data.activeControls. toggleVisibility = function( preset ) { _.each( [ 'background_position', 'background_size', 'background_repeat', 'background_attachment' ], function( controlId, i ) { var control = api.control( controlId ); if ( control ) { control.container.toggle( visibility[ preset ][ i ] ); } } ); };
// Set up the section description behaviors. sectionReady.done( function setupSectionDescription( section ) { var control = api.control( 'custom_css' );
// Close the section description when clicking the close button. section.container.find( '.section-description-buttons .section-description-close' ).on( 'click', function() { section.container.find( '.section-meta .customize-section-description:first' ) .removeClass( 'open' ) .slideUp();
// Reveal help text if setting is empty. if ( control && ! control.setting.get() ) { section.container.find( '.section-meta .customize-section-description:first' ) .addClass( 'open' ) .show() .trigger( 'toggled' );
// Focus on the control that is associated with the given setting. api.previewer.bind( 'focus-control-for-setting', function( settingId ) { var matchedControls = []; api.control.each( function( control ) { var settingIds = _.pluck( control.settings, 'id' ); if ( -1 !== _.indexOf( settingIds, settingId ) ) { matchedControls.push( control ); } } );
// Focus on the matched control with the lowest priority (appearing higher). if ( matchedControls.length ) { matchedControls.sort( function( a, b ) { return a.priority() - b.priority(); } ); matchedControls[0].focus(); } } );
// Refresh the preview when it requests. api.previewer.bind( 'refresh', function() { api.previewer.refresh(); });