UNPKG

@progress/kendo-angular-gantt

Version:
78 lines (77 loc) 3.81 kB
/**----------------------------------------------------------------------------------------- * Copyright © 2025 Progress Software Corporation. All rights reserved. * Licensed under commercial license. See LICENSE.md in the project root for more information *-------------------------------------------------------------------------------------------*/ import { isDocumentAvailable } from '@progress/kendo-angular-common'; import { ScrollAxis, ScrollDirection } from './drag-scroll-settings'; import { isPresent } from '../utils'; /** * @hidden * * Checks if the beginning of the scrollable element is reached (top/left). * Floors the top value. */ const isUpperLimitReached = (element, axis) => Math.floor(element[axis]) <= 0; /** * @hidden * * Checks if the end of the scrollable element is reached (bottom/right). * Ceils the top value. */ const isBottomLimitReached = (element, axis) => { const elementSize = axis === ScrollAxis.Horizontal ? element.scrollWidth - element.clientWidth : element.scrollHeight - element.clientHeight; return Math.ceil(element[axis]) >= elementSize; }; /** * @hidden * * Scrolls the element in the given direction by the provided step in the provided scroll axis. * * If the targeted scroll incrementation doesn't yield any result due to device pixel ratio issues (https://github.com/dimitar-pechev/RenderingIndependentScrollOffsets#readme), * increments the step with 1px and again attempts to change the scrollTop of the element, until the content is actually scrolled. * * Cuts the operation short after 20 unsuccessful attempts to prevent infinite loops in possible corner-case scenarios. */ export const scrollElement = (element, step, direction, scrollAxis) => { if (!(isPresent(element) && isDocumentAvailable())) { return; } const initialScrollPosition = element[scrollAxis]; let currentStep = step; let iterations = 0; while (initialScrollPosition === element[scrollAxis] && !(direction === ScrollDirection.Backwards && isUpperLimitReached(element, scrollAxis)) && !(direction === ScrollDirection.Forward && isBottomLimitReached(element, scrollAxis)) && iterations < 20 // cut the operation short in 20 attempts - in case of a wild corner case ) { element[scrollAxis] += (currentStep * direction); // try with a larger step if the current one doesn't update the scroll position successfully currentStep += 1; iterations += 1; } }; /** * @hidden * * As client coordinates are not restricted to the range 0px - {viewportSize}px, but can have negative starting values or ending values greater than the viewport size, * this function extracts the visible boundaries of the provided element - fall-backing to 0 when the top/left are below 0, * and fall-backing to the actual visible size of the container for bottom/right. */ export const getViewportBoundaries = (element) => { const elementRect = element.getBoundingClientRect(); // if the beginning of the scrollable container is above/before the current viewport, fall-back to 0 const topLimit = Math.max(elementRect.top, 0); const leftLimit = Math.max(elementRect.left, 0); // if the end of the scrollable container is beneath/after the current viewport, fall-back to its client height // add the distance from the start of the viewport to the beginning of the container to ensure scrolling bottom begins when the actual end of the container is reached const bottomLimit = topLimit + Math.min(elementRect.bottom, element.clientHeight); const rightLimit = leftLimit + Math.min(elementRect.right, element.clientWidth); return { top: topLimit, bottom: bottomLimit, left: leftLimit, right: rightLimit }; };