2014-07-08 19:04:15 +02:00
|
|
|
/* global _wpCustomizeLoaderSettings, confirm */
|
2012-10-06 02:43:36 +02:00
|
|
|
window.wp = window.wp || {};
|
2012-08-23 02:04:18 +02:00
|
|
|
|
|
|
|
(function( exports, $ ){
|
|
|
|
var api = wp.customize,
|
|
|
|
Loader;
|
|
|
|
|
|
|
|
$.extend( $.support, {
|
|
|
|
history: !! ( window.history && history.pushState ),
|
|
|
|
hashchange: ('onhashchange' in window) && (document.documentMode === undefined || document.documentMode > 7)
|
|
|
|
});
|
|
|
|
|
2014-08-09 00:10:15 +02:00
|
|
|
/**
|
|
|
|
* Allows the Customizer to be overlayed on any page.
|
|
|
|
*
|
|
|
|
* By default, any element in the body with the load-customize class will open
|
2014-10-05 20:40:15 +02:00
|
|
|
* an iframe overlay with the URL specified.
|
2014-08-09 00:10:15 +02:00
|
|
|
*
|
2014-10-15 19:21:19 +02:00
|
|
|
* e.g. <a class="load-customize" href="<?php echo wp_customize_url(); ?>">Open Customizer</a>
|
2014-08-09 00:10:15 +02:00
|
|
|
*
|
|
|
|
* @augments wp.customize.Events
|
|
|
|
*/
|
2012-08-23 02:04:18 +02:00
|
|
|
Loader = $.extend( {}, api.Events, {
|
2014-08-09 00:10:15 +02:00
|
|
|
/**
|
|
|
|
* Setup the Loader; triggered on document#ready.
|
|
|
|
*/
|
2012-08-23 02:04:18 +02:00
|
|
|
initialize: function() {
|
|
|
|
this.body = $( document.body );
|
|
|
|
|
|
|
|
// Ensure the loader is supported.
|
|
|
|
// Check for settings, postMessage support, and whether we require CORS support.
|
|
|
|
if ( ! Loader.settings || ! $.support.postMessage || ( ! $.support.cors && Loader.settings.isCrossDomain ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.window = $( window );
|
|
|
|
this.element = $( '<div id="customize-container" />' ).appendTo( this.body );
|
|
|
|
|
2014-08-09 00:10:15 +02:00
|
|
|
// Bind events for opening and closing the overlay.
|
2012-08-23 02:04:18 +02:00
|
|
|
this.bind( 'open', this.overlay.show );
|
|
|
|
this.bind( 'close', this.overlay.hide );
|
|
|
|
|
2014-08-09 00:10:15 +02:00
|
|
|
// Any element in the body with the `load-customize` class opens
|
|
|
|
// the Customizer.
|
2012-08-23 02:04:18 +02:00
|
|
|
$('#wpbody').on( 'click', '.load-customize', function( event ) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
2014-10-15 19:21:19 +02:00
|
|
|
// Store a reference to the link that opened the Customizer.
|
2012-11-21 14:17:10 +01:00
|
|
|
Loader.link = $(this);
|
2012-08-23 02:04:18 +02:00
|
|
|
// Load the theme.
|
2012-11-21 14:17:10 +01:00
|
|
|
Loader.open( Loader.link.attr('href') );
|
2012-08-23 02:04:18 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// Add navigation listeners.
|
2014-07-08 19:04:15 +02:00
|
|
|
if ( $.support.history ) {
|
2012-08-23 02:04:18 +02:00
|
|
|
this.window.on( 'popstate', Loader.popstate );
|
2014-07-08 19:04:15 +02:00
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
|
|
|
|
if ( $.support.hashchange ) {
|
|
|
|
this.window.on( 'hashchange', Loader.hashchange );
|
|
|
|
this.window.triggerHandler( 'hashchange' );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
popstate: function( e ) {
|
|
|
|
var state = e.originalEvent.state;
|
2014-07-08 19:04:15 +02:00
|
|
|
if ( state && state.customize ) {
|
2012-08-23 02:04:18 +02:00
|
|
|
Loader.open( state.customize );
|
2014-07-08 19:04:15 +02:00
|
|
|
} else if ( Loader.active ) {
|
2012-08-23 02:04:18 +02:00
|
|
|
Loader.close();
|
2014-07-08 19:04:15 +02:00
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
},
|
|
|
|
|
2013-11-15 05:31:10 +01:00
|
|
|
hashchange: function() {
|
2012-08-23 02:04:18 +02:00
|
|
|
var hash = window.location.toString().split('#')[1];
|
|
|
|
|
2014-07-08 19:04:15 +02:00
|
|
|
if ( hash && 0 === hash.indexOf( 'wp_customize=on' ) ) {
|
2012-08-23 02:04:18 +02:00
|
|
|
Loader.open( Loader.settings.url + '?' + hash );
|
2014-07-08 19:04:15 +02:00
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2014-11-11 23:29:23 +01:00
|
|
|
if ( ! hash && ! $.support.history ) {
|
2012-08-23 02:04:18 +02:00
|
|
|
Loader.close();
|
2014-07-08 19:04:15 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
beforeunload: function () {
|
|
|
|
if ( ! Loader.saved() ) {
|
|
|
|
return Loader.settings.l10n.saveAlert;
|
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
},
|
|
|
|
|
2014-08-09 00:10:15 +02:00
|
|
|
/**
|
2014-10-15 19:21:19 +02:00
|
|
|
* Open the Customizer overlay for a specific URL.
|
2014-08-09 00:10:15 +02:00
|
|
|
*
|
|
|
|
* @param string src URL to load in the Customizer.
|
|
|
|
*/
|
2012-08-23 02:04:18 +02:00
|
|
|
open: function( src ) {
|
|
|
|
|
2014-07-08 19:04:15 +02:00
|
|
|
if ( this.active ) {
|
2012-08-23 02:04:18 +02:00
|
|
|
return;
|
2014-07-08 19:04:15 +02:00
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
|
|
|
|
// Load the full page on mobile devices.
|
2014-07-08 19:04:15 +02:00
|
|
|
if ( Loader.settings.browser.mobile ) {
|
2012-08-23 02:04:18 +02:00
|
|
|
return window.location = src;
|
2014-07-08 19:04:15 +02:00
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2014-11-11 23:29:23 +01:00
|
|
|
// Store the document title prior to opening the Live Preview
|
|
|
|
this.originalDocumentTitle = document.title;
|
|
|
|
|
2012-08-23 02:04:18 +02:00
|
|
|
this.active = true;
|
|
|
|
this.body.addClass('customize-loading');
|
|
|
|
|
2014-10-15 19:21:19 +02:00
|
|
|
// Dirty state of Customizer in iframe
|
2014-07-08 19:04:15 +02:00
|
|
|
this.saved = new api.Value( true );
|
|
|
|
|
2012-08-23 02:04:18 +02:00
|
|
|
this.iframe = $( '<iframe />', { src: src }).appendTo( this.element );
|
|
|
|
this.iframe.one( 'load', this.loaded );
|
|
|
|
|
|
|
|
// Create a postMessage connection with the iframe.
|
|
|
|
this.messenger = new api.Messenger({
|
|
|
|
url: src,
|
|
|
|
channel: 'loader',
|
|
|
|
targetWindow: 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' );
|
|
|
|
});
|
|
|
|
|
|
|
|
this.messenger.bind( 'close', function() {
|
2014-07-08 19:04:15 +02:00
|
|
|
if ( $.support.history ) {
|
2012-08-23 02:04:18 +02:00
|
|
|
history.back();
|
2014-07-08 19:04:15 +02:00
|
|
|
} else if ( $.support.hashchange ) {
|
2012-08-23 02:04:18 +02:00
|
|
|
window.location.hash = '';
|
2014-07-08 19:04:15 +02:00
|
|
|
} else {
|
2012-08-23 02:04:18 +02:00
|
|
|
Loader.close();
|
2014-07-08 19:04:15 +02:00
|
|
|
}
|
2014-11-11 23:29:23 +01:00
|
|
|
});
|
2014-07-08 19:04:15 +02:00
|
|
|
|
|
|
|
// Prompt AYS dialog when navigating away
|
|
|
|
$( window ).on( 'beforeunload', this.beforeunload );
|
2012-08-23 02:04:18 +02:00
|
|
|
|
|
|
|
this.messenger.bind( 'activated', function( location ) {
|
2014-07-08 19:04:15 +02:00
|
|
|
if ( location ) {
|
2012-08-23 02:04:18 +02:00
|
|
|
window.location = location;
|
2014-07-08 19:04:15 +02:00
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
});
|
|
|
|
|
2014-07-08 19:04:15 +02:00
|
|
|
this.messenger.bind( 'saved', function () {
|
|
|
|
Loader.saved( true );
|
|
|
|
} );
|
|
|
|
this.messenger.bind( 'change', function () {
|
|
|
|
Loader.saved( false );
|
|
|
|
} );
|
|
|
|
|
|
|
|
this.pushState( src );
|
|
|
|
|
|
|
|
this.trigger( 'open' );
|
|
|
|
},
|
|
|
|
|
|
|
|
pushState: function ( src ) {
|
2014-11-11 23:29:23 +01:00
|
|
|
var hash = src.split( '?' )[1];
|
2012-08-23 02:04:18 +02:00
|
|
|
|
|
|
|
// Ensure we don't call pushState if the user hit the forward button.
|
2014-07-08 19:04:15 +02:00
|
|
|
if ( $.support.history && window.location.href !== src ) {
|
2012-08-23 02:04:18 +02:00
|
|
|
history.pushState( { customize: src }, '', src );
|
2014-07-08 19:04:15 +02:00
|
|
|
} else if ( ! $.support.history && $.support.hashchange && hash ) {
|
2012-08-23 02:04:18 +02:00
|
|
|
window.location.hash = 'wp_customize=on&' + hash;
|
2014-07-08 19:04:15 +02:00
|
|
|
}
|
2014-11-11 23:29:23 +01:00
|
|
|
|
|
|
|
this.trigger( 'open' );
|
2012-08-23 02:04:18 +02:00
|
|
|
},
|
|
|
|
|
2014-08-09 00:10:15 +02:00
|
|
|
/**
|
2014-10-15 19:21:19 +02:00
|
|
|
* Callback after the Customizer has been opened.
|
2014-08-09 00:10:15 +02:00
|
|
|
*/
|
2012-08-23 02:04:18 +02:00
|
|
|
opened: function() {
|
|
|
|
Loader.body.addClass( 'customize-active full-overlay-active' );
|
|
|
|
},
|
|
|
|
|
2014-08-09 00:10:15 +02:00
|
|
|
/**
|
|
|
|
* Close the Customizer overlay and return focus to the link that opened it.
|
|
|
|
*/
|
2012-08-23 02:04:18 +02:00
|
|
|
close: function() {
|
2014-07-08 19:04:15 +02:00
|
|
|
if ( ! this.active ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-15 19:21:19 +02:00
|
|
|
// Display AYS dialog if Customizer is dirty
|
2014-07-08 19:04:15 +02:00
|
|
|
if ( ! this.saved() && ! confirm( Loader.settings.l10n.saveAlert ) ) {
|
|
|
|
// Go forward since Customizer is exited by history.back()
|
|
|
|
history.forward();
|
2012-08-23 02:04:18 +02:00
|
|
|
return;
|
2014-07-08 19:04:15 +02:00
|
|
|
}
|
|
|
|
|
2012-08-23 02:04:18 +02:00
|
|
|
this.active = false;
|
|
|
|
|
|
|
|
this.trigger( 'close' );
|
2012-11-21 14:17:10 +01:00
|
|
|
|
2014-11-11 23:29:23 +01:00
|
|
|
// Restore document title prior to opening the Live Preview
|
|
|
|
if ( this.originalDocumentTitle ) {
|
|
|
|
document.title = this.originalDocumentTitle;
|
|
|
|
}
|
|
|
|
|
2012-11-21 14:17:10 +01:00
|
|
|
// Return focus to link that was originally clicked.
|
2014-07-08 19:04:15 +02:00
|
|
|
if ( this.link ) {
|
2012-11-21 14:17:10 +01:00
|
|
|
this.link.focus();
|
2014-07-08 19:04:15 +02:00
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
},
|
|
|
|
|
2014-08-09 00:10:15 +02:00
|
|
|
/**
|
2014-10-15 19:21:19 +02:00
|
|
|
* Callback after the Customizer has been closed.
|
2014-08-09 00:10:15 +02:00
|
|
|
*/
|
2012-08-23 02:04:18 +02:00
|
|
|
closed: function() {
|
|
|
|
Loader.iframe.remove();
|
|
|
|
Loader.messenger.destroy();
|
|
|
|
Loader.iframe = null;
|
|
|
|
Loader.messenger = null;
|
2014-07-08 19:04:15 +02:00
|
|
|
Loader.saved = null;
|
2012-08-23 02:04:18 +02:00
|
|
|
Loader.body.removeClass( 'customize-active full-overlay-active' ).removeClass( 'customize-loading' );
|
2014-07-08 19:04:15 +02:00
|
|
|
$( window ).off( 'beforeunload', Loader.beforeunload );
|
2012-08-23 02:04:18 +02:00
|
|
|
},
|
|
|
|
|
2014-08-09 00:10:15 +02:00
|
|
|
/**
|
|
|
|
* Callback for the `load` event on the Customizer iframe.
|
|
|
|
*/
|
2012-08-23 02:04:18 +02:00
|
|
|
loaded: function() {
|
|
|
|
Loader.body.removeClass('customize-loading');
|
|
|
|
},
|
|
|
|
|
2014-08-09 00:10:15 +02:00
|
|
|
/**
|
|
|
|
* Overlay hide/show utility methods.
|
|
|
|
*/
|
2012-08-23 02:04:18 +02:00
|
|
|
overlay: {
|
|
|
|
show: function() {
|
|
|
|
this.element.fadeIn( 200, Loader.opened );
|
|
|
|
},
|
|
|
|
|
|
|
|
hide: function() {
|
|
|
|
this.element.fadeOut( 200, Loader.closed );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-08-09 00:10:15 +02:00
|
|
|
// Bootstrap the Loader on document#ready.
|
2012-08-23 02:04:18 +02:00
|
|
|
$( function() {
|
|
|
|
Loader.settings = _wpCustomizeLoaderSettings;
|
|
|
|
Loader.initialize();
|
|
|
|
});
|
|
|
|
|
2014-08-09 00:10:15 +02:00
|
|
|
// Expose the API publicly on window.wp.customize.Loader
|
2012-08-23 02:04:18 +02:00
|
|
|
api.Loader = Loader;
|
|
|
|
})( wp, jQuery );
|