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

https://gitlab.com/haque.mdmanzurul/wp-harpar-carolyn · JavaScript · 154 lines · 102 code · 29 blank · 23 comment · 8 complexity · e313328820b62adfcf516062ab6567b4 MD5 · raw file

  1. (function( exports, $ ){
  2. var api = wp.customize,
  3. debounce;
  4. /**
  5. * Returns a debounced version of the function.
  6. *
  7. * @todo Require Underscore.js for this file and retire this.
  8. */
  9. debounce = function( fn, delay, context ) {
  10. var timeout;
  11. return function() {
  12. var args = arguments;
  13. context = context || this;
  14. clearTimeout( timeout );
  15. timeout = setTimeout( function() {
  16. timeout = null;
  17. fn.apply( context, args );
  18. }, delay );
  19. };
  20. };
  21. /**
  22. * @constructor
  23. * @augments wp.customize.Messenger
  24. * @augments wp.customize.Class
  25. * @mixes wp.customize.Events
  26. */
  27. api.Preview = api.Messenger.extend({
  28. /**
  29. * Requires params:
  30. * - url - the URL of preview frame
  31. */
  32. initialize: function( params, options ) {
  33. var self = this;
  34. api.Messenger.prototype.initialize.call( this, params, options );
  35. this.body = $( document.body );
  36. this.body.on( 'click.preview', 'a', function( event ) {
  37. event.preventDefault();
  38. self.send( 'scroll', 0 );
  39. self.send( 'url', $(this).prop('href') );
  40. });
  41. // You cannot submit forms.
  42. // @todo: Allow form submissions by mixing $_POST data with the customize setting $_POST data.
  43. this.body.on( 'submit.preview', 'form', function( event ) {
  44. event.preventDefault();
  45. });
  46. this.window = $( window );
  47. this.window.on( 'scroll.preview', debounce( function() {
  48. self.send( 'scroll', self.window.scrollTop() );
  49. }, 200 ));
  50. this.bind( 'scroll', function( distance ) {
  51. self.window.scrollTop( distance );
  52. });
  53. }
  54. });
  55. $( function() {
  56. api.settings = window._wpCustomizeSettings;
  57. if ( ! api.settings )
  58. return;
  59. var preview, bg;
  60. preview = new api.Preview({
  61. url: window.location.href,
  62. channel: api.settings.channel
  63. });
  64. preview.bind( 'settings', function( values ) {
  65. $.each( values, function( id, value ) {
  66. if ( api.has( id ) )
  67. api( id ).set( value );
  68. else
  69. api.create( id, value );
  70. });
  71. });
  72. preview.trigger( 'settings', api.settings.values );
  73. preview.bind( 'setting', function( args ) {
  74. var value;
  75. args = args.slice();
  76. if ( value = api( args.shift() ) )
  77. value.set.apply( value, args );
  78. });
  79. preview.bind( 'sync', function( events ) {
  80. $.each( events, function( event, args ) {
  81. preview.trigger( event, args );
  82. });
  83. preview.send( 'synced' );
  84. });
  85. preview.bind( 'active', function() {
  86. if ( api.settings.nonce )
  87. preview.send( 'nonce', api.settings.nonce );
  88. });
  89. preview.send( 'ready', {
  90. activeControls: api.settings.activeControls
  91. } );
  92. /* Custom Backgrounds */
  93. bg = $.map(['color', 'image', 'position_x', 'repeat', 'attachment'], function( prop ) {
  94. return 'background_' + prop;
  95. });
  96. api.when.apply( api, bg ).done( function( color, image, position_x, repeat, attachment ) {
  97. var body = $(document.body),
  98. head = $('head'),
  99. style = $('#custom-background-css'),
  100. update;
  101. update = function() {
  102. var css = '';
  103. // The body will support custom backgrounds if either
  104. // the color or image are set.
  105. //
  106. // See get_body_class() in /wp-includes/post-template.php
  107. body.toggleClass( 'custom-background', !! ( color() || image() ) );
  108. if ( color() )
  109. css += 'background-color: ' + color() + ';';
  110. if ( image() ) {
  111. css += 'background-image: url("' + image() + '");';
  112. css += 'background-position: top ' + position_x() + ';';
  113. css += 'background-repeat: ' + repeat() + ';';
  114. css += 'background-attachment: ' + attachment() + ';';
  115. }
  116. // Refresh the stylesheet by removing and recreating it.
  117. style.remove();
  118. style = $('<style type="text/css" id="custom-background-css">body.custom-background { ' + css + ' }</style>').appendTo( head );
  119. };
  120. $.each( arguments, function() {
  121. this.bind( update );
  122. });
  123. });
  124. });
  125. })( wp, jQuery );