/files/wordpress/3.5.1/js/customize-loader.js

https://gitlab.com/Mirros/jsdelivr · JavaScript · 164 lines · 117 code · 36 blank · 11 comment · 32 complexity · c157349a6efc9efd6903671b36811615 MD5 · raw file

  1. window.wp = window.wp || {};
  2. (function( exports, $ ){
  3. var api = wp.customize,
  4. Loader;
  5. $.extend( $.support, {
  6. history: !! ( window.history && history.pushState ),
  7. hashchange: ('onhashchange' in window) && (document.documentMode === undefined || document.documentMode > 7)
  8. });
  9. Loader = $.extend( {}, api.Events, {
  10. initialize: function() {
  11. this.body = $( document.body );
  12. // Ensure the loader is supported.
  13. // Check for settings, postMessage support, and whether we require CORS support.
  14. if ( ! Loader.settings || ! $.support.postMessage || ( ! $.support.cors && Loader.settings.isCrossDomain ) ) {
  15. return;
  16. }
  17. this.window = $( window );
  18. this.element = $( '<div id="customize-container" />' ).appendTo( this.body );
  19. this.bind( 'open', this.overlay.show );
  20. this.bind( 'close', this.overlay.hide );
  21. $('#wpbody').on( 'click', '.load-customize', function( event ) {
  22. event.preventDefault();
  23. // Store a reference to the link that opened the customizer.
  24. Loader.link = $(this);
  25. // Load the theme.
  26. Loader.open( Loader.link.attr('href') );
  27. });
  28. // Add navigation listeners.
  29. if ( $.support.history )
  30. this.window.on( 'popstate', Loader.popstate );
  31. if ( $.support.hashchange ) {
  32. this.window.on( 'hashchange', Loader.hashchange );
  33. this.window.triggerHandler( 'hashchange' );
  34. }
  35. },
  36. popstate: function( e ) {
  37. var state = e.originalEvent.state;
  38. if ( state && state.customize )
  39. Loader.open( state.customize );
  40. else if ( Loader.active )
  41. Loader.close();
  42. },
  43. hashchange: function( e ) {
  44. var hash = window.location.toString().split('#')[1];
  45. if ( hash && 0 === hash.indexOf( 'wp_customize=on' ) )
  46. Loader.open( Loader.settings.url + '?' + hash );
  47. if ( ! hash && ! $.support.history )
  48. Loader.close();
  49. },
  50. open: function( src ) {
  51. var hash;
  52. if ( this.active )
  53. return;
  54. // Load the full page on mobile devices.
  55. if ( Loader.settings.browser.mobile )
  56. return window.location = src;
  57. this.active = true;
  58. this.body.addClass('customize-loading');
  59. this.iframe = $( '<iframe />', { src: src }).appendTo( this.element );
  60. this.iframe.one( 'load', this.loaded );
  61. // Create a postMessage connection with the iframe.
  62. this.messenger = new api.Messenger({
  63. url: src,
  64. channel: 'loader',
  65. targetWindow: this.iframe[0].contentWindow
  66. });
  67. // Wait for the connection from the iframe before sending any postMessage events.
  68. this.messenger.bind( 'ready', function() {
  69. Loader.messenger.send( 'back' );
  70. });
  71. this.messenger.bind( 'close', function() {
  72. if ( $.support.history )
  73. history.back();
  74. else if ( $.support.hashchange )
  75. window.location.hash = '';
  76. else
  77. Loader.close();
  78. });
  79. this.messenger.bind( 'activated', function( location ) {
  80. if ( location )
  81. window.location = location;
  82. });
  83. hash = src.split('?')[1];
  84. // Ensure we don't call pushState if the user hit the forward button.
  85. if ( $.support.history && window.location.href !== src )
  86. history.pushState( { customize: src }, '', src );
  87. else if ( ! $.support.history && $.support.hashchange && hash )
  88. window.location.hash = 'wp_customize=on&' + hash;
  89. this.trigger( 'open' );
  90. },
  91. opened: function() {
  92. Loader.body.addClass( 'customize-active full-overlay-active' );
  93. },
  94. close: function() {
  95. if ( ! this.active )
  96. return;
  97. this.active = false;
  98. this.trigger( 'close' );
  99. // Return focus to link that was originally clicked.
  100. if ( this.link )
  101. this.link.focus();
  102. },
  103. closed: function() {
  104. Loader.iframe.remove();
  105. Loader.messenger.destroy();
  106. Loader.iframe = null;
  107. Loader.messenger = null;
  108. Loader.body.removeClass( 'customize-active full-overlay-active' ).removeClass( 'customize-loading' );
  109. },
  110. loaded: function() {
  111. Loader.body.removeClass('customize-loading');
  112. },
  113. overlay: {
  114. show: function() {
  115. this.element.fadeIn( 200, Loader.opened );
  116. },
  117. hide: function() {
  118. this.element.fadeOut( 200, Loader.closed );
  119. }
  120. }
  121. });
  122. $( function() {
  123. Loader.settings = _wpCustomizeLoaderSettings;
  124. Loader.initialize();
  125. });
  126. // Expose the API to the world.
  127. api.Loader = Loader;
  128. })( wp, jQuery );