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