|
1 | 1 | import { isDxMouseWheelEvent } from '@js/common/core/events/utils/index'; |
| 2 | +import type { dxElementWrapper } from '@js/core/renderer'; |
2 | 3 | import $ from '@js/core/renderer'; |
| 4 | +import type { PointerInteractionEvent } from '@js/events'; |
3 | 5 |
|
4 | | -// @ts-expect-error |
5 | | -const allowScroll = function (container, delta, shiftKey?: boolean) { |
| 6 | +type PointerInteractionEventTarget = Element | null; |
| 7 | + |
| 8 | +interface TextBoxScrollData { |
| 9 | + validate: (e: PointerInteractionEvent & { |
| 10 | + target?: PointerInteractionEventTarget; |
| 11 | + delta?: number; |
| 12 | + _needSkipEvent?: boolean; |
| 13 | + }) => boolean; |
| 14 | +} |
| 15 | + |
| 16 | +export const allowScroll = ( |
| 17 | + container: dxElementWrapper, |
| 18 | + delta: number, |
| 19 | + shiftKey?: boolean, |
| 20 | +): boolean | undefined => { |
6 | 21 | const $container = $(container); |
7 | | - const scrollTopPos = shiftKey ? $container.scrollLeft() : $container.scrollTop(); |
| 22 | + // @ts-expect-error scrollLeft, scrollTop should be correctly typed in renderer.d.ts |
| 23 | + const scrollTopPos = parseFloat(shiftKey ? $container.scrollLeft() : $container.scrollTop()); |
8 | 24 |
|
9 | 25 | const prop = shiftKey ? 'Width' : 'Height'; |
10 | | - // @ts-expect-error |
11 | | - const scrollSize = $container.prop(`scroll${prop}`); |
12 | | - // @ts-expect-error |
13 | | - const clientSize = $container.prop(`client${prop}`); |
14 | | - // @ts-expect-error |
| 26 | + |
| 27 | + // @ts-expect-error prop should be correctly typed in renderer.d.ts |
| 28 | + const scrollSize = parseFloat($container.prop(`scroll${prop}`)); |
| 29 | + // @ts-expect-error prop should be correctly typed in renderer.d.ts |
| 30 | + const clientSize = parseFloat($container.prop(`client${prop}`)); |
| 31 | + |
15 | 32 | // NOTE: round to the nearest integer towards zero |
16 | 33 | const scrollBottomPos = (scrollSize - clientSize - scrollTopPos) | 0; |
17 | | - // @ts-expect-error |
| 34 | + |
18 | 35 | if (scrollTopPos === 0 && scrollBottomPos === 0) { |
19 | 36 | return false; |
20 | 37 | } |
21 | | - // @ts-expect-error |
| 38 | + |
22 | 39 | const isScrollFromTop = scrollTopPos === 0 && delta >= 0; |
23 | 40 | const isScrollFromBottom = scrollBottomPos === 0 && delta <= 0; |
24 | | - // @ts-expect-error |
25 | 41 | const isScrollFromMiddle = scrollTopPos > 0 && scrollBottomPos > 0; |
26 | 42 |
|
27 | 43 | if (isScrollFromTop || isScrollFromBottom || isScrollFromMiddle) { |
28 | 44 | return true; |
29 | 45 | } |
| 46 | + |
| 47 | + return undefined; |
30 | 48 | }; |
31 | 49 |
|
32 | | -const prepareScrollData = function (container, validateTarget?: any) { |
| 50 | +export const prepareScrollData = ( |
| 51 | + container: dxElementWrapper, |
| 52 | + validateTarget?: boolean, |
| 53 | +): TextBoxScrollData => { |
33 | 54 | const $container = $(container); |
34 | | - const isCorrectTarget = function (eventTarget) { |
35 | | - return validateTarget ? $(eventTarget).is(container) : true; |
36 | | - }; |
37 | 55 |
|
38 | | - return { |
39 | | - // @ts-expect-error |
40 | | - validate(e) { |
| 56 | + const isCorrectTarget = ( |
| 57 | + eventTarget: PointerInteractionEventTarget, |
| 58 | + ): boolean => (validateTarget ? $(eventTarget).is(container) : true); |
| 59 | + |
| 60 | + const scrollData: TextBoxScrollData = { |
| 61 | + validate: (e) => { |
41 | 62 | if (isDxMouseWheelEvent(e) && isCorrectTarget(e.target)) { |
42 | | - if (allowScroll($container, -e.delta, e.shiftKey)) { |
| 63 | + if (allowScroll($container, -(e.delta ?? 0), e.shiftKey)) { |
43 | 64 | e._needSkipEvent = true; |
| 65 | + |
44 | 66 | return true; |
45 | 67 | } |
| 68 | + |
46 | 69 | return false; |
47 | 70 | } |
| 71 | + |
| 72 | + return false; |
48 | 73 | }, |
49 | 74 | }; |
50 | | -}; |
51 | 75 |
|
52 | | -export { |
53 | | - allowScroll, |
54 | | - prepareScrollData, |
| 76 | + return scrollData; |
55 | 77 | }; |
0 commit comments