var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
import { window } from './utils/globals';
import { isArray } from './utils/type-utils';
import { addEventListener } from './utils';
// This class is responsible for tracking scroll events and maintaining the scroll context
var ScrollManager = /** @class */ (function () {
function ScrollManager(instance) {
var _this = this;
this.instance = instance;
this._updateScrollData = function () {
var _a, _b, _c, _d;
if (!_this.context) {
_this.context = {};
}
var el = _this.scrollElement();
var scrollY = _this.scrollY();
var scrollHeight = el ? Math.max(0, el.scrollHeight - el.clientHeight) : 0;
var contentY = scrollY + ((el === null || el === void 0 ? void 0 : el.clientHeight) || 0);
var contentHeight = (el === null || el === void 0 ? void 0 : el.scrollHeight) || 0;
_this.context.lastScrollY = Math.ceil(scrollY);
_this.context.maxScrollY = Math.max(scrollY, (_a = _this.context.maxScrollY) !== null && _a !== void 0 ? _a : 0);
_this.context.maxScrollHeight = Math.max(scrollHeight, (_b = _this.context.maxScrollHeight) !== null && _b !== void 0 ? _b : 0);
_this.context.lastContentY = contentY;
_this.context.maxContentY = Math.max(contentY, (_c = _this.context.maxContentY) !== null && _c !== void 0 ? _c : 0);
_this.context.maxContentHeight = Math.max(contentHeight, (_d = _this.context.maxContentHeight) !== null && _d !== void 0 ? _d : 0);
};
}
ScrollManager.prototype.getContext = function () {
return this.context;
};
ScrollManager.prototype.resetContext = function () {
var ctx = this.context;
// update the scroll properties for the new page, but wait until the next tick
// of the event loop
setTimeout(this._updateScrollData, 0);
return ctx;
};
// `capture: true` is required to get scroll events for other scrollable elements
// on the page, not just the window
// see https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#usecapture
ScrollManager.prototype.startMeasuringScrollPosition = function () {
addEventListener(window, 'scroll', this._updateScrollData, { capture: true });
addEventListener(window, 'scrollend', this._updateScrollData, { capture: true });
addEventListener(window, 'resize', this._updateScrollData);
};
ScrollManager.prototype.scrollElement = function () {
var e_1, _a;
if (this.instance.config.scroll_root_selector) {
var selectors = isArray(this.instance.config.scroll_root_selector)
? this.instance.config.scroll_root_selector
: [this.instance.config.scroll_root_selector];
try {
for (var selectors_1 = __values(selectors), selectors_1_1 = selectors_1.next(); !selectors_1_1.done; selectors_1_1 = selectors_1.next()) {
var selector = selectors_1_1.value;
var element = window === null || window === void 0 ? void 0 : window.document.querySelector(selector);
if (element) {
return element;
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (selectors_1_1 && !selectors_1_1.done && (_a = selectors_1.return)) _a.call(selectors_1);
}
finally { if (e_1) throw e_1.error; }
}
return undefined;
}
else {
return window === null || window === void 0 ? void 0 : window.document.documentElement;
}
};
ScrollManager.prototype.scrollY = function () {
if (this.instance.config.scroll_root_selector) {
var element = this.scrollElement();
return (element && element.scrollTop) || 0;
}
else {
return window ? window.scrollY || window.pageYOffset || window.document.documentElement.scrollTop || 0 : 0;
}
};
ScrollManager.prototype.scrollX = function () {
if (this.instance.config.scroll_root_selector) {
var element = this.scrollElement();
return (element && element.scrollLeft) || 0;
}
else {
return window ? window.scrollX || window.pageXOffset || window.document.documentElement.scrollLeft || 0 : 0;
}
};
return ScrollManager;
}());
export { ScrollManager };
//# sourceMappingURL=scroll-manager.js.map