@@ -69,37 +69,35 @@ export class SelectPositioningStrategy extends BaseFitPositionStrategy implement
69
69
this . setStyles ( contentElement , selectFit , initialCall ) ;
70
70
}
71
71
72
- public itemIsInvisible ( selectFit : SelectFit ) {
73
- // selected item is completely invisible
74
- return Math . round ( selectFit . itemRect . top * 100 ) / 100 >=
75
- Math . round ( this . select . scrollContainer . getBoundingClientRect ( ) . bottom * 100 ) / 100 ||
76
- Math . round ( selectFit . itemRect . bottom * 100 ) / 100 <=
77
- Math . round ( this . select . scrollContainer . getBoundingClientRect ( ) . top * 100 ) / 100 ||
78
- // selected item is partially invisible at ddl bottom
79
- Math . round ( selectFit . itemRect . top * 100 ) / 100 <=
80
- ( this . select . scrollContainer . getBoundingClientRect ( ) . bottom * 100 ) / 100 &&
81
- Math . round ( selectFit . itemRect . bottom * 100 ) / 100 >=
82
- ( this . select . scrollContainer . getBoundingClientRect ( ) . bottom * 100 ) / 100 ;
83
- }
84
-
85
72
private manageScrollToItem ( selectFit : SelectFit ) {
86
73
// Scroll and compensate the item's container position, when the selected item is not visible.
87
- if ( this . itemIsInvisible ( selectFit ) ) {
88
- const compensation = this . scrollToItem ( selectFit ) ;
89
- this . compensateYScroll ( selectFit , compensation ) ;
74
+ // selected item is completely invisible
75
+ const itemTop = Math . round ( selectFit . itemRect . top * 100 ) / 100 ;
76
+ const itemBottom = Math . round ( selectFit . itemRect . bottom * 100 ) / 100 ;
77
+ const scrollContainerRect = this . select . scrollContainer . getBoundingClientRect ( ) ;
78
+ const scrollContainerBottom = Math . round ( scrollContainerRect . bottom * 100 ) / 100 ;
79
+ const scrollContainerTop = Math . round ( scrollContainerRect . top * 100 ) / 100 ;
80
+ const itemIsNotCompletelyVisible =
81
+ itemTop >= scrollContainerBottom || itemBottom <= scrollContainerTop ||
82
+ // selected item is partially invisible at ddl bottom
83
+ itemTop <= scrollContainerBottom && itemBottom >= scrollContainerBottom ;
84
+ if ( itemIsNotCompletelyVisible ) {
85
+ const scrollAmount = selectFit . itemElement ? Math . floor ( itemBottom - scrollContainerBottom ) : 0 ;
86
+ this . scrollToItem ( scrollAmount ) ;
87
+ selectFit . verticalOffset += scrollAmount ;
88
+ this . global_yOffset = selectFit . verticalOffset ;
90
89
}
91
90
}
92
91
93
- private scrollToItem ( selectFit : SelectFit ) : number {
94
- const itemPosition = this . calculateScrollPosition ( selectFit ) ;
92
+ private scrollToItem ( scrollAmount : number ) : number {
95
93
if ( isIE ( ) ) {
96
94
setTimeout ( ( ) => {
97
- this . select . scrollContainer . scrollTop = ( itemPosition ) ;
95
+ this . select . scrollContainer . scrollTop = ( scrollAmount ) ;
98
96
} , 1 ) ;
99
97
} else {
100
- this . select . scrollContainer . scrollTop = ( itemPosition ) ;
98
+ this . select . scrollContainer . scrollTop = ( scrollAmount ) ;
101
99
}
102
- return itemPosition ;
100
+ return scrollAmount ;
103
101
}
104
102
105
103
private calculateScrollPosition ( selectFit : SelectFit ) : number {
@@ -113,16 +111,15 @@ export class SelectPositioningStrategy extends BaseFitPositionStrategy implement
113
111
return Math . floor ( scrollPosition ) ;
114
112
}
115
113
116
- // This method can be scrambled and combined in manageScrollToItem()
117
- private compensateYScroll ( selectFit : SelectFit , compensation : number ) {
118
- selectFit . verticalOffset += compensation ;
119
- this . global_yOffset = selectFit . verticalOffset ;
120
- }
121
-
122
114
// Position the items outer container Below or Above the input.
123
115
fitInViewport ( contentElement : HTMLElement , selectFit : SelectFit ) {
124
116
// Position Select component's container below target/input as preferred positioning over above target/input
125
- if ( this . canFitBelowInput ( selectFit ) || ! this . canFitAboveInput ( selectFit ) ) {
117
+ const canFitBelowInput = selectFit . targetRect . top - selectFit . styles . itemTextToInputTextDiff + selectFit . contentElementRect . height <
118
+ selectFit . viewPortRect . bottom ;
119
+ const canFitAboveInput = selectFit . targetRect . bottom + selectFit . styles . itemTextToInputTextDiff -
120
+ selectFit . contentElementRect . height > selectFit . viewPortRect . top ;
121
+ // Position Select component's container below target/input as preferred positioning over above target/input
122
+ if ( canFitBelowInput || ! canFitAboveInput ) {
126
123
// Calculate container starting point;
127
124
// TODO: modify the yOffset instead & use one call to super.setStyle
128
125
selectFit . top = selectFit . targetRect . top - selectFit . styles . itemTextToInputTextDiff ;
@@ -135,16 +132,6 @@ export class SelectPositioningStrategy extends BaseFitPositionStrategy implement
135
132
}
136
133
}
137
134
138
- protected canFitBelowInput ( selectFit : SelectFit ) : boolean {
139
- return selectFit . targetRect . top - selectFit . styles . itemTextToInputTextDiff + selectFit . contentElementRect . height <
140
- selectFit . viewPortRect . bottom ;
141
- }
142
-
143
- protected canFitAboveInput ( selectFit : SelectFit ) : boolean {
144
- return selectFit . targetRect . bottom + selectFit . styles . itemTextToInputTextDiff -
145
- selectFit . contentElementRect . height > selectFit . viewPortRect . top ;
146
- }
147
-
148
135
protected setStyles ( contentElement : HTMLElement , selectFit : SelectFit , initialCall ?: boolean ) {
149
136
// The Select component's container is about to be displayed. Set its position.
150
137
if ( initialCall ) {
0 commit comments