2012-08-22 20:04:18 -04:00
|
|
|
(function( exports, $ ){
|
|
|
|
var api = wp.customize,
|
|
|
|
debounce;
|
|
|
|
|
2014-08-08 19:31:15 -04:00
|
|
|
/**
|
|
|
|
* Returns a debounced version of the function.
|
|
|
|
*
|
|
|
|
* @todo Require Underscore.js for this file and retire this.
|
|
|
|
*/
|
2012-08-22 20:04:18 -04:00
|
|
|
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 );
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2014-08-08 19:31:15 -04:00
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @augments wp.customize.Messenger
|
|
|
|
* @augments wp.customize.Class
|
|
|
|
* @mixes wp.customize.Events
|
|
|
|
*/
|
2012-08-22 20:04:18 -04:00
|
|
|
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;
|
|
|
|
|
2014-12-16 06:53:22 -05:00
|
|
|
var bg;
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2014-12-16 06:53:22 -05:00
|
|
|
api.preview = new api.Preview({
|
2012-08-22 20:04:18 -04:00
|
|
|
url: window.location.href,
|
|
|
|
channel: api.settings.channel
|
|
|
|
});
|
|
|
|
|
2014-12-16 06:53:22 -05:00
|
|
|
api.preview.bind( 'settings', function( values ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
$.each( values, function( id, value ) {
|
|
|
|
if ( api.has( id ) )
|
|
|
|
api( id ).set( value );
|
|
|
|
else
|
|
|
|
api.create( id, value );
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-12-16 06:53:22 -05:00
|
|
|
api.preview.trigger( 'settings', api.settings.values );
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2014-12-16 06:53:22 -05:00
|
|
|
api.preview.bind( 'setting', function( args ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
var value;
|
|
|
|
|
|
|
|
args = args.slice();
|
|
|
|
|
|
|
|
if ( value = api( args.shift() ) )
|
|
|
|
value.set.apply( value, args );
|
|
|
|
});
|
|
|
|
|
2014-12-16 06:53:22 -05:00
|
|
|
api.preview.bind( 'sync', function( events ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
$.each( events, function( event, args ) {
|
2014-12-16 06:53:22 -05:00
|
|
|
api.preview.trigger( event, args );
|
2012-08-22 20:04:18 -04:00
|
|
|
});
|
2014-12-16 06:53:22 -05:00
|
|
|
api.preview.send( 'synced' );
|
2012-08-22 20:04:18 -04:00
|
|
|
});
|
|
|
|
|
2014-12-16 06:53:22 -05:00
|
|
|
api.preview.bind( 'active', function() {
|
2014-11-11 17:29:23 -05:00
|
|
|
if ( api.settings.nonce ) {
|
2014-12-16 06:53:22 -05:00
|
|
|
api.preview.send( 'nonce', api.settings.nonce );
|
2014-11-11 17:29:23 -05:00
|
|
|
}
|
|
|
|
|
2014-12-16 06:53:22 -05:00
|
|
|
api.preview.send( 'documentTitle', document.title );
|
2014-11-11 17:29:23 -05:00
|
|
|
});
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2014-12-16 06:53:22 -05:00
|
|
|
api.preview.send( 'ready', {
|
Improve/introduce Customizer JavaScript models for Controls, Sections, and Panels.
* Introduce models for panels and sections.
* Introduce API to expand and focus a control, section or panel.
* Allow deep-linking to panels, sections, and controls inside of the Customizer.
* Clean up `accordion.js`, removing all Customizer-specific logic.
* Add initial unit tests for `wp.customize.Class` in `customize-base.js`.
https://make.wordpress.org/core/2014/10/27/toward-a-complete-javascript-api-for-the-customizer/ provides an overview of how to use the JavaScript API.
props westonruter, celloexpressions, ryankienstra.
see #28032, #28579, #28580, #28650, #28709, #29758.
fixes #29529.
Built from https://develop.svn.wordpress.org/trunk@30102
git-svn-id: http://core.svn.wordpress.org/trunk@30102 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2014-10-29 18:51:22 -04:00
|
|
|
activePanels: api.settings.activePanels,
|
|
|
|
activeSections: api.settings.activeSections,
|
2014-07-09 19:58:16 -04:00
|
|
|
activeControls: api.settings.activeControls
|
|
|
|
} );
|
2012-08-22 20:04:18 -04: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 body = $(document.body),
|
|
|
|
head = $('head'),
|
|
|
|
style = $('#custom-background-css'),
|
|
|
|
update;
|
|
|
|
|
|
|
|
update = function() {
|
|
|
|
var css = '';
|
|
|
|
|
|
|
|
// The body will support custom backgrounds if either
|
|
|
|
// the color or image are set.
|
|
|
|
//
|
|
|
|
// See get_body_class() in /wp-includes/post-template.php
|
|
|
|
body.toggleClass( 'custom-background', !! ( color() || image() ) );
|
|
|
|
|
|
|
|
if ( color() )
|
|
|
|
css += 'background-color: ' + color() + ';';
|
|
|
|
|
|
|
|
if ( image() ) {
|
|
|
|
css += 'background-image: url("' + image() + '");';
|
|
|
|
css += 'background-position: top ' + position_x() + ';';
|
|
|
|
css += 'background-repeat: ' + repeat() + ';';
|
2012-10-07 10:46:39 -04:00
|
|
|
css += 'background-attachment: ' + attachment() + ';';
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Refresh the stylesheet by removing and recreating it.
|
|
|
|
style.remove();
|
|
|
|
style = $('<style type="text/css" id="custom-background-css">body.custom-background { ' + css + ' }</style>').appendTo( head );
|
|
|
|
};
|
|
|
|
|
|
|
|
$.each( arguments, function() {
|
|
|
|
this.bind( update );
|
|
|
|
});
|
|
|
|
});
|
2014-12-16 06:53:22 -05:00
|
|
|
|
|
|
|
api.trigger( 'preview-ready' );
|
2012-08-22 20:04:18 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
})( wp, jQuery );
|