2012-02-25 05:12:43 +01:00
|
|
|
(function( exports, $ ){
|
2012-05-09 02:23:05 +02:00
|
|
|
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 );
|
|
|
|
};
|
|
|
|
};
|
2012-02-25 05:12:43 +01:00
|
|
|
|
|
|
|
api.Preview = api.Messenger.extend({
|
|
|
|
/**
|
|
|
|
* Requires params:
|
|
|
|
* - url - the URL of preview frame
|
|
|
|
*
|
|
|
|
* @todo: Perhaps add a window.onbeforeunload dialog in case the theme
|
|
|
|
* somehow attempts to leave the page and we don't catch it
|
|
|
|
* (which really shouldn't happen).
|
|
|
|
*/
|
|
|
|
initialize: function( url, options ) {
|
|
|
|
var self = this;
|
|
|
|
|
2012-04-16 16:02:28 +02:00
|
|
|
api.Messenger.prototype.initialize.call( this, url, null, options );
|
2012-02-25 05:12:43 +01:00
|
|
|
|
|
|
|
this.body = $( document.body );
|
|
|
|
this.body.on( 'click.preview', 'a', function( event ) {
|
|
|
|
event.preventDefault();
|
2012-05-21 19:58:17 +02:00
|
|
|
self.send( 'scroll', 0 );
|
2012-05-16 07:55:54 +02:00
|
|
|
self.send( 'url', $(this).prop('href') );
|
2012-02-25 05:12:43 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// You cannot submit forms.
|
|
|
|
// @todo: Namespace customizer settings so that we can mix the
|
|
|
|
// $_POST data with the customize setting $_POST data.
|
|
|
|
this.body.on( 'submit.preview', 'form', function( event ) {
|
|
|
|
event.preventDefault();
|
|
|
|
});
|
2012-05-09 02:23:05 +02:00
|
|
|
|
|
|
|
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 );
|
|
|
|
});
|
2012-02-25 05:12:43 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$( function() {
|
2012-05-07 22:03:39 +02:00
|
|
|
api.settings = window._wpCustomizeSettings;
|
2012-03-06 03:49:02 +01:00
|
|
|
if ( ! api.settings )
|
|
|
|
return;
|
|
|
|
|
2012-05-25 20:41:22 +02:00
|
|
|
var preview, bg;
|
2012-02-25 05:12:43 +01:00
|
|
|
|
2012-05-08 22:13:34 +02:00
|
|
|
preview = new api.Preview( window.location.href );
|
2012-03-06 03:49:02 +01:00
|
|
|
|
|
|
|
$.each( api.settings.values, function( id, value ) {
|
2012-05-16 00:14:46 +02:00
|
|
|
api.create( id, value );
|
2012-03-06 03:49:02 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
preview.bind( 'setting', function( args ) {
|
2012-05-16 00:14:46 +02:00
|
|
|
var value = api( args.shift() );
|
|
|
|
if ( value )
|
2012-05-16 07:55:54 +02:00
|
|
|
value.set.apply( value, args );
|
2012-03-06 03:49:02 +01:00
|
|
|
});
|
|
|
|
|
2012-05-25 20:41:22 +02:00
|
|
|
/* Custom Backgrounds */
|
|
|
|
bg = $.map(['color', 'image', 'position_x', 'repeat', 'attachment'], function( prop ) {
|
|
|
|
return 'background_' + prop;
|
|
|
|
});
|
|
|
|
|
|
|
|
api.when.apply( api, bg ).done( function( color, image, position_x, repeat, attachment ) {
|
|
|
|
var style = $('#custom-background-css'),
|
|
|
|
update;
|
|
|
|
|
|
|
|
if ( ! style.length )
|
|
|
|
return;
|
|
|
|
|
|
|
|
update = function() {
|
|
|
|
var css = '';
|
|
|
|
|
|
|
|
if ( color() )
|
|
|
|
css += 'background-color: #' + color() + ';';
|
|
|
|
|
|
|
|
if ( image() ) {
|
|
|
|
css += 'background-image: url("' + image() + '");';
|
|
|
|
css += 'background-position: top ' + position_x() + ';';
|
|
|
|
css += 'background-repeat: ' + repeat() + ';';
|
|
|
|
css += 'background-position: top ' + attachment() + ';';
|
|
|
|
}
|
|
|
|
|
|
|
|
style.html( 'body.custom-background { ' + css + ' }' );
|
|
|
|
};
|
|
|
|
|
|
|
|
$.each( arguments, function() {
|
|
|
|
this.bind( update );
|
2012-03-22 08:03:44 +01:00
|
|
|
});
|
2012-03-06 03:49:02 +01:00
|
|
|
});
|
2012-02-25 05:12:43 +01:00
|
|
|
});
|
|
|
|
|
2012-05-21 19:58:17 +02:00
|
|
|
})( wp, jQuery );
|