@@ -16,7 +16,14 @@ import {Math} from 'angular2/src/facade/math';
1616// TODO(jelbourn): Conditional (responsive) column count / row size.
1717// TODO(jelbourn): Re-layout on window resize / media change (debounced).
1818// TODO(jelbourn): gridTileHeader and gridTileFooter.
19- // TODO(jelbourn): rowHeightMode enum (after TS conversion).
19+
20+ /** Row hieght mode options. Use a static class b/c TypeScript enums are strictly number-based. */
21+ class RowHeightMode {
22+ static FIT = 'fit' ;
23+ static FIXED = 'fixed' ;
24+ static RATIO = 'ratio' ;
25+ }
26+
2027
2128@Component ( {
2229 selector : 'md-grid-list' ,
@@ -64,19 +71,19 @@ export class MdGridList {
6471
6572 /** Set internal representation of row height from the user-provided value. */
6673 set rowHeight ( value ) {
67- if ( value === 'fit' ) {
68- this . rowHeightMode = 'fit' ;
74+ if ( value === RowHeightMode . FIT ) {
75+ this . rowHeightMode = RowHeightMode . FIT ;
6976 } else if ( StringWrapper . contains ( value , ':' ) ) {
70- var ratioParts = value . split ( ':' ) ;
77+ let ratioParts = value . split ( ':' ) ;
7178 if ( ratioParts . length !== 2 ) {
7279 throw `md-grid-list: invalid ratio given for row-height: "${ value } "` ;
7380 }
7481
75- this . rowHeightMode = 'ratio' ;
82+ this . rowHeightMode = RowHeightMode . RATIO ;
7683 this . rowHeightRatio =
7784 NumberWrapper . parseFloat ( ratioParts [ 0 ] ) / NumberWrapper . parseFloat ( ratioParts [ 1 ] ) ;
7885 } else {
79- this . rowHeightMode = 'fixed' ;
86+ this . rowHeightMode = RowHeightMode . FIXED ;
8087 this . fixedRowHeight = value ;
8188 }
8289 }
@@ -87,20 +94,13 @@ export class MdGridList {
8794
8895 /** Computes and applies the size and position for all children grid tiles. */
8996 layoutTiles ( ) {
90- var tracker = new TileCoordinator ( this . cols , this . tiles ) ;
97+ let tracker = new TileCoordinator ( this . cols , this . tiles ) ;
9198 this . rows = tracker . rowCount ;
9299
93- for ( var i = 0 ; i < this . tiles . length ; i ++ ) {
94- var pos = tracker . positions [ i ] ;
95- var tile = this . tiles [ i ] ;
96- var style = this . getTileStyle ( tile , pos . row , pos . col ) ;
97-
98- tile . styleWidth = style . width ;
99- tile . styleHeight = style . height ;
100- tile . styleTop = style . top ;
101- tile . styleLeft = style . left ;
102- tile . styleMarginTop = style . marginTop ;
103- tile . stylePaddingTop = style . paddingTop ;
100+ for ( let i = 0 ; i < this . tiles . length ; i ++ ) {
101+ let pos = tracker . positions [ i ] ;
102+ let tile = this . tiles [ i ] ;
103+ tile . style = this . getTileStyle ( tile , pos . row , pos . col ) ;
104104 }
105105 }
106106
@@ -164,55 +164,50 @@ export class MdGridList {
164164 /** Gets the style properties to be applied to a tile for the given row and column index. */
165165 getTileStyle ( tile : MdGridTile , rowIndex : number , colIndex : number ) : TileStyle {
166166 // Percent of the available horizontal space that one column takes up.
167- var percentWidthPerTile = 100 / this . cols ;
167+ let percentWidthPerTile = 100 / this . cols ;
168168
169169 // Fraction of the vertical gutter size that each column takes up.
170170 // For example, if there are 5 columns, each column uses 4/5 = 0.8 times the gutter width.
171- var gutterWidthFractionPerTile = ( this . cols - 1 ) / this . cols ;
171+ let gutterWidthFractionPerTile = ( this . cols - 1 ) / this . cols ;
172172
173173 // Base horizontal size of a column.
174- var baseTileWidth = this . getBaseTileSize ( percentWidthPerTile , gutterWidthFractionPerTile ) ;
174+ let baseTileWidth = this . getBaseTileSize ( percentWidthPerTile , gutterWidthFractionPerTile ) ;
175175
176176 // The width and horizontal position of each tile is always calculated the same way, but the
177177 // height and vertical position depends on the rowMode.
178- var tileStyle = new TileStyle ( ) ;
178+ let tileStyle = new TileStyle ( ) ;
179179 tileStyle . left = this . getTilePosition ( baseTileWidth , colIndex ) ;
180180 tileStyle . width = this . getTileSize ( baseTileWidth , tile . colspan ) ;
181181
182- // TODO: make cases enums when we support enums
183- switch ( this . rowHeightMode ) {
184- case 'fixed' :
185- // In fixed mode, simply use the given row height.
186- tileStyle . top = this . getTilePosition ( this . fixedRowHeight , rowIndex ) ;
187- tileStyle . height = this . getTileSize ( this . fixedRowHeight , tile . rowspan ) ;
188- break ;
189-
190- case 'ratio' :
191- var percentHeightPerTile = percentWidthPerTile / this . rowHeightRatio ;
192- var baseTileHeight = this . getBaseTileSize ( percentHeightPerTile , gutterWidthFractionPerTile ) ;
182+ if ( this . rowHeightMode == RowHeightMode . FIXED ) {
183+ // In fixed mode, simply use the given row height.
184+ tileStyle . top = this . getTilePosition ( this . fixedRowHeight , rowIndex ) ;
185+ tileStyle . height = this . getTileSize ( this . fixedRowHeight , tile . rowspan ) ;
186+ }
193187
194- // Use paddingTop and marginTop to maintain the given aspect ratio, as
195- // a percentage-based value for these properties is applied versus the *width* of the
196- // containing block. See http://www.w3.org/TR/CSS2/box.html#margin-properties
197- tileStyle . marginTop = this . getTilePosition ( baseTileHeight , rowIndex ) ;
198- tileStyle . paddingTop = this . getTileSize ( baseTileHeight , tile . rowspan ) ;
199- break ;
188+ if ( this . rowHeightMode == RowHeightMode . RATIO ) {
189+ let percentHeightPerTile = percentWidthPerTile / this . rowHeightRatio ;
190+ let baseTileHeight = this . getBaseTileSize ( percentHeightPerTile , gutterWidthFractionPerTile ) ;
200191
201- case 'fit' :
202- // Percent of the available vertical space that one row takes up.
203- var percentHeightPerTile = 100 / this . cols ;
192+ // Use paddingTop and marginTop to maintain the given aspect ratio, as
193+ // a percentage-based value for these properties is applied versus the *width* of the
194+ // containing block. See http://www.w3.org/TR/CSS2/box.html#margin-properties
195+ tileStyle . marginTop = this . getTilePosition ( baseTileHeight , rowIndex ) ;
196+ tileStyle . paddingTop = this . getTileSize ( baseTileHeight , tile . rowspan ) ;
197+ }
204198
205- // Fraction of the horizontal gutter size that each column takes up.
206- var gutterHeightFractionPerTile = ( this . rows - 1 ) / this . rows ;
199+ if ( this . rowHeightMode == RowHeightMode . FIT ) {
200+ // Percent of the available vertical space that one row takes up.
201+ let percentHeightPerTile = 100 / this . cols ;
207202
208- // Base vertical size of a column.
209- var baseTileHeight =
210- this . getBaseTileSize ( percentHeightPerTile , gutterHeightFractionPerTile ) ;
203+ // Fraction of the horizontal gutter size that each column takes up.
204+ let gutterHeightFractionPerTile = ( this . rows - 1 ) / this . rows ;
211205
212- tileStyle . top = this . getTilePosition ( baseTileHeight , rowIndex ) ;
213- tileStyle . height = this . getTileSize ( baseTileHeight , tile . rowspan ) ;
206+ // Base vertical size of a column.
207+ let baseTileHeight = this . getBaseTileSize ( percentHeightPerTile , gutterHeightFractionPerTile ) ;
214208
215- break ;
209+ tileStyle . top = this . getTilePosition ( baseTileHeight , rowIndex ) ;
210+ tileStyle . height = this . getTileSize ( baseTileHeight , tile . rowspan ) ;
216211 }
217212
218213 return tileStyle ;
@@ -223,13 +218,13 @@ export class MdGridList {
223218 selector : 'md-grid-tile' ,
224219 properties : [ 'rowspan' , 'colspan' ] ,
225220 host : {
226- '[style.height] ' : 'styleHeight ' ,
227- '[style.width ]' : 'styleWidth ' ,
228- '[style.top ]' : 'styleTop ' ,
229- '[style.left ]' : 'styleLeft ' ,
230- '[style.marginTop ]' : 'styleMarginTop ' ,
231- '[style.paddingTop ]' : 'stylePaddingTop ' ,
232- '[attr.role ]' : '"listitem"'
221+ 'role ' : 'listitem ' ,
222+ '[style.height ]' : 'style.height ' ,
223+ '[style.width ]' : 'style.width ' ,
224+ '[style.top ]' : 'style.top ' ,
225+ '[style.left ]' : 'style.left ' ,
226+ '[style.marginTop ]' : 'style.marginTop ' ,
227+ '[style.paddingTop ]' : 'style.paddingTop' ,
233228 } ,
234229 lifecycle : [ LifecycleEvent . onDestroy , LifecycleEvent . onChange ]
235230} )
@@ -242,13 +237,7 @@ export class MdGridTile {
242237 _rowspan : number ;
243238 _colspan : number ;
244239
245- styleHeight : string ;
246- styleWidth : string ;
247- styleTop : string ;
248- styleLeft : string ;
249- styleMarginTop : string ;
250- stylePaddingTop : string ;
251-
240+ style : TileStyle ;
252241 isRegisteredWithGridList : boolean ;
253242
254243 constructor ( @SkipSelf ( ) @Host ( ) gridList : MdGridList ) {
@@ -257,6 +246,7 @@ export class MdGridTile {
257246 // Tiles default to 1x1, but rowspan and colspan can be changed via binding.
258247 this . rowspan = 1 ;
259248 this . colspan = 1 ;
249+ this . style = new TileStyle ( ) ;
260250 }
261251
262252 set rowspan ( value ) {
@@ -330,7 +320,7 @@ class TileCoordinator {
330320 this . tracker = ListWrapper . createFixedSize ( numColumns ) ;
331321 ListWrapper . fill ( this . tracker , 0 ) ;
332322
333- this . positions = ListWrapper . map ( tiles , tile => this . _trackTile ( tile ) ) ;
323+ this . positions = tiles . map ( tile => this . _trackTile ( tile ) ) ;
334324 }
335325
336326 /** Gets the number of rows occupied by tiles. */
@@ -345,8 +335,8 @@ class TileCoordinator {
345335 }
346336
347337 // Start index is inclusive, end index is exclusive.
348- var gapStartIndex = - 1 ;
349- var gapEndIndex = - 1 ;
338+ let gapStartIndex = - 1 ;
339+ let gapEndIndex = - 1 ;
350340
351341 // Look for a gap large enough to fit the given tile. Empty spaces are marked with a zero.
352342 do {
@@ -389,7 +379,7 @@ class TileCoordinator {
389379 this . rowIndex ++ ;
390380
391381 // Decrement all spaces by one to reflect moving down one row.
392- for ( var i = 0 ; i < this . tracker . length ; i ++ ) {
382+ for ( let i = 0 ; i < this . tracker . length ; i ++ ) {
393383 this . tracker [ i ] = Math . max ( 0 , this . tracker [ i ] - 1 ) ;
394384 }
395385 }
@@ -399,7 +389,7 @@ class TileCoordinator {
399389 * The gap ends when a non-zero value is found.
400390 */
401391 _findGapEndIndex ( gapStartIndex : number ) : number {
402- for ( var i = gapStartIndex + 1 ; i < this . tracker . length ; i ++ ) {
392+ for ( let i = gapStartIndex + 1 ; i < this . tracker . length ; i ++ ) {
403393 if ( this . tracker [ i ] != 0 ) {
404394 return i ;
405395 }
@@ -411,7 +401,7 @@ class TileCoordinator {
411401
412402 /** Update the tile tracker to account for the given tile in the given space. */
413403 _markTilePosition ( start , tile ) {
414- for ( var i = 0 ; i < tile . colspan ; i ++ ) {
404+ for ( let i = 0 ; i < tile . colspan ; i ++ ) {
415405 this . tracker [ start + i ] = tile . rowspan ;
416406 }
417407 }
0 commit comments