Theme Customizer: Integrate with browser history. Use window.history by default, with window.onhashchange as a fallback. fixes #20337, see #19910.
git-svn-id: http://svn.automattic.com/wordpress/trunk@20488 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
9501ff721d
commit
e11cc3ec4f
|
@ -6,8 +6,14 @@ if ( typeof wp === 'undefined' )
|
||||||
Loader;
|
Loader;
|
||||||
|
|
||||||
Loader = {
|
Loader = {
|
||||||
|
supports: {
|
||||||
|
history: !! ( window.history && history.pushState ),
|
||||||
|
hashchange: ('onhashchange' in window) && (document.documentMode === undefined || document.documentMode > 7)
|
||||||
|
},
|
||||||
|
|
||||||
initialize: function() {
|
initialize: function() {
|
||||||
this.body = $( document.body ).addClass('customize-support');
|
this.body = $( document.body ).addClass('customize-support');
|
||||||
|
this.window = $( window );
|
||||||
this.element = $( '<div id="customize-container" class="wp-full-overlay" />' ).appendTo( this.body );
|
this.element = $( '<div id="customize-container" class="wp-full-overlay" />' ).appendTo( this.body );
|
||||||
|
|
||||||
$('#wpbody').on( 'click', '.load-customize', function( event ) {
|
$('#wpbody').on( 'click', '.load-customize', function( event ) {
|
||||||
|
@ -16,8 +22,35 @@ if ( typeof wp === 'undefined' )
|
||||||
// Load the theme.
|
// Load the theme.
|
||||||
Loader.open( $(this).attr('href') );
|
Loader.open( $(this).attr('href') );
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 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 );
|
||||||
|
|
||||||
|
if ( ! hash )
|
||||||
|
Loader.close();
|
||||||
},
|
},
|
||||||
open: function( src ) {
|
open: function( src ) {
|
||||||
|
if ( this.active )
|
||||||
|
return;
|
||||||
|
this.active = true;
|
||||||
|
|
||||||
this.iframe = $( '<iframe />', { src: src }).appendTo( this.element );
|
this.iframe = $( '<iframe />', { src: src }).appendTo( this.element );
|
||||||
|
|
||||||
// Create a postMessage connection with the iframe.
|
// Create a postMessage connection with the iframe.
|
||||||
|
@ -29,14 +62,31 @@ if ( typeof wp === 'undefined' )
|
||||||
});
|
});
|
||||||
|
|
||||||
this.messenger.bind( 'close', function() {
|
this.messenger.bind( 'close', function() {
|
||||||
|
if ( Loader.supports.history )
|
||||||
|
history.back();
|
||||||
|
else if ( Loader.supports.hashchange )
|
||||||
|
window.location.hash = '';
|
||||||
|
else
|
||||||
Loader.close();
|
Loader.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
this.element.fadeIn( 200, function() {
|
this.element.fadeIn( 200, function() {
|
||||||
|
var hash = src.split('?')[1];
|
||||||
|
|
||||||
Loader.body.addClass( 'customize-active full-overlay-active' );
|
Loader.body.addClass( 'customize-active full-overlay-active' );
|
||||||
|
|
||||||
|
// 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 );
|
||||||
|
else if ( Loader.supports.hashchange && hash )
|
||||||
|
window.location.hash = hash;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
close: function() {
|
close: function() {
|
||||||
|
if ( ! this.active )
|
||||||
|
return;
|
||||||
|
this.active = false;
|
||||||
|
|
||||||
this.element.fadeOut( 200, function() {
|
this.element.fadeOut( 200, function() {
|
||||||
Loader.iframe.remove();
|
Loader.iframe.remove();
|
||||||
Loader.iframe = null;
|
Loader.iframe = null;
|
||||||
|
@ -47,7 +97,7 @@ if ( typeof wp === 'undefined' )
|
||||||
};
|
};
|
||||||
|
|
||||||
$( function() {
|
$( function() {
|
||||||
if ( !! window.postMessage )
|
if ( window.postMessage )
|
||||||
Loader.initialize();
|
Loader.initialize();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -1590,6 +1590,7 @@ add_action( 'plugins_loaded', '_wp_customize_include' );
|
||||||
function _wp_customize_loader_localize() {
|
function _wp_customize_loader_localize() {
|
||||||
wp_localize_script( 'customize-loader', 'wpCustomizeLoaderL10n', array(
|
wp_localize_script( 'customize-loader', 'wpCustomizeLoaderL10n', array(
|
||||||
'back' => sprintf( __( '← Return to %s' ), get_admin_page_title() ),
|
'back' => sprintf( __( '← Return to %s' ), get_admin_page_title() ),
|
||||||
|
'url' => admin_url( 'admin.php' ),
|
||||||
) );
|
) );
|
||||||
}
|
}
|
||||||
add_action( 'admin_enqueue_scripts', '_wp_customize_loader_localize' );
|
add_action( 'admin_enqueue_scripts', '_wp_customize_loader_localize' );
|
||||||
|
|
Loading…
Reference in New Issue