1
/* global _wpCustomizeLoaderSettings, confirm */
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)
14
* Allows the Customizer to be overlayed on any page.
16
* By default, any element in the body with the load-customize class will open
17
* the Customizer overlay with the URL specified.
19
* e.g. <a class="load-customize" href="http://siteurl.com/2014/01/02/post">Open customizer</a>
21
* @augments wp.customize.Events
23
Loader = $.extend( {}, api.Events, {
25
* Setup the Loader; triggered on document#ready.
27
initialize: function() {
28
this.body = $( document.body );
30
// Ensure the loader is supported.
31
// Check for settings, postMessage support, and whether we require CORS support.
32
if ( ! Loader.settings || ! $.support.postMessage || ( ! $.support.cors && Loader.settings.isCrossDomain ) ) {
36
this.window = $( window );
37
this.element = $( '<div id="customize-container" />' ).appendTo( this.body );
39
// Bind events for opening and closing the overlay.
40
this.bind( 'open', this.overlay.show );
41
this.bind( 'close', this.overlay.hide );
43
// Any element in the body with the `load-customize` class opens
45
$('#wpbody').on( 'click', '.load-customize', function( event ) {
46
event.preventDefault();
48
// Store a reference to the link that opened the customizer.
49
Loader.link = $(this);
51
Loader.open( Loader.link.attr('href') );
54
// Add navigation listeners.
55
if ( $.support.history ) {
56
this.window.on( 'popstate', Loader.popstate );
59
if ( $.support.hashchange ) {
60
this.window.on( 'hashchange', Loader.hashchange );
61
this.window.triggerHandler( 'hashchange' );
65
popstate: function( e ) {
66
var state = e.originalEvent.state;
67
if ( state && state.customize ) {
68
Loader.open( state.customize );
69
} else if ( Loader.active ) {
74
hashchange: function() {
75
var hash = window.location.toString().split('#')[1];
77
if ( hash && 0 === hash.indexOf( 'wp_customize=on' ) ) {
78
Loader.open( Loader.settings.url + '?' + hash );
81
if ( ! hash && ! $.support.history ){
86
beforeunload: function () {
87
if ( ! Loader.saved() ) {
88
return Loader.settings.l10n.saveAlert;
93
* Open the customizer overlay for a specific URL.
95
* @param string src URL to load in the Customizer.
97
open: function( src ) {
103
// Load the full page on mobile devices.
104
if ( Loader.settings.browser.mobile ) {
105
return window.location = src;
109
this.body.addClass('customize-loading');
111
// Dirty state of customizer in iframe
112
this.saved = new api.Value( true );
114
this.iframe = $( '<iframe />', { src: src }).appendTo( this.element );
115
this.iframe.one( 'load', this.loaded );
117
// Create a postMessage connection with the iframe.
118
this.messenger = new api.Messenger({
121
targetWindow: this.iframe[0].contentWindow
124
// Wait for the connection from the iframe before sending any postMessage events.
125
this.messenger.bind( 'ready', function() {
126
Loader.messenger.send( 'back' );
129
this.messenger.bind( 'close', function() {
130
if ( $.support.history ) {
132
} else if ( $.support.hashchange ) {
133
window.location.hash = '';
139
// Prompt AYS dialog when navigating away
140
$( window ).on( 'beforeunload', this.beforeunload );
142
this.messenger.bind( 'activated', function( location ) {
144
window.location = location;
148
this.messenger.bind( 'saved', function () {
149
Loader.saved( true );
151
this.messenger.bind( 'change', function () {
152
Loader.saved( false );
155
this.pushState( src );
157
this.trigger( 'open' );
160
pushState: function ( src ) {
163
// Ensure we don't call pushState if the user hit the forward button.
164
if ( $.support.history && window.location.href !== src ) {
165
history.pushState( { customize: src }, '', src );
166
} else if ( ! $.support.history && $.support.hashchange && hash ) {
167
hash = src.split( '?' )[1];
168
window.location.hash = 'wp_customize=on&' + hash;
173
* Callback after the customizer has been opened.
176
Loader.body.addClass( 'customize-active full-overlay-active' );
180
* Close the Customizer overlay and return focus to the link that opened it.
183
if ( ! this.active ) {
187
// Display AYS dialog if customizer is dirty
188
if ( ! this.saved() && ! confirm( Loader.settings.l10n.saveAlert ) ) {
189
// Go forward since Customizer is exited by history.back()
196
this.trigger( 'close' );
198
// Return focus to link that was originally clicked.
205
* Callback after the customizer has been closed.
208
Loader.iframe.remove();
209
Loader.messenger.destroy();
210
Loader.iframe = null;
211
Loader.messenger = null;
213
Loader.body.removeClass( 'customize-active full-overlay-active' ).removeClass( 'customize-loading' );
214
$( window ).off( 'beforeunload', Loader.beforeunload );
218
* Callback for the `load` event on the Customizer iframe.
221
Loader.body.removeClass('customize-loading');
225
* Overlay hide/show utility methods.
229
this.element.fadeIn( 200, Loader.opened );
233
this.element.fadeOut( 200, Loader.closed );
238
// Bootstrap the Loader on document#ready.
240
Loader.settings = _wpCustomizeLoaderSettings;
244
// Expose the API publicly on window.wp.customize.Loader