1
- // tslint:disable: max-line-length
2
-
3
1
import { VerticalAlignment , HorizontalAlignment , PositionSettings , Size , Point , Util } from '../services/overlay/utilities' ;
4
2
import { IPositionStrategy } from '../services/overlay/position' ;
5
3
import { fadeOut , fadeIn } from '../animations/main' ;
@@ -27,45 +25,32 @@ export class SelectPositioningStrategy extends BaseFitPositionStrategy implement
27
25
this . settings = Object . assign ( { } , this . _selectDefaultSettings , settings ) ;
28
26
}
29
27
30
- // TODO: Get this dynamically from styles?
31
- private get getItemPaddingBasedOnDisplayDensity ( ) : number {
32
- switch ( this . select . displayDensity ) {
33
- case DisplayDensity . compact :
34
- return 16 ;
35
- case DisplayDensity . cosy :
36
- return 20 ;
37
- case DisplayDensity . comfortable :
38
- return 24 ;
39
- default : return 24 ;
40
- }
41
- }
42
- // private viewPort = Util.getViewportRect(document); // in SelectFit
43
- // private ddl = this.select.getListItemsContainer(); // in SelectFit
44
- private itemElement = this . getInteractionItemElement ( ) ;
45
- private itemBoundRect = this . itemElement . getBoundingClientRect ( ) as DOMRect ;
46
- private itemHeight = this . itemBoundRect . height ;
28
+ // private itemElement = this.getInteractionItemElement();
29
+ // private itemBoundRect = this.itemElement.getBoundingClientRect() as DOMRect;
47
30
48
- private nyOffset = 0 ;
49
- private nxOffset = 0 ;
50
- private nstyles : SelectStyles = { } ;
51
- private nInitialCall : boolean ;
31
+
32
+ // Global variables required for cases of !initialCall (page scroll/overlay repositionAll)
33
+ private global_yOffset = 0 ;
34
+ private global_xOffset = 0 ;
35
+ private global_styles : SelectStyles = { } ;
52
36
53
37
position ( contentElement : HTMLElement , size : Size , document ?: Document , initialCall ?: boolean ) : void {
54
- // 1st: Check if the interaction item is visible. If NO it should be scrolled in view. Adjust the new container position with the scrolled amount and store it.
38
+ // 1st: Check if the interaction item is visible. If NO it should be scrolled in view.
39
+ // Adjust the new container position with the scrolled amount.
55
40
// 2nd: Try position the container after the item is scrolled.
56
41
// 3rd: If unsuccessful, position the overlay container on TOP/ABOVE or BOTTOM/BELLOW of the input. BOTTOM/BELLOW is preferred.
42
+ // 4th: On page scroll, persist the ddl container position relative to its target element.
57
43
58
- // is it OK to use a method for this and keeps all stuff in Obj, instead of global vars initialized on the go when needed?
59
44
const rects = super . calculateElementRectangles ( contentElement ) ;
60
-
45
+ // selectFit obj, to be used for both cases of initialCall and !initialCall(page scroll/overlay repositionAll)
61
46
const selectFit : SelectFit = {
62
- yOffset : this . nyOffset , xOffset : this . nxOffset , targetRect : rects . targetRect ,
63
- contentElementRect : rects . elementRect , styles : this . nstyles
47
+ yOffset : this . global_yOffset , xOffset : this . global_xOffset ,
48
+ targetRect : rects . targetRect ,
49
+ contentElementRect : rects . elementRect ,
50
+ styles : this . global_styles
64
51
} ;
65
52
66
- this . nInitialCall = false ;
67
53
if ( initialCall ) {
68
- this . nInitialCall = true ;
69
54
selectFit . viewPortRect = Util . getViewportRect ( document ) ;
70
55
71
56
// Fill in the required selectFit object properties.
@@ -81,13 +66,12 @@ export class SelectPositioningStrategy extends BaseFitPositionStrategy implement
81
66
82
67
super . updateViewPortFit ( selectFit ) ;
83
68
if ( ! selectFit . fitVertical ) {
84
- this . fitInViewport ( contentElement , selectFit ) ; // TODO: Cleanup Not really using contentElement
69
+ this . fitInViewport ( contentElement , selectFit ) ;
85
70
}
86
71
}
87
- this . setStyles ( contentElement , selectFit ) ;
72
+ this . setStyles ( contentElement , selectFit , initialCall ) ;
88
73
}
89
74
90
- // TODO: can be a getter but needs access to an exposed selectFit object //can't be passed as a param
91
75
public itemIsInvisible ( selectFit : SelectFit ) {
92
76
return selectFit . itemElement . getBoundingClientRect ( ) . top >= selectFit . dropDownList . getBoundingClientRect ( ) . bottom ||
93
77
selectFit . itemElement . getBoundingClientRect ( ) . bottom <= selectFit . dropDownList . getBoundingClientRect ( ) . top ;
@@ -127,7 +111,7 @@ export class SelectPositioningStrategy extends BaseFitPositionStrategy implement
127
111
// This method can be scrambled and combined in manageScrollToItem()
128
112
private compensateYScroll ( selectFit : SelectFit , compensation : number ) {
129
113
selectFit . yOffset += compensation ;
130
- this . nyOffset = selectFit . yOffset ;
114
+ this . global_yOffset = selectFit . yOffset ;
131
115
}
132
116
133
117
// Position the items outer container Below or Above the input.
@@ -136,36 +120,39 @@ export class SelectPositioningStrategy extends BaseFitPositionStrategy implement
136
120
if ( this . canFitBelowInput ( selectFit ) && this . canFitAboveInput ( selectFit ) ||
137
121
! this . canFitAboveInput ( selectFit ) ) {
138
122
// Calculate container starting point;
139
- selectFit . top = selectFit . targetRect . top - selectFit . styles . itemTextToInputTextDiff ; // TODO: modify the yOffset instead & use one call to super.setStyle
123
+ // TODO: modify the yOffset instead & use one call to super.setStyle
124
+ selectFit . top = selectFit . targetRect . top - selectFit . styles . itemTextToInputTextDiff ;
140
125
141
- } else { // Position Select component's container above target/input
126
+ } else {
127
+ // Position Select component's container above target/input
128
+ // TODO: modify the yOffset instead & use one call to super.setStyle
142
129
selectFit . top = selectFit . targetRect . bottom + selectFit . styles . itemTextToInputTextDiff -
143
- selectFit . contentElementRect . height ; // TODO: modify the yOffset instead & use one call to super.setStyle
144
- }
130
+ selectFit . contentElementRect . height ;
131
+ }
145
132
}
146
133
147
134
protected canFitBelowInput ( selectFit : SelectFit ) : boolean {
148
- selectFit . top = selectFit . targetRect . top - selectFit . styles . itemTextToInputTextDiff ;
149
- return selectFit . targetRect . top - selectFit . styles . itemTextToInputTextDiff + selectFit . contentElementRect . height < selectFit . viewPortRect . bottom ;
135
+ return selectFit . targetRect . top - selectFit . styles . itemTextToInputTextDiff + selectFit . contentElementRect . height <
136
+ selectFit . viewPortRect . bottom ;
150
137
}
151
138
152
139
protected canFitAboveInput ( selectFit : SelectFit ) : boolean {
153
140
return selectFit . targetRect . bottom + selectFit . styles . itemTextToInputTextDiff -
154
141
selectFit . contentElementRect . height > selectFit . viewPortRect . top ;
155
142
}
156
143
157
- protected setStyles ( contentElement : HTMLElement , selectFit : SelectFit ) {
144
+ protected setStyles ( contentElement : HTMLElement , selectFit : SelectFit , initialCall ?: boolean ) {
158
145
// The Select component's container is about to be displayed. Set its position.
159
- if ( this . nInitialCall ) {
146
+ if ( initialCall ) {
160
147
contentElement . style . top = `${ selectFit . top } px` ;
161
148
contentElement . style . left = `${ selectFit . left } px` ;
162
149
// Page's View Window container is scrolled. Reposition Select's container.
163
- } else if ( this . nInitialCall === false ) {
164
- super . setStyle ( contentElement , selectFit . targetRect , selectFit . contentElementRect , selectFit ) ; // TODO Cleanup? passing too much stuff;
150
+ } else {
151
+ super . setStyle ( contentElement , selectFit . targetRect , selectFit . contentElementRect , selectFit ) ;
165
152
}
166
153
167
- this . nstyles . contentElementNewWidth = selectFit . styles . contentElementNewWidth ;
168
154
contentElement . style . width = `${ selectFit . styles . contentElementNewWidth } px` ; // manage container based on paddings?
155
+ this . global_styles . contentElementNewWidth = selectFit . styles . contentElementNewWidth ;
169
156
}
170
157
171
158
private calculateVariables ( contentElement : HTMLElement , selectFit : SelectFit ) {
@@ -178,24 +165,11 @@ export class SelectPositioningStrategy extends BaseFitPositionStrategy implement
178
165
selectFit . dropDownList = this . select . getListItemsContainer ( ) ;
179
166
selectFit . targetRect = targetRect ;
180
167
selectFit . contentElementRect = contentElementRect ;
181
- // selectFit.inputElement = this._selectDefaultSettings.target; // TODO Get as Point target is used update to getEditElement();
182
168
selectFit . inputElement = this . select . getEditElement ( ) ;
183
169
// Calculate input and selected item elements style related variables
184
170
this . calculateStyles ( selectFit ) ;
185
-
186
171
}
187
172
188
- // private getItemPaddingBasedOnDisplayDensity(): number {
189
- // switch (this.select.displayDensity) {
190
- // case DisplayDensity.compact:
191
- // return 16;
192
- // case DisplayDensity.cosy:
193
- // return 20;
194
- // case DisplayDensity.comfortable:
195
- // return 24;
196
- // default: return 24;
197
- // }
198
- // }
199
173
public calculateStyles ( selectFit : SelectFit ) {
200
174
const inputFontSize = window . getComputedStyle ( selectFit . inputElement ) . fontSize ;
201
175
const numericInputFontSize = parseFloat ( inputFontSize ) ;
@@ -210,15 +184,15 @@ export class SelectPositioningStrategy extends BaseFitPositionStrategy implement
210
184
) / 2 ;
211
185
selectFit . styles . itemTextToInputTextDiff = Math . ceil ( itemTextToItemTop - inputTextToInputTop + negateInputPaddings ) ;
212
186
213
- const selectItemPaddingHorizontal = this . getItemPaddingBasedOnDisplayDensity ; //24; //maybe get this from styles directly instead of hardoding it //16px/20px/24px based on DisplayDensty // TODO: Get it dinamicaly from styles
214
187
const itemLeftPadding = window . getComputedStyle ( selectFit . itemElement ) . paddingLeft ;
215
188
const itemTextIndent = window . getComputedStyle ( selectFit . itemElement ) . textIndent ;
216
189
const numericLeftPadding = parseFloat ( itemLeftPadding ) ;
217
190
const numericTextIndent = parseFloat ( itemTextIndent ) ;
218
191
219
192
selectFit . styles . itemTextPadding = numericLeftPadding ;
220
193
selectFit . styles . itemTextIndent = numericTextIndent ;
221
- selectFit . styles . contentElementNewWidth = selectFit . targetRect . width + 24 + selectItemPaddingHorizontal * 2 ; // 24 is the input dd icon width
194
+ // 24 is the input's toggle ddl icon width
195
+ selectFit . styles . contentElementNewWidth = selectFit . targetRect . width + 24 + numericLeftPadding * 2 ;
222
196
}
223
197
224
198
private getInteractionItemElement ( ) : HTMLElement {
@@ -237,13 +211,14 @@ export class SelectPositioningStrategy extends BaseFitPositionStrategy implement
237
211
238
212
protected calculateYoffset ( selectFit : SelectFit ) {
239
213
const contentElementTopLeftPointY = selectFit . contentElementRect . top ;
240
- selectFit . yOffset = - ( selectFit . itemElement . getBoundingClientRect ( ) . top - contentElementTopLeftPointY + selectFit . styles . itemTextToInputTextDiff ) ;
241
- this . nyOffset = selectFit . yOffset ;
214
+ selectFit . yOffset =
215
+ - ( selectFit . itemElement . getBoundingClientRect ( ) . top - contentElementTopLeftPointY + selectFit . styles . itemTextToInputTextDiff ) ;
216
+ this . global_yOffset = selectFit . yOffset ;
242
217
}
243
218
244
219
protected calculateXoffset ( selectFit : SelectFit ) {
245
220
selectFit . xOffset = selectFit . styles . itemTextIndent - selectFit . styles . itemTextPadding ;
246
- this . nxOffset = selectFit . xOffset ;
221
+ this . global_xOffset = selectFit . xOffset ;
247
222
}
248
223
}
249
224
@@ -264,7 +239,7 @@ export interface SelectFit extends ConnectedFit {
264
239
inputElement ?: HTMLElement ;
265
240
dropDownList ?: HTMLElement ;
266
241
itemElement ?: HTMLElement ;
267
- itemHeight ?: number ; // TODO: Can reduce code and use inputElement directly, Yet code is more readable with itemHeight
242
+ itemHeight ?: number ;
268
243
styles ?: SelectStyles ;
269
244
}
270
245
@@ -274,5 +249,6 @@ export interface SelectStyles {
274
249
itemTextToInputTextDiff ?: number ;
275
250
contentElementNewWidth ?: number ;
276
251
displayDensity ?: DisplayDensity | string ;
252
+ numericLeftPadding ?: number ;
277
253
}
278
254
0 commit comments