summaryrefslogtreecommitdiffstatshomepage
path: root/src/wp-includes/js/customize-preview.js
diff options
context:
space:
mode:
authorAndrew Nacin <nacin@git.wordpress.org>2013-08-07 05:25:25 +0000
committerAndrew Nacin <nacin@git.wordpress.org>2013-08-07 05:25:25 +0000
commitb43712e0f79a9f5bea52217e06155e2f471c490c (patch)
tree23d331e7b2f29689571f03cfa5f9b7b3b6cafab8 /src/wp-includes/js/customize-preview.js
parent5bbd08e1d17079a2e852517351f7405884a156b3 (diff)
downloadwordpress-b43712e0f79a9f5bea52217e06155e2f471c490c.tar.gz
wordpress-b43712e0f79a9f5bea52217e06155e2f471c490c.zip
New develop.svn.wordpress.org repository based on the old core.svn repository.
* All WordPress files move to a src/ directory. * New task runner (Grunt), configured to copy a built WordPress to build/. * svn:ignore and .gitignore for Gruntfile.js, wp-config.php, and node.js. * Remove Akismet external from develop.svn. Still exists in core.svn. * Drop minified files from src/. The build process will now generate these. props koop. see #24976. and see http://wp.me/p2AvED-1AI. git-svn-id: https://develop.svn.wordpress.org/trunk@25001 602fd350-edb4-49c9-b593-d223f7449a82
Diffstat (limited to 'src/wp-includes/js/customize-preview.js')
-rw-r--r--src/wp-includes/js/customize-preview.js146
1 files changed, 146 insertions, 0 deletions
diff --git a/src/wp-includes/js/customize-preview.js b/src/wp-includes/js/customize-preview.js
new file mode 100644
index 0000000000..caefe653a9
--- /dev/null
+++ b/src/wp-includes/js/customize-preview.js
@@ -0,0 +1,146 @@
+(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
+ api.create( id, value );
+ });
+ });
+
+ preview.trigger( 'settings', api.settings.values );
+
+ preview.bind( 'setting', function( args ) {
+ var value;
+
+ args = args.slice();
+
+ if ( value = api( args.shift() ) )
+ value.set.apply( value, args );
+ });
+
+ preview.bind( 'sync', function( events ) {
+ $.each( events, function( event, args ) {
+ preview.trigger( event, args );
+ });
+ preview.send( 'synced' );
+ });
+
+ preview.bind( 'active', function() {
+ if ( api.settings.nonce )
+ preview.send( 'nonce', api.settings.nonce );
+ });
+
+ preview.send( 'ready' );
+
+ /* 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;
+
+ // If custom backgrounds are active and we can't find the
+ // default output, bail.
+ if ( body.hasClass('custom-background') && ! style.length )
+ return;
+
+ 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() + ';';
+ css += 'background-attachment: ' + attachment() + ';';
+ }
+
+ // 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 );
+ });
+ });
+ });
+
+})( wp, jQuery );