Customize: Split out link `click.preview` and form `submit.preview` event handlers from anonymous functions into named methods on `wp.customize.Preview` for extensibility.

See #30937.


git-svn-id: https://develop.svn.wordpress.org/trunk@38811 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Weston Ruter 2016-10-19 02:06:00 +00:00
parent 83b059aa19
commit 500041a4bc
1 changed files with 103 additions and 78 deletions

View File

@ -105,9 +105,38 @@
preview.add( 'scheme', urlParser.protocol.replace( /:$/, '' ) ); preview.add( 'scheme', urlParser.protocol.replace( /:$/, '' ) );
preview.body = $( document.body ); preview.body = $( document.body );
preview.body.on( 'click.preview', 'a', function( event ) { preview.body.on( 'click.preview', 'a', function( event ) {
var link, isInternalJumpLink; preview.handleLinkClick( event );
link = $( this ); } );
preview.body.on( 'submit.preview', 'form', function( event ) {
preview.handleFormSubmit( event );
} );
preview.window = $( window );
if ( api.settings.channel ) {
preview.window.on( 'scroll.preview', debounce( function() {
preview.send( 'scroll', preview.window.scrollTop() );
}, 200 ) );
preview.bind( 'scroll', function( distance ) {
preview.window.scrollTop( distance );
});
}
},
/**
* Handle link clicks in preview.
*
* @since 4.7.0
*
* @param {jQuery.Event} event Event.
*/
handleLinkClick: function( event ) {
var preview = this, link, isInternalJumpLink;
link = $( event.target );
// No-op if the anchor is not a link. // No-op if the anchor is not a link.
if ( _.isUndefined( link.attr( 'href' ) ) ) { if ( _.isUndefined( link.attr( 'href' ) ) ) {
@ -147,14 +176,23 @@
// Note: It's not relevant to send scroll because sending url message will have the same effect. // Note: It's not relevant to send scroll because sending url message will have the same effect.
preview.send( 'url', link.prop( 'href' ) ); preview.send( 'url', link.prop( 'href' ) );
} ); },
preview.body.on( 'submit.preview', 'form', function( event ) { /**
var urlParser = document.createElement( 'a' ); * Handle form submit.
urlParser.href = this.action; *
* @since 4.7.0
*
* @param {jQuery.Event} event Event.
*/
handleFormSubmit: function( event ) {
var preview = this, urlParser, form;
urlParser = document.createElement( 'a' );
form = $( event.target );
urlParser.href = form.prop( 'action' );
// If the link is not previewable, prevent the browser from navigating to it. // If the link is not previewable, prevent the browser from navigating to it.
if ( 'GET' !== this.method.toUpperCase() || ! api.isLinkPreviewable( urlParser ) ) { if ( 'GET' !== form.prop( 'method' ).toUpperCase() || ! api.isLinkPreviewable( urlParser ) ) {
wp.a11y.speak( api.settings.l10n.formUnpreviewable ); wp.a11y.speak( api.settings.l10n.formUnpreviewable );
event.preventDefault(); event.preventDefault();
return; return;
@ -180,25 +218,12 @@
if ( urlParser.search.length > 1 ) { if ( urlParser.search.length > 1 ) {
urlParser.search += '&'; urlParser.search += '&';
} }
urlParser.search += $( this ).serialize(); urlParser.search += form.serialize();
api.preview.send( 'url', urlParser.href ); preview.send( 'url', urlParser.href );
} }
// Prevent default since navigation should be done via sending url message or via JS submit handler. // Prevent default since navigation should be done via sending url message or via JS submit handler.
event.preventDefault(); event.preventDefault();
});
preview.window = $( window );
if ( api.settings.channel ) {
preview.window.on( 'scroll.preview', debounce( function() {
preview.send( 'scroll', preview.window.scrollTop() );
}, 200 ) );
preview.bind( 'scroll', function( distance ) {
preview.window.scrollTop( distance );
});
}
} }
}); });