1 (function( exports, $ ){
2 var api = wp.customize,
6 * Returns a debounced version of the function.
8 * @todo Require Underscore.js for this file and retire this.
10 debounce = function( fn, delay, context ) {
15 context = context || this;
17 clearTimeout( timeout );
18 timeout = setTimeout( function() {
20 fn.apply( context, args );
27 * @augments wp.customize.Messenger
28 * @augments wp.customize.Class
29 * @mixes wp.customize.Events
31 api.Preview = api.Messenger.extend({
34 * - url - the URL of preview frame
36 initialize: function( params, options ) {
39 api.Messenger.prototype.initialize.call( this, params, options );
41 this.body = $( document.body );
42 this.body.on( 'click.preview', 'a', function( event ) {
43 event.preventDefault();
44 self.send( 'scroll', 0 );
45 self.send( 'url', $(this).prop('href') );
48 // You cannot submit forms.
49 // @todo: Allow form submissions by mixing $_POST data with the customize setting $_POST data.
50 this.body.on( 'submit.preview', 'form', function( event ) {
51 event.preventDefault();
54 this.window = $( window );
55 this.window.on( 'scroll.preview', debounce( function() {
56 self.send( 'scroll', self.window.scrollTop() );
59 this.bind( 'scroll', function( distance ) {
60 self.window.scrollTop( distance );
66 api.settings = window._wpCustomizeSettings;
72 preview = new api.Preview({
73 url: window.location.href,
74 channel: api.settings.channel
77 preview.bind( 'settings', function( values ) {
78 $.each( values, function( id, value ) {
80 api( id ).set( value );
82 api.create( id, value );
86 preview.trigger( 'settings', api.settings.values );
88 preview.bind( 'setting', function( args ) {
93 if ( value = api( args.shift() ) )
94 value.set.apply( value, args );
97 preview.bind( 'sync', function( events ) {
98 $.each( events, function( event, args ) {
99 preview.trigger( event, args );
101 preview.send( 'synced' );
104 preview.bind( 'active', function() {
105 if ( api.settings.nonce )
106 preview.send( 'nonce', api.settings.nonce );
109 preview.send( 'ready', {
110 activeControls: api.settings.activeControls
113 /* Custom Backgrounds */
114 bg = $.map(['color', 'image', 'position_x', 'repeat', 'attachment'], function( prop ) {
115 return 'background_' + prop;
118 api.when.apply( api, bg ).done( function( color, image, position_x, repeat, attachment ) {
119 var body = $(document.body),
121 style = $('#custom-background-css'),
124 update = function() {
127 // The body will support custom backgrounds if either
128 // the color or image are set.
130 // See get_body_class() in /wp-includes/post-template.php
131 body.toggleClass( 'custom-background', !! ( color() || image() ) );
134 css += 'background-color: ' + color() + ';';
137 css += 'background-image: url("' + image() + '");';
138 css += 'background-position: top ' + position_x() + ';';
139 css += 'background-repeat: ' + repeat() + ';';
140 css += 'background-attachment: ' + attachment() + ';';
143 // Refresh the stylesheet by removing and recreating it.
145 style = $('<style type="text/css" id="custom-background-css">body.custom-background { ' + css + ' }</style>').appendTo( head );
148 $.each( arguments, function() {