Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/lib/grid-list/grid-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export class MatGridList implements OnInit, AfterContentChecked {
/** Number of columns being rendered. */
private _cols: number;

/** Used for determiningthe position of each tile in the grid. */
private _tileCoordinator: TileCoordinator;

/**
* Row height value passed in by user. This can be one of three types:
* - Number value (ex: "100px"): sets a fixed row height to that value
Expand Down Expand Up @@ -135,8 +138,14 @@ export class MatGridList implements OnInit, AfterContentChecked {

/** Computes and applies the size and position for all children grid tiles. */
private _layoutTiles(): void {
const tracker = new TileCoordinator(this.cols, this._tiles);
if (!this._tileCoordinator) {
this._tileCoordinator = new TileCoordinator(this._tiles);
}

const tracker = this._tileCoordinator;
const direction = this._dir ? this._dir.value : 'ltr';

this._tileCoordinator.update(this.cols);
this._tileStyler.init(this.gutterSize, tracker, this.cols, direction);

this._tiles.forEach((tile, index) => {
Expand Down
18 changes: 13 additions & 5 deletions src/lib/grid-list/tile-coordinator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class TileCoordinator {
* Ex: A list with 1 row that contains a tile with rowspan 2 will have a total rowspan of 2.
*/
get rowspan() {
let lastRowMax = Math.max(...this.tracker);
const lastRowMax = Math.max(...this.tracker);
// if any of the tiles has a rowspan that pushes it beyond the total row count,
// add the difference to the rowcount
return lastRowMax > 1 ? this.rowCount + lastRowMax - 1 : this.rowCount;
Expand All @@ -53,17 +53,25 @@ export class TileCoordinator {
/** The computed (row, col) position of each tile (the output). */
positions: TilePosition[];

constructor(numColumns: number, tiles: QueryList<MatGridTile>) {
constructor(private _tiles: QueryList<MatGridTile>) {}

/**
* Updates the tile positions.
* @param numColumns Amount of columns in the grid.
*/
update(numColumns: number) {
this.columnIndex = 0;
this.rowIndex = 0;

this.tracker = new Array(numColumns);
this.tracker.fill(0, 0, this.tracker.length);

this.positions = tiles.map(tile => this._trackTile(tile));
this.positions = this._tiles.map(tile => this._trackTile(tile));
}

/** Calculates the row and col position of a tile. */
private _trackTile(tile: MatGridTile): TilePosition {
// Find a gap large enough for this tile.
let gapStartIndex = this._findMatchingGap(tile.colspan);
const gapStartIndex = this._findMatchingGap(tile.colspan);

// Place tile in the resulting gap.
this._markTilePosition(gapStartIndex, tile);
Expand Down