]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/customize-loader.js
WordPress 4.5.1-scripts
[autoinstalls/wordpress.git] / wp-includes / js / customize-loader.js
1 /* global _wpCustomizeLoaderSettings, confirm */
2 /*
3  * Expose a public API that allows the customizer to be
4  * loaded on any page.
5  */
6 window.wp = window.wp || {};
7
8 (function( exports, $ ){
9         var api = wp.customize,
10                 Loader;
11
12         $.extend( $.support, {
13                 history: !! ( window.history && history.pushState ),
14                 hashchange: ('onhashchange' in window) && (document.documentMode === undefined || document.documentMode > 7)
15         });
16
17         /**
18          * Allows the Customizer to be overlayed on any page.
19          *
20          * By default, any element in the body with the load-customize class will open
21          * an iframe overlay with the URL specified.
22          *
23          *     e.g. <a class="load-customize" href="<?php echo wp_customize_url(); ?>">Open Customizer</a>
24          *
25          * @augments wp.customize.Events
26          */
27         Loader = $.extend( {}, api.Events, {
28                 /**
29                  * Setup the Loader; triggered on document#ready.
30                  */
31                 initialize: function() {
32                         this.body = $( document.body );
33
34                         // Ensure the loader is supported.
35                         // Check for settings, postMessage support, and whether we require CORS support.
36                         if ( ! Loader.settings || ! $.support.postMessage || ( ! $.support.cors && Loader.settings.isCrossDomain ) ) {
37                                 return;
38                         }
39
40                         this.window  = $( window );
41                         this.element = $( '<div id="customize-container" />' ).appendTo( this.body );
42
43                         // Bind events for opening and closing the overlay.
44                         this.bind( 'open', this.overlay.show );
45                         this.bind( 'close', this.overlay.hide );
46
47                         // Any element in the body with the `load-customize` class opens
48                         // the Customizer.
49                         $('#wpbody').on( 'click', '.load-customize', function( event ) {
50                                 event.preventDefault();
51
52                                 // Store a reference to the link that opened the Customizer.
53                                 Loader.link = $(this);
54                                 // Load the theme.
55                                 Loader.open( Loader.link.attr('href') );
56                         });
57
58                         // Add navigation listeners.
59                         if ( $.support.history ) {
60                                 this.window.on( 'popstate', Loader.popstate );
61                         }
62
63                         if ( $.support.hashchange ) {
64                                 this.window.on( 'hashchange', Loader.hashchange );
65                                 this.window.triggerHandler( 'hashchange' );
66                         }
67                 },
68
69                 popstate: function( e ) {
70                         var state = e.originalEvent.state;
71                         if ( state && state.customize ) {
72                                 Loader.open( state.customize );
73                         } else if ( Loader.active ) {
74                                 Loader.close();
75                         }
76                 },
77
78                 hashchange: function() {
79                         var hash = window.location.toString().split('#')[1];
80
81                         if ( hash && 0 === hash.indexOf( 'wp_customize=on' ) ) {
82                                 Loader.open( Loader.settings.url + '?' + hash );
83                         }
84
85                         if ( ! hash && ! $.support.history ) {
86                                 Loader.close();
87                         }
88                 },
89
90                 beforeunload: function () {
91                         if ( ! Loader.saved() ) {
92                                 return Loader.settings.l10n.saveAlert;
93                         }
94                 },
95
96                 /**
97                  * Open the Customizer overlay for a specific URL.
98                  *
99                  * @param  string src URL to load in the Customizer.
100                  */
101                 open: function( src ) {
102
103                         if ( this.active ) {
104                                 return;
105                         }
106
107                         // Load the full page on mobile devices.
108                         if ( Loader.settings.browser.mobile ) {
109                                 return window.location = src;
110                         }
111
112                         // Store the document title prior to opening the Live Preview
113                         this.originalDocumentTitle = document.title;
114
115                         this.active = true;
116                         this.body.addClass('customize-loading');
117
118                         /*
119                          * Track the dirtiness state (whether the drafted changes have been published)
120                          * of the Customizer in the iframe. This is used to decide whether to display
121                          * an AYS alert if the user tries to close the window before saving changes.
122                          */
123                         this.saved = new api.Value( true );
124
125                         this.iframe = $( '<iframe />', { 'src': src, 'title': Loader.settings.l10n.mainIframeTitle } ).appendTo( this.element );
126                         this.iframe.one( 'load', this.loaded );
127
128                         // Create a postMessage connection with the iframe.
129                         this.messenger = new api.Messenger({
130                                 url: src,
131                                 channel: 'loader',
132                                 targetWindow: this.iframe[0].contentWindow
133                         });
134
135                         // Wait for the connection from the iframe before sending any postMessage events.
136                         this.messenger.bind( 'ready', function() {
137                                 Loader.messenger.send( 'back' );
138                         });
139
140                         this.messenger.bind( 'close', function() {
141                                 if ( $.support.history ) {
142                                         history.back();
143                                 } else if ( $.support.hashchange ) {
144                                         window.location.hash = '';
145                                 } else {
146                                         Loader.close();
147                                 }
148                         });
149
150                         // Prompt AYS dialog when navigating away
151                         $( window ).on( 'beforeunload', this.beforeunload );
152
153                         this.messenger.bind( 'activated', function( location ) {
154                                 if ( location ) {
155                                         window.location = location;
156                                 }
157                         });
158
159                         this.messenger.bind( 'saved', function () {
160                                 Loader.saved( true );
161                         } );
162                         this.messenger.bind( 'change', function () {
163                                 Loader.saved( false );
164                         } );
165
166                         this.messenger.bind( 'title', function( newTitle ){
167                                 window.document.title = newTitle;
168                         });
169
170                         this.pushState( src );
171
172                         this.trigger( 'open' );
173                 },
174
175                 pushState: function ( src ) {
176                         var hash = src.split( '?' )[1];
177
178                         // Ensure we don't call pushState if the user hit the forward button.
179                         if ( $.support.history && window.location.href !== src ) {
180                                 history.pushState( { customize: src }, '', src );
181                         } else if ( ! $.support.history && $.support.hashchange && hash ) {
182                                 window.location.hash = 'wp_customize=on&' + hash;
183                         }
184
185                         this.trigger( 'open' );
186                 },
187
188                 /**
189                  * Callback after the Customizer has been opened.
190                  */
191                 opened: function() {
192                         Loader.body.addClass( 'customize-active full-overlay-active' );
193                 },
194
195                 /**
196                  * Close the Customizer overlay and return focus to the link that opened it.
197                  */
198                 close: function() {
199                         if ( ! this.active ) {
200                                 return;
201                         }
202
203                         // Display AYS dialog if Customizer is dirty
204                         if ( ! this.saved() && ! confirm( Loader.settings.l10n.saveAlert ) ) {
205                                 // Go forward since Customizer is exited by history.back()
206                                 history.forward();
207                                 return;
208                         }
209
210                         this.active = false;
211
212                         this.trigger( 'close' );
213
214                         // Restore document title prior to opening the Live Preview
215                         if ( this.originalDocumentTitle ) {
216                                 document.title = this.originalDocumentTitle;
217                         }
218
219                         // Return focus to link that was originally clicked.
220                         if ( this.link ) {
221                                 this.link.focus();
222                         }
223                 },
224
225                 /**
226                  * Callback after the Customizer has been closed.
227                  */
228                 closed: function() {
229                         Loader.iframe.remove();
230                         Loader.messenger.destroy();
231                         Loader.iframe    = null;
232                         Loader.messenger = null;
233                         Loader.saved     = null;
234                         Loader.body.removeClass( 'customize-active full-overlay-active' ).removeClass( 'customize-loading' );
235                         $( window ).off( 'beforeunload', Loader.beforeunload );
236                 },
237
238                 /**
239                  * Callback for the `load` event on the Customizer iframe.
240                  */
241                 loaded: function() {
242                         Loader.body.removeClass('customize-loading');
243                 },
244
245                 /**
246                  * Overlay hide/show utility methods.
247                  */
248                 overlay: {
249                         show: function() {
250                                 this.element.fadeIn( 200, Loader.opened );
251                         },
252
253                         hide: function() {
254                                 this.element.fadeOut( 200, Loader.closed );
255                         }
256                 }
257         });
258
259         // Bootstrap the Loader on document#ready.
260         $( function() {
261                 Loader.settings = _wpCustomizeLoaderSettings;
262                 Loader.initialize();
263         });
264
265         // Expose the API publicly on window.wp.customize.Loader
266         api.Loader = Loader;
267 })( wp, jQuery );