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