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 // Wait for the connection from the iframe before sending any postMessage events.
136 this.messenger.bind( 'ready', function() {
137 Loader.messenger.send( 'back' );
140 this.messenger.bind( 'close', function() {
141 if ( $.support.history ) {
143 } else if ( $.support.hashchange ) {
144 window.location.hash = '';
150 // Prompt AYS dialog when navigating away
151 $( window ).on( 'beforeunload', this.beforeunload );
153 this.messenger.bind( 'activated', function( location ) {
155 window.location = location;
159 this.messenger.bind( 'saved', function () {
160 Loader.saved( true );
162 this.messenger.bind( 'change', function () {
163 Loader.saved( false );
166 this.messenger.bind( 'title', function( newTitle ){
167 window.document.title = newTitle;
170 this.pushState( src );
172 this.trigger( 'open' );
175 pushState: function ( src ) {
176 var hash = src.split( '?' )[1];
178 // Ensure we don't call pushState if the user hit the forward button.
179 if ( $.support.history && window.location.href !== src ) {
180 history.pushState( { customize: src }, '', src );
181 } else if ( ! $.support.history && $.support.hashchange && hash ) {
182 window.location.hash = 'wp_customize=on&' + hash;
185 this.trigger( 'open' );
189 * Callback after the Customizer has been opened.
192 Loader.body.addClass( 'customize-active full-overlay-active' );
196 * Close the Customizer overlay and return focus to the link that opened it.
199 if ( ! this.active ) {
203 // Display AYS dialog if Customizer is dirty
204 if ( ! this.saved() && ! confirm( Loader.settings.l10n.saveAlert ) ) {
205 // Go forward since Customizer is exited by history.back()
212 this.trigger( 'close' );
214 // Restore document title prior to opening the Live Preview
215 if ( this.originalDocumentTitle ) {
216 document.title = this.originalDocumentTitle;
219 // Return focus to link that was originally clicked.
226 * Callback after the Customizer has been closed.
229 Loader.iframe.remove();
230 Loader.messenger.destroy();
231 Loader.iframe = null;
232 Loader.messenger = null;
234 Loader.body.removeClass( 'customize-active full-overlay-active' ).removeClass( 'customize-loading' );
235 $( window ).off( 'beforeunload', Loader.beforeunload );
239 * Callback for the `load` event on the Customizer iframe.
242 Loader.body.removeClass('customize-loading');
246 * Overlay hide/show utility methods.
250 this.element.fadeIn( 200, Loader.opened );
254 this.element.fadeOut( 200, Loader.closed );
259 // Bootstrap the Loader on document#ready.
261 Loader.settings = _wpCustomizeLoaderSettings;
265 // Expose the API publicly on window.wp.customize.Loader