UNPKG

vscroll

Version:
75 lines 2.31 kB
import { Paddings } from './paddings'; import { Direction } from '../inputs/index'; export class Viewport { constructor(settings, routines, state, logger) { this.settings = settings; this.routines = routines; this.state = state; this.logger = logger; this.paddings = new Paddings(this.routines, settings); } reset(startIndex) { this.setOffset(); this.paddings.reset(this.getSize(), startIndex, this.offset); this.scrollPosition = this.paddings.backward.size || 0; this.state.scroll.reset(); } setPosition(value) { const oldPosition = this.scrollPosition; if (oldPosition === value) { this.logger.log(() => ['setting scroll position at', value, '[cancelled]']); return value; } this.routines.setScrollPosition(value); const position = this.scrollPosition; this.logger.log(() => [ 'setting scroll position at', position, ...(position !== value ? [`(${value})`] : []) ]); return position; } get scrollPosition() { return this.routines.getScrollPosition(); } set scrollPosition(value) { this.setPosition(value); } getSize() { return this.routines.getViewportSize(); } getScrollableSize() { return this.routines.getScrollerSize(); } getMaxScrollPosition() { return this.getScrollableSize() - this.getSize(); } getBufferPadding() { return this.getSize() * this.settings.padding; } getEdge(direction) { return this.routines.getViewportEdge(direction); } setOffset() { this.offset = this.routines.getOffset(); } findItemElementById(id) { return this.routines.findItemElement(id); } getEdgeVisibleItem(items, direction) { const bwd = direction === Direction.backward; const opposite = bwd ? Direction.forward : Direction.backward; const viewportEdge = this.getEdge(direction); let item, diff = 0; for (let i = bwd ? 0 : items.length - 1; bwd ? i <= items.length - 1 : i >= 0; i += bwd ? 1 : -1) { const itemEdge = this.routines.getEdge(items[i].element, opposite); diff = itemEdge - viewportEdge; if ((bwd && diff > 0) || (!bwd && diff < 0)) { item = items[i]; break; } } return { item, index: item ? item.$index : NaN, diff }; } } //# sourceMappingURL=viewport.js.map