jellyfin-web/dashboard-ui/components/scrollthreshold.js

74 lines
2.1 KiB
JavaScript
Raw Normal View History

2016-04-23 13:30:45 -07:00
define(['events'], function (events) {
function thresholdMonitor(elem, horizontal, lowerTolerance, upperTolerance) {
var defaultTolerance = horizontal ? (screen.availWidth / 2) : (screen.availHeight / 2);
lowerTolerance = lowerTolerance || defaultTolerance;
upperTolerance = upperTolerance || defaultTolerance;
var self = this;
var upperTriggered = false;
var lowerTriggered = false;
var scrollSize;
function getScrollSize() {
if (!scrollSize) {
2016-04-23 13:49:14 -07:00
scrollSize = horizontal ? (elem.scrollWidth - elem.clientWidth) : (elem.scrollHeight - elem.offsetHeight);
2016-04-23 13:30:45 -07:00
}
return scrollSize;
}
function onScroll(e) {
if (lowerTriggered && upperTriggered) {
return;
}
var position = horizontal ? elem.scrollLeft : elem.scrollTop;
2016-04-23 13:49:14 -07:00
//console.log('onscroll: ' + position + '-' + getScrollSize());
2016-04-23 13:30:45 -07:00
// Detect upper threshold
if (!upperTriggered && position < upperTolerance) {
upperTriggered = true;
events.trigger(self, 'upper-threshold');
}
// Detect lower threshold
if (!lowerTriggered && position >= (getScrollSize() - lowerTolerance)) {
lowerTriggered = false;
events.trigger(self, 'lower-threshold');
}
}
self.reset = function () {
self.resetSize();
upperTriggered = false;
lowerTriggered = false;
};
self.resetSize = function () {
scrollSize = null;
};
self.enabled = function (enabled) {
self.reset();
if (enabled) {
elem.addEventListener('scroll', onScroll, true);
} else {
elem.removeEventListener('scroll', onScroll, true);
}
};
self.enabled(true);
self.destroy = function () {
self.enabled(false);
};
}
return thresholdMonitor;
});