PageRenderTime 23ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/js/customize-preview.js

https://gitlab.com/juanito.abelo/nlmobile
JavaScript | 244 lines | 154 code | 39 blank | 51 comment | 14 complexity | 7baba5f606cab24916f7802fd55f2d12 MD5 | raw file
  1. /*
  2. * Script run inside a Customizer preview frame.
  3. */
  4. (function( exports, $ ){
  5. var api = wp.customize,
  6. debounce;
  7. /**
  8. * Returns a debounced version of the function.
  9. *
  10. * @todo Require Underscore.js for this file and retire this.
  11. */
  12. debounce = function( fn, delay, context ) {
  13. var timeout;
  14. return function() {
  15. var args = arguments;
  16. context = context || this;
  17. clearTimeout( timeout );
  18. timeout = setTimeout( function() {
  19. timeout = null;
  20. fn.apply( context, args );
  21. }, delay );
  22. };
  23. };
  24. /**
  25. * @constructor
  26. * @augments wp.customize.Messenger
  27. * @augments wp.customize.Class
  28. * @mixes wp.customize.Events
  29. */
  30. api.Preview = api.Messenger.extend({
  31. /**
  32. * @param {object} params - Parameters to configure the messenger.
  33. * @param {object} options - Extend any instance parameter or method with this object.
  34. */
  35. initialize: function( params, options ) {
  36. var self = this;
  37. api.Messenger.prototype.initialize.call( this, params, options );
  38. this.body = $( document.body );
  39. this.body.on( 'click.preview', 'a', function( event ) {
  40. var link, isInternalJumpLink;
  41. link = $( this );
  42. isInternalJumpLink = ( '#' === link.attr( 'href' ).substr( 0, 1 ) );
  43. event.preventDefault();
  44. if ( isInternalJumpLink && '#' !== link.attr( 'href' ) ) {
  45. $( link.attr( 'href' ) ).each( function() {
  46. this.scrollIntoView();
  47. } );
  48. }
  49. /*
  50. * Note the shift key is checked so shift+click on widgets or
  51. * nav menu items can just result on focusing on the corresponding
  52. * control instead of also navigating to the URL linked to.
  53. */
  54. if ( event.shiftKey || isInternalJumpLink ) {
  55. return;
  56. }
  57. self.send( 'scroll', 0 );
  58. self.send( 'url', link.prop( 'href' ) );
  59. });
  60. // You cannot submit forms.
  61. // @todo: Allow form submissions by mixing $_POST data with the customize setting $_POST data.
  62. this.body.on( 'submit.preview', 'form', function( event ) {
  63. event.preventDefault();
  64. });
  65. this.window = $( window );
  66. this.window.on( 'scroll.preview', debounce( function() {
  67. self.send( 'scroll', self.window.scrollTop() );
  68. }, 200 ));
  69. this.bind( 'scroll', function( distance ) {
  70. self.window.scrollTop( distance );
  71. });
  72. }
  73. });
  74. $( function() {
  75. var bg, setValue;
  76. api.settings = window._wpCustomizeSettings;
  77. if ( ! api.settings ) {
  78. return;
  79. }
  80. api.preview = new api.Preview({
  81. url: window.location.href,
  82. channel: api.settings.channel
  83. });
  84. /**
  85. * Create/update a setting value.
  86. *
  87. * @param {string} id - Setting ID.
  88. * @param {*} value - Setting value.
  89. * @param {boolean} [createDirty] - Whether to create a setting as dirty. Defaults to false.
  90. */
  91. setValue = function( id, value, createDirty ) {
  92. var setting = api( id );
  93. if ( setting ) {
  94. setting.set( value );
  95. } else {
  96. createDirty = createDirty || false;
  97. setting = api.create( id, value, {
  98. id: id
  99. } );
  100. // Mark dynamically-created settings as dirty so they will get posted.
  101. if ( createDirty ) {
  102. setting._dirty = true;
  103. }
  104. }
  105. };
  106. api.preview.bind( 'settings', function( values ) {
  107. $.each( values, setValue );
  108. });
  109. api.preview.trigger( 'settings', api.settings.values );
  110. $.each( api.settings._dirty, function( i, id ) {
  111. var setting = api( id );
  112. if ( setting ) {
  113. setting._dirty = true;
  114. }
  115. } );
  116. api.preview.bind( 'setting', function( args ) {
  117. var createDirty = true;
  118. setValue.apply( null, args.concat( createDirty ) );
  119. });
  120. api.preview.bind( 'sync', function( events ) {
  121. $.each( events, function( event, args ) {
  122. api.preview.trigger( event, args );
  123. });
  124. api.preview.send( 'synced' );
  125. });
  126. api.preview.bind( 'active', function() {
  127. api.preview.send( 'nonce', api.settings.nonce );
  128. api.preview.send( 'documentTitle', document.title );
  129. });
  130. api.preview.bind( 'saved', function( response ) {
  131. api.trigger( 'saved', response );
  132. } );
  133. api.bind( 'saved', function() {
  134. api.each( function( setting ) {
  135. setting._dirty = false;
  136. } );
  137. } );
  138. api.preview.bind( 'nonce-refresh', function( nonce ) {
  139. $.extend( api.settings.nonce, nonce );
  140. } );
  141. /*
  142. * Send a message to the parent customize frame with a list of which
  143. * containers and controls are active.
  144. */
  145. api.preview.send( 'ready', {
  146. activePanels: api.settings.activePanels,
  147. activeSections: api.settings.activeSections,
  148. activeControls: api.settings.activeControls,
  149. settingValidities: api.settings.settingValidities
  150. } );
  151. // Display a loading indicator when preview is reloading, and remove on failure.
  152. api.preview.bind( 'loading-initiated', function () {
  153. $( 'body' ).addClass( 'wp-customizer-unloading' );
  154. });
  155. api.preview.bind( 'loading-failed', function () {
  156. $( 'body' ).removeClass( 'wp-customizer-unloading' );
  157. });
  158. /* Custom Backgrounds */
  159. bg = $.map(['color', 'image', 'position_x', 'repeat', 'attachment'], function( prop ) {
  160. return 'background_' + prop;
  161. });
  162. api.when.apply( api, bg ).done( function( color, image, position_x, repeat, attachment ) {
  163. var body = $(document.body),
  164. head = $('head'),
  165. style = $('#custom-background-css'),
  166. update;
  167. update = function() {
  168. var css = '';
  169. // The body will support custom backgrounds if either
  170. // the color or image are set.
  171. //
  172. // See get_body_class() in /wp-includes/post-template.php
  173. body.toggleClass( 'custom-background', !! ( color() || image() ) );
  174. if ( color() )
  175. css += 'background-color: ' + color() + ';';
  176. if ( image() ) {
  177. css += 'background-image: url("' + image() + '");';
  178. css += 'background-position: top ' + position_x() + ';';
  179. css += 'background-repeat: ' + repeat() + ';';
  180. css += 'background-attachment: ' + attachment() + ';';
  181. }
  182. // Refresh the stylesheet by removing and recreating it.
  183. style.remove();
  184. style = $('<style type="text/css" id="custom-background-css">body.custom-background { ' + css + ' }</style>').appendTo( head );
  185. };
  186. $.each( arguments, function() {
  187. this.bind( update );
  188. });
  189. });
  190. /**
  191. * Custom Logo
  192. *
  193. * Toggle the wp-custom-logo body class when a logo is added or removed.
  194. *
  195. * @since 4.5.0
  196. */
  197. api( 'custom_logo', function( setting ) {
  198. $( 'body' ).toggleClass( 'wp-custom-logo', !! setting.get() );
  199. setting.bind( function( attachmentId ) {
  200. $( 'body' ).toggleClass( 'wp-custom-logo', !! attachmentId );
  201. } );
  202. } );
  203. api.trigger( 'preview-ready' );
  204. });
  205. })( wp, jQuery );