1 if ( typeof wp === 'undefined' )
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)
13 Loader = $.extend( {}, api.Events, {
14 initialize: function() {
15 this.body = $( document.body );
17 // Ensure the loader is supported.
18 // Check for settings, postMessage support, and whether we require CORS support.
19 if ( ! Loader.settings || ! $.support.postMessage || ( ! $.support.cors && Loader.settings.isCrossDomain ) ) {
23 this.window = $( window );
24 this.element = $( '<div id="customize-container" />' ).appendTo( this.body );
26 this.bind( 'open', this.overlay.show );
27 this.bind( 'close', this.overlay.hide );
29 $('#wpbody').on( 'click', '.load-customize', function( event ) {
30 event.preventDefault();
33 Loader.open( $(this).attr('href') );
36 // Add navigation listeners.
37 if ( $.support.history )
38 this.window.on( 'popstate', Loader.popstate );
40 if ( $.support.hashchange ) {
41 this.window.on( 'hashchange', Loader.hashchange );
42 this.window.triggerHandler( 'hashchange' );
46 popstate: function( e ) {
47 var state = e.originalEvent.state;
48 if ( state && state.customize )
49 Loader.open( state.customize );
50 else if ( Loader.active )
54 hashchange: function( e ) {
55 var hash = window.location.toString().split('#')[1];
57 if ( hash && 0 === hash.indexOf( 'wp_customize=on' ) )
58 Loader.open( Loader.settings.url + '?' + hash );
60 if ( ! hash && ! $.support.history )
64 open: function( src ) {
70 // Load the full page on mobile devices.
71 if ( Loader.settings.browser.mobile )
72 return window.location = src;
75 this.body.addClass('customize-loading');
77 this.iframe = $( '<iframe />', { src: src }).appendTo( this.element );
78 this.iframe.one( 'load', this.loaded );
80 // Create a postMessage connection with the iframe.
81 this.messenger = new api.Messenger({
84 targetWindow: this.iframe[0].contentWindow
87 // Wait for the connection from the iframe before sending any postMessage events.
88 this.messenger.bind( 'ready', function() {
89 Loader.messenger.send( 'back' );
92 this.messenger.bind( 'close', function() {
93 if ( $.support.history )
95 else if ( $.support.hashchange )
96 window.location.hash = '';
101 this.messenger.bind( 'activated', function( location ) {
103 window.location = location;
106 hash = src.split('?')[1];
108 // Ensure we don't call pushState if the user hit the forward button.
109 if ( $.support.history && window.location.href !== src )
110 history.pushState( { customize: src }, '', src );
111 else if ( ! $.support.history && $.support.hashchange && hash )
112 window.location.hash = 'wp_customize=on&' + hash;
114 this.trigger( 'open' );
118 Loader.body.addClass( 'customize-active full-overlay-active' );
126 this.trigger( 'close' );
130 Loader.iframe.remove();
131 Loader.messenger.destroy();
132 Loader.iframe = null;
133 Loader.messenger = null;
134 Loader.body.removeClass( 'customize-active full-overlay-active' ).removeClass( 'customize-loading' );
138 Loader.body.removeClass('customize-loading');
143 this.element.fadeIn( 200, Loader.opened );
147 this.element.fadeOut( 200, Loader.closed );
153 Loader.settings = _wpCustomizeLoaderSettings;
157 // Expose the API to the world.