|
1 window.wp = window.wp || {}; |
|
2 |
|
3 (function( exports, $ ){ |
|
4 var api = wp.customize, |
|
5 Loader; |
|
6 |
|
7 $.extend( $.support, { |
|
8 history: !! ( window.history && history.pushState ), |
|
9 hashchange: ('onhashchange' in window) && (document.documentMode === undefined || document.documentMode > 7) |
|
10 }); |
|
11 |
|
12 Loader = $.extend( {}, api.Events, { |
|
13 initialize: function() { |
|
14 this.body = $( document.body ); |
|
15 |
|
16 // Ensure the loader is supported. |
|
17 // Check for settings, postMessage support, and whether we require CORS support. |
|
18 if ( ! Loader.settings || ! $.support.postMessage || ( ! $.support.cors && Loader.settings.isCrossDomain ) ) { |
|
19 return; |
|
20 } |
|
21 |
|
22 this.window = $( window ); |
|
23 this.element = $( '<div id="customize-container" />' ).appendTo( this.body ); |
|
24 |
|
25 this.bind( 'open', this.overlay.show ); |
|
26 this.bind( 'close', this.overlay.hide ); |
|
27 |
|
28 $('#wpbody').on( 'click', '.load-customize', function( event ) { |
|
29 event.preventDefault(); |
|
30 |
|
31 // Store a reference to the link that opened the customizer. |
|
32 Loader.link = $(this); |
|
33 // Load the theme. |
|
34 Loader.open( Loader.link.attr('href') ); |
|
35 }); |
|
36 |
|
37 // Add navigation listeners. |
|
38 if ( $.support.history ) |
|
39 this.window.on( 'popstate', Loader.popstate ); |
|
40 |
|
41 if ( $.support.hashchange ) { |
|
42 this.window.on( 'hashchange', Loader.hashchange ); |
|
43 this.window.triggerHandler( 'hashchange' ); |
|
44 } |
|
45 }, |
|
46 |
|
47 popstate: function( e ) { |
|
48 var state = e.originalEvent.state; |
|
49 if ( state && state.customize ) |
|
50 Loader.open( state.customize ); |
|
51 else if ( Loader.active ) |
|
52 Loader.close(); |
|
53 }, |
|
54 |
|
55 hashchange: function( e ) { |
|
56 var hash = window.location.toString().split('#')[1]; |
|
57 |
|
58 if ( hash && 0 === hash.indexOf( 'wp_customize=on' ) ) |
|
59 Loader.open( Loader.settings.url + '?' + hash ); |
|
60 |
|
61 if ( ! hash && ! $.support.history ) |
|
62 Loader.close(); |
|
63 }, |
|
64 |
|
65 open: function( src ) { |
|
66 var hash; |
|
67 |
|
68 if ( this.active ) |
|
69 return; |
|
70 |
|
71 // Load the full page on mobile devices. |
|
72 if ( Loader.settings.browser.mobile ) |
|
73 return window.location = src; |
|
74 |
|
75 this.active = true; |
|
76 this.body.addClass('customize-loading'); |
|
77 |
|
78 this.iframe = $( '<iframe />', { src: src }).appendTo( this.element ); |
|
79 this.iframe.one( 'load', this.loaded ); |
|
80 |
|
81 // Create a postMessage connection with the iframe. |
|
82 this.messenger = new api.Messenger({ |
|
83 url: src, |
|
84 channel: 'loader', |
|
85 targetWindow: this.iframe[0].contentWindow |
|
86 }); |
|
87 |
|
88 // Wait for the connection from the iframe before sending any postMessage events. |
|
89 this.messenger.bind( 'ready', function() { |
|
90 Loader.messenger.send( 'back' ); |
|
91 }); |
|
92 |
|
93 this.messenger.bind( 'close', function() { |
|
94 if ( $.support.history ) |
|
95 history.back(); |
|
96 else if ( $.support.hashchange ) |
|
97 window.location.hash = ''; |
|
98 else |
|
99 Loader.close(); |
|
100 }); |
|
101 |
|
102 this.messenger.bind( 'activated', function( location ) { |
|
103 if ( location ) |
|
104 window.location = location; |
|
105 }); |
|
106 |
|
107 hash = src.split('?')[1]; |
|
108 |
|
109 // Ensure we don't call pushState if the user hit the forward button. |
|
110 if ( $.support.history && window.location.href !== src ) |
|
111 history.pushState( { customize: src }, '', src ); |
|
112 else if ( ! $.support.history && $.support.hashchange && hash ) |
|
113 window.location.hash = 'wp_customize=on&' + hash; |
|
114 |
|
115 this.trigger( 'open' ); |
|
116 }, |
|
117 |
|
118 opened: function() { |
|
119 Loader.body.addClass( 'customize-active full-overlay-active' ); |
|
120 }, |
|
121 |
|
122 close: function() { |
|
123 if ( ! this.active ) |
|
124 return; |
|
125 this.active = false; |
|
126 |
|
127 this.trigger( 'close' ); |
|
128 |
|
129 // Return focus to link that was originally clicked. |
|
130 if ( this.link ) |
|
131 this.link.focus(); |
|
132 }, |
|
133 |
|
134 closed: function() { |
|
135 Loader.iframe.remove(); |
|
136 Loader.messenger.destroy(); |
|
137 Loader.iframe = null; |
|
138 Loader.messenger = null; |
|
139 Loader.body.removeClass( 'customize-active full-overlay-active' ).removeClass( 'customize-loading' ); |
|
140 }, |
|
141 |
|
142 loaded: function() { |
|
143 Loader.body.removeClass('customize-loading'); |
|
144 }, |
|
145 |
|
146 overlay: { |
|
147 show: function() { |
|
148 this.element.fadeIn( 200, Loader.opened ); |
|
149 }, |
|
150 |
|
151 hide: function() { |
|
152 this.element.fadeOut( 200, Loader.closed ); |
|
153 } |
|
154 } |
|
155 }); |
|
156 |
|
157 $( function() { |
|
158 Loader.settings = _wpCustomizeLoaderSettings; |
|
159 Loader.initialize(); |
|
160 }); |
|
161 |
|
162 // Expose the API to the world. |
|
163 api.Loader = Loader; |
|
164 })( wp, jQuery ); |