web/wp-includes/js/customize-loader.js
changeset 204 09a1c134465b
parent 194 32102edaa81b
equal deleted inserted replaced
203:f507feede89a 204:09a1c134465b
     1 if(typeof wp==="undefined"){var wp={}}(function(a,c){var b=wp.customize,d;c.extend(c.support,{history:!!(window.history&&history.pushState),hashchange:("onhashchange" in window)&&(document.documentMode===undefined||document.documentMode>7)});d=c.extend({},b.Events,{initialize:function(){this.body=c(document.body);if(!d.settings||!c.support.postMessage||(!c.support.cors&&d.settings.isCrossDomain)){return}this.window=c(window);this.element=c('<div id="customize-container" />').appendTo(this.body);this.bind("open",this.overlay.show);this.bind("close",this.overlay.hide);c("#wpbody").on("click",".load-customize",function(e){e.preventDefault();d.open(c(this).attr("href"))});if(c.support.history){this.window.on("popstate",d.popstate)}if(c.support.hashchange){this.window.on("hashchange",d.hashchange);this.window.triggerHandler("hashchange")}},popstate:function(g){var f=g.originalEvent.state;if(f&&f.customize){d.open(f.customize)}else{if(d.active){d.close()}}},hashchange:function(g){var f=window.location.toString().split("#")[1];if(f&&0===f.indexOf("wp_customize=on")){d.open(d.settings.url+"?"+f)}if(!f&&!c.support.history){d.close()}},open:function(f){var e;if(this.active){return}if(d.settings.browser.mobile){return window.location=f}this.active=true;this.body.addClass("customize-loading");this.iframe=c("<iframe />",{src:f}).appendTo(this.element);this.iframe.one("load",this.loaded);this.messenger=new b.Messenger({url:f,channel:"loader",targetWindow:this.iframe[0].contentWindow});this.messenger.bind("ready",function(){d.messenger.send("back")});this.messenger.bind("close",function(){if(c.support.history){history.back()}else{if(c.support.hashchange){window.location.hash=""}else{d.close()}}});this.messenger.bind("activated",function(g){if(g){window.location=g}});e=f.split("?")[1];if(c.support.history&&window.location.href!==f){history.pushState({customize:f},"",f)}else{if(!c.support.history&&c.support.hashchange&&e){window.location.hash="wp_customize=on&"+e}}this.trigger("open")},opened:function(){d.body.addClass("customize-active full-overlay-active")},close:function(){if(!this.active){return}this.active=false;this.trigger("close")},closed:function(){d.iframe.remove();d.messenger.destroy();d.iframe=null;d.messenger=null;d.body.removeClass("customize-active full-overlay-active").removeClass("customize-loading")},loaded:function(){d.body.removeClass("customize-loading")},overlay:{show:function(){this.element.fadeIn(200,d.opened)},hide:function(){this.element.fadeOut(200,d.closed)}}});c(function(){d.settings=_wpCustomizeLoaderSettings;d.initialize()});b.Loader=d})(wp,jQuery);
     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 );