1 (function( exports, $ ){
2 var api = wp.customize,
5 debounce = function( fn, delay, context ) {
10 context = context || this;
12 clearTimeout( timeout );
13 timeout = setTimeout( function() {
15 fn.apply( context, args );
20 api.Preview = api.Messenger.extend({
23 * - url - the URL of preview frame
25 initialize: function( params, options ) {
28 api.Messenger.prototype.initialize.call( this, params, options );
30 this.body = $( document.body );
31 this.body.on( 'click.preview', 'a', function( event ) {
32 event.preventDefault();
33 self.send( 'scroll', 0 );
34 self.send( 'url', $(this).prop('href') );
37 // You cannot submit forms.
38 // @todo: Allow form submissions by mixing $_POST data with the customize setting $_POST data.
39 this.body.on( 'submit.preview', 'form', function( event ) {
40 event.preventDefault();
43 this.window = $( window );
44 this.window.on( 'scroll.preview', debounce( function() {
45 self.send( 'scroll', self.window.scrollTop() );
48 this.bind( 'scroll', function( distance ) {
49 self.window.scrollTop( distance );
55 api.settings = window._wpCustomizeSettings;
61 preview = new api.Preview({
62 url: window.location.href,
63 channel: api.settings.channel
66 preview.bind( 'settings', function( values ) {
67 $.each( values, function( id, value ) {
69 api( id ).set( value );
71 api.create( id, value );
75 preview.trigger( 'settings', api.settings.values );
77 preview.bind( 'setting', function( args ) {
82 if ( value = api( args.shift() ) )
83 value.set.apply( value, args );
86 preview.bind( 'sync', function( events ) {
87 $.each( events, function( event, args ) {
88 preview.trigger( event, args );
90 preview.send( 'synced' );
93 preview.bind( 'active', function() {
94 if ( api.settings.nonce )
95 preview.send( 'nonce', api.settings.nonce );
98 preview.send( 'ready' );
100 /* Custom Backgrounds */
101 bg = $.map(['color', 'image', 'position_x', 'repeat', 'attachment'], function( prop ) {
102 return 'background_' + prop;
105 api.when.apply( api, bg ).done( function( color, image, position_x, repeat, attachment ) {
106 var body = $(document.body),
108 style = $('#custom-background-css'),
111 // If custom backgrounds are active and we can't find the
112 // default output, bail.
113 if ( body.hasClass('custom-background') && ! style.length )
116 update = function() {
119 // The body will support custom backgrounds if either
120 // the color or image are set.
122 // See get_body_class() in /wp-includes/post-template.php
123 body.toggleClass( 'custom-background', !! ( color() || image() ) );
126 css += 'background-color: ' + color() + ';';
129 css += 'background-image: url("' + image() + '");';
130 css += 'background-position: top ' + position_x() + ';';
131 css += 'background-repeat: ' + repeat() + ';';
132 css += 'background-position: top ' + attachment() + ';';
135 // Refresh the stylesheet by removing and recreating it.
137 style = $('<style type="text/css" id="custom-background-css">body.custom-background { ' + css + ' }</style>').appendTo( head );
140 $.each( arguments, function() {