2 * Script run inside a Customizer preview frame.
4 (function( exports, $ ){
5 var api = wp.customize,
9 * Returns a debounced version of the function.
11 * @todo Require Underscore.js for this file and retire this.
13 debounce = function( fn, delay, context ) {
18 context = context || this;
20 clearTimeout( timeout );
21 timeout = setTimeout( function() {
23 fn.apply( context, args );
30 * @augments wp.customize.Messenger
31 * @augments wp.customize.Class
32 * @mixes wp.customize.Events
34 api.Preview = api.Messenger.extend({
36 * @param {string} url The URL of preview frame
38 initialize: function( params, options ) {
41 api.Messenger.prototype.initialize.call( this, params, options );
43 this.body = $( document.body );
44 this.body.on( 'click.preview', 'a', function( event ) {
45 event.preventDefault();
46 self.send( 'scroll', 0 );
47 self.send( 'url', $(this).prop('href') );
50 // You cannot submit forms.
51 // @todo: Allow form submissions by mixing $_POST data with the customize setting $_POST data.
52 this.body.on( 'submit.preview', 'form', function( event ) {
53 event.preventDefault();
56 this.window = $( window );
57 this.window.on( 'scroll.preview', debounce( function() {
58 self.send( 'scroll', self.window.scrollTop() );
61 this.bind( 'scroll', function( distance ) {
62 self.window.scrollTop( distance );
68 api.settings = window._wpCustomizeSettings;
74 api.preview = new api.Preview({
75 url: window.location.href,
76 channel: api.settings.channel
79 api.preview.bind( 'settings', function( values ) {
80 $.each( values, function( id, value ) {
82 api( id ).set( value );
84 api.create( id, value );
88 api.preview.trigger( 'settings', api.settings.values );
90 api.preview.bind( 'setting', function( args ) {
95 if ( value = api( args.shift() ) )
96 value.set.apply( value, args );
99 api.preview.bind( 'sync', function( events ) {
100 $.each( events, function( event, args ) {
101 api.preview.trigger( event, args );
103 api.preview.send( 'synced' );
106 api.preview.bind( 'active', function() {
107 if ( api.settings.nonce ) {
108 api.preview.send( 'nonce', api.settings.nonce );
111 api.preview.send( 'documentTitle', document.title );
115 * Send a message to the parent customize frame with a list of which
116 * containers and controls are active.
118 api.preview.send( 'ready', {
119 activePanels: api.settings.activePanels,
120 activeSections: api.settings.activeSections,
121 activeControls: api.settings.activeControls
124 // Display a loading indicator when preview is reloading, and remove on failure.
125 api.preview.bind( 'loading-initiated', function () {
126 $( 'body' ).addClass( 'wp-customizer-unloading' );
128 api.preview.bind( 'loading-failed', function () {
129 $( 'body' ).removeClass( 'wp-customizer-unloading' );
132 /* Custom Backgrounds */
133 bg = $.map(['color', 'image', 'position_x', 'repeat', 'attachment'], function( prop ) {
134 return 'background_' + prop;
137 api.when.apply( api, bg ).done( function( color, image, position_x, repeat, attachment ) {
138 var body = $(document.body),
140 style = $('#custom-background-css'),
143 update = function() {
146 // The body will support custom backgrounds if either
147 // the color or image are set.
149 // See get_body_class() in /wp-includes/post-template.php
150 body.toggleClass( 'custom-background', !! ( color() || image() ) );
153 css += 'background-color: ' + color() + ';';
156 css += 'background-image: url("' + image() + '");';
157 css += 'background-position: top ' + position_x() + ';';
158 css += 'background-repeat: ' + repeat() + ';';
159 css += 'background-attachment: ' + attachment() + ';';
162 // Refresh the stylesheet by removing and recreating it.
164 style = $('<style type="text/css" id="custom-background-css">body.custom-background { ' + css + ' }</style>').appendTo( head );
167 $.each( arguments, function() {
172 api.trigger( 'preview-ready' );