PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/df_home/static/test/portalbkd/wp-includes/js/customize-loader.js

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