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:
parent
83b059aa19
commit
500041a4bc
@ -105,9 +105,38 @@
|
||||
preview.add( 'scheme', urlParser.protocol.replace( /:$/, '' ) );
|
||||
|
||||
preview.body = $( document.body );
|
||||
|
||||
preview.body.on( 'click.preview', 'a', function( event ) {
|
||||
var link, isInternalJumpLink;
|
||||
link = $( this );
|
||||
preview.handleLinkClick( event );
|
||||
} );
|
||||
|
||||
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.
|
||||
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.
|
||||
preview.send( 'url', link.prop( 'href' ) );
|
||||
} );
|
||||
},
|
||||
|
||||
preview.body.on( 'submit.preview', 'form', function( event ) {
|
||||
var urlParser = document.createElement( 'a' );
|
||||
urlParser.href = this.action;
|
||||
/**
|
||||
* Handle form submit.
|
||||
*
|
||||
* @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 ( 'GET' !== this.method.toUpperCase() || ! api.isLinkPreviewable( urlParser ) ) {
|
||||
if ( 'GET' !== form.prop( 'method' ).toUpperCase() || ! api.isLinkPreviewable( urlParser ) ) {
|
||||
wp.a11y.speak( api.settings.l10n.formUnpreviewable );
|
||||
event.preventDefault();
|
||||
return;
|
||||
@ -180,25 +218,12 @@
|
||||
if ( urlParser.search.length > 1 ) {
|
||||
urlParser.search += '&';
|
||||
}
|
||||
urlParser.search += $( this ).serialize();
|
||||
api.preview.send( 'url', urlParser.href );
|
||||
urlParser.search += form.serialize();
|
||||
preview.send( 'url', urlParser.href );
|
||||
}
|
||||
|
||||
// Prevent default since navigation should be done via sending url message or via JS submit handler.
|
||||
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 );
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user