2012-02-24 23:12:43 -05:00
|
|
|
if ( typeof wp === 'undefined' )
|
|
|
|
var wp = {};
|
|
|
|
|
|
|
|
(function( exports, $ ){
|
2012-04-16 10:02:28 -04:00
|
|
|
var api = wp.customize,
|
|
|
|
Loader;
|
|
|
|
|
|
|
|
Loader = {
|
2012-04-16 19:07:23 -04:00
|
|
|
supports: {
|
|
|
|
history: !! ( window.history && history.pushState ),
|
|
|
|
hashchange: ('onhashchange' in window) && (document.documentMode === undefined || document.documentMode > 7)
|
|
|
|
},
|
|
|
|
|
2012-02-24 23:12:43 -05:00
|
|
|
initialize: function() {
|
2012-04-16 19:07:23 -04: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-24 23:12:43 -05:00
|
|
|
|
2012-04-16 10:02:28 -04:00
|
|
|
$('#wpbody').on( 'click', '.load-customize', function( event ) {
|
|
|
|
event.preventDefault();
|
2012-02-24 23:12:43 -05:00
|
|
|
|
2012-04-16 10:02:28 -04:00
|
|
|
// Load the theme.
|
|
|
|
Loader.open( $(this).attr('href') );
|
2012-02-24 23:12:43 -05:00
|
|
|
});
|
2012-04-16 19:07:23 -04: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 16:25:31 -04:00
|
|
|
if ( ! hash && ! Loader.supports.history )
|
2012-04-16 19:07:23 -04:00
|
|
|
Loader.close();
|
2012-02-24 23:12:43 -05:00
|
|
|
},
|
2012-04-16 10:02:28 -04:00
|
|
|
open: function( src ) {
|
2012-04-16 19:07:23 -04:00
|
|
|
if ( this.active )
|
|
|
|
return;
|
|
|
|
this.active = true;
|
2012-05-08 19:37:05 -04:00
|
|
|
this.body.addClass('customize-loading');
|
2012-04-16 19:07:23 -04:00
|
|
|
|
2012-04-16 10:02:28 -04:00
|
|
|
this.iframe = $( '<iframe />', { src: src }).appendTo( this.element );
|
2012-05-08 19:37:05 -04:00
|
|
|
this.iframe.one( 'load', this.loaded );
|
2012-04-16 10:02:28 -04:00
|
|
|
|
|
|
|
// 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-24 23:12:43 -05:00
|
|
|
|
2012-04-16 10:02:28 -04:00
|
|
|
this.messenger.bind( 'close', function() {
|
2012-04-16 19:07:23 -04:00
|
|
|
if ( Loader.supports.history )
|
|
|
|
history.back();
|
|
|
|
else if ( Loader.supports.hashchange )
|
|
|
|
window.location.hash = '';
|
|
|
|
else
|
|
|
|
Loader.close();
|
2012-04-16 10:02:28 -04:00
|
|
|
});
|
2012-02-24 23:12:43 -05:00
|
|
|
|
|
|
|
this.element.fadeIn( 200, function() {
|
2012-04-16 19:07:23 -04:00
|
|
|
var hash = src.split('?')[1];
|
|
|
|
|
2012-03-06 22:19:35 -05:00
|
|
|
Loader.body.addClass( 'customize-active full-overlay-active' );
|
2012-04-16 19:07:23 -04: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 16:25:31 -04:00
|
|
|
else if ( ! Loader.supports.history && Loader.supports.hashchange && hash )
|
2012-04-16 19:07:23 -04:00
|
|
|
window.location.hash = hash;
|
2012-02-24 23:12:43 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
close: function() {
|
2012-04-16 19:07:23 -04:00
|
|
|
if ( ! this.active )
|
|
|
|
return;
|
|
|
|
this.active = false;
|
|
|
|
|
2012-02-24 23:12:43 -05:00
|
|
|
this.element.fadeOut( 200, function() {
|
|
|
|
Loader.iframe.remove();
|
2012-04-18 16:25:31 -04:00
|
|
|
Loader.messenger.destroy();
|
2012-04-16 10:02:28 -04:00
|
|
|
Loader.iframe = null;
|
|
|
|
Loader.messenger = null;
|
2012-05-08 19:37:05 -04:00
|
|
|
Loader.body.removeClass( 'customize-active full-overlay-active' ).removeClass( 'customize-loading' );
|
2012-02-24 23:12:43 -05:00
|
|
|
});
|
2012-05-08 19:37:05 -04:00
|
|
|
},
|
|
|
|
loaded: function() {
|
|
|
|
Loader.body.removeClass('customize-loading');
|
2012-02-24 23:12:43 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$( function() {
|
2012-04-16 19:07:23 -04:00
|
|
|
if ( window.postMessage )
|
2012-04-16 10:02:28 -04:00
|
|
|
Loader.initialize();
|
2012-02-24 23:12:43 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
// Expose the API to the world.
|
2012-04-16 10:02:28 -04:00
|
|
|
api.Loader = Loader;
|
2012-02-24 23:12:43 -05:00
|
|
|
})( wp, jQuery );
|