You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

277 lines
7.4 KiB

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