web/wp-includes/js/customize-loader.dev.js
changeset 204 09a1c134465b
parent 203 f507feede89a
child 205 a4f7897e21a9
equal deleted inserted replaced
203:f507feede89a 204:09a1c134465b
     1 if ( typeof wp === 'undefined' )
       
     2 	var 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 				// Load the theme.
       
    33 				Loader.open( $(this).attr('href') );
       
    34 			});
       
    35 
       
    36 			// Add navigation listeners.
       
    37 			if ( $.support.history )
       
    38 				this.window.on( 'popstate', Loader.popstate );
       
    39 
       
    40 			if ( $.support.hashchange ) {
       
    41 				this.window.on( 'hashchange', Loader.hashchange );
       
    42 				this.window.triggerHandler( 'hashchange' );
       
    43 			}
       
    44 		},
       
    45 
       
    46 		popstate: function( e ) {
       
    47 			var state = e.originalEvent.state;
       
    48 			if ( state && state.customize )
       
    49 				Loader.open( state.customize );
       
    50 			else if ( Loader.active )
       
    51 				Loader.close();
       
    52 		},
       
    53 
       
    54 		hashchange: function( e ) {
       
    55 			var hash = window.location.toString().split('#')[1];
       
    56 
       
    57 			if ( hash && 0 === hash.indexOf( 'wp_customize=on' ) )
       
    58 				Loader.open( Loader.settings.url + '?' + hash );
       
    59 
       
    60 			if ( ! hash && ! $.support.history )
       
    61 				Loader.close();
       
    62 		},
       
    63 
       
    64 		open: function( src ) {
       
    65 			var hash;
       
    66 
       
    67 			if ( this.active )
       
    68 				return;
       
    69 
       
    70 			// Load the full page on mobile devices.
       
    71 			if ( Loader.settings.browser.mobile )
       
    72 				return window.location = src;
       
    73 
       
    74 			this.active = true;
       
    75 			this.body.addClass('customize-loading');
       
    76 
       
    77 			this.iframe = $( '<iframe />', { src: src }).appendTo( this.element );
       
    78 			this.iframe.one( 'load', this.loaded );
       
    79 
       
    80 			// Create a postMessage connection with the iframe.
       
    81 			this.messenger = new api.Messenger({
       
    82 				url: src,
       
    83 				channel: 'loader',
       
    84 				targetWindow: this.iframe[0].contentWindow
       
    85 			});
       
    86 
       
    87 			// Wait for the connection from the iframe before sending any postMessage events.
       
    88 			this.messenger.bind( 'ready', function() {
       
    89 				Loader.messenger.send( 'back' );
       
    90 			});
       
    91 
       
    92 			this.messenger.bind( 'close', function() {
       
    93 				if ( $.support.history )
       
    94 					history.back();
       
    95 				else if ( $.support.hashchange )
       
    96 					window.location.hash = '';
       
    97 				else
       
    98 					Loader.close();
       
    99 			});
       
   100 
       
   101 			this.messenger.bind( 'activated', function( location ) {
       
   102 				if ( location )
       
   103 					window.location = location;
       
   104 			});
       
   105 
       
   106 			hash = src.split('?')[1];
       
   107 
       
   108 			// Ensure we don't call pushState if the user hit the forward button.
       
   109 			if ( $.support.history && window.location.href !== src )
       
   110 				history.pushState( { customize: src }, '', src );
       
   111 			else if ( ! $.support.history && $.support.hashchange && hash )
       
   112 				window.location.hash = 'wp_customize=on&' + hash;
       
   113 
       
   114 			this.trigger( 'open' );
       
   115 		},
       
   116 
       
   117 		opened: function() {
       
   118 			Loader.body.addClass( 'customize-active full-overlay-active' );
       
   119 		},
       
   120 
       
   121 		close: function() {
       
   122 			if ( ! this.active )
       
   123 				return;
       
   124 			this.active = false;
       
   125 
       
   126 			this.trigger( 'close' );
       
   127 		},
       
   128 
       
   129 		closed: function() {
       
   130 			Loader.iframe.remove();
       
   131 			Loader.messenger.destroy();
       
   132 			Loader.iframe    = null;
       
   133 			Loader.messenger = null;
       
   134 			Loader.body.removeClass( 'customize-active full-overlay-active' ).removeClass( 'customize-loading' );
       
   135 		},
       
   136 
       
   137 		loaded: function() {
       
   138 			Loader.body.removeClass('customize-loading');
       
   139 		},
       
   140 
       
   141 		overlay: {
       
   142 			show: function() {
       
   143 				this.element.fadeIn( 200, Loader.opened );
       
   144 			},
       
   145 
       
   146 			hide: function() {
       
   147 				this.element.fadeOut( 200, Loader.closed );
       
   148 			}
       
   149 		}
       
   150 	});
       
   151 
       
   152 	$( function() {
       
   153 		Loader.settings = _wpCustomizeLoaderSettings;
       
   154 		Loader.initialize();
       
   155 	});
       
   156 
       
   157 	// Expose the API to the world.
       
   158 	api.Loader = Loader;
       
   159 })( wp, jQuery );