2015-08-22 13:24:25 -04:00
|
|
|
/*
|
|
|
|
* Script run inside a Customizer preview frame.
|
|
|
|
*/
|
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({
|
|
|
|
/**
|
2016-01-21 01:08:27 -05:00
|
|
|
* @param {object} params - Parameters to configure the messenger.
|
|
|
|
* @param {object} options - Extend any instance parameter or method with this object.
|
2012-08-22 20:04:18 -04:00
|
|
|
*/
|
|
|
|
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 ) {
|
2016-01-21 01:08:27 -05:00
|
|
|
var link, isInternalJumpLink;
|
|
|
|
link = $( this );
|
|
|
|
isInternalJumpLink = ( '#' === link.attr( 'href' ).substr( 0, 1 ) );
|
2012-08-22 20:04:18 -04:00
|
|
|
event.preventDefault();
|
2016-01-21 01:08:27 -05:00
|
|
|
|
|
|
|
if ( isInternalJumpLink && '#' !== link.attr( 'href' ) ) {
|
|
|
|
$( link.attr( 'href' ) ).each( function() {
|
|
|
|
this.scrollIntoView();
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note the shift key is checked so shift+click on widgets or
|
|
|
|
* nav menu items can just result on focusing on the corresponding
|
|
|
|
* control instead of also navigating to the URL linked to.
|
|
|
|
*/
|
|
|
|
if ( event.shiftKey || isInternalJumpLink ) {
|
|
|
|
return;
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
self.send( 'scroll', 0 );
|
2016-01-21 01:08:27 -05:00
|
|
|
self.send( 'url', link.prop( 'href' ) );
|
2012-08-22 20:04:18 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
// 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() {
|
2016-01-26 18:52:25 -05:00
|
|
|
var bg, setValue;
|
|
|
|
|
2012-08-22 20:04:18 -04:00
|
|
|
api.settings = window._wpCustomizeSettings;
|
2016-01-26 18:52:25 -05:00
|
|
|
if ( ! api.settings ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
return;
|
2016-01-26 18:52:25 -05:00
|
|
|
}
|
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
|
|
|
|
});
|
|
|
|
|
2016-01-26 18:52:25 -05:00
|
|
|
/**
|
|
|
|
* Create/update a setting value.
|
|
|
|
*
|
|
|
|
* @param {string} id - Setting ID.
|
|
|
|
* @param {*} value - Setting value.
|
|
|
|
* @param {boolean} [createDirty] - Whether to create a setting as dirty. Defaults to false.
|
|
|
|
*/
|
|
|
|
setValue = function( id, value, createDirty ) {
|
|
|
|
var setting = api( id );
|
|
|
|
if ( setting ) {
|
|
|
|
setting.set( value );
|
|
|
|
} else {
|
|
|
|
createDirty = createDirty || false;
|
|
|
|
setting = api.create( id, value, {
|
|
|
|
id: id
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Mark dynamically-created settings as dirty so they will get posted.
|
|
|
|
if ( createDirty ) {
|
|
|
|
setting._dirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-16 06:53:22 -05:00
|
|
|
api.preview.bind( 'settings', function( values ) {
|
2016-01-26 18:52:25 -05:00
|
|
|
$.each( values, setValue );
|
2012-08-22 20:04:18 -04:00
|
|
|
});
|
|
|
|
|
2014-12-16 06:53:22 -05:00
|
|
|
api.preview.trigger( 'settings', api.settings.values );
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2016-01-26 18:52:25 -05:00
|
|
|
$.each( api.settings._dirty, function( i, id ) {
|
|
|
|
var setting = api( id );
|
|
|
|
if ( setting ) {
|
|
|
|
setting._dirty = true;
|
|
|
|
}
|
|
|
|
} );
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2016-01-26 18:52:25 -05:00
|
|
|
api.preview.bind( 'setting', function( args ) {
|
|
|
|
var createDirty = true;
|
|
|
|
setValue.apply( null, args.concat( createDirty ) );
|
2012-08-22 20:04:18 -04:00
|
|
|
});
|
|
|
|
|
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() {
|
2016-01-27 12:55:26 -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
|
|
|
|
2016-01-26 18:52:25 -05:00
|
|
|
api.preview.bind( 'saved', function( response ) {
|
|
|
|
api.trigger( 'saved', response );
|
|
|
|
} );
|
|
|
|
|
|
|
|
api.bind( 'saved', function() {
|
|
|
|
api.each( function( setting ) {
|
|
|
|
setting._dirty = false;
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2016-01-27 12:55:26 -05:00
|
|
|
api.preview.bind( 'nonce-refresh', function( nonce ) {
|
|
|
|
$.extend( api.settings.nonce, nonce );
|
|
|
|
} );
|
|
|
|
|
2015-09-05 15:53:24 -04:00
|
|
|
/*
|
|
|
|
* Send a message to the parent customize frame with a list of which
|
|
|
|
* containers and controls are active.
|
|
|
|
*/
|
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
|
|
|
|
2015-03-10 11:56:26 -04:00
|
|
|
// Display a loading indicator when preview is reloading, and remove on failure.
|
|
|
|
api.preview.bind( 'loading-initiated', function () {
|
|
|
|
$( 'body' ).addClass( 'wp-customizer-unloading' );
|
|
|
|
});
|
|
|
|
api.preview.bind( 'loading-failed', function () {
|
|
|
|
$( 'body' ).removeClass( 'wp-customizer-unloading' );
|
|
|
|
});
|
|
|
|
|
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
|
|
|
|
Customize: Introduce Logo support for themes.
Allows a common theme feature to have a common implementation provided by core and available in a consistent location for users.
See https://make.wordpress.org/core/2016/02/24/theme-logo-support/
Props kwight, enejb, jeherve, bhubbard, samhotchkiss, zinigor, eliorivero, adamsilverstein, melchoyce, ryan, mikeschroder, westonruter, pento, karmatosed, celloexpressions, obenland.
See #33755.
Built from https://develop.svn.wordpress.org/trunk@36698
git-svn-id: http://core.svn.wordpress.org/trunk@36665 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-02-24 17:10:26 -05:00
|
|
|
/**
|
|
|
|
* Site Logo
|
|
|
|
*
|
|
|
|
* The site logo setting only contains the attachment ID. To avoid having to send an AJAX request to get more
|
|
|
|
* data, we send a separate message with the attachment data we get from the Customizer's media modal.
|
|
|
|
* Therefore first callback handles only the event of a new logo being selected.
|
|
|
|
*
|
|
|
|
* We don't need any information about a removed logo, so the second callback only handles that.
|
|
|
|
*
|
|
|
|
* @since 4.5.0
|
|
|
|
*/
|
|
|
|
api.preview.bind( 'site-logo-attachment-data', function( attachment ) {
|
|
|
|
var $logo = $( '.site-logo' ),
|
|
|
|
size = $logo.data( 'size' ),
|
|
|
|
srcset = [];
|
|
|
|
|
|
|
|
// If the source was smaller than the size required by the theme, give the biggest we've got.
|
|
|
|
if ( ! attachment.sizes[ size ] ) {
|
|
|
|
size = 'full';
|
|
|
|
}
|
|
|
|
|
|
|
|
_.each( attachment.sizes, function( size ) {
|
|
|
|
srcset.push( size.url + ' ' + size.width + 'w' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
$logo.attr( {
|
|
|
|
height: attachment.sizes[ size ].height,
|
|
|
|
width: attachment.sizes[ size ].width,
|
|
|
|
src: attachment.sizes[ size ].url,
|
|
|
|
srcset: srcset
|
|
|
|
} );
|
|
|
|
|
|
|
|
$( '.site-logo-link' ).show();
|
|
|
|
$( 'body' ).addClass( 'wp-site-logo' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
api( 'site_logo', function( setting ) {
|
|
|
|
setting.bind( function( newValue ) {
|
|
|
|
if ( ! newValue ) {
|
|
|
|
$( '.site-logo-link' ).hide();
|
|
|
|
$( 'body' ).removeClass( 'wp-site-logo' );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Focus on the control when the logo is clicked, if there is no site_logo partial.
|
|
|
|
if ( ! api.selectiveRefresh || ! api.selectiveRefresh.partial.has( 'site_logo' ) ) {
|
|
|
|
$( document.body ).on( 'click', '.site-logo-link', function( e ) {
|
|
|
|
if ( ! e.shiftKey ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
api.preview.send( 'focus-control-for-setting', 'site_logo' );
|
|
|
|
} );
|
|
|
|
$( '.site-logo-link' ).attr( 'title', api.settings.l10n.shiftClickToEdit );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2014-12-16 06:53:22 -05:00
|
|
|
api.trigger( 'preview-ready' );
|
2012-08-22 20:04:18 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
})( wp, jQuery );
|