2018-06-28 04:30:15 +02:00
|
|
|
/**
|
|
|
|
* @output wp-includes/js/customize-loader.js
|
|
|
|
*/
|
|
|
|
|
2017-10-09 18:04:48 +02:00
|
|
|
/* global _wpCustomizeLoaderSettings */
|
2018-06-28 04:30:15 +02:00
|
|
|
|
2017-09-08 20:42:49 +02:00
|
|
|
/**
|
2015-08-22 19:24:25 +02:00
|
|
|
* Expose a public API that allows the customizer to be
|
|
|
|
* loaded on any page.
|
2017-09-08 20:42:49 +02:00
|
|
|
*
|
|
|
|
* @namespace wp
|
2015-08-22 19:24:25 +02:00
|
|
|
*/
|
2012-10-06 02:43:36 +02:00
|
|
|
window.wp = window.wp || {};
|
2012-08-23 02:04:18 +02:00
|
|
|
|
|
|
|
(function( exports, $ ){
|
|
|
|
var api = wp.customize,
|
|
|
|
Loader;
|
|
|
|
|
|
|
|
$.extend( $.support, {
|
|
|
|
history: !! ( window.history && history.pushState ),
|
|
|
|
hashchange: ('onhashchange' in window) && (document.documentMode === undefined || document.documentMode > 7)
|
|
|
|
});
|
|
|
|
|
2014-08-09 00:10:15 +02:00
|
|
|
/**
|
2024-04-12 19:47:13 +02:00
|
|
|
* Allows the Customizer to be overlaid on any page.
|
2014-08-09 00:10:15 +02:00
|
|
|
*
|
|
|
|
* By default, any element in the body with the load-customize class will open
|
2014-10-05 20:40:15 +02:00
|
|
|
* an iframe overlay with the URL specified.
|
2014-08-09 00:10:15 +02:00
|
|
|
*
|
2014-10-15 19:21:19 +02:00
|
|
|
* e.g. <a class="load-customize" href="<?php echo wp_customize_url(); ?>">Open Customizer</a>
|
2014-08-09 00:10:15 +02:00
|
|
|
*
|
2017-09-08 20:42:49 +02:00
|
|
|
* @memberOf wp.customize
|
|
|
|
*
|
|
|
|
* @class
|
2014-08-09 00:10:15 +02:00
|
|
|
* @augments wp.customize.Events
|
|
|
|
*/
|
2017-09-08 20:42:49 +02:00
|
|
|
Loader = $.extend( {}, api.Events,/** @lends wp.customize.Loader.prototype */{
|
2014-08-09 00:10:15 +02:00
|
|
|
/**
|
|
|
|
* Setup the Loader; triggered on document#ready.
|
|
|
|
*/
|
2012-08-23 02:04:18 +02:00
|
|
|
initialize: function() {
|
|
|
|
this.body = $( document.body );
|
|
|
|
|
|
|
|
// Ensure the loader is supported.
|
|
|
|
// Check for settings, postMessage support, and whether we require CORS support.
|
|
|
|
if ( ! Loader.settings || ! $.support.postMessage || ( ! $.support.cors && Loader.settings.isCrossDomain ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.window = $( window );
|
|
|
|
this.element = $( '<div id="customize-container" />' ).appendTo( this.body );
|
|
|
|
|
2014-08-09 00:10:15 +02:00
|
|
|
// Bind events for opening and closing the overlay.
|
2012-08-23 02:04:18 +02:00
|
|
|
this.bind( 'open', this.overlay.show );
|
|
|
|
this.bind( 'close', this.overlay.hide );
|
|
|
|
|
2014-08-09 00:10:15 +02:00
|
|
|
// Any element in the body with the `load-customize` class opens
|
|
|
|
// the Customizer.
|
2012-08-23 02:04:18 +02:00
|
|
|
$('#wpbody').on( 'click', '.load-customize', function( event ) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
2014-10-15 19:21:19 +02:00
|
|
|
// Store a reference to the link that opened the Customizer.
|
2012-11-21 14:17:10 +01:00
|
|
|
Loader.link = $(this);
|
2012-08-23 02:04:18 +02:00
|
|
|
// Load the theme.
|
2012-11-21 14:17:10 +01:00
|
|
|
Loader.open( Loader.link.attr('href') );
|
2012-08-23 02:04:18 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// Add navigation listeners.
|
2014-07-08 19:04:15 +02:00
|
|
|
if ( $.support.history ) {
|
2012-08-23 02:04:18 +02:00
|
|
|
this.window.on( 'popstate', Loader.popstate );
|
2014-07-08 19:04:15 +02:00
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
|
|
|
|
if ( $.support.hashchange ) {
|
|
|
|
this.window.on( 'hashchange', Loader.hashchange );
|
|
|
|
this.window.triggerHandler( 'hashchange' );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
popstate: function( e ) {
|
|
|
|
var state = e.originalEvent.state;
|
2014-07-08 19:04:15 +02:00
|
|
|
if ( state && state.customize ) {
|
2012-08-23 02:04:18 +02:00
|
|
|
Loader.open( state.customize );
|
2014-07-08 19:04:15 +02:00
|
|
|
} else if ( Loader.active ) {
|
2012-08-23 02:04:18 +02:00
|
|
|
Loader.close();
|
2014-07-08 19:04:15 +02:00
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
},
|
|
|
|
|
2013-11-15 05:31:10 +01:00
|
|
|
hashchange: function() {
|
2012-08-23 02:04:18 +02:00
|
|
|
var hash = window.location.toString().split('#')[1];
|
|
|
|
|
2014-07-08 19:04:15 +02:00
|
|
|
if ( hash && 0 === hash.indexOf( 'wp_customize=on' ) ) {
|
2012-08-23 02:04:18 +02:00
|
|
|
Loader.open( Loader.settings.url + '?' + hash );
|
2014-07-08 19:04:15 +02:00
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2014-11-11 23:29:23 +01:00
|
|
|
if ( ! hash && ! $.support.history ) {
|
2012-08-23 02:04:18 +02:00
|
|
|
Loader.close();
|
2014-07-08 19:04:15 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
beforeunload: function () {
|
|
|
|
if ( ! Loader.saved() ) {
|
|
|
|
return Loader.settings.l10n.saveAlert;
|
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
},
|
|
|
|
|
2014-08-09 00:10:15 +02:00
|
|
|
/**
|
2014-10-15 19:21:19 +02:00
|
|
|
* Open the Customizer overlay for a specific URL.
|
2014-08-09 00:10:15 +02:00
|
|
|
*
|
2020-06-20 14:58:10 +02:00
|
|
|
* @param string src URL to load in the Customizer.
|
2014-08-09 00:10:15 +02:00
|
|
|
*/
|
2012-08-23 02:04:18 +02:00
|
|
|
open: function( src ) {
|
|
|
|
|
2014-07-08 19:04:15 +02:00
|
|
|
if ( this.active ) {
|
2012-08-23 02:04:18 +02:00
|
|
|
return;
|
2014-07-08 19:04:15 +02:00
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
|
|
|
|
// Load the full page on mobile devices.
|
2014-07-08 19:04:15 +02:00
|
|
|
if ( Loader.settings.browser.mobile ) {
|
2012-08-23 02:04:18 +02:00
|
|
|
return window.location = src;
|
2014-07-08 19:04:15 +02:00
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// Store the document title prior to opening the Live Preview.
|
2014-11-11 23:29:23 +01:00
|
|
|
this.originalDocumentTitle = document.title;
|
|
|
|
|
2012-08-23 02:04:18 +02:00
|
|
|
this.active = true;
|
|
|
|
this.body.addClass('customize-loading');
|
|
|
|
|
2015-09-05 21:53:24 +02:00
|
|
|
/*
|
|
|
|
* Track the dirtiness state (whether the drafted changes have been published)
|
|
|
|
* of the Customizer in the iframe. This is used to decide whether to display
|
|
|
|
* an AYS alert if the user tries to close the window before saving changes.
|
|
|
|
*/
|
2014-07-08 19:04:15 +02:00
|
|
|
this.saved = new api.Value( true );
|
|
|
|
|
2015-03-10 22:20:26 +01:00
|
|
|
this.iframe = $( '<iframe />', { 'src': src, 'title': Loader.settings.l10n.mainIframeTitle } ).appendTo( this.element );
|
2012-08-23 02:04:18 +02:00
|
|
|
this.iframe.one( 'load', this.loaded );
|
|
|
|
|
|
|
|
// Create a postMessage connection with the iframe.
|
|
|
|
this.messenger = new api.Messenger({
|
|
|
|
url: src,
|
|
|
|
channel: 'loader',
|
|
|
|
targetWindow: this.iframe[0].contentWindow
|
|
|
|
});
|
|
|
|
|
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 22:05:31 +02:00
|
|
|
// Expose the changeset UUID on the parent window's URL so that the customized state can survive a refresh.
|
|
|
|
if ( history.replaceState ) {
|
|
|
|
this.messenger.bind( 'changeset-uuid', function( changesetUuid ) {
|
|
|
|
var urlParser = document.createElement( 'a' );
|
|
|
|
urlParser.href = location.href;
|
|
|
|
urlParser.search = $.param( _.extend(
|
|
|
|
api.utils.parseQueryString( urlParser.search.substr( 1 ) ),
|
|
|
|
{ changeset_uuid: changesetUuid }
|
|
|
|
) );
|
|
|
|
history.replaceState( { customize: urlParser.href }, '', urlParser.href );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2012-08-23 02:04:18 +02:00
|
|
|
// Wait for the connection from the iframe before sending any postMessage events.
|
|
|
|
this.messenger.bind( 'ready', function() {
|
|
|
|
Loader.messenger.send( 'back' );
|
|
|
|
});
|
|
|
|
|
|
|
|
this.messenger.bind( 'close', function() {
|
2014-07-08 19:04:15 +02:00
|
|
|
if ( $.support.history ) {
|
2012-08-23 02:04:18 +02:00
|
|
|
history.back();
|
2014-07-08 19:04:15 +02:00
|
|
|
} else if ( $.support.hashchange ) {
|
2012-08-23 02:04:18 +02:00
|
|
|
window.location.hash = '';
|
2014-07-08 19:04:15 +02:00
|
|
|
} else {
|
2012-08-23 02:04:18 +02:00
|
|
|
Loader.close();
|
2014-07-08 19:04:15 +02:00
|
|
|
}
|
2014-11-11 23:29:23 +01:00
|
|
|
});
|
2014-07-08 19:04:15 +02:00
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// Prompt AYS dialog when navigating away.
|
2014-07-08 19:04:15 +02:00
|
|
|
$( window ).on( 'beforeunload', this.beforeunload );
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2014-07-08 19:04:15 +02:00
|
|
|
this.messenger.bind( 'saved', function () {
|
|
|
|
Loader.saved( true );
|
|
|
|
} );
|
|
|
|
this.messenger.bind( 'change', function () {
|
|
|
|
Loader.saved( false );
|
|
|
|
} );
|
|
|
|
|
2015-03-25 18:26:26 +01:00
|
|
|
this.messenger.bind( 'title', function( newTitle ){
|
|
|
|
window.document.title = newTitle;
|
|
|
|
});
|
|
|
|
|
2014-07-08 19:04:15 +02:00
|
|
|
this.pushState( src );
|
|
|
|
|
|
|
|
this.trigger( 'open' );
|
|
|
|
},
|
|
|
|
|
|
|
|
pushState: function ( src ) {
|
2014-11-11 23:29:23 +01:00
|
|
|
var hash = src.split( '?' )[1];
|
2012-08-23 02:04:18 +02:00
|
|
|
|
|
|
|
// Ensure we don't call pushState if the user hit the forward button.
|
2014-07-08 19:04:15 +02:00
|
|
|
if ( $.support.history && window.location.href !== src ) {
|
2012-08-23 02:04:18 +02:00
|
|
|
history.pushState( { customize: src }, '', src );
|
2014-07-08 19:04:15 +02:00
|
|
|
} else if ( ! $.support.history && $.support.hashchange && hash ) {
|
2012-08-23 02:04:18 +02:00
|
|
|
window.location.hash = 'wp_customize=on&' + hash;
|
2014-07-08 19:04:15 +02:00
|
|
|
}
|
2014-11-11 23:29:23 +01:00
|
|
|
|
|
|
|
this.trigger( 'open' );
|
2012-08-23 02:04:18 +02:00
|
|
|
},
|
|
|
|
|
2014-08-09 00:10:15 +02:00
|
|
|
/**
|
2014-10-15 19:21:19 +02:00
|
|
|
* Callback after the Customizer has been opened.
|
2014-08-09 00:10:15 +02:00
|
|
|
*/
|
2012-08-23 02:04:18 +02:00
|
|
|
opened: function() {
|
2016-09-04 23:51:31 +02:00
|
|
|
Loader.body.addClass( 'customize-active full-overlay-active' ).attr( 'aria-busy', 'true' );
|
2012-08-23 02:04:18 +02:00
|
|
|
},
|
|
|
|
|
2014-08-09 00:10:15 +02:00
|
|
|
/**
|
2016-09-04 23:51:31 +02:00
|
|
|
* Close the Customizer overlay.
|
2014-08-09 00:10:15 +02:00
|
|
|
*/
|
2012-08-23 02:04:18 +02:00
|
|
|
close: function() {
|
2017-10-09 18:04:48 +02:00
|
|
|
var self = this, onConfirmClose;
|
|
|
|
if ( ! self.active ) {
|
2014-07-08 19:04:15 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-09 18:04:48 +02:00
|
|
|
onConfirmClose = function( confirmed ) {
|
|
|
|
if ( confirmed ) {
|
|
|
|
self.active = false;
|
|
|
|
self.trigger( 'close' );
|
2014-07-08 19:04:15 +02:00
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// Restore document title prior to opening the Live Preview.
|
2017-10-09 18:04:48 +02:00
|
|
|
if ( self.originalDocumentTitle ) {
|
|
|
|
document.title = self.originalDocumentTitle;
|
|
|
|
}
|
|
|
|
} else {
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// Go forward since Customizer is exited by history.back().
|
2017-10-09 18:04:48 +02:00
|
|
|
history.forward();
|
|
|
|
}
|
|
|
|
self.messenger.unbind( 'confirmed-close', onConfirmClose );
|
|
|
|
};
|
|
|
|
self.messenger.bind( 'confirmed-close', onConfirmClose );
|
2012-11-21 14:17:10 +01:00
|
|
|
|
2017-10-09 18:04:48 +02:00
|
|
|
Loader.messenger.send( 'confirm-close' );
|
2012-08-23 02:04:18 +02:00
|
|
|
},
|
|
|
|
|
2014-08-09 00:10:15 +02:00
|
|
|
/**
|
2014-10-15 19:21:19 +02:00
|
|
|
* Callback after the Customizer has been closed.
|
2014-08-09 00:10:15 +02:00
|
|
|
*/
|
2012-08-23 02:04:18 +02:00
|
|
|
closed: function() {
|
|
|
|
Loader.iframe.remove();
|
|
|
|
Loader.messenger.destroy();
|
|
|
|
Loader.iframe = null;
|
|
|
|
Loader.messenger = null;
|
2014-07-08 19:04:15 +02:00
|
|
|
Loader.saved = null;
|
2012-08-23 02:04:18 +02:00
|
|
|
Loader.body.removeClass( 'customize-active full-overlay-active' ).removeClass( 'customize-loading' );
|
2014-07-08 19:04:15 +02:00
|
|
|
$( window ).off( 'beforeunload', Loader.beforeunload );
|
2016-09-04 23:51:31 +02:00
|
|
|
/*
|
|
|
|
* Return focus to the link that opened the Customizer overlay after
|
|
|
|
* the body element visibility is restored.
|
|
|
|
*/
|
|
|
|
if ( Loader.link ) {
|
|
|
|
Loader.link.focus();
|
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
},
|
|
|
|
|
2014-08-09 00:10:15 +02:00
|
|
|
/**
|
|
|
|
* Callback for the `load` event on the Customizer iframe.
|
|
|
|
*/
|
2012-08-23 02:04:18 +02:00
|
|
|
loaded: function() {
|
2016-09-04 23:51:31 +02:00
|
|
|
Loader.body.removeClass( 'customize-loading' ).attr( 'aria-busy', 'false' );
|
2012-08-23 02:04:18 +02:00
|
|
|
},
|
|
|
|
|
2014-08-09 00:10:15 +02:00
|
|
|
/**
|
|
|
|
* Overlay hide/show utility methods.
|
|
|
|
*/
|
2012-08-23 02:04:18 +02:00
|
|
|
overlay: {
|
|
|
|
show: function() {
|
|
|
|
this.element.fadeIn( 200, Loader.opened );
|
|
|
|
},
|
|
|
|
|
|
|
|
hide: function() {
|
|
|
|
this.element.fadeOut( 200, Loader.closed );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-08-09 00:10:15 +02:00
|
|
|
// Bootstrap the Loader on document#ready.
|
2012-08-23 02:04:18 +02:00
|
|
|
$( function() {
|
|
|
|
Loader.settings = _wpCustomizeLoaderSettings;
|
|
|
|
Loader.initialize();
|
|
|
|
});
|
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// Expose the API publicly on window.wp.customize.Loader.
|
2012-08-23 02:04:18 +02:00
|
|
|
api.Loader = Loader;
|
|
|
|
})( wp, jQuery );
|