2012-02-25 05:12:43 +01:00
|
|
|
if ( typeof wp === 'undefined' )
|
|
|
|
var wp = {};
|
|
|
|
|
|
|
|
(function( exports, $ ){
|
2012-04-16 16:02:28 +02:00
|
|
|
var api = wp.customize,
|
|
|
|
Loader;
|
|
|
|
|
|
|
|
Loader = {
|
2012-04-17 01:07:23 +02:00
|
|
|
supports: {
|
|
|
|
history: !! ( window.history && history.pushState ),
|
|
|
|
hashchange: ('onhashchange' in window) && (document.documentMode === undefined || document.documentMode > 7)
|
|
|
|
},
|
|
|
|
|
2012-02-25 05:12:43 +01:00
|
|
|
initialize: function() {
|
2012-04-17 01:07:23 +02:00
|
|
|
this.body = $( document.body ).addClass('customize-support');
|
|
|
|
this.window = $( window );
|
|
|
|
this.element = $( '<div id="customize-container" class="wp-full-overlay" />' ).appendTo( this.body );
|
2012-02-25 05:12:43 +01:00
|
|
|
|
2012-04-16 16:02:28 +02:00
|
|
|
$('#wpbody').on( 'click', '.load-customize', function( event ) {
|
|
|
|
event.preventDefault();
|
2012-02-25 05:12:43 +01:00
|
|
|
|
2012-04-16 16:02:28 +02:00
|
|
|
// Load the theme.
|
|
|
|
Loader.open( $(this).attr('href') );
|
2012-02-25 05:12:43 +01:00
|
|
|
});
|
2012-04-17 01:07:23 +02:00
|
|
|
|
|
|
|
// Add navigation listeners.
|
|
|
|
if ( this.supports.history )
|
|
|
|
this.window.on( 'popstate', Loader.popstate );
|
|
|
|
|
|
|
|
if ( this.supports.hashchange )
|
|
|
|
this.window.on( 'hashchange', Loader.hashchange );
|
|
|
|
},
|
|
|
|
popstate: function( e ) {
|
|
|
|
var state = e.originalEvent.state;
|
|
|
|
if ( state && state.customize )
|
|
|
|
Loader.open( state.customize );
|
|
|
|
else if ( Loader.active )
|
|
|
|
Loader.close();
|
|
|
|
},
|
|
|
|
hashchange: function( e ) {
|
|
|
|
var hash = window.location.toString().split('#')[1];
|
|
|
|
|
|
|
|
if ( hash && 0 === hash.indexOf( 'customize=on' ) )
|
|
|
|
Loader.open( wpCustomizeLoaderL10n.url + '?' + hash );
|
|
|
|
|
2012-04-18 22:25:31 +02:00
|
|
|
if ( ! hash && ! Loader.supports.history )
|
2012-04-17 01:07:23 +02:00
|
|
|
Loader.close();
|
2012-02-25 05:12:43 +01:00
|
|
|
},
|
2012-04-16 16:02:28 +02:00
|
|
|
open: function( src ) {
|
2012-04-17 01:07:23 +02:00
|
|
|
if ( this.active )
|
|
|
|
return;
|
|
|
|
this.active = true;
|
|
|
|
|
2012-04-16 16:02:28 +02:00
|
|
|
this.iframe = $( '<iframe />', { src: src }).appendTo( this.element );
|
|
|
|
|
|
|
|
// Create a postMessage connection with the iframe.
|
|
|
|
this.messenger = new api.Messenger( src, this.iframe[0].contentWindow );
|
|
|
|
|
|
|
|
// Wait for the connection from the iframe before sending any postMessage events.
|
|
|
|
this.messenger.bind( 'ready', function() {
|
|
|
|
Loader.messenger.send( 'back', wpCustomizeLoaderL10n.back );
|
|
|
|
});
|
2012-02-25 05:12:43 +01:00
|
|
|
|
2012-04-16 16:02:28 +02:00
|
|
|
this.messenger.bind( 'close', function() {
|
2012-04-17 01:07:23 +02:00
|
|
|
if ( Loader.supports.history )
|
|
|
|
history.back();
|
|
|
|
else if ( Loader.supports.hashchange )
|
|
|
|
window.location.hash = '';
|
|
|
|
else
|
|
|
|
Loader.close();
|
2012-04-16 16:02:28 +02:00
|
|
|
});
|
2012-02-25 05:12:43 +01:00
|
|
|
|
|
|
|
this.element.fadeIn( 200, function() {
|
2012-04-17 01:07:23 +02:00
|
|
|
var hash = src.split('?')[1];
|
|
|
|
|
2012-03-07 04:19:35 +01:00
|
|
|
Loader.body.addClass( 'customize-active full-overlay-active' );
|
2012-04-17 01:07:23 +02:00
|
|
|
|
|
|
|
// Ensure we don't call pushState if the user hit the forward button.
|
|
|
|
if ( Loader.supports.history && window.location.href !== src )
|
|
|
|
history.pushState( { customize: src }, '', src );
|
2012-04-18 22:25:31 +02:00
|
|
|
else if ( ! Loader.supports.history && Loader.supports.hashchange && hash )
|
2012-04-17 01:07:23 +02:00
|
|
|
window.location.hash = hash;
|
2012-02-25 05:12:43 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
close: function() {
|
2012-04-17 01:07:23 +02:00
|
|
|
if ( ! this.active )
|
|
|
|
return;
|
|
|
|
this.active = false;
|
|
|
|
|
2012-02-25 05:12:43 +01:00
|
|
|
this.element.fadeOut( 200, function() {
|
|
|
|
Loader.iframe.remove();
|
2012-04-18 22:25:31 +02:00
|
|
|
Loader.messenger.destroy();
|
2012-04-16 16:02:28 +02:00
|
|
|
Loader.iframe = null;
|
|
|
|
Loader.messenger = null;
|
2012-03-07 04:19:35 +01:00
|
|
|
Loader.body.removeClass( 'customize-active full-overlay-active' );
|
2012-02-25 05:12:43 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$( function() {
|
2012-04-17 01:07:23 +02:00
|
|
|
if ( window.postMessage )
|
2012-04-16 16:02:28 +02:00
|
|
|
Loader.initialize();
|
2012-02-25 05:12:43 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// Expose the API to the world.
|
2012-04-16 16:02:28 +02:00
|
|
|
api.Loader = Loader;
|
2012-02-25 05:12:43 +01:00
|
|
|
})( wp, jQuery );
|