1 /* global _wpCustomizeLoaderSettings */
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)
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();
32 // Store a reference to the link that opened the customizer.
33 Loader.link = $(this);
35 Loader.open( Loader.link.attr('href') );
38 // Add navigation listeners.
39 if ( $.support.history )
40 this.window.on( 'popstate', Loader.popstate );
42 if ( $.support.hashchange ) {
43 this.window.on( 'hashchange', Loader.hashchange );
44 this.window.triggerHandler( 'hashchange' );
48 popstate: function( e ) {
49 var state = e.originalEvent.state;
50 if ( state && state.customize )
51 Loader.open( state.customize );
52 else if ( Loader.active )
56 hashchange: function() {
57 var hash = window.location.toString().split('#')[1];
59 if ( hash && 0 === hash.indexOf( 'wp_customize=on' ) )
60 Loader.open( Loader.settings.url + '?' + hash );
62 if ( ! hash && ! $.support.history )
66 open: function( src ) {
72 // Load the full page on mobile devices.
73 if ( Loader.settings.browser.mobile )
74 return window.location = src;
77 this.body.addClass('customize-loading');
79 this.iframe = $( '<iframe />', { src: src }).appendTo( this.element );
80 this.iframe.one( 'load', this.loaded );
82 // Create a postMessage connection with the iframe.
83 this.messenger = new api.Messenger({
86 targetWindow: this.iframe[0].contentWindow
89 // Wait for the connection from the iframe before sending any postMessage events.
90 this.messenger.bind( 'ready', function() {
91 Loader.messenger.send( 'back' );
94 this.messenger.bind( 'close', function() {
95 if ( $.support.history )
97 else if ( $.support.hashchange )
98 window.location.hash = '';
103 this.messenger.bind( 'activated', function( location ) {
105 window.location = location;
108 hash = src.split('?')[1];
110 // Ensure we don't call pushState if the user hit the forward button.
111 if ( $.support.history && window.location.href !== src )
112 history.pushState( { customize: src }, '', src );
113 else if ( ! $.support.history && $.support.hashchange && hash )
114 window.location.hash = 'wp_customize=on&' + hash;
116 this.trigger( 'open' );
120 Loader.body.addClass( 'customize-active full-overlay-active' );
128 this.trigger( 'close' );
130 // Return focus to link that was originally clicked.
136 Loader.iframe.remove();
137 Loader.messenger.destroy();
138 Loader.iframe = null;
139 Loader.messenger = null;
140 Loader.body.removeClass( 'customize-active full-overlay-active' ).removeClass( 'customize-loading' );
144 Loader.body.removeClass('customize-loading');
149 this.element.fadeIn( 200, Loader.opened );
153 this.element.fadeOut( 200, Loader.closed );
159 Loader.settings = _wpCustomizeLoaderSettings;
163 // Expose the API to the world.