/**
* @output wp-admin/js/customize-controls.js
*/
/* global _wpCustomizeHeader, _wpCustomizeBackground, _wpMediaViewsL10n, MediaElementPlayer, console, confirm */
(function( exports, $ ){
var Container, focus, normalizedTransitionendEventName, api = wp.customize;
var reducedMotionMediaQuery = window.matchMedia( '(prefers-reduced-motion: reduce)' );
var isReducedMotion = reducedMotionMediaQuery.matches;
reducedMotionMediaQuery.addEventListener( 'change' , function handleReducedMotionChange( event ) {
isReducedMotion = event.matches;
});
api.OverlayNotification = api.Notification.extend(/** @lends wp.customize.OverlayNotification.prototype */{
/**
* Whether the notification should show a loading spinner.
*
* @since 4.9.0
* @var {boolean}
*/
loading: false,
/**
* A notification that is displayed in a full-screen overlay.
*
* @constructs wp.customize.OverlayNotification
* @augments wp.customize.Notification
*
* @since 4.9.0
*
* @param {string} code - Code.
* @param {Object} params - Params.
*/
initialize: function( code, params ) {
var notification = this;
api.Notification.prototype.initialize.call( notification, code, params );
notification.containerClasses += ' notification-overlay';
if ( notification.loading ) {
notification.containerClasses += ' notification-loading';
}
},
/**
* Render notification.
*
* @since 4.9.0
*
* @return {jQuery} Notification container.
*/
render: function() {
var li = api.Notification.prototype.render.call( this );
li.on( 'keydown', _.bind( this.handleEscape, this ) );
return li;
},
/**
* Stop propagation on escape key presses, but also dismiss notification if it is dismissible.
*
* @since 4.9.0
*
* @param {jQuery.Event} event - Event.
* @return {void}
*/
handleEscape: function( event ) {
var notification = this;
if ( 27 === event.which ) {
event.stopPropagation();
if ( notification.dismissible && notification.parent ) {
notification.parent.remove( notification.code );
}
}
}
});
api.Notifications = api.Values.extend(/** @lends wp.customize.Notifications.prototype */{
/**
* Whether the alternative style should be used.
*
* @since 4.9.0
* @type {boolean}
*/
alt: false,
/**
* The default constructor for items of the collection.
*
* @since 4.9.0
* @type {object}
*/
defaultConstructor: api.Notification,
/**
* A collection of observable notifications.
*
* @since 4.9.0
*
* @constructs wp.customize.Notifications
* @augments wp.customize.Values
*
* @param {Object} options - Options.
* @param {jQuery} [options.container] - Container element for notifications. This can be injected later.
* @param {boolean} [options.alt] - Whether alternative style should be used when rendering notifications.
*
* @return {void}
*/
initialize: function( options ) {
var collection = this;
api.Values.prototype.initialize.call( collection, options );
_.bindAll( collection, 'constrainFocus' );
// Keep track of the order in which the notifications were added for sorting purposes.
collection._addedIncrement = 0;
collection._addedOrder = {};
// Trigger change event when notification is added or removed.
collection.bind( 'add', function( notification ) {
collection.trigger( 'change', notification );
});
collection.bind( 'removed', function( notification ) {
collection.trigger( 'change', notification );
});
},
/**
* Get the number of notifications added.
*
* @since 4.9.0
* @return {number} Count of notifications.
*/
count: function() {
return _.size( this._value );
},
/**
* Add notification to the collection.
*
* @since 4.9.0
*
* @param {string|wp.customize.Notification} notification - Notification object to add. Alternatively code may be supplied, and in that case the second notificationObject argument must be supplied.
* @param {wp.customize.Notification} [notificationObject] - Notification to add when first argument is the code string.
* @return {wp.customize.Notification} Added notification (or existing instance if it was already added).
*/
add: function( notification, notificationObject ) {
var collection = this, code, instance;
if ( 'string' === typeof notification ) {
code = notification;
instance = notificationObject;
} else {
code = notification.code;
instance = notification;
}
if ( ! collection.has( code ) ) {
collection._addedIncrement += 1;
collection._addedOrder[ code ] = collection._addedIncrement;
}
return api.Values.prototype.add.call( collection, code, instance );
},
/**
* Add notification to the collection.
*
* @since 4.9.0
* @param {string} code - Notification code to remove.
* @return {api.Notification} Added instance (or existing instance if it was already added).
*/
remove: function( code ) {
var collection = this;
delete collection._addedOrder[ code ];
return api.Values.prototype.remove.call( this, code );
},
/**
* Get list of notifications.
*
* Notifications may be sorted by type followed by added time.
*
* @since 4.9.0
* @param {Object} args - Args.
* @param {boolean} [args.sort=false] - Whether to return the notifications sorted.
* @return {Array.