Skip to content

Commit 3bf681a

Browse files
committed
Lazy Images: Back-compat for events
1 parent 16e6bfc commit 3bf681a

2 files changed

Lines changed: 43 additions & 19 deletions

File tree

modules/infinite-scroll/infinity.js

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -126,26 +126,16 @@
126126
* Renders the results from a successful response.
127127
*/
128128
Scroller.prototype.render = function( response ) {
129-
var postLoadEvent;
130-
131129
this.body.addClass( 'infinity-success' );
132130

133131
// Check if we can wrap the html
134132
this.element.append( response.html );
135133

136-
try {
137-
postLoadEvent = new CustomEvent( 'post-load', {
138-
bubbles: true,
139-
cancelable: true,
140-
detail: response,
141-
} );
142-
} catch ( e ) {
143-
postLoadEvent = document.createEvent( 'CustomEvent' );
144-
postLoadEvent.initCustomEvent( 'post-load', true, true, response );
145-
}
134+
this.trigger( this.body.get( 0 ), 'is.post-load', {
135+
jqueryEventName: 'post-load',
136+
data: response,
137+
} );
146138

147-
this.body.get( 0 ).dispatchEvent( postLoadEvent );
148-
this.body.trigger( 'post-load', response );
149139
this.ready = true;
150140
};
151141

@@ -634,7 +624,10 @@
634624
$set.css( 'min-height', '' ).removeClass( 'is--replaced' );
635625
if ( this.pageNum in self.pageCache ) {
636626
$set.html( self.pageCache[ this.pageNum ].html );
637-
self.body.trigger( 'post-load', self.pageCache[ this.pageNum ] );
627+
self.trigger( self.body.get( 0 ), 'is.post-load', {
628+
jqueryEventName: 'post-load',
629+
data: self.pageCache[ this.pageNum ],
630+
} );
638631
}
639632
}
640633
} );
@@ -731,6 +724,40 @@
731724
this.disabled = false;
732725
};
733726

727+
/**
728+
* Emits custom JS events.
729+
*
730+
* @param {Node} el
731+
* @param {string} eventName
732+
* @param {*} data
733+
*/
734+
Scroller.prototype.trigger = function( el, eventName, opts ) {
735+
opts = opts || {};
736+
737+
/**
738+
* Emit the event in a jQuery way for backwards compatibility where necessary.
739+
*/
740+
if ( opts.jqueryEventName && jQuery ) {
741+
jQuery( el ).trigger( opts.jqueryEventName, opts.data || null );
742+
}
743+
744+
/**
745+
* Emit the event in a standard way.
746+
*/
747+
var e;
748+
try {
749+
e = new CustomEvent( eventName, {
750+
bubbles: true,
751+
cancelable: true,
752+
detail: opts.data || null,
753+
} );
754+
} catch ( err ) {
755+
e = document.createEvent( 'CustomEvent' );
756+
e.initCustomEvent( eventName, true, true, opts.data || null );
757+
}
758+
el.dispatchEvent( e );
759+
};
760+
734761
/**
735762
* Ready, set, go!
736763
*/

modules/lazy-images/js/lazy-images.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,17 @@ var jetpackLazyImagesModule = function() {
1212
image,
1313
i;
1414

15-
16-
1715
lazy_load_init();
1816

1917
var bodyEl = document.querySelector( 'body' );
2018
if ( bodyEl ) {
2119
// Lazy load images that are brought in from Infinite Scroll
22-
bodyEl.addEventListener( 'post-load', lazy_load_init );
20+
bodyEl.addEventListener( 'is.post-load', lazy_load_init );
2321

2422
// Add event to provide optional compatibility for other code.
2523
bodyEl.addEventListener( 'jetpack-lazy-images-load', lazy_load_init );
2624
}
2725

28-
2926
function lazy_load_init() {
3027
images = document.querySelectorAll( 'img.jetpack-lazy-image:not(.jetpack-lazy-image--handled)' );
3128
imageCount = images.length;

0 commit comments

Comments
 (0)