UNPKG

@devexperts/dxcharts-lite

Version:
49 lines (48 loc) 2.67 kB
/* * Copyright (C) 2019 - 2026 Devexperts Solutions IE Limited * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. * If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. */ /* * Copyright (C) 2019 - 2024 Devexperts Solutions IE Limited * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. * If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. */ import { animationFrameScheduler, BehaviorSubject, identity } from 'rxjs'; import { distinctUntilChanged, switchMap, throttleTime } from 'rxjs/operators'; import { defaultDragComponentOptions, DragNDropComponent, } from './drag-n-drop.component'; import { ONE_FRAME_MS } from '../../utils/numeric-constants.utils'; export class DragNDropXComponent extends DragNDropComponent { constructor(hitTest, dragCallbacks, canvasInputListener, chartPanComponent, dragComponentOptions = defaultDragComponentOptions, shouldThrottle) { super(hitTest, dragCallbacks, canvasInputListener, chartPanComponent, dragComponentOptions); this.shouldThrottleSubject = new BehaviorSubject(!!shouldThrottle); } /** * When true, X drag ticks are throttled to one animation frame (e.g. percent axis). */ setShouldThrottle(value) { this.shouldThrottleSubject.next(value); } /** * This method is used to activate the component and add the necessary subscriptions to the canvasInputListener. * It calls the super method doActivate() and then adds the following subscriptions: * - observeXDragStart() subscription with hitTest() as parameter and onDragStart() as callback function * - observeXDrag() subscription with onDragTick() as callback function * - observeXDragEnd() subscription with onDragEnd() as callback function * @returns {void} */ doActivate() { const dragThrottleTime = ONE_FRAME_MS; super.doActivate(); this.addRxSubscription(this.canvasInputListener.observeXDragStart(this.hitTest).subscribe(this.onDragStart)); this.addRxSubscription(this.shouldThrottleSubject .pipe(distinctUntilChanged(), switchMap(shouldThrottle => this.canvasInputListener.observeXDrag().pipe(shouldThrottle ? throttleTime(dragThrottleTime, animationFrameScheduler, { trailing: true, leading: true, }) : identity))) .subscribe(this.onDragTick)); this.addRxSubscription(this.canvasInputListener.observeXDragEnd().subscribe(this.onDragEnd)); } }