2018-06-27 22:30:15 -04:00
|
|
|
/**
|
|
|
|
* @output wp-includes/js/customize-preview-widgets.js
|
|
|
|
*/
|
|
|
|
|
2016-02-19 13:41:28 -05:00
|
|
|
/* global _wpWidgetCustomizerPreviewSettings */
|
2017-09-08 14:42:49 -04:00
|
|
|
|
2017-12-21 10:12:49 -05:00
|
|
|
/**
|
|
|
|
* Handles the initialization, refreshing and rendering of widget partials and sidebar widgets.
|
|
|
|
*
|
|
|
|
* @since 4.5.0
|
|
|
|
*
|
|
|
|
* @namespace wp.customize.widgetsPreview
|
|
|
|
*
|
|
|
|
* @param {jQuery} $ The jQuery object.
|
|
|
|
* @param {Object} _ The utilities library.
|
|
|
|
* @param {Object} wp Current WordPress environment instance.
|
|
|
|
* @param {Object} api Information from the API.
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {Object} Widget-related variables.
|
2017-12-21 10:12:49 -05:00
|
|
|
*/
|
2016-02-19 13:41:28 -05:00
|
|
|
wp.customize.widgetsPreview = wp.customize.WidgetCustomizerPreview = (function( $, _, wp, api ) {
|
2014-03-22 17:04:15 -04:00
|
|
|
|
2016-02-19 13:41:28 -05:00
|
|
|
var self;
|
2014-03-22 17:04:15 -04:00
|
|
|
|
2016-02-19 13:41:28 -05:00
|
|
|
self = {
|
|
|
|
renderedSidebars: {},
|
|
|
|
renderedWidgets: {},
|
|
|
|
registeredSidebars: [],
|
|
|
|
registeredWidgets: {},
|
|
|
|
widgetSelectors: [],
|
|
|
|
preview: null,
|
|
|
|
l10n: {
|
|
|
|
widgetTooltip: ''
|
2016-03-21 17:59:29 -04:00
|
|
|
},
|
|
|
|
selectiveRefreshableWidgets: {}
|
2016-02-19 13:41:28 -05:00
|
|
|
};
|
2014-03-22 17:04:15 -04:00
|
|
|
|
|
|
|
/**
|
2017-12-21 10:12:49 -05:00
|
|
|
* Initializes the widgets preview.
|
2014-03-22 17:04:15 -04:00
|
|
|
*
|
2016-02-19 13:41:28 -05:00
|
|
|
* @since 4.5.0
|
2017-12-21 10:12:49 -05:00
|
|
|
*
|
|
|
|
* @memberOf wp.customize.widgetsPreview
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {void}
|
2014-03-22 17:04:15 -04:00
|
|
|
*/
|
2016-02-19 13:41:28 -05:00
|
|
|
self.init = function() {
|
|
|
|
var self = this;
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2016-02-19 13:41:28 -05:00
|
|
|
self.preview = api.preview;
|
2016-03-21 17:59:29 -04:00
|
|
|
if ( ! _.isEmpty( self.selectiveRefreshableWidgets ) ) {
|
2016-02-19 13:41:28 -05:00
|
|
|
self.addPartials();
|
|
|
|
}
|
2015-12-06 13:39:25 -05:00
|
|
|
|
2016-02-19 13:41:28 -05:00
|
|
|
self.buildWidgetSelectors();
|
|
|
|
self.highlightControls();
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2016-02-19 13:41:28 -05:00
|
|
|
self.preview.bind( 'highlight-widget', self.highlightWidget );
|
|
|
|
|
|
|
|
api.preview.bind( 'active', function() {
|
|
|
|
self.highlightControls();
|
|
|
|
} );
|
2017-10-16 02:46:48 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Refresh a partial when the controls pane requests it. This is used currently just by the
|
|
|
|
* Gallery widget so that when an attachment's caption is updated in the media modal,
|
|
|
|
* the widget in the preview will then be refreshed to show the change. Normally doing this
|
|
|
|
* would not be necessary because all of the state should be contained inside the changeset,
|
|
|
|
* as everything done in the Customizer should not make a change to the site unless the
|
|
|
|
* changeset itself is published. Attachments are a current exception to this rule.
|
|
|
|
* For a proposal to include attachments in the customized state, see #37887.
|
|
|
|
*/
|
|
|
|
api.preview.bind( 'refresh-widget-partial', function( widgetId ) {
|
|
|
|
var partialId = 'widget[' + widgetId + ']';
|
|
|
|
if ( api.selectiveRefresh.partial.has( partialId ) ) {
|
|
|
|
api.selectiveRefresh.partial( partialId ).refresh();
|
|
|
|
} else if ( self.renderedWidgets[ widgetId ] ) {
|
|
|
|
api.preview.send( 'refresh' ); // Fallback in case theme does not support 'customize-selective-refresh-widgets'.
|
|
|
|
}
|
|
|
|
} );
|
2016-02-19 13:41:28 -05:00
|
|
|
};
|
|
|
|
|
2017-09-08 14:42:49 -04:00
|
|
|
self.WidgetPartial = api.selectiveRefresh.Partial.extend(/** @lends wp.customize.widgetsPreview.WidgetPartial.prototype */{
|
2014-03-05 15:41:14 -05:00
|
|
|
|
|
|
|
/**
|
2017-12-21 10:12:49 -05:00
|
|
|
* Represents a partial widget instance.
|
2016-02-19 13:41:28 -05:00
|
|
|
*
|
|
|
|
* @since 4.5.0
|
2017-12-21 10:12:49 -05:00
|
|
|
*
|
|
|
|
* @constructs
|
|
|
|
* @augments wp.customize.selectiveRefresh.Partial
|
|
|
|
*
|
|
|
|
* @alias wp.customize.widgetsPreview.WidgetPartial
|
|
|
|
* @memberOf wp.customize.widgetsPreview
|
|
|
|
*
|
|
|
|
* @param {string} id The partial's ID.
|
|
|
|
* @param {Object} options Options used to initialize the partial's
|
|
|
|
* instance.
|
|
|
|
* @param {Object} options.params The options parameters.
|
2014-03-05 15:41:14 -05:00
|
|
|
*/
|
2016-03-21 17:59:29 -04:00
|
|
|
initialize: function( id, options ) {
|
|
|
|
var partial = this, matches;
|
|
|
|
matches = id.match( /^widget\[(.+)]$/ );
|
|
|
|
if ( ! matches ) {
|
|
|
|
throw new Error( 'Illegal id for widget partial.' );
|
|
|
|
}
|
2014-04-13 12:41:15 -04:00
|
|
|
|
2016-03-21 17:59:29 -04:00
|
|
|
partial.widgetId = matches[1];
|
|
|
|
partial.widgetIdParts = self.parseWidgetId( partial.widgetId );
|
|
|
|
options = options || {};
|
|
|
|
options.params = _.extend(
|
|
|
|
{
|
|
|
|
settings: [ self.getWidgetSettingId( partial.widgetId ) ],
|
|
|
|
containerInclusive: true
|
|
|
|
},
|
|
|
|
options.params || {}
|
|
|
|
);
|
2016-02-19 13:41:28 -05:00
|
|
|
|
2016-03-21 17:59:29 -04:00
|
|
|
api.selectiveRefresh.Partial.prototype.initialize.call( partial, id, options );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2017-12-21 10:12:49 -05:00
|
|
|
* Refreshes the widget partial.
|
2016-03-21 17:59:29 -04:00
|
|
|
*
|
2017-12-21 10:12:49 -05:00
|
|
|
* @since 4.5.0
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {Promise|void} Either a promise postponing the refresh, or void.
|
2016-03-21 17:59:29 -04:00
|
|
|
*/
|
|
|
|
refresh: function() {
|
|
|
|
var partial = this, refreshDeferred;
|
|
|
|
if ( ! self.selectiveRefreshableWidgets[ partial.widgetIdParts.idBase ] ) {
|
|
|
|
refreshDeferred = $.Deferred();
|
|
|
|
refreshDeferred.reject();
|
|
|
|
partial.fallback();
|
|
|
|
return refreshDeferred.promise();
|
|
|
|
} else {
|
|
|
|
return api.selectiveRefresh.Partial.prototype.refresh.call( partial );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2017-12-21 10:12:49 -05:00
|
|
|
* Sends the widget-updated message to the parent so the spinner will get
|
|
|
|
* removed from the widget control.
|
|
|
|
*
|
|
|
|
* @inheritDoc
|
|
|
|
* @param {wp.customize.selectiveRefresh.Placement} placement The placement
|
|
|
|
* function.
|
2016-03-21 17:59:29 -04:00
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {void}
|
2016-03-21 17:59:29 -04:00
|
|
|
*/
|
|
|
|
renderContent: function( placement ) {
|
|
|
|
var partial = this;
|
|
|
|
if ( api.selectiveRefresh.Partial.prototype.renderContent.call( partial, placement ) ) {
|
|
|
|
api.preview.send( 'widget-updated', partial.widgetId );
|
|
|
|
api.selectiveRefresh.trigger( 'widget-updated', partial );
|
2016-02-19 13:41:28 -05:00
|
|
|
}
|
2016-03-21 17:59:29 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-09-08 14:42:49 -04:00
|
|
|
self.SidebarPartial = api.selectiveRefresh.Partial.extend(/** @lends wp.customize.widgetsPreview.SidebarPartial.prototype */{
|
2014-03-05 15:41:14 -05:00
|
|
|
|
|
|
|
/**
|
2017-12-21 10:12:49 -05:00
|
|
|
* Represents a partial widget area.
|
2014-03-05 15:41:14 -05:00
|
|
|
*
|
2016-02-19 13:41:28 -05:00
|
|
|
* @since 4.5.0
|
2017-12-21 10:12:49 -05:00
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @augments wp.customize.selectiveRefresh.Partial
|
|
|
|
*
|
|
|
|
* @memberOf wp.customize.widgetsPreview
|
|
|
|
* @alias wp.customize.widgetsPreview.SidebarPartial
|
|
|
|
*
|
|
|
|
* @param {string} id The partial's ID.
|
|
|
|
* @param {Object} options Options used to initialize the partial's instance.
|
|
|
|
* @param {Object} options.params The options parameters.
|
2014-03-05 15:41:14 -05:00
|
|
|
*/
|
2016-03-21 17:59:29 -04:00
|
|
|
initialize: function( id, options ) {
|
|
|
|
var partial = this, matches;
|
|
|
|
matches = id.match( /^sidebar\[(.+)]$/ );
|
|
|
|
if ( ! matches ) {
|
|
|
|
throw new Error( 'Illegal id for sidebar partial.' );
|
|
|
|
}
|
|
|
|
partial.sidebarId = matches[1];
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2016-03-21 17:59:29 -04:00
|
|
|
options = options || {};
|
|
|
|
options.params = _.extend(
|
|
|
|
{
|
|
|
|
settings: [ 'sidebars_widgets[' + partial.sidebarId + ']' ]
|
|
|
|
},
|
|
|
|
options.params || {}
|
|
|
|
);
|
2016-02-19 13:41:28 -05:00
|
|
|
|
2016-03-21 17:59:29 -04:00
|
|
|
api.selectiveRefresh.Partial.prototype.initialize.call( partial, id, options );
|
2016-02-19 13:41:28 -05:00
|
|
|
|
2016-03-21 17:59:29 -04:00
|
|
|
if ( ! partial.params.sidebarArgs ) {
|
|
|
|
throw new Error( 'The sidebarArgs param was not provided.' );
|
|
|
|
}
|
|
|
|
if ( partial.params.settings.length > 1 ) {
|
|
|
|
throw new Error( 'Expected SidebarPartial to only have one associated setting' );
|
|
|
|
}
|
|
|
|
},
|
2016-02-19 13:41:28 -05:00
|
|
|
|
2016-03-21 17:59:29 -04:00
|
|
|
/**
|
2017-12-21 10:12:49 -05:00
|
|
|
* Sets up the partial.
|
2016-03-21 17:59:29 -04:00
|
|
|
*
|
|
|
|
* @since 4.5.0
|
2017-12-21 10:12:49 -05:00
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {void}
|
2016-03-21 17:59:29 -04:00
|
|
|
*/
|
|
|
|
ready: function() {
|
|
|
|
var sidebarPartial = this;
|
|
|
|
|
|
|
|
// Watch for changes to the sidebar_widgets setting.
|
|
|
|
_.each( sidebarPartial.settings(), function( settingId ) {
|
|
|
|
api( settingId ).bind( _.bind( sidebarPartial.handleSettingChange, sidebarPartial ) );
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Trigger an event for this sidebar being updated whenever a widget inside is rendered.
|
|
|
|
api.selectiveRefresh.bind( 'partial-content-rendered', function( placement ) {
|
|
|
|
var isAssignedWidgetPartial = (
|
|
|
|
placement.partial.extended( self.WidgetPartial ) &&
|
|
|
|
( -1 !== _.indexOf( sidebarPartial.getWidgetIds(), placement.partial.widgetId ) )
|
|
|
|
);
|
|
|
|
if ( isAssignedWidgetPartial ) {
|
|
|
|
api.selectiveRefresh.trigger( 'sidebar-updated', sidebarPartial );
|
2016-02-19 13:41:28 -05:00
|
|
|
}
|
2016-03-21 17:59:29 -04:00
|
|
|
} );
|
|
|
|
|
|
|
|
// Make sure that a widget partial has a container in the DOM prior to a refresh.
|
|
|
|
api.bind( 'change', function( widgetSetting ) {
|
|
|
|
var widgetId, parsedId;
|
|
|
|
parsedId = self.parseWidgetSettingId( widgetSetting.id );
|
|
|
|
if ( ! parsedId ) {
|
|
|
|
return;
|
2016-02-19 13:41:28 -05:00
|
|
|
}
|
2016-03-21 17:59:29 -04:00
|
|
|
widgetId = parsedId.idBase;
|
|
|
|
if ( parsedId.number ) {
|
|
|
|
widgetId += '-' + String( parsedId.number );
|
2016-02-19 13:41:28 -05:00
|
|
|
}
|
2016-03-21 17:59:29 -04:00
|
|
|
if ( -1 !== _.indexOf( sidebarPartial.getWidgetIds(), widgetId ) ) {
|
|
|
|
sidebarPartial.ensureWidgetPlacementContainers( widgetId );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2017-12-21 10:12:49 -05:00
|
|
|
* Gets the before/after boundary nodes for all instances of this sidebar
|
|
|
|
* (usually one).
|
2016-03-21 17:59:29 -04:00
|
|
|
*
|
|
|
|
* Note that TreeWalker is not implemented in IE8.
|
|
|
|
*
|
|
|
|
* @since 4.5.0
|
2017-12-21 10:12:49 -05:00
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {Array.<{before: Comment, after: Comment, instanceNumber: number}>}
|
|
|
|
* An array with an object for each sidebar instance, containing the
|
|
|
|
* node before and after the sidebar instance and its instance number.
|
2016-03-21 17:59:29 -04:00
|
|
|
*/
|
|
|
|
findDynamicSidebarBoundaryNodes: function() {
|
|
|
|
var partial = this, regExp, boundaryNodes = {}, recursiveCommentTraversal;
|
|
|
|
regExp = /^(dynamic_sidebar_before|dynamic_sidebar_after):(.+):(\d+)$/;
|
|
|
|
recursiveCommentTraversal = function( childNodes ) {
|
|
|
|
_.each( childNodes, function( node ) {
|
|
|
|
var matches;
|
|
|
|
if ( 8 === node.nodeType ) {
|
|
|
|
matches = node.nodeValue.match( regExp );
|
|
|
|
if ( ! matches || matches[2] !== partial.sidebarId ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( _.isUndefined( boundaryNodes[ matches[3] ] ) ) {
|
|
|
|
boundaryNodes[ matches[3] ] = {
|
|
|
|
before: null,
|
|
|
|
after: null,
|
|
|
|
instanceNumber: parseInt( matches[3], 10 )
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if ( 'dynamic_sidebar_before' === matches[1] ) {
|
|
|
|
boundaryNodes[ matches[3] ].before = node;
|
|
|
|
} else {
|
|
|
|
boundaryNodes[ matches[3] ].after = node;
|
|
|
|
}
|
|
|
|
} else if ( 1 === node.nodeType ) {
|
|
|
|
recursiveCommentTraversal( node.childNodes );
|
2016-02-19 13:41:28 -05:00
|
|
|
}
|
|
|
|
} );
|
2016-03-21 17:59:29 -04:00
|
|
|
};
|
2016-02-19 13:41:28 -05:00
|
|
|
|
2016-03-21 17:59:29 -04:00
|
|
|
recursiveCommentTraversal( document.body.childNodes );
|
|
|
|
return _.values( boundaryNodes );
|
|
|
|
},
|
2016-02-19 13:41:28 -05:00
|
|
|
|
2016-03-21 17:59:29 -04:00
|
|
|
/**
|
2017-12-21 10:12:49 -05:00
|
|
|
* Gets the placements for this partial.
|
2016-03-21 17:59:29 -04:00
|
|
|
*
|
|
|
|
* @since 4.5.0
|
2017-12-21 10:12:49 -05:00
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {Array} An array containing placement objects for each of the
|
|
|
|
* dynamic sidebar boundary nodes.
|
2016-03-21 17:59:29 -04:00
|
|
|
*/
|
|
|
|
placements: function() {
|
|
|
|
var partial = this;
|
|
|
|
return _.map( partial.findDynamicSidebarBoundaryNodes(), function( boundaryNodes ) {
|
|
|
|
return new api.selectiveRefresh.Placement( {
|
|
|
|
partial: partial,
|
|
|
|
container: null,
|
|
|
|
startNode: boundaryNodes.before,
|
|
|
|
endNode: boundaryNodes.after,
|
|
|
|
context: {
|
|
|
|
instanceNumber: boundaryNodes.instanceNumber
|
2016-02-19 13:41:28 -05:00
|
|
|
}
|
|
|
|
} );
|
2016-03-21 17:59:29 -04:00
|
|
|
} );
|
|
|
|
},
|
2016-02-19 13:41:28 -05:00
|
|
|
|
2016-03-21 17:59:29 -04:00
|
|
|
/**
|
|
|
|
* Get the list of widget IDs associated with this widget area.
|
|
|
|
*
|
|
|
|
* @since 4.5.0
|
|
|
|
*
|
2017-12-21 10:12:49 -05:00
|
|
|
* @throws {Error} If there's no settingId.
|
|
|
|
* @throws {Error} If the setting doesn't exist in the API.
|
|
|
|
* @throws {Error} If the API doesn't pass an array of widget ids.
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {Array} A shallow copy of the array containing widget IDs.
|
2016-03-21 17:59:29 -04:00
|
|
|
*/
|
|
|
|
getWidgetIds: function() {
|
|
|
|
var sidebarPartial = this, settingId, widgetIds;
|
|
|
|
settingId = sidebarPartial.settings()[0];
|
|
|
|
if ( ! settingId ) {
|
|
|
|
throw new Error( 'Missing associated setting.' );
|
|
|
|
}
|
|
|
|
if ( ! api.has( settingId ) ) {
|
|
|
|
throw new Error( 'Setting does not exist.' );
|
|
|
|
}
|
|
|
|
widgetIds = api( settingId ).get();
|
|
|
|
if ( ! _.isArray( widgetIds ) ) {
|
|
|
|
throw new Error( 'Expected setting to be array of widget IDs' );
|
|
|
|
}
|
|
|
|
return widgetIds.slice( 0 );
|
|
|
|
},
|
2016-02-19 13:41:28 -05:00
|
|
|
|
2016-03-21 17:59:29 -04:00
|
|
|
/**
|
2017-12-21 10:12:49 -05:00
|
|
|
* Reflows widgets in the sidebar, ensuring they have the proper position in the
|
|
|
|
* DOM.
|
2016-03-21 17:59:29 -04:00
|
|
|
*
|
|
|
|
* @since 4.5.0
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {Array.<wp.customize.selectiveRefresh.Placement>} List of placements
|
|
|
|
* that were reflowed.
|
2016-03-21 17:59:29 -04:00
|
|
|
*/
|
|
|
|
reflowWidgets: function() {
|
|
|
|
var sidebarPartial = this, sidebarPlacements, widgetIds, widgetPartials, sortedSidebarContainers = [];
|
|
|
|
widgetIds = sidebarPartial.getWidgetIds();
|
|
|
|
sidebarPlacements = sidebarPartial.placements();
|
|
|
|
|
|
|
|
widgetPartials = {};
|
|
|
|
_.each( widgetIds, function( widgetId ) {
|
|
|
|
var widgetPartial = api.selectiveRefresh.partial( 'widget[' + widgetId + ']' );
|
|
|
|
if ( widgetPartial ) {
|
|
|
|
widgetPartials[ widgetId ] = widgetPartial;
|
2016-02-19 13:41:28 -05:00
|
|
|
}
|
2016-03-21 17:59:29 -04:00
|
|
|
} );
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2016-03-21 17:59:29 -04:00
|
|
|
_.each( sidebarPlacements, function( sidebarPlacement ) {
|
|
|
|
var sidebarWidgets = [], needsSort = false, thisPosition, lastPosition = -1;
|
|
|
|
|
|
|
|
// Gather list of widget partial containers in this sidebar, and determine if a sort is needed.
|
|
|
|
_.each( widgetPartials, function( widgetPartial ) {
|
|
|
|
_.each( widgetPartial.placements(), function( widgetPlacement ) {
|
|
|
|
|
|
|
|
if ( sidebarPlacement.context.instanceNumber === widgetPlacement.context.sidebar_instance_number ) {
|
|
|
|
thisPosition = widgetPlacement.container.index();
|
|
|
|
sidebarWidgets.push( {
|
|
|
|
partial: widgetPartial,
|
|
|
|
placement: widgetPlacement,
|
|
|
|
position: thisPosition
|
|
|
|
} );
|
|
|
|
if ( thisPosition < lastPosition ) {
|
|
|
|
needsSort = true;
|
|
|
|
}
|
|
|
|
lastPosition = thisPosition;
|
|
|
|
}
|
2016-02-19 13:41:28 -05:00
|
|
|
} );
|
2016-03-21 17:59:29 -04:00
|
|
|
} );
|
2014-03-05 15:41:14 -05:00
|
|
|
|
2016-03-21 17:59:29 -04:00
|
|
|
if ( needsSort ) {
|
|
|
|
_.each( sidebarWidgets, function( sidebarWidget ) {
|
|
|
|
sidebarPlacement.endNode.parentNode.insertBefore(
|
|
|
|
sidebarWidget.placement.container[0],
|
|
|
|
sidebarPlacement.endNode
|
|
|
|
);
|
2016-02-19 13:41:28 -05:00
|
|
|
|
2016-03-21 17:59:29 -04:00
|
|
|
// @todo Rename partial-placement-moved?
|
|
|
|
api.selectiveRefresh.trigger( 'partial-content-moved', sidebarWidget.placement );
|
|
|
|
} );
|
2016-02-19 13:41:28 -05:00
|
|
|
|
2016-03-21 17:59:29 -04:00
|
|
|
sortedSidebarContainers.push( sidebarPlacement );
|
2016-02-19 13:41:28 -05:00
|
|
|
}
|
2016-03-21 17:59:29 -04:00
|
|
|
} );
|
2016-02-19 13:41:28 -05:00
|
|
|
|
2016-03-21 17:59:29 -04:00
|
|
|
if ( sortedSidebarContainers.length > 0 ) {
|
|
|
|
api.selectiveRefresh.trigger( 'sidebar-updated', sidebarPartial );
|
|
|
|
}
|
2014-04-01 11:26:15 -04:00
|
|
|
|
2016-03-21 17:59:29 -04:00
|
|
|
return sortedSidebarContainers;
|
|
|
|
},
|
2016-02-19 13:41:28 -05:00
|
|
|
|
2016-03-21 17:59:29 -04:00
|
|
|
/**
|
2017-12-21 10:12:49 -05:00
|
|
|
* Makes sure there is a widget instance container in this sidebar for the given
|
|
|
|
* widget ID.
|
2016-03-21 17:59:29 -04:00
|
|
|
*
|
|
|
|
* @since 4.5.0
|
|
|
|
*
|
2017-12-21 10:12:49 -05:00
|
|
|
* @param {string} widgetId The widget ID.
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {wp.customize.selectiveRefresh.Partial} The widget instance partial.
|
2016-03-21 17:59:29 -04:00
|
|
|
*/
|
|
|
|
ensureWidgetPlacementContainers: function( widgetId ) {
|
|
|
|
var sidebarPartial = this, widgetPartial, wasInserted = false, partialId = 'widget[' + widgetId + ']';
|
|
|
|
widgetPartial = api.selectiveRefresh.partial( partialId );
|
|
|
|
if ( ! widgetPartial ) {
|
|
|
|
widgetPartial = new self.WidgetPartial( partialId, {
|
|
|
|
params: {}
|
2016-02-19 13:41:28 -05:00
|
|
|
} );
|
2016-03-21 17:59:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that there is a container element for the widget in the sidebar, if at least a placeholder.
|
|
|
|
_.each( sidebarPartial.placements(), function( sidebarPlacement ) {
|
|
|
|
var foundWidgetPlacement, widgetContainerElement;
|
2016-02-19 13:41:28 -05:00
|
|
|
|
2016-03-21 17:59:29 -04:00
|
|
|
foundWidgetPlacement = _.find( widgetPartial.placements(), function( widgetPlacement ) {
|
|
|
|
return ( widgetPlacement.context.sidebar_instance_number === sidebarPlacement.context.instanceNumber );
|
2016-02-19 13:41:28 -05:00
|
|
|
} );
|
2016-03-21 17:59:29 -04:00
|
|
|
if ( foundWidgetPlacement ) {
|
|
|
|
return;
|
|
|
|
}
|
2016-02-19 13:41:28 -05:00
|
|
|
|
2016-03-21 17:59:29 -04:00
|
|
|
widgetContainerElement = $(
|
2016-04-28 14:20:29 -04:00
|
|
|
sidebarPartial.params.sidebarArgs.before_widget.replace( /%1\$s/g, widgetId ).replace( /%2\$s/g, 'widget' ) +
|
2016-03-21 17:59:29 -04:00
|
|
|
sidebarPartial.params.sidebarArgs.after_widget
|
|
|
|
);
|
|
|
|
|
2016-07-27 12:27:30 -04:00
|
|
|
// Handle rare case where before_widget and after_widget are empty.
|
|
|
|
if ( ! widgetContainerElement[0] ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-21 17:59:29 -04:00
|
|
|
widgetContainerElement.attr( 'data-customize-partial-id', widgetPartial.id );
|
|
|
|
widgetContainerElement.attr( 'data-customize-partial-type', 'widget' );
|
|
|
|
widgetContainerElement.attr( 'data-customize-widget-id', widgetId );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure the widget container element has the customize-container context data.
|
|
|
|
* The sidebar_instance_number is used to disambiguate multiple instances of the
|
|
|
|
* same sidebar are rendered onto the template, and so the same widget is embedded
|
|
|
|
* multiple times.
|
|
|
|
*/
|
|
|
|
widgetContainerElement.data( 'customize-partial-placement-context', {
|
|
|
|
'sidebar_id': sidebarPartial.sidebarId,
|
|
|
|
'sidebar_instance_number': sidebarPlacement.context.instanceNumber
|
2016-02-19 13:41:28 -05:00
|
|
|
} );
|
|
|
|
|
2016-03-21 17:59:29 -04:00
|
|
|
sidebarPlacement.endNode.parentNode.insertBefore( widgetContainerElement[0], sidebarPlacement.endNode );
|
|
|
|
wasInserted = true;
|
|
|
|
} );
|
2016-02-19 13:41:28 -05:00
|
|
|
|
2017-10-04 02:48:46 -04:00
|
|
|
api.selectiveRefresh.partial.add( widgetPartial );
|
2016-10-26 16:03:32 -04:00
|
|
|
|
2016-03-21 17:59:29 -04:00
|
|
|
if ( wasInserted ) {
|
|
|
|
sidebarPartial.reflowWidgets();
|
2016-02-19 13:41:28 -05:00
|
|
|
}
|
|
|
|
|
2016-03-21 17:59:29 -04:00
|
|
|
return widgetPartial;
|
|
|
|
},
|
2016-02-19 13:41:28 -05:00
|
|
|
|
|
|
|
/**
|
2017-12-21 10:12:49 -05:00
|
|
|
* Handles changes to the sidebars_widgets[] setting.
|
2016-02-19 13:41:28 -05:00
|
|
|
*
|
|
|
|
* @since 4.5.0
|
2016-03-21 17:59:29 -04:00
|
|
|
*
|
2017-12-21 10:12:49 -05:00
|
|
|
* @param {Array} newWidgetIds New widget IDs.
|
|
|
|
* @param {Array} oldWidgetIds Old widget IDs.
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {void}
|
2016-02-19 13:41:28 -05:00
|
|
|
*/
|
2016-03-21 17:59:29 -04:00
|
|
|
handleSettingChange: function( newWidgetIds, oldWidgetIds ) {
|
|
|
|
var sidebarPartial = this, needsRefresh, widgetsRemoved, widgetsAdded, addedWidgetPartials = [];
|
|
|
|
|
|
|
|
needsRefresh = (
|
|
|
|
( oldWidgetIds.length > 0 && 0 === newWidgetIds.length ) ||
|
|
|
|
( newWidgetIds.length > 0 && 0 === oldWidgetIds.length )
|
|
|
|
);
|
|
|
|
if ( needsRefresh ) {
|
|
|
|
sidebarPartial.fallback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle removal of widgets.
|
|
|
|
widgetsRemoved = _.difference( oldWidgetIds, newWidgetIds );
|
|
|
|
_.each( widgetsRemoved, function( removedWidgetId ) {
|
|
|
|
var widgetPartial = api.selectiveRefresh.partial( 'widget[' + removedWidgetId + ']' );
|
|
|
|
if ( widgetPartial ) {
|
|
|
|
_.each( widgetPartial.placements(), function( placement ) {
|
|
|
|
var isRemoved = (
|
|
|
|
placement.context.sidebar_id === sidebarPartial.sidebarId ||
|
|
|
|
( placement.context.sidebar_args && placement.context.sidebar_args.id === sidebarPartial.sidebarId )
|
|
|
|
);
|
|
|
|
if ( isRemoved ) {
|
|
|
|
placement.container.remove();
|
2016-02-19 13:41:28 -05:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
2017-10-16 02:46:48 -04:00
|
|
|
delete self.renderedWidgets[ removedWidgetId ];
|
2016-02-19 13:41:28 -05:00
|
|
|
} );
|
|
|
|
|
2016-03-21 17:59:29 -04:00
|
|
|
// Handle insertion of widgets.
|
|
|
|
widgetsAdded = _.difference( newWidgetIds, oldWidgetIds );
|
|
|
|
_.each( widgetsAdded, function( addedWidgetId ) {
|
|
|
|
var widgetPartial = sidebarPartial.ensureWidgetPlacementContainers( addedWidgetId );
|
|
|
|
addedWidgetPartials.push( widgetPartial );
|
2017-10-16 02:46:48 -04:00
|
|
|
self.renderedWidgets[ addedWidgetId ] = true;
|
2016-03-21 17:59:29 -04:00
|
|
|
} );
|
|
|
|
|
|
|
|
_.each( addedWidgetPartials, function( widgetPartial ) {
|
|
|
|
widgetPartial.refresh();
|
|
|
|
} );
|
|
|
|
|
|
|
|
api.selectiveRefresh.trigger( 'sidebar-updated', sidebarPartial );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2017-12-21 10:12:49 -05:00
|
|
|
* Refreshes the sidebar partial.
|
|
|
|
*
|
|
|
|
* Note that the meat is handled in handleSettingChange because it has the
|
|
|
|
* context of which widgets were removed.
|
2016-03-21 17:59:29 -04:00
|
|
|
*
|
|
|
|
* @since 4.5.0
|
2017-12-21 10:12:49 -05:00
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {Promise} A promise postponing the refresh.
|
2016-03-21 17:59:29 -04:00
|
|
|
*/
|
|
|
|
refresh: function() {
|
|
|
|
var partial = this, deferred = $.Deferred();
|
|
|
|
|
|
|
|
deferred.fail( function() {
|
|
|
|
partial.fallback();
|
|
|
|
} );
|
|
|
|
|
|
|
|
if ( 0 === partial.placements().length ) {
|
|
|
|
deferred.reject();
|
|
|
|
} else {
|
|
|
|
_.each( partial.reflowWidgets(), function( sidebarPlacement ) {
|
|
|
|
api.selectiveRefresh.trigger( 'partial-content-rendered', sidebarPlacement );
|
|
|
|
} );
|
|
|
|
deferred.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
return deferred.promise();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
api.selectiveRefresh.partialConstructor.sidebar = self.SidebarPartial;
|
|
|
|
api.selectiveRefresh.partialConstructor.widget = self.WidgetPartial;
|
|
|
|
|
|
|
|
/**
|
2017-12-21 10:12:49 -05:00
|
|
|
* Adds partials for the registered widget areas (sidebars).
|
2016-03-21 17:59:29 -04:00
|
|
|
*
|
|
|
|
* @since 4.5.0
|
2017-12-21 10:12:49 -05:00
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {void}
|
2016-03-21 17:59:29 -04:00
|
|
|
*/
|
|
|
|
self.addPartials = function() {
|
|
|
|
_.each( self.registeredSidebars, function( registeredSidebar ) {
|
|
|
|
var partial, partialId = 'sidebar[' + registeredSidebar.id + ']';
|
|
|
|
partial = api.selectiveRefresh.partial( partialId );
|
|
|
|
if ( ! partial ) {
|
|
|
|
partial = new self.SidebarPartial( partialId, {
|
|
|
|
params: {
|
|
|
|
sidebarArgs: registeredSidebar
|
|
|
|
}
|
|
|
|
} );
|
2017-10-04 02:48:46 -04:00
|
|
|
api.selectiveRefresh.partial.add( partial );
|
2016-03-21 17:59:29 -04:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
};
|
2016-02-19 13:41:28 -05:00
|
|
|
|
|
|
|
/**
|
2017-12-21 10:12:49 -05:00
|
|
|
* Calculates the selector for the sidebar's widgets based on the registered
|
|
|
|
* sidebar's info.
|
2016-02-19 13:41:28 -05:00
|
|
|
*
|
2017-09-08 14:42:49 -04:00
|
|
|
* @memberOf wp.customize.widgetsPreview
|
|
|
|
*
|
2016-02-19 13:41:28 -05:00
|
|
|
* @since 3.9.0
|
2017-12-21 10:12:49 -05:00
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {void}
|
2016-02-19 13:41:28 -05:00
|
|
|
*/
|
|
|
|
self.buildWidgetSelectors = function() {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
$.each( self.registeredSidebars, function( i, sidebar ) {
|
|
|
|
var widgetTpl = [
|
2016-04-28 14:20:29 -04:00
|
|
|
sidebar.before_widget,
|
2016-02-19 13:41:28 -05:00
|
|
|
sidebar.before_title,
|
|
|
|
sidebar.after_title,
|
|
|
|
sidebar.after_widget
|
|
|
|
].join( '' ),
|
|
|
|
emptyWidget,
|
|
|
|
widgetSelector,
|
|
|
|
widgetClasses;
|
|
|
|
|
|
|
|
emptyWidget = $( widgetTpl );
|
2016-07-27 12:27:30 -04:00
|
|
|
widgetSelector = emptyWidget.prop( 'tagName' ) || '';
|
|
|
|
widgetClasses = emptyWidget.prop( 'className' ) || '';
|
2016-04-28 14:20:29 -04:00
|
|
|
|
2016-02-19 13:41:28 -05:00
|
|
|
// Prevent a rare case when before_widget, before_title, after_title and after_widget is empty.
|
|
|
|
if ( ! widgetClasses ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-27 12:27:30 -04:00
|
|
|
// Remove class names that incorporate the string formatting placeholders %1$s and %2$s.
|
|
|
|
widgetClasses = widgetClasses.replace( /\S*%[12]\$s\S*/g, '' );
|
|
|
|
widgetClasses = widgetClasses.replace( /^\s+|\s+$/g, '' );
|
2016-09-08 11:45:31 -04:00
|
|
|
if ( widgetClasses ) {
|
|
|
|
widgetSelector += '.' + widgetClasses.split( /\s+/ ).join( '.' );
|
|
|
|
}
|
2016-02-19 13:41:28 -05:00
|
|
|
self.widgetSelectors.push( widgetSelector );
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-12-21 10:12:49 -05:00
|
|
|
* Highlights the widget on widget updates or widget control mouse overs.
|
2016-02-19 13:41:28 -05:00
|
|
|
*
|
2017-09-08 14:42:49 -04:00
|
|
|
* @memberOf wp.customize.widgetsPreview
|
|
|
|
*
|
2016-02-19 13:41:28 -05:00
|
|
|
* @since 3.9.0
|
|
|
|
* @param {string} widgetId ID of the widget.
|
2017-12-21 10:12:49 -05:00
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {void}
|
2016-02-19 13:41:28 -05:00
|
|
|
*/
|
|
|
|
self.highlightWidget = function( widgetId ) {
|
|
|
|
var $body = $( document.body ),
|
|
|
|
$widget = $( '#' + widgetId );
|
|
|
|
|
|
|
|
$body.find( '.widget-customizer-highlighted-widget' ).removeClass( 'widget-customizer-highlighted-widget' );
|
|
|
|
|
|
|
|
$widget.addClass( 'widget-customizer-highlighted-widget' );
|
|
|
|
setTimeout( function() {
|
|
|
|
$widget.removeClass( 'widget-customizer-highlighted-widget' );
|
|
|
|
}, 500 );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-12-21 10:12:49 -05:00
|
|
|
* Shows a title and highlights widgets on hover. On shift+clicking focuses the
|
|
|
|
* widget control.
|
2016-02-19 13:41:28 -05:00
|
|
|
*
|
2017-09-08 14:42:49 -04:00
|
|
|
* @memberOf wp.customize.widgetsPreview
|
|
|
|
*
|
2016-02-19 13:41:28 -05:00
|
|
|
* @since 3.9.0
|
2017-12-21 10:12:49 -05:00
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {void}
|
2016-02-19 13:41:28 -05:00
|
|
|
*/
|
|
|
|
self.highlightControls = function() {
|
|
|
|
var self = this,
|
|
|
|
selector = this.widgetSelectors.join( ',' );
|
|
|
|
|
Customize: Implement customized state persistence with changesets.
Includes infrastructure developed in the Customize Snapshots feature plugin.
See https://make.wordpress.org/core/2016/10/12/customize-changesets-technical-design-decisions/
Props westonruter, valendesigns, utkarshpatel, stubgo, lgedeon, ocean90, ryankienstra, mihai2u, dlh, aaroncampbell, jonathanbardo, jorbin.
See #28721.
See #31089.
Fixes #30937.
Fixes #31517.
Fixes #30028.
Fixes #23225.
Fixes #34142.
Fixes #36485.
Built from https://develop.svn.wordpress.org/trunk@38810
git-svn-id: http://core.svn.wordpress.org/trunk@38753 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-10-18 16:05:31 -04:00
|
|
|
// Skip adding highlights if not in the customizer preview iframe.
|
|
|
|
if ( ! api.settings.channel ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-19 13:41:28 -05:00
|
|
|
$( selector ).attr( 'title', this.l10n.widgetTooltip );
|
2017-12-21 10:12:49 -05:00
|
|
|
// Highlights widget when entering the widget editor.
|
2016-02-19 13:41:28 -05:00
|
|
|
$( document ).on( 'mouseenter', selector, function() {
|
|
|
|
self.preview.send( 'highlight-widget-control', $( this ).prop( 'id' ) );
|
|
|
|
});
|
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// Open expand the widget control when shift+clicking the widget element.
|
2016-02-19 13:41:28 -05:00
|
|
|
$( document ).on( 'click', selector, function( e ) {
|
|
|
|
if ( ! e.shiftKey ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
self.preview.send( 'focus-widget-control', $( this ).prop( 'id' ) );
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-12-21 10:12:49 -05:00
|
|
|
* Parses a widget ID.
|
2016-02-19 13:41:28 -05:00
|
|
|
*
|
2017-09-08 14:42:49 -04:00
|
|
|
* @memberOf wp.customize.widgetsPreview
|
|
|
|
*
|
2016-02-19 13:41:28 -05:00
|
|
|
* @since 4.5.0
|
|
|
|
*
|
2017-12-21 10:12:49 -05:00
|
|
|
* @param {string} widgetId The widget ID.
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {{idBase: string, number: number|null}} An object containing the idBase
|
|
|
|
* and number of the parsed widget ID.
|
2016-02-19 13:41:28 -05:00
|
|
|
*/
|
|
|
|
self.parseWidgetId = function( widgetId ) {
|
|
|
|
var matches, parsed = {
|
|
|
|
idBase: '',
|
|
|
|
number: null
|
|
|
|
};
|
|
|
|
|
|
|
|
matches = widgetId.match( /^(.+)-(\d+)$/ );
|
|
|
|
if ( matches ) {
|
|
|
|
parsed.idBase = matches[1];
|
|
|
|
parsed.number = parseInt( matches[2], 10 );
|
|
|
|
} else {
|
|
|
|
parsed.idBase = widgetId; // Likely an old single widget.
|
|
|
|
}
|
|
|
|
|
|
|
|
return parsed;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-12-21 10:12:49 -05:00
|
|
|
* Parses a widget setting ID.
|
2016-02-19 13:41:28 -05:00
|
|
|
*
|
2017-09-08 14:42:49 -04:00
|
|
|
* @memberOf wp.customize.widgetsPreview
|
|
|
|
*
|
2016-02-19 13:41:28 -05:00
|
|
|
* @since 4.5.0
|
|
|
|
*
|
|
|
|
* @param {string} settingId Widget setting ID.
|
2017-12-21 10:12:49 -05:00
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {{idBase: string, number: number|null}|null} Either an object containing the idBase
|
|
|
|
* and number of the parsed widget setting ID,
|
|
|
|
* or null.
|
2016-02-19 13:41:28 -05:00
|
|
|
*/
|
|
|
|
self.parseWidgetSettingId = function( settingId ) {
|
|
|
|
var matches, parsed = {
|
|
|
|
idBase: '',
|
|
|
|
number: null
|
|
|
|
};
|
|
|
|
|
|
|
|
matches = settingId.match( /^widget_([^\[]+?)(?:\[(\d+)])?$/ );
|
|
|
|
if ( ! matches ) {
|
|
|
|
return null;
|
2014-03-05 15:41:14 -05:00
|
|
|
}
|
2016-02-19 13:41:28 -05:00
|
|
|
parsed.idBase = matches[1];
|
|
|
|
if ( matches[2] ) {
|
|
|
|
parsed.number = parseInt( matches[2], 10 );
|
|
|
|
}
|
|
|
|
return parsed;
|
2014-03-05 15:41:14 -05:00
|
|
|
};
|
|
|
|
|
2016-02-19 13:41:28 -05:00
|
|
|
/**
|
2017-12-21 10:12:49 -05:00
|
|
|
* Converts a widget ID into a Customizer setting ID.
|
2016-02-19 13:41:28 -05:00
|
|
|
*
|
2017-09-08 14:42:49 -04:00
|
|
|
* @memberOf wp.customize.widgetsPreview
|
|
|
|
*
|
2016-02-19 13:41:28 -05:00
|
|
|
* @since 4.5.0
|
|
|
|
*
|
2017-12-21 10:12:49 -05:00
|
|
|
* @param {string} widgetId The widget ID.
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {string} The setting ID.
|
2016-02-19 13:41:28 -05:00
|
|
|
*/
|
|
|
|
self.getWidgetSettingId = function( widgetId ) {
|
|
|
|
var parsed = this.parseWidgetId( widgetId ), settingId;
|
|
|
|
|
|
|
|
settingId = 'widget_' + parsed.idBase;
|
|
|
|
if ( parsed.number ) {
|
|
|
|
settingId += '[' + String( parsed.number ) + ']';
|
2014-03-22 17:04:15 -04:00
|
|
|
}
|
|
|
|
|
2016-02-19 13:41:28 -05:00
|
|
|
return settingId;
|
|
|
|
};
|
2014-03-22 17:04:15 -04:00
|
|
|
|
2016-02-19 13:41:28 -05:00
|
|
|
api.bind( 'preview-ready', function() {
|
|
|
|
$.extend( self, _wpWidgetCustomizerPreviewSettings );
|
|
|
|
self.init();
|
2014-03-05 15:41:14 -05:00
|
|
|
});
|
|
|
|
|
2016-02-19 13:41:28 -05:00
|
|
|
return self;
|
|
|
|
})( jQuery, _, wp, wp.customize );
|