1 /* global _wpCustomizeLoaderSettings, confirm */
3 * Expose a public API that allows the customizer to be
6 window.wp = window.wp || {};
8 (function( exports, $ ){
9 var api = wp.customize,
12 $.extend( $.support, {
13 history: !! ( window.history && history.pushState ),
14 hashchange: ('onhashchange' in window) && (document.documentMode === undefined || document.documentMode > 7)
18 * Allows the Customizer to be overlayed on any page.
20 * By default, any element in the body with the load-customize class will open
21 * an iframe overlay with the URL specified.
23 * e.g. <a class="load-customize" href="<?php echo wp_customize_url(); ?>">Open Customizer</a>
25 * @augments wp.customize.Events
27 Loader = $.extend( {}, api.Events, {
29 * Setup the Loader; triggered on document#ready.
31 initialize: function() {
32 this.body = $( document.body );
34 // Ensure the loader is supported.
35 // Check for settings, postMessage support, and whether we require CORS support.
36 if ( ! Loader.settings || ! $.support.postMessage || ( ! $.support.cors && Loader.settings.isCrossDomain ) ) {
40 this.window = $( window );
41 this.element = $( '<div id="customize-container" />' ).appendTo( this.body );
43 // Bind events for opening and closing the overlay.
44 this.bind( 'open', this.overlay.show );
45 this.bind( 'close', this.overlay.hide );
47 // Any element in the body with the `load-customize` class opens
49 $('#wpbody').on( 'click', '.load-customize', function( event ) {
50 event.preventDefault();
52 // Store a reference to the link that opened the Customizer.
53 Loader.link = $(this);
55 Loader.open( Loader.link.attr('href') );
58 // Add navigation listeners.
59 if ( $.support.history ) {
60 this.window.on( 'popstate', Loader.popstate );
63 if ( $.support.hashchange ) {
64 this.window.on( 'hashchange', Loader.hashchange );
65 this.window.triggerHandler( 'hashchange' );
69 popstate: function( e ) {
70 var state = e.originalEvent.state;
71 if ( state && state.customize ) {
72 Loader.open( state.customize );
73 } else if ( Loader.active ) {
78 hashchange: function() {
79 var hash = window.location.toString().split('#')[1];
81 if ( hash && 0 === hash.indexOf( 'wp_customize=on' ) ) {
82 Loader.open( Loader.settings.url + '?' + hash );
85 if ( ! hash && ! $.support.history ) {
90 beforeunload: function () {
91 if ( ! Loader.saved() ) {
92 return Loader.settings.l10n.saveAlert;
97 * Open the Customizer overlay for a specific URL.
99 * @param string src URL to load in the Customizer.
101 open: function( src ) {
107 // Load the full page on mobile devices.
108 if ( Loader.settings.browser.mobile ) {
109 return window.location = src;
112 // Store the document title prior to opening the Live Preview
113 this.originalDocumentTitle = document.title;
116 this.body.addClass('customize-loading');
119 * Track the dirtiness state (whether the drafted changes have been published)
120 * of the Customizer in the iframe. This is used to decide whether to display
121 * an AYS alert if the user tries to close the window before saving changes.
123 this.saved = new api.Value( true );
125 this.iframe = $( '<iframe />', { 'src': src, 'title': Loader.settings.l10n.mainIframeTitle } ).appendTo( this.element );
126 this.iframe.one( 'load', this.loaded );
128 // Create a postMessage connection with the iframe.
129 this.messenger = new api.Messenger({
132 targetWindow: this.iframe[0].contentWindow
135 // Expose the changeset UUID on the parent window's URL so that the customized state can survive a refresh.
136 if ( history.replaceState ) {
137 this.messenger.bind( 'changeset-uuid', function( changesetUuid ) {
138 var urlParser = document.createElement( 'a' );
139 urlParser.href = location.href;
140 urlParser.search = $.param( _.extend(
141 api.utils.parseQueryString( urlParser.search.substr( 1 ) ),
142 { changeset_uuid: changesetUuid }
144 history.replaceState( { customize: urlParser.href }, '', urlParser.href );
148 // Wait for the connection from the iframe before sending any postMessage events.
149 this.messenger.bind( 'ready', function() {
150 Loader.messenger.send( 'back' );
153 this.messenger.bind( 'close', function() {
154 if ( $.support.history ) {
156 } else if ( $.support.hashchange ) {
157 window.location.hash = '';
163 // Prompt AYS dialog when navigating away
164 $( window ).on( 'beforeunload', this.beforeunload );
166 this.messenger.bind( 'saved', function () {
167 Loader.saved( true );
169 this.messenger.bind( 'change', function () {
170 Loader.saved( false );
173 this.messenger.bind( 'title', function( newTitle ){
174 window.document.title = newTitle;
177 this.pushState( src );
179 this.trigger( 'open' );
182 pushState: function ( src ) {
183 var hash = src.split( '?' )[1];
185 // Ensure we don't call pushState if the user hit the forward button.
186 if ( $.support.history && window.location.href !== src ) {
187 history.pushState( { customize: src }, '', src );
188 } else if ( ! $.support.history && $.support.hashchange && hash ) {
189 window.location.hash = 'wp_customize=on&' + hash;
192 this.trigger( 'open' );
196 * Callback after the Customizer has been opened.
199 Loader.body.addClass( 'customize-active full-overlay-active' ).attr( 'aria-busy', 'true' );
203 * Close the Customizer overlay.
206 if ( ! this.active ) {
210 // Display AYS dialog if Customizer is dirty
211 if ( ! this.saved() && ! confirm( Loader.settings.l10n.saveAlert ) ) {
212 // Go forward since Customizer is exited by history.back()
219 this.trigger( 'close' );
221 // Restore document title prior to opening the Live Preview
222 if ( this.originalDocumentTitle ) {
223 document.title = this.originalDocumentTitle;
228 * Callback after the Customizer has been closed.
231 Loader.iframe.remove();
232 Loader.messenger.destroy();
233 Loader.iframe = null;
234 Loader.messenger = null;
236 Loader.body.removeClass( 'customize-active full-overlay-active' ).removeClass( 'customize-loading' );
237 $( window ).off( 'beforeunload', Loader.beforeunload );
239 * Return focus to the link that opened the Customizer overlay after
240 * the body element visibility is restored.
248 * Callback for the `load` event on the Customizer iframe.
251 Loader.body.removeClass( 'customize-loading' ).attr( 'aria-busy', 'false' );
255 * Overlay hide/show utility methods.
259 this.element.fadeIn( 200, Loader.opened );
263 this.element.fadeOut( 200, Loader.closed );
268 // Bootstrap the Loader on document#ready.
270 Loader.settings = _wpCustomizeLoaderSettings;
274 // Expose the API publicly on window.wp.customize.Loader