Select Git revision
start-server.sh
customize-preview.js 3.48 KiB
(function( exports, $ ){
var api = wp.customize,
debounce;
debounce = function( fn, delay, context ) {
var timeout;
return function() {
var args = arguments;
context = context || this;
clearTimeout( timeout );
timeout = setTimeout( function() {
timeout = null;
fn.apply( context, args );
}, delay );
};
};
api.Preview = api.Messenger.extend({
/**
* Requires params:
* - url - the URL of preview frame
*/
initialize: function( params, options ) {
var self = this;
api.Messenger.prototype.initialize.call( this, params, options );
this.body = $( document.body );
this.body.on( 'click.preview', 'a', function( event ) {
event.preventDefault();
self.send( 'scroll', 0 );
self.send( 'url', $(this).prop('href') );
});
// You cannot submit forms.
// @todo: Allow form submissions by mixing $_POST data with the customize setting $_POST data.
this.body.on( 'submit.preview', 'form', function( event ) {
event.preventDefault();
});
this.window = $( window );
this.window.on( 'scroll.preview', debounce( function() {
self.send( 'scroll', self.window.scrollTop() );
}, 200 ));
this.bind( 'scroll', function( distance ) {
self.window.scrollTop( distance );
});
}
});
$( function() {
api.settings = window._wpCustomizeSettings;
if ( ! api.settings )
return;
var preview, bg;
preview = new api.Preview({
url: window.location.href,
channel: api.settings.channel
});
preview.bind( 'settings', function( values ) {
$.each( values, function( id, value ) {
if ( api.has( id ) )
api( id ).set( value );
else