@@ -2,27 +2,41 @@ import type { dxElementWrapper } from '@js/core/renderer';
22import $ from '@js/core/renderer' ;
33import { getInnerHeight , getOuterHeight } from '@js/core/utils/size' ;
44import { isNumeric } from '@js/core/utils/type' ;
5-
6- import windowUtils from '../../core/utils/m_window' ;
5+ import { getWindow } from '@ts/core/utils/m_window' ;
76
87const WINDOW_HEIGHT_PERCENT = 0.9 ;
98
10- export const getElementMaxHeightByWindow = ( $element : dxElementWrapper , startLocation ?: number ) => {
11- const $window = $ ( windowUtils . getWindow ( ) ) ;
12- // @ts -expect-error
13- const { top : elementOffset } = $element . offset ( ) ;
14- let actualOffset ;
9+ export const getElementMaxHeightByWindow = (
10+ $element : dxElementWrapper ,
11+ startLocation ?: number ,
12+ ) : number | undefined => {
13+ const offset = $element . offset ( ) ;
14+
15+ // offset can be undefined if the element is not inserted into the DOM
16+ // or the element does not exist
17+ if ( offset === undefined ) {
18+ return undefined ;
19+ }
20+
21+ const $window = $ ( getWindow ( ) ) ;
22+ const { top : elementOffset } = offset ;
23+
24+ let actualOffset = 0 ;
25+
26+ // @ts -expect-error scrollTop should be typed correctly with return type
27+ const windowScrollTop : number = $window . scrollTop ( ) ;
28+ const windowHeight = getInnerHeight ( $window ) ;
1529
1630 if ( isNumeric ( startLocation ) ) {
1731 if ( startLocation < elementOffset ) {
1832 return elementOffset - startLocation ;
1933 }
20- // @ts -expect-error
21- actualOffset = getInnerHeight ( $window ) - startLocation + $window . scrollTop ( ) ;
34+
35+ actualOffset = windowHeight - startLocation + windowScrollTop ;
2236 } else {
23- // @ts -expect-error
24- const offsetTop = elementOffset - $window . scrollTop ( ) ;
25- const offsetBottom = getInnerHeight ( $window ) - offsetTop - getOuterHeight ( $element ) ;
37+ const offsetTop = elementOffset - windowScrollTop ;
38+ const offsetBottom = windowHeight - offsetTop - getOuterHeight ( $element ) ;
39+
2640 actualOffset = Math . max ( offsetTop , offsetBottom ) ;
2741 }
2842
0 commit comments