]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/customize-loader.js
WordPress 4.7-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                         // Expose the changeset UUID on the parent window's URL so that the customized state can survive a refresh.
136                         if ( history.replaceState ) {
137                                 this.messenger.bind( 'changeset-uuid', function( changesetUuid ) {
138                                         var urlParser = document.createElement( 'a' );
139                                         urlParser.href = location.href;
140                                         urlParser.search = $.param( _.extend(
141                                                 api.utils.parseQueryString( urlParser.search.substr( 1 ) ),
142                                                 { changeset_uuid: changesetUuid }
143                                         ) );
144                                         history.replaceState( { customize: urlParser.href }, '', urlParser.href );
145                                 } );
146                         }
147
148                         // Wait for the connection from the iframe before sending any postMessage events.
149                         this.messenger.bind( 'ready', function() {
150                                 Loader.messenger.send( 'back' );
151                         });
152
153                         this.messenger.bind( 'close', function() {
154                                 if ( $.support.history ) {
155                                         history.back();
156                                 } else if ( $.support.hashchange ) {
157                                         window.location.hash = '';
158                                 } else {
159                                         Loader.close();
160                                 }
161                         });
162
163                         // Prompt AYS dialog when navigating away
164                         $( window ).on( 'beforeunload', this.beforeunload );
165
166                         this.messenger.bind( 'saved', function () {
167                                 Loader.saved( true );
168                         } );
169                         this.messenger.bind( 'change', function () {
170                                 Loader.saved( false );
171                         } );
172
173                         this.messenger.bind( 'title', function( newTitle ){
174                                 window.document.title = newTitle;
175                         });
176
177                         this.pushState( src );
178
179                         this.trigger( 'open' );
180                 },
181
182                 pushState: function ( src ) {
183                         var hash = src.split( '?' )[1];
184
185                         // Ensure we don't call pushState if the user hit the forward button.
186                         if ( $.support.history && window.location.href !== src ) {
187                                 history.pushState( { customize: src }, '', src );
188                         } else if ( ! $.support.history && $.support.hashchange && hash ) {
189                                 window.location.hash = 'wp_customize=on&' + hash;
190                         }
191
192                         this.trigger( 'open' );
193                 },
194
195                 /**
196                  * Callback after the Customizer has been opened.
197                  */
198                 opened: function() {
199                         Loader.body.addClass( 'customize-active full-overlay-active' ).attr( 'aria-busy', 'true' );
200                 },
201
202                 /**
203                  * Close the Customizer overlay.
204                  */
205                 close: function() {
206                         if ( ! this.active ) {
207                                 return;
208                         }
209
210                         // Display AYS dialog if Customizer is dirty
211                         if ( ! this.saved() && ! confirm( Loader.settings.l10n.saveAlert ) ) {
212                                 // Go forward since Customizer is exited by history.back()
213                                 history.forward();
214                                 return;
215                         }
216
217                         this.active = false;
218
219                         this.trigger( 'close' );
220
221                         // Restore document title prior to opening the Live Preview
222                         if ( this.originalDocumentTitle ) {
223                                 document.title = this.originalDocumentTitle;
224                         }
225                 },
226
227                 /**
228                  * Callback after the Customizer has been closed.
229                  */
230                 closed: function() {
231                         Loader.iframe.remove();
232                         Loader.messenger.destroy();
233                         Loader.iframe    = null;
234                         Loader.messenger = null;
235                         Loader.saved     = null;
236                         Loader.body.removeClass( 'customize-active full-overlay-active' ).removeClass( 'customize-loading' );
237                         $( window ).off( 'beforeunload', Loader.beforeunload );
238                         /*
239                          * Return focus to the link that opened the Customizer overlay after
240                          * the body element visibility is restored.
241                          */
242                         if ( Loader.link ) {
243                                 Loader.link.focus();
244                         }
245                 },
246
247                 /**
248                  * Callback for the `load` event on the Customizer iframe.
249                  */
250                 loaded: function() {
251                         Loader.body.removeClass( 'customize-loading' ).attr( 'aria-busy', 'false' );
252                 },
253
254                 /**
255                  * Overlay hide/show utility methods.
256                  */
257                 overlay: {
258                         show: function() {
259                                 this.element.fadeIn( 200, Loader.opened );
260                         },
261
262                         hide: function() {
263                                 this.element.fadeOut( 200, Loader.closed );
264                         }
265                 }
266         });
267
268         // Bootstrap the Loader on document#ready.
269         $( function() {
270                 Loader.settings = _wpCustomizeLoaderSettings;
271                 Loader.initialize();
272         });
273
274         // Expose the API publicly on window.wp.customize.Loader
275         api.Loader = Loader;
276 })( wp, jQuery );