customize-loader.js 7.7 KB

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