]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/customize-loader.js
WordPress 4.6.3-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( 'saved', function () {
154                                 Loader.saved( true );
155                         } );
156                         this.messenger.bind( 'change', function () {
157                                 Loader.saved( false );
158                         } );
159
160                         this.messenger.bind( 'title', function( newTitle ){
161                                 window.document.title = newTitle;
162                         });
163
164                         this.pushState( src );
165
166                         this.trigger( 'open' );
167                 },
168
169                 pushState: function ( src ) {
170                         var hash = src.split( '?' )[1];
171
172                         // Ensure we don't call pushState if the user hit the forward button.
173                         if ( $.support.history && window.location.href !== src ) {
174                                 history.pushState( { customize: src }, '', src );
175                         } else if ( ! $.support.history && $.support.hashchange && hash ) {
176                                 window.location.hash = 'wp_customize=on&' + hash;
177                         }
178
179                         this.trigger( 'open' );
180                 },
181
182                 /**
183                  * Callback after the Customizer has been opened.
184                  */
185                 opened: function() {
186                         Loader.body.addClass( 'customize-active full-overlay-active' );
187                 },
188
189                 /**
190                  * Close the Customizer overlay and return focus to the link that opened it.
191                  */
192                 close: function() {
193                         if ( ! this.active ) {
194                                 return;
195                         }
196
197                         // Display AYS dialog if Customizer is dirty
198                         if ( ! this.saved() && ! confirm( Loader.settings.l10n.saveAlert ) ) {
199                                 // Go forward since Customizer is exited by history.back()
200                                 history.forward();
201                                 return;
202                         }
203
204                         this.active = false;
205
206                         this.trigger( 'close' );
207
208                         // Restore document title prior to opening the Live Preview
209                         if ( this.originalDocumentTitle ) {
210                                 document.title = this.originalDocumentTitle;
211                         }
212
213                         // Return focus to link that was originally clicked.
214                         if ( this.link ) {
215                                 this.link.focus();
216                         }
217                 },
218
219                 /**
220                  * Callback after the Customizer has been closed.
221                  */
222                 closed: function() {
223                         Loader.iframe.remove();
224                         Loader.messenger.destroy();
225                         Loader.iframe    = null;
226                         Loader.messenger = null;
227                         Loader.saved     = null;
228                         Loader.body.removeClass( 'customize-active full-overlay-active' ).removeClass( 'customize-loading' );
229                         $( window ).off( 'beforeunload', Loader.beforeunload );
230                 },
231
232                 /**
233                  * Callback for the `load` event on the Customizer iframe.
234                  */
235                 loaded: function() {
236                         Loader.body.removeClass('customize-loading');
237                 },
238
239                 /**
240                  * Overlay hide/show utility methods.
241                  */
242                 overlay: {
243                         show: function() {
244                                 this.element.fadeIn( 200, Loader.opened );
245                         },
246
247                         hide: function() {
248                                 this.element.fadeOut( 200, Loader.closed );
249                         }
250                 }
251         });
252
253         // Bootstrap the Loader on document#ready.
254         $( function() {
255                 Loader.settings = _wpCustomizeLoaderSettings;
256                 Loader.initialize();
257         });
258
259         // Expose the API publicly on window.wp.customize.Loader
260         api.Loader = Loader;
261 })( wp, jQuery );