1 window.wp = window.wp || {};
3 (function( exports, $ ){
4 var api = wp.customize,
8 history: !! ( window.history && history.pushState ),
9 hashchange: ('onhashchange' in window) && (document.documentMode === undefined || document.documentMode > 7)
12 Loader = $.extend( {}, api.Events, {
13 initialize: function() {
14 this.body = $( document.body );
16 // Ensure the loader is supported.
17 // Check for settings, postMessage support, and whether we require CORS support.
18 if ( ! Loader.settings || ! $.support.postMessage || ( ! $.support.cors && Loader.settings.isCrossDomain ) ) {
22 this.window = $( window );
23 this.element = $( '<div id="customize-container" />' ).appendTo( this.body );
25 this.bind( 'open', this.overlay.show );
26 this.bind( 'close', this.overlay.hide );
28 $('#wpbody').on( 'click', '.load-customize', function( event ) {
29 event.preventDefault();
31 // Store a reference to the link that opened the customizer.
32 Loader.link = $(this);
34 Loader.open( Loader.link.attr('href') );
37 // Add navigation listeners.
38 if ( $.support.history )
39 this.window.on( 'popstate', Loader.popstate );
41 if ( $.support.hashchange ) {
42 this.window.on( 'hashchange', Loader.hashchange );
43 this.window.triggerHandler( 'hashchange' );
47 popstate: function( e ) {
48 var state = e.originalEvent.state;
49 if ( state && state.customize )
50 Loader.open( state.customize );
51 else if ( Loader.active )
55 hashchange: function( e ) {
56 var hash = window.location.toString().split('#')[1];
58 if ( hash && 0 === hash.indexOf( 'wp_customize=on' ) )
59 Loader.open( Loader.settings.url + '?' + hash );
61 if ( ! hash && ! $.support.history )
65 open: function( src ) {
71 // Load the full page on mobile devices.
72 if ( Loader.settings.browser.mobile )
73 return window.location = src;
76 this.body.addClass('customize-loading');
78 this.iframe = $( '<iframe />', { src: src }).appendTo( this.element );
79 this.iframe.one( 'load', this.loaded );
81 // Create a postMessage connection with the iframe.
82 this.messenger = new api.Messenger({
85 targetWindow: this.iframe[0].contentWindow
88 // Wait for the connection from the iframe before sending any postMessage events.
89 this.messenger.bind( 'ready', function() {
90 Loader.messenger.send( 'back' );
93 this.messenger.bind( 'close', function() {
94 if ( $.support.history )
96 else if ( $.support.hashchange )
97 window.location.hash = '';
102 this.messenger.bind( 'activated', function( location ) {
104 window.location = location;
107 hash = src.split('?')[1];
109 // Ensure we don't call pushState if the user hit the forward button.
110 if ( $.support.history && window.location.href !== src )
111 history.pushState( { customize: src }, '', src );
112 else if ( ! $.support.history && $.support.hashchange && hash )
113 window.location.hash = 'wp_customize=on&' + hash;
115 this.trigger( 'open' );
119 Loader.body.addClass( 'customize-active full-overlay-active' );
127 this.trigger( 'close' );
129 // Return focus to link that was originally clicked.
135 Loader.iframe.remove();
136 Loader.messenger.destroy();
137 Loader.iframe = null;
138 Loader.messenger = null;
139 Loader.body.removeClass( 'customize-active full-overlay-active' ).removeClass( 'customize-loading' );
143 Loader.body.removeClass('customize-loading');
148 this.element.fadeIn( 200, Loader.opened );
152 this.element.fadeOut( 200, Loader.closed );
158 Loader.settings = _wpCustomizeLoaderSettings;
162 // Expose the API to the world.