1 /* global _wpCustomizeLoaderSettings, confirm */
2 window.wp = window.wp || {};
4 (function( exports, $ ){
5 var api = wp.customize,
9 history: !! ( window.history && history.pushState ),
10 hashchange: ('onhashchange' in window) && (document.documentMode === undefined || document.documentMode > 7)
14 * Allows the Customizer to be overlayed on any page.
16 * By default, any element in the body with the load-customize class will open
17 * an iframe overlay with the URL specified.
19 * e.g. <a class="load-customize" href="<?php echo wp_customize_url(); ?>">Open Customizer</a>
21 * @augments wp.customize.Events
23 Loader = $.extend( {}, api.Events, {
25 * Setup the Loader; triggered on document#ready.
27 initialize: function() {
28 this.body = $( document.body );
30 // Ensure the loader is supported.
31 // Check for settings, postMessage support, and whether we require CORS support.
32 if ( ! Loader.settings || ! $.support.postMessage || ( ! $.support.cors && Loader.settings.isCrossDomain ) ) {
36 this.window = $( window );
37 this.element = $( '<div id="customize-container" />' ).appendTo( this.body );
39 // Bind events for opening and closing the overlay.
40 this.bind( 'open', this.overlay.show );
41 this.bind( 'close', this.overlay.hide );
43 // Any element in the body with the `load-customize` class opens
45 $('#wpbody').on( 'click', '.load-customize', function( event ) {
46 event.preventDefault();
48 // Store a reference to the link that opened the Customizer.
49 Loader.link = $(this);
51 Loader.open( Loader.link.attr('href') );
54 // Add navigation listeners.
55 if ( $.support.history ) {
56 this.window.on( 'popstate', Loader.popstate );
59 if ( $.support.hashchange ) {
60 this.window.on( 'hashchange', Loader.hashchange );
61 this.window.triggerHandler( 'hashchange' );
65 popstate: function( e ) {
66 var state = e.originalEvent.state;
67 if ( state && state.customize ) {
68 Loader.open( state.customize );
69 } else if ( Loader.active ) {
74 hashchange: function() {
75 var hash = window.location.toString().split('#')[1];
77 if ( hash && 0 === hash.indexOf( 'wp_customize=on' ) ) {
78 Loader.open( Loader.settings.url + '?' + hash );
81 if ( ! hash && ! $.support.history ) {
86 beforeunload: function () {
87 if ( ! Loader.saved() ) {
88 return Loader.settings.l10n.saveAlert;
93 * Open the Customizer overlay for a specific URL.
95 * @param string src URL to load in the Customizer.
97 open: function( src ) {
103 // Load the full page on mobile devices.
104 if ( Loader.settings.browser.mobile ) {
105 return window.location = src;
108 // Store the document title prior to opening the Live Preview
109 this.originalDocumentTitle = document.title;
112 this.body.addClass('customize-loading');
114 // Dirty state of Customizer in iframe
115 this.saved = new api.Value( true );
117 this.iframe = $( '<iframe />', { src: src }).appendTo( this.element );
118 this.iframe.one( 'load', this.loaded );
120 // Create a postMessage connection with the iframe.
121 this.messenger = new api.Messenger({
124 targetWindow: this.iframe[0].contentWindow
127 // Wait for the connection from the iframe before sending any postMessage events.
128 this.messenger.bind( 'ready', function() {
129 Loader.messenger.send( 'back' );
132 this.messenger.bind( 'close', function() {
133 if ( $.support.history ) {
135 } else if ( $.support.hashchange ) {
136 window.location.hash = '';
142 // Prompt AYS dialog when navigating away
143 $( window ).on( 'beforeunload', this.beforeunload );
145 this.messenger.bind( 'activated', function( location ) {
147 window.location = location;
151 this.messenger.bind( 'saved', function () {
152 Loader.saved( true );
154 this.messenger.bind( 'change', function () {
155 Loader.saved( false );
158 this.pushState( src );
160 this.trigger( 'open' );
163 pushState: function ( src ) {
164 var hash = src.split( '?' )[1];
166 // Ensure we don't call pushState if the user hit the forward button.
167 if ( $.support.history && window.location.href !== src ) {
168 history.pushState( { customize: src }, '', src );
169 } else if ( ! $.support.history && $.support.hashchange && hash ) {
170 window.location.hash = 'wp_customize=on&' + hash;
173 this.trigger( 'open' );
177 * Callback after the Customizer has been opened.
180 Loader.body.addClass( 'customize-active full-overlay-active' );
184 * Close the Customizer overlay and return focus to the link that opened it.
187 if ( ! this.active ) {
191 // Display AYS dialog if Customizer is dirty
192 if ( ! this.saved() && ! confirm( Loader.settings.l10n.saveAlert ) ) {
193 // Go forward since Customizer is exited by history.back()
200 this.trigger( 'close' );
202 // Restore document title prior to opening the Live Preview
203 if ( this.originalDocumentTitle ) {
204 document.title = this.originalDocumentTitle;
207 // Return focus to link that was originally clicked.
214 * Callback after the Customizer has been closed.
217 Loader.iframe.remove();
218 Loader.messenger.destroy();
219 Loader.iframe = null;
220 Loader.messenger = null;
222 Loader.body.removeClass( 'customize-active full-overlay-active' ).removeClass( 'customize-loading' );
223 $( window ).off( 'beforeunload', Loader.beforeunload );
227 * Callback for the `load` event on the Customizer iframe.
230 Loader.body.removeClass('customize-loading');
234 * Overlay hide/show utility methods.
238 this.element.fadeIn( 200, Loader.opened );
242 this.element.fadeOut( 200, Loader.closed );
247 // Bootstrap the Loader on document#ready.
249 Loader.settings = _wpCustomizeLoaderSettings;
253 // Expose the API publicly on window.wp.customize.Loader