@exadel/esl
Version:
Exadel Smart Library (ESL) is the lightweight custom elements library that provide a set of super-flexible components
42 lines (41 loc) • 1.8 kB
JavaScript
import { Rect } from '../../esl-utils/dom/rect';
import { keepPosition } from './incremental-scroll-align-strategies';
import { ESLIncrementalScrollAxisStepper } from './incremental-scroll-axis-stepper';
/** Coordinates incremental scrolling for both axes using provided alignment strategies */
export class ESLIncrementalScroller {
constructor($el, options) {
var _a, _b;
this.$el = $el;
this.options = options;
this.steppers = [];
const { alignment = {} } = options;
this.steppers = [
(_a = alignment.x) !== null && _a !== void 0 ? _a : keepPosition,
(_b = alignment.y) !== null && _b !== void 0 ? _b : keepPosition
].map((strategy) => new ESLIncrementalScrollAxisStepper(strategy(options), options));
}
/** Indicates whether scrolling should continue based on axis steppers' states */
get shouldContinue() {
return this.steppers.some((s) => s.shouldContinueStepping());
}
/** Current geometry snapshot used by axis steppers to compute deltas */
get stepOptions() {
return {
elRect: this.$el ? Rect.from(this.$el) : undefined,
containerRect: this.options.scrollContainer ? Rect.from(this.options.scrollContainer) : undefined
};
}
/** Executes a single scroll step for both axes based on computed deltas */
step() {
const { stepOptions } = this;
const [dX, dY] = this.steppers.map((stepper) => (stepper).computeStep(stepOptions));
const { scrollContainer } = this.options;
if (scrollContainer) {
scrollContainer.scrollLeft += dX;
scrollContainer.scrollTop += dY;
}
else {
window.scrollTo(window.scrollX + dX, window.scrollY + dY);
}
}
}