1- // tslint:disable: max-line-length
2-
31import { VerticalAlignment , HorizontalAlignment , PositionSettings , Size , Point , Util } from '../services/overlay/utilities' ;
42import { IPositionStrategy } from '../services/overlay/position' ;
53import { fadeOut , fadeIn } from '../animations/main' ;
@@ -27,45 +25,32 @@ export class SelectPositioningStrategy extends BaseFitPositionStrategy implement
2725 this . settings = Object . assign ( { } , this . _selectDefaultSettings , settings ) ;
2826 }
2927
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;
4730
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 = { } ;
5236
5337 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.
5540 // 2nd: Try position the container after the item is scrolled.
5641 // 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.
5743
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?
5944 const rects = super . calculateElementRectangles ( contentElement ) ;
60-
45+ // selectFit obj, to be used for both cases of initialCall and !initialCall(page scroll/overlay repositionAll)
6146 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
6451 } ;
6552
66- this . nInitialCall = false ;
6753 if ( initialCall ) {
68- this . nInitialCall = true ;
6954 selectFit . viewPortRect = Util . getViewportRect ( document ) ;
7055
7156 // Fill in the required selectFit object properties.
@@ -81,13 +66,12 @@ export class SelectPositioningStrategy extends BaseFitPositionStrategy implement
8166
8267 super . updateViewPortFit ( selectFit ) ;
8368 if ( ! selectFit . fitVertical ) {
84- this . fitInViewport ( contentElement , selectFit ) ; // TODO: Cleanup Not really using contentElement
69+ this . fitInViewport ( contentElement , selectFit ) ;
8570 }
8671 }
87- this . setStyles ( contentElement , selectFit ) ;
72+ this . setStyles ( contentElement , selectFit , initialCall ) ;
8873 }
8974
90- // TODO: can be a getter but needs access to an exposed selectFit object //can't be passed as a param
9175 public itemIsInvisible ( selectFit : SelectFit ) {
9276 return selectFit . itemElement . getBoundingClientRect ( ) . top >= selectFit . dropDownList . getBoundingClientRect ( ) . bottom ||
9377 selectFit . itemElement . getBoundingClientRect ( ) . bottom <= selectFit . dropDownList . getBoundingClientRect ( ) . top ;
@@ -127,7 +111,7 @@ export class SelectPositioningStrategy extends BaseFitPositionStrategy implement
127111 // This method can be scrambled and combined in manageScrollToItem()
128112 private compensateYScroll ( selectFit : SelectFit , compensation : number ) {
129113 selectFit . yOffset += compensation ;
130- this . nyOffset = selectFit . yOffset ;
114+ this . global_yOffset = selectFit . yOffset ;
131115 }
132116
133117 // Position the items outer container Below or Above the input.
@@ -136,36 +120,39 @@ export class SelectPositioningStrategy extends BaseFitPositionStrategy implement
136120 if ( this . canFitBelowInput ( selectFit ) && this . canFitAboveInput ( selectFit ) ||
137121 ! this . canFitAboveInput ( selectFit ) ) {
138122 // 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 ;
140125
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
142129 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+ }
145132 }
146133
147134 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 ;
150137 }
151138
152139 protected canFitAboveInput ( selectFit : SelectFit ) : boolean {
153140 return selectFit . targetRect . bottom + selectFit . styles . itemTextToInputTextDiff -
154141 selectFit . contentElementRect . height > selectFit . viewPortRect . top ;
155142 }
156143
157- protected setStyles ( contentElement : HTMLElement , selectFit : SelectFit ) {
144+ protected setStyles ( contentElement : HTMLElement , selectFit : SelectFit , initialCall ?: boolean ) {
158145 // The Select component's container is about to be displayed. Set its position.
159- if ( this . nInitialCall ) {
146+ if ( initialCall ) {
160147 contentElement . style . top = `${ selectFit . top } px` ;
161148 contentElement . style . left = `${ selectFit . left } px` ;
162149 // 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 ) ;
165152 }
166153
167- this . nstyles . contentElementNewWidth = selectFit . styles . contentElementNewWidth ;
168154 contentElement . style . width = `${ selectFit . styles . contentElementNewWidth } px` ; // manage container based on paddings?
155+ this . global_styles . contentElementNewWidth = selectFit . styles . contentElementNewWidth ;
169156 }
170157
171158 private calculateVariables ( contentElement : HTMLElement , selectFit : SelectFit ) {
@@ -178,24 +165,11 @@ export class SelectPositioningStrategy extends BaseFitPositionStrategy implement
178165 selectFit . dropDownList = this . select . getListItemsContainer ( ) ;
179166 selectFit . targetRect = targetRect ;
180167 selectFit . contentElementRect = contentElementRect ;
181- // selectFit.inputElement = this._selectDefaultSettings.target; // TODO Get as Point target is used update to getEditElement();
182168 selectFit . inputElement = this . select . getEditElement ( ) ;
183169 // Calculate input and selected item elements style related variables
184170 this . calculateStyles ( selectFit ) ;
185-
186171 }
187172
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- // }
199173 public calculateStyles ( selectFit : SelectFit ) {
200174 const inputFontSize = window . getComputedStyle ( selectFit . inputElement ) . fontSize ;
201175 const numericInputFontSize = parseFloat ( inputFontSize ) ;
@@ -210,15 +184,15 @@ export class SelectPositioningStrategy extends BaseFitPositionStrategy implement
210184 ) / 2 ;
211185 selectFit . styles . itemTextToInputTextDiff = Math . ceil ( itemTextToItemTop - inputTextToInputTop + negateInputPaddings ) ;
212186
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
214187 const itemLeftPadding = window . getComputedStyle ( selectFit . itemElement ) . paddingLeft ;
215188 const itemTextIndent = window . getComputedStyle ( selectFit . itemElement ) . textIndent ;
216189 const numericLeftPadding = parseFloat ( itemLeftPadding ) ;
217190 const numericTextIndent = parseFloat ( itemTextIndent ) ;
218191
219192 selectFit . styles . itemTextPadding = numericLeftPadding ;
220193 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 ;
222196 }
223197
224198 private getInteractionItemElement ( ) : HTMLElement {
@@ -237,13 +211,14 @@ export class SelectPositioningStrategy extends BaseFitPositionStrategy implement
237211
238212 protected calculateYoffset ( selectFit : SelectFit ) {
239213 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 ;
242217 }
243218
244219 protected calculateXoffset ( selectFit : SelectFit ) {
245220 selectFit . xOffset = selectFit . styles . itemTextIndent - selectFit . styles . itemTextPadding ;
246- this . nxOffset = selectFit . xOffset ;
221+ this . global_xOffset = selectFit . xOffset ;
247222 }
248223}
249224
@@ -264,7 +239,7 @@ export interface SelectFit extends ConnectedFit {
264239 inputElement ?: HTMLElement ;
265240 dropDownList ?: HTMLElement ;
266241 itemElement ?: HTMLElement ;
267- itemHeight ?: number ; // TODO: Can reduce code and use inputElement directly, Yet code is more readable with itemHeight
242+ itemHeight ?: number ;
268243 styles ?: SelectStyles ;
269244}
270245
@@ -274,5 +249,6 @@ export interface SelectStyles {
274249 itemTextToInputTextDiff ?: number ;
275250 contentElementNewWidth ?: number ;
276251 displayDensity ?: DisplayDensity | string ;
252+ numericLeftPadding ?: number ;
277253}
278254
0 commit comments