2014-07-08 13:04:15 -04:00
|
|
|
/* global _wpCustomizeLoaderSettings, confirm */
|
2015-08-22 13:24:25 -04:00
|
|
|
/*
|
|
|
|
* Expose a public API that allows the customizer to be
|
|
|
|
* loaded on any page.
|
|
|
|
*/
|
2012-10-05 20:43:36 -04:00
|
|
|
window.wp = window.wp || {};
|
2012-08-22 20:04:18 -04: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-08 18:10:15 -04:00
|
|
|
/**
|
|
|
|
* Allows the Customizer to be overlayed on any page.
|
|
|
|
*
|
|
|
|
* By default, any element in the body with the load-customize class will open
|
2014-10-05 14:40:15 -04:00
|
|
|
* an iframe overlay with the URL specified.
|
2014-08-08 18:10:15 -04:00
|
|
|
*
|
2014-10-15 13:21:19 -04:00
|
|
|
* e.g. <a class="load-customize" href="<?php echo wp_customize_url(); ?>">Open Customizer</a>
|
2014-08-08 18:10:15 -04:00
|
|
|
*
|
|
|
|
* @augments wp.customize.Events
|
|
|
|
*/
|
2012-08-22 20:04:18 -04:00
|
|
|
Loader = $.extend( {}, api.Events, {
|
2014-08-08 18:10:15 -04:00
|
|
|
/**
|
|
|
|
* Setup the Loader; triggered on document#ready.
|
|
|
|
*/
|
2012-08-22 20:04:18 -04: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-08 18:10:15 -04:00
|
|
|
// Bind events for opening and closing the overlay.
|
2012-08-22 20:04:18 -04:00
|
|
|
this.bind( 'open', this.overlay.show );
|
|
|
|
this.bind( 'close', this.overlay.hide );
|
|
|
|
|
2014-08-08 18:10:15 -04:00
|
|
|
// Any element in the body with the `load-customize` class opens
|
|
|
|
// the Customizer.
|
2012-08-22 20:04:18 -04:00
|
|
|
$('#wpbody').on( 'click', '.load-customize', function( event ) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
2014-10-15 13:21:19 -04:00
|
|
|
// Store a reference to the link that opened the Customizer.
|
2012-11-21 08:17:10 -05:00
|
|
|
Loader.link = $(this);
|
2012-08-22 20:04:18 -04:00
|
|
|
// Load the theme.
|
2012-11-21 08:17:10 -05:00
|
|
|
Loader.open( Loader.link.attr('href') );
|
2012-08-22 20:04:18 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
// Add navigation listeners.
|
2014-07-08 13:04:15 -04:00
|
|
|
if ( $.support.history ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
this.window.on( 'popstate', Loader.popstate );
|
2014-07-08 13:04:15 -04:00
|
|
|
}
|
2012-08-22 20:04:18 -04: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 13:04:15 -04:00
|
|
|
if ( state && state.customize ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
Loader.open( state.customize );
|
2014-07-08 13:04:15 -04:00
|
|
|
} else if ( Loader.active ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
Loader.close();
|
2014-07-08 13:04:15 -04:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
},
|
|
|
|
|
2013-11-14 23:31:10 -05:00
|
|
|
hashchange: function() {
|
2012-08-22 20:04:18 -04:00
|
|
|
var hash = window.location.toString().split('#')[1];
|
|
|
|
|
2014-07-08 13:04:15 -04:00
|
|
|
if ( hash && 0 === hash.indexOf( 'wp_customize=on' ) ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
Loader.open( Loader.settings.url + '?' + hash );
|
2014-07-08 13:04:15 -04:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2014-11-11 17:29:23 -05:00
|
|
|
if ( ! hash && ! $.support.history ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
Loader.close();
|
2014-07-08 13:04:15 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
beforeunload: function () {
|
|
|
|
if ( ! Loader.saved() ) {
|
|
|
|
return Loader.settings.l10n.saveAlert;
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
},
|
|
|
|
|
2014-08-08 18:10:15 -04:00
|
|
|
/**
|
2014-10-15 13:21:19 -04:00
|
|
|
* Open the Customizer overlay for a specific URL.
|
2014-08-08 18:10:15 -04:00
|
|
|
*
|
|
|
|
* @param string src URL to load in the Customizer.
|
|
|
|
*/
|
2012-08-22 20:04:18 -04:00
|
|
|
open: function( src ) {
|
|
|
|
|
2014-07-08 13:04:15 -04:00
|
|
|
if ( this.active ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
return;
|
2014-07-08 13:04:15 -04:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
|
|
|
// Load the full page on mobile devices.
|
2014-07-08 13:04:15 -04:00
|
|
|
if ( Loader.settings.browser.mobile ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
return window.location = src;
|
2014-07-08 13:04:15 -04:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2014-11-11 17:29:23 -05:00
|
|
|
// Store the document title prior to opening the Live Preview
|
|
|
|
this.originalDocumentTitle = document.title;
|
|
|
|
|
2012-08-22 20:04:18 -04:00
|
|
|
this.active = true;
|
|
|
|
this.body.addClass('customize-loading');
|
|
|
|
|
2015-09-05 15:53:24 -04: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 13:04:15 -04:00
|
|
|
this.saved = new api.Value( true );
|
|
|
|
|
2015-03-10 17:20:26 -04:00
|
|
|
this.iframe = $( '<iframe />', { 'src': src, 'title': Loader.settings.l10n.mainIframeTitle } ).appendTo( this.element );
|
2012-08-22 20:04:18 -04: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 16:05:31 -04: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-22 20:04:18 -04: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 13:04:15 -04:00
|
|
|
if ( $.support.history ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
history.back();
|
2014-07-08 13:04:15 -04:00
|
|
|
} else if ( $.support.hashchange ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
window.location.hash = '';
|
2014-07-08 13:04:15 -04:00
|
|
|
} else {
|
2012-08-22 20:04:18 -04:00
|
|
|
Loader.close();
|
2014-07-08 13:04:15 -04:00
|
|
|
}
|
2014-11-11 17:29:23 -05:00
|
|
|
});
|
2014-07-08 13:04:15 -04:00
|
|
|
|
|
|
|
// Prompt AYS dialog when navigating away
|
|
|
|
$( window ).on( 'beforeunload', this.beforeunload );
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2014-07-08 13:04:15 -04:00
|
|
|
this.messenger.bind( 'saved', function () {
|
|
|
|
Loader.saved( true );
|
|
|
|
} );
|
|
|
|
this.messenger.bind( 'change', function () {
|
|
|
|
Loader.saved( false );
|
|
|
|
} );
|
|
|
|
|
2015-03-25 13:26:26 -04:00
|
|
|
this.messenger.bind( 'title', function( newTitle ){
|
|
|
|
window.document.title = newTitle;
|
|
|
|
});
|
|
|
|
|
2014-07-08 13:04:15 -04:00
|
|
|
this.pushState( src );
|
|
|
|
|
|
|
|
this.trigger( 'open' );
|
|
|
|
},
|
|
|
|
|
|
|
|
pushState: function ( src ) {
|
2014-11-11 17:29:23 -05:00
|
|
|
var hash = src.split( '?' )[1];
|
2012-08-22 20:04:18 -04:00
|
|
|
|
|
|
|
// Ensure we don't call pushState if the user hit the forward button.
|
2014-07-08 13:04:15 -04:00
|
|
|
if ( $.support.history && window.location.href !== src ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
history.pushState( { customize: src }, '', src );
|
2014-07-08 13:04:15 -04:00
|
|
|
} else if ( ! $.support.history && $.support.hashchange && hash ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
window.location.hash = 'wp_customize=on&' + hash;
|
2014-07-08 13:04:15 -04:00
|
|
|
}
|
2014-11-11 17:29:23 -05:00
|
|
|
|
|
|
|
this.trigger( 'open' );
|
2012-08-22 20:04:18 -04:00
|
|
|
},
|
|
|
|
|
2014-08-08 18:10:15 -04:00
|
|
|
/**
|
2014-10-15 13:21:19 -04:00
|
|
|
* Callback after the Customizer has been opened.
|
2014-08-08 18:10:15 -04:00
|
|
|
*/
|
2012-08-22 20:04:18 -04:00
|
|
|
opened: function() {
|
2016-09-04 17:51:31 -04:00
|
|
|
Loader.body.addClass( 'customize-active full-overlay-active' ).attr( 'aria-busy', 'true' );
|
2012-08-22 20:04:18 -04:00
|
|
|
},
|
|
|
|
|
2014-08-08 18:10:15 -04:00
|
|
|
/**
|
2016-09-04 17:51:31 -04:00
|
|
|
* Close the Customizer overlay.
|
2014-08-08 18:10:15 -04:00
|
|
|
*/
|
2012-08-22 20:04:18 -04:00
|
|
|
close: function() {
|
2014-07-08 13:04:15 -04:00
|
|
|
if ( ! this.active ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-15 13:21:19 -04:00
|
|
|
// Display AYS dialog if Customizer is dirty
|
2014-07-08 13:04:15 -04:00
|
|
|
if ( ! this.saved() && ! confirm( Loader.settings.l10n.saveAlert ) ) {
|
|
|
|
// Go forward since Customizer is exited by history.back()
|
|
|
|
history.forward();
|
2012-08-22 20:04:18 -04:00
|
|
|
return;
|
2014-07-08 13:04:15 -04:00
|
|
|
}
|
|
|
|
|
2012-08-22 20:04:18 -04:00
|
|
|
this.active = false;
|
|
|
|
|
|
|
|
this.trigger( 'close' );
|
2012-11-21 08:17:10 -05:00
|
|
|
|
2014-11-11 17:29:23 -05:00
|
|
|
// Restore document title prior to opening the Live Preview
|
|
|
|
if ( this.originalDocumentTitle ) {
|
|
|
|
document.title = this.originalDocumentTitle;
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
},
|
|
|
|
|
2014-08-08 18:10:15 -04:00
|
|
|
/**
|
2014-10-15 13:21:19 -04:00
|
|
|
* Callback after the Customizer has been closed.
|
2014-08-08 18:10:15 -04:00
|
|
|
*/
|
2012-08-22 20:04:18 -04:00
|
|
|
closed: function() {
|
|
|
|
Loader.iframe.remove();
|
|
|
|
Loader.messenger.destroy();
|
|
|
|
Loader.iframe = null;
|
|
|
|
Loader.messenger = null;
|
2014-07-08 13:04:15 -04:00
|
|
|
Loader.saved = null;
|
2012-08-22 20:04:18 -04:00
|
|
|
Loader.body.removeClass( 'customize-active full-overlay-active' ).removeClass( 'customize-loading' );
|
2014-07-08 13:04:15 -04:00
|
|
|
$( window ).off( 'beforeunload', Loader.beforeunload );
|
2016-09-04 17:51:31 -04: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-22 20:04:18 -04:00
|
|
|
},
|
|
|
|
|
2014-08-08 18:10:15 -04:00
|
|
|
/**
|
|
|
|
* Callback for the `load` event on the Customizer iframe.
|
|
|
|
*/
|
2012-08-22 20:04:18 -04:00
|
|
|
loaded: function() {
|
2016-09-04 17:51:31 -04:00
|
|
|
Loader.body.removeClass( 'customize-loading' ).attr( 'aria-busy', 'false' );
|
2012-08-22 20:04:18 -04:00
|
|
|
},
|
|
|
|
|
2014-08-08 18:10:15 -04:00
|
|
|
/**
|
|
|
|
* Overlay hide/show utility methods.
|
|
|
|
*/
|
2012-08-22 20:04:18 -04:00
|
|
|
overlay: {
|
|
|
|
show: function() {
|
|
|
|
this.element.fadeIn( 200, Loader.opened );
|
|
|
|
},
|
|
|
|
|
|
|
|
hide: function() {
|
|
|
|
this.element.fadeOut( 200, Loader.closed );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-08-08 18:10:15 -04:00
|
|
|
// Bootstrap the Loader on document#ready.
|
2012-08-22 20:04:18 -04:00
|
|
|
$( function() {
|
|
|
|
Loader.settings = _wpCustomizeLoaderSettings;
|
|
|
|
Loader.initialize();
|
|
|
|
});
|
|
|
|
|
2014-08-08 18:10:15 -04:00
|
|
|
// Expose the API publicly on window.wp.customize.Loader
|
2012-08-22 20:04:18 -04:00
|
|
|
api.Loader = Loader;
|
|
|
|
})( wp, jQuery );
|