mirror of
https://github.com/DanielnetoDotCom/YouPHPTube
synced 2025-10-03 09:49:28 +02:00
97 lines
2.4 KiB
JavaScript
97 lines
2.4 KiB
JavaScript
// scroll-watch
|
|
( function( window, factory ) {
|
|
// universal module definition
|
|
if ( typeof module == 'object' && module.exports ) {
|
|
// CommonJS
|
|
module.exports = factory(
|
|
window,
|
|
require('./core'),
|
|
require('fizzy-ui-utils'),
|
|
);
|
|
} else {
|
|
// browser global
|
|
factory(
|
|
window,
|
|
window.InfiniteScroll,
|
|
window.fizzyUIUtils,
|
|
);
|
|
}
|
|
|
|
}( window, function factory( window, InfiniteScroll, utils ) {
|
|
|
|
let proto = InfiniteScroll.prototype;
|
|
|
|
// default options
|
|
Object.assign( InfiniteScroll.defaults, {
|
|
scrollThreshold: 400,
|
|
// elementScroll: null,
|
|
} );
|
|
|
|
InfiniteScroll.create.scrollWatch = function() {
|
|
// events
|
|
this.pageScrollHandler = this.onPageScroll.bind( this );
|
|
this.resizeHandler = this.onResize.bind( this );
|
|
|
|
let scrollThreshold = this.options.scrollThreshold;
|
|
let isEnable = scrollThreshold || scrollThreshold === 0;
|
|
if ( isEnable ) this.enableScrollWatch();
|
|
};
|
|
|
|
InfiniteScroll.destroy.scrollWatch = function() {
|
|
this.disableScrollWatch();
|
|
};
|
|
|
|
proto.enableScrollWatch = function() {
|
|
if ( this.isScrollWatching ) return;
|
|
|
|
this.isScrollWatching = true;
|
|
this.updateMeasurements();
|
|
this.updateScroller();
|
|
// TODO disable after error?
|
|
this.on( 'last', this.disableScrollWatch );
|
|
this.bindScrollWatchEvents( true );
|
|
};
|
|
|
|
proto.disableScrollWatch = function() {
|
|
if ( !this.isScrollWatching ) return;
|
|
|
|
this.bindScrollWatchEvents( false );
|
|
delete this.isScrollWatching;
|
|
};
|
|
|
|
proto.bindScrollWatchEvents = function( isBind ) {
|
|
let addRemove = isBind ? 'addEventListener' : 'removeEventListener';
|
|
this.scroller[ addRemove ]( 'scroll', this.pageScrollHandler );
|
|
window[ addRemove ]( 'resize', this.resizeHandler );
|
|
};
|
|
|
|
proto.onPageScroll = InfiniteScroll.throttle( function() {
|
|
let distance = this.getBottomDistance();
|
|
if ( distance <= this.options.scrollThreshold ) {
|
|
this.dispatchEvent('scrollThreshold');
|
|
}
|
|
} );
|
|
|
|
proto.getBottomDistance = function() {
|
|
let bottom, scrollY;
|
|
if ( this.options.elementScroll ) {
|
|
bottom = this.scroller.scrollHeight;
|
|
scrollY = this.scroller.scrollTop + this.scroller.clientHeight;
|
|
} else {
|
|
bottom = this.top + this.element.clientHeight;
|
|
scrollY = window.scrollY + this.windowHeight;
|
|
}
|
|
return bottom - scrollY;
|
|
};
|
|
|
|
proto.onResize = function() {
|
|
this.updateMeasurements();
|
|
};
|
|
|
|
utils.debounceMethod( InfiniteScroll, 'onResize', 150 );
|
|
|
|
// -------------------------- -------------------------- //
|
|
|
|
return InfiniteScroll;
|
|
|
|
} ) );
|