@@ -60,7 +60,6 @@ import {
60
60
} from './select-errors' ;
61
61
import { CanDisable , mixinDisabled } from '../core/common-behaviors/disabled' ;
62
62
import { MdOptgroup , MdOption , MdOptionSelectionChange } from '../core/option/index' ;
63
- import { Platform } from '@angular/cdk/platform' ;
64
63
import { MdFormFieldControl } from '../form-field/index' ;
65
64
import { Subject } from 'rxjs/Subject' ;
66
65
@@ -100,13 +99,6 @@ export const SELECT_MULTIPLE_PANEL_PADDING_X = SELECT_PANEL_PADDING_X * 1.5 + 20
100
99
*/
101
100
export const SELECT_PANEL_VIEWPORT_PADDING = 8 ;
102
101
103
- /**
104
- * Default minimum width of the trigger based on the CSS.
105
- * Used as a fallback for server-side rendering.
106
- * @docs -private
107
- */
108
- const SELECT_TRIGGER_MIN_WIDTH = 112 ;
109
-
110
102
/** Injection token that determines the scroll handling while a select is open. */
111
103
export const MD_SELECT_SCROLL_STRATEGY =
112
104
new InjectionToken < ( ) => ScrollStrategy > ( 'md-select-scroll-strategy' ) ;
@@ -216,8 +208,8 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
216
208
/** Tab index for the element. */
217
209
private _tabIndex : number ;
218
210
219
- /** The cached height of the trigger element . */
220
- private _triggerHeight : number ;
211
+ /** The last measured value for the trigger's client bounding rect . */
212
+ _triggerRect : ClientRect ;
221
213
222
214
/** The aria-describedby attribute on the select for improved a11y. */
223
215
_ariaDescribedby : string ;
@@ -228,12 +220,6 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
228
220
/** Deals with the selection logic. */
229
221
_selectionModel : SelectionModel < MdOption > ;
230
222
231
- /**
232
- * The width of the trigger. Must be saved to set the min width of the overlay panel
233
- * and the width of the selected value.
234
- */
235
- _triggerWidth : number ;
236
-
237
223
/** Manages keyboard events for options in the panel. */
238
224
_keyManager : FocusKeyManager < MdOption > ;
239
225
@@ -298,9 +284,6 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
298
284
/** Trigger that opens the select. */
299
285
@ViewChild ( 'trigger' ) trigger : ElementRef ;
300
286
301
- /** Element used to measure the font-size of the trigger element. */
302
- @ViewChild ( 'measureFontSize' ) _measureFontSizeEl : ElementRef ;
303
-
304
287
/** Overlay pane containing the options. */
305
288
@ViewChild ( ConnectedOverlayDirective ) overlayDir : ConnectedOverlayDirective ;
306
289
@@ -322,9 +305,6 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
322
305
set placeholder ( value : string ) {
323
306
this . _placeholder = value ;
324
307
this . stateChanges . next ( ) ;
325
-
326
- // Must wait to record the trigger width to ensure placeholder width is included.
327
- Promise . resolve ( null ) . then ( ( ) => this . _setTriggerWidth ( ) ) ;
328
308
}
329
309
330
310
/** Whether the component is required. */
@@ -430,7 +410,6 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
430
410
constructor (
431
411
private _viewportRuler : ViewportRuler ,
432
412
private _changeDetectorRef : ChangeDetectorRef ,
433
- private _platform : Platform ,
434
413
private _ngZone : NgZone ,
435
414
renderer : Renderer2 ,
436
415
elementRef : ElementRef ,
@@ -490,9 +469,10 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
490
469
return ;
491
470
}
492
471
493
- if ( ! this . _triggerWidth ) {
494
- this . _setTriggerWidth ( ) ;
495
- }
472
+ this . _triggerRect = this . trigger . nativeElement . getBoundingClientRect ( ) ;
473
+ // Note: The computed font-size will be a string pixel value (e.g. "16px").
474
+ // `parseInt` ignores the trailing 'px' and converts this to a number.
475
+ this . _triggerFontSize = parseInt ( getComputedStyle ( this . trigger . nativeElement ) [ 'font-size' ] ) ;
496
476
497
477
this . _calculateOverlayPosition ( ) ;
498
478
this . _panelOpen = true ;
@@ -593,17 +573,6 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
593
573
return this . _dir ? this . _dir . value === 'rtl' : false ;
594
574
}
595
575
596
- /**
597
- * Sets the width of the trigger element. This is necessary to match
598
- * the overlay width to the trigger width.
599
- */
600
- private _setTriggerWidth ( ) : void {
601
- this . _triggerWidth = this . _platform . isBrowser ? this . _getTriggerRect ( ) . width :
602
- SELECT_TRIGGER_MIN_WIDTH ;
603
-
604
- this . _changeDetectorRef . markForCheck ( ) ;
605
- }
606
-
607
576
/** Handles the keyboard interactions of a closed select. */
608
577
_handleClosedKeydown ( event : KeyboardEvent ) : void {
609
578
if ( ! this . disabled ) {
@@ -785,10 +754,6 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
785
754
this . stateChanges . next ( ) ;
786
755
}
787
756
788
- private _getTriggerRect ( ) : ClientRect {
789
- return this . trigger . nativeElement . getBoundingClientRect ( ) ;
790
- }
791
-
792
757
/** Sets up a key manager to listen to keyboard events on the overlay panel. */
793
758
private _initKeyManager ( ) {
794
759
this . _keyManager = new FocusKeyManager < MdOption > ( this . options ) . withTypeAhead ( ) ;
@@ -934,9 +899,6 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
934
899
935
900
/** Calculates the scroll position and x- and y-offsets of the overlay panel. */
936
901
private _calculateOverlayPosition ( ) : void {
937
- this . _triggerHeight = this . trigger . nativeElement . getBoundingClientRect ( ) . height ;
938
- this . _triggerFontSize = this . _measureFontSizeEl . nativeElement . getBoundingClientRect ( ) . height ;
939
-
940
902
const itemHeight = this . _triggerFontSize * SELECT_ITEM_HEIGHT_EM ;
941
903
942
904
const items = this . _getItemCount ( ) ;
@@ -1043,7 +1005,7 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
1043
1005
private _calculateOverlayOffsetY ( selectedIndex : number , scrollBuffer : number ,
1044
1006
maxScroll : number ) : number {
1045
1007
const itemHeight = this . _triggerFontSize * SELECT_ITEM_HEIGHT_EM ;
1046
- const optionHeightAdjustment = ( itemHeight - this . _triggerHeight ) / 2 ;
1008
+ const optionHeightAdjustment = ( itemHeight - this . _triggerRect . height ) / 2 ;
1047
1009
const maxOptionsDisplayed = Math . floor ( SELECT_PANEL_MAX_HEIGHT / itemHeight ) ;
1048
1010
let optionOffsetFromPanelTop : number ;
1049
1011
@@ -1081,20 +1043,21 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
1081
1043
* If it will not fit, tries to adjust the scroll position and the associated
1082
1044
* y-offset so the panel can open fully on-screen. If it still won't fit,
1083
1045
* sets the offset back to 0 to allow the fallback position to take over.
1046
+ *
1047
+ * Note: This method
1084
1048
*/
1085
1049
private _checkOverlayWithinViewport ( maxScroll : number ) : void {
1086
1050
const itemHeight = this . _triggerFontSize * SELECT_ITEM_HEIGHT_EM ;
1087
1051
const viewportRect = this . _viewportRuler . getViewportRect ( ) ;
1088
- const triggerRect = this . _getTriggerRect ( ) ;
1089
1052
1090
- const topSpaceAvailable = triggerRect . top - SELECT_PANEL_VIEWPORT_PADDING ;
1053
+ const topSpaceAvailable = this . _triggerRect . top - SELECT_PANEL_VIEWPORT_PADDING ;
1091
1054
const bottomSpaceAvailable =
1092
- viewportRect . height - triggerRect . bottom - SELECT_PANEL_VIEWPORT_PADDING ;
1055
+ viewportRect . height - this . _triggerRect . bottom - SELECT_PANEL_VIEWPORT_PADDING ;
1093
1056
1094
1057
const panelHeightTop = Math . abs ( this . _offsetY ) ;
1095
1058
const totalPanelHeight =
1096
1059
Math . min ( this . _getItemCount ( ) * itemHeight , SELECT_PANEL_MAX_HEIGHT ) ;
1097
- const panelHeightBottom = totalPanelHeight - panelHeightTop - triggerRect . height ;
1060
+ const panelHeightBottom = totalPanelHeight - panelHeightTop - this . _triggerRect . height ;
1098
1061
1099
1062
if ( panelHeightBottom > bottomSpaceAvailable ) {
1100
1063
this . _adjustPanelUp ( panelHeightBottom , bottomSpaceAvailable ) ;
@@ -1152,7 +1115,7 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
1152
1115
/** Sets the transform origin point based on the selected option. */
1153
1116
private _getOriginBasedOnOption ( ) : string {
1154
1117
const itemHeight = this . _triggerFontSize * SELECT_ITEM_HEIGHT_EM ;
1155
- const optionHeightAdjustment = ( itemHeight - this . _triggerHeight ) / 2 ;
1118
+ const optionHeightAdjustment = ( itemHeight - this . _triggerRect . height ) / 2 ;
1156
1119
const originY = Math . abs ( this . _offsetY ) - optionHeightAdjustment + itemHeight / 2 ;
1157
1120
return `50% ${ originY } px 0px` ;
1158
1121
}
0 commit comments