grid-list.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. /**
  2. * @license
  3. * Copyright Google LLC All Rights Reserved.
  4. *
  5. * Use of this source code is governed by an MIT-style license that can be
  6. * found in the LICENSE file at https://angular.io/license
  7. */
  8. import { InjectionToken, Component, ViewEncapsulation, ElementRef, Input, Optional, ContentChildren, Directive, ChangeDetectionStrategy, Inject, NgModule } from '@angular/core';
  9. import { MatLine, setLines, MatLineModule, MatCommonModule } from '@angular/material/core';
  10. import { coerceNumberProperty } from '@angular/cdk/coercion';
  11. import { Directionality } from '@angular/cdk/bidi';
  12. /**
  13. * @fileoverview added by tsickle
  14. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  15. */
  16. /**
  17. * Injection token used to provide a grid list to a tile and to avoid circular imports.
  18. * \@docs-private
  19. * @type {?}
  20. */
  21. const MAT_GRID_LIST = new InjectionToken('MAT_GRID_LIST');
  22. /**
  23. * @fileoverview added by tsickle
  24. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  25. */
  26. class MatGridTile {
  27. /**
  28. * @param {?} _element
  29. * @param {?=} _gridList
  30. */
  31. constructor(_element, _gridList) {
  32. this._element = _element;
  33. this._gridList = _gridList;
  34. this._rowspan = 1;
  35. this._colspan = 1;
  36. }
  37. /**
  38. * Amount of rows that the grid tile takes up.
  39. * @return {?}
  40. */
  41. get rowspan() { return this._rowspan; }
  42. /**
  43. * @param {?} value
  44. * @return {?}
  45. */
  46. set rowspan(value) { this._rowspan = Math.round(coerceNumberProperty(value)); }
  47. /**
  48. * Amount of columns that the grid tile takes up.
  49. * @return {?}
  50. */
  51. get colspan() { return this._colspan; }
  52. /**
  53. * @param {?} value
  54. * @return {?}
  55. */
  56. set colspan(value) { this._colspan = Math.round(coerceNumberProperty(value)); }
  57. /**
  58. * Sets the style of the grid-tile element. Needs to be set manually to avoid
  59. * "Changed after checked" errors that would occur with HostBinding.
  60. * @param {?} property
  61. * @param {?} value
  62. * @return {?}
  63. */
  64. _setStyle(property, value) {
  65. ((/** @type {?} */ (this._element.nativeElement.style)))[property] = value;
  66. }
  67. }
  68. MatGridTile.decorators = [
  69. { type: Component, args: [{selector: 'mat-grid-tile',
  70. exportAs: 'matGridTile',
  71. host: {
  72. 'class': 'mat-grid-tile',
  73. },
  74. template: "<figure class=\"mat-figure\"><ng-content></ng-content></figure>",
  75. styles: [".mat-grid-list{display:block;position:relative}.mat-grid-tile{display:block;position:absolute;overflow:hidden}.mat-grid-tile .mat-figure{top:0;left:0;right:0;bottom:0;position:absolute;display:flex;align-items:center;justify-content:center;height:100%;padding:0;margin:0}.mat-grid-tile .mat-grid-tile-footer,.mat-grid-tile .mat-grid-tile-header{display:flex;align-items:center;height:48px;color:#fff;background:rgba(0,0,0,.38);overflow:hidden;padding:0 16px;position:absolute;left:0;right:0}.mat-grid-tile .mat-grid-tile-footer>*,.mat-grid-tile .mat-grid-tile-header>*{margin:0;padding:0;font-weight:400;font-size:inherit}.mat-grid-tile .mat-grid-tile-footer.mat-2-line,.mat-grid-tile .mat-grid-tile-header.mat-2-line{height:68px}.mat-grid-tile .mat-grid-list-text{display:flex;flex-direction:column;width:100%;box-sizing:border-box;overflow:hidden}.mat-grid-tile .mat-grid-list-text>*{margin:0;padding:0;font-weight:400;font-size:inherit}.mat-grid-tile .mat-grid-list-text:empty{display:none}.mat-grid-tile .mat-grid-tile-header{top:0}.mat-grid-tile .mat-grid-tile-footer{bottom:0}.mat-grid-tile .mat-grid-avatar{padding-right:16px}[dir=rtl] .mat-grid-tile .mat-grid-avatar{padding-right:0;padding-left:16px}.mat-grid-tile .mat-grid-avatar:empty{display:none}"],
  76. encapsulation: ViewEncapsulation.None,
  77. changeDetection: ChangeDetectionStrategy.OnPush,
  78. },] },
  79. ];
  80. /** @nocollapse */
  81. MatGridTile.ctorParameters = () => [
  82. { type: ElementRef },
  83. { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [MAT_GRID_LIST,] }] }
  84. ];
  85. MatGridTile.propDecorators = {
  86. rowspan: [{ type: Input }],
  87. colspan: [{ type: Input }]
  88. };
  89. class MatGridTileText {
  90. /**
  91. * @param {?} _element
  92. */
  93. constructor(_element) {
  94. this._element = _element;
  95. }
  96. /**
  97. * @return {?}
  98. */
  99. ngAfterContentInit() {
  100. setLines(this._lines, this._element);
  101. }
  102. }
  103. MatGridTileText.decorators = [
  104. { type: Component, args: [{selector: 'mat-grid-tile-header, mat-grid-tile-footer',
  105. template: "<ng-content select=\"[mat-grid-avatar], [matGridAvatar]\"></ng-content><div class=\"mat-grid-list-text\"><ng-content select=\"[mat-line], [matLine]\"></ng-content></div><ng-content></ng-content>",
  106. changeDetection: ChangeDetectionStrategy.OnPush,
  107. encapsulation: ViewEncapsulation.None,
  108. },] },
  109. ];
  110. /** @nocollapse */
  111. MatGridTileText.ctorParameters = () => [
  112. { type: ElementRef }
  113. ];
  114. MatGridTileText.propDecorators = {
  115. _lines: [{ type: ContentChildren, args: [MatLine,] }]
  116. };
  117. /**
  118. * Directive whose purpose is to add the mat- CSS styling to this selector.
  119. * \@docs-private
  120. */
  121. class MatGridAvatarCssMatStyler {
  122. }
  123. MatGridAvatarCssMatStyler.decorators = [
  124. { type: Directive, args: [{
  125. selector: '[mat-grid-avatar], [matGridAvatar]',
  126. host: { 'class': 'mat-grid-avatar' }
  127. },] },
  128. ];
  129. /**
  130. * Directive whose purpose is to add the mat- CSS styling to this selector.
  131. * \@docs-private
  132. */
  133. class MatGridTileHeaderCssMatStyler {
  134. }
  135. MatGridTileHeaderCssMatStyler.decorators = [
  136. { type: Directive, args: [{
  137. selector: 'mat-grid-tile-header',
  138. host: { 'class': 'mat-grid-tile-header' }
  139. },] },
  140. ];
  141. /**
  142. * Directive whose purpose is to add the mat- CSS styling to this selector.
  143. * \@docs-private
  144. */
  145. class MatGridTileFooterCssMatStyler {
  146. }
  147. MatGridTileFooterCssMatStyler.decorators = [
  148. { type: Directive, args: [{
  149. selector: 'mat-grid-tile-footer',
  150. host: { 'class': 'mat-grid-tile-footer' }
  151. },] },
  152. ];
  153. /**
  154. * @fileoverview added by tsickle
  155. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  156. */
  157. /**
  158. * Class for determining, from a list of tiles, the (row, col) position of each of those tiles
  159. * in the grid. This is necessary (rather than just rendering the tiles in normal document flow)
  160. * because the tiles can have a rowspan.
  161. *
  162. * The positioning algorithm greedily places each tile as soon as it encounters a gap in the grid
  163. * large enough to accommodate it so that the tiles still render in the same order in which they
  164. * are given.
  165. *
  166. * The basis of the algorithm is the use of an array to track the already placed tiles. Each
  167. * element of the array corresponds to a column, and the value indicates how many cells in that
  168. * column are already occupied; zero indicates an empty cell. Moving "down" to the next row
  169. * decrements each value in the tracking array (indicating that the column is one cell closer to
  170. * being free).
  171. *
  172. * \@docs-private
  173. */
  174. class TileCoordinator {
  175. constructor() {
  176. /**
  177. * Index at which the search for the next gap will start.
  178. */
  179. this.columnIndex = 0;
  180. /**
  181. * The current row index.
  182. */
  183. this.rowIndex = 0;
  184. }
  185. /**
  186. * Gets the total number of rows occupied by tiles
  187. * @return {?}
  188. */
  189. get rowCount() { return this.rowIndex + 1; }
  190. /**
  191. * Gets the total span of rows occupied by tiles.
  192. * Ex: A list with 1 row that contains a tile with rowspan 2 will have a total rowspan of 2.
  193. * @return {?}
  194. */
  195. get rowspan() {
  196. /** @type {?} */
  197. const lastRowMax = Math.max(...this.tracker);
  198. // if any of the tiles has a rowspan that pushes it beyond the total row count,
  199. // add the difference to the rowcount
  200. return lastRowMax > 1 ? this.rowCount + lastRowMax - 1 : this.rowCount;
  201. }
  202. /**
  203. * Updates the tile positions.
  204. * @param {?} numColumns Amount of columns in the grid.
  205. * @param {?} tiles
  206. * @return {?}
  207. */
  208. update(numColumns, tiles) {
  209. this.columnIndex = 0;
  210. this.rowIndex = 0;
  211. this.tracker = new Array(numColumns);
  212. this.tracker.fill(0, 0, this.tracker.length);
  213. this.positions = tiles.map((/**
  214. * @param {?} tile
  215. * @return {?}
  216. */
  217. tile => this._trackTile(tile)));
  218. }
  219. /**
  220. * Calculates the row and col position of a tile.
  221. * @private
  222. * @param {?} tile
  223. * @return {?}
  224. */
  225. _trackTile(tile) {
  226. // Find a gap large enough for this tile.
  227. /** @type {?} */
  228. const gapStartIndex = this._findMatchingGap(tile.colspan);
  229. // Place tile in the resulting gap.
  230. this._markTilePosition(gapStartIndex, tile);
  231. // The next time we look for a gap, the search will start at columnIndex, which should be
  232. // immediately after the tile that has just been placed.
  233. this.columnIndex = gapStartIndex + tile.colspan;
  234. return new TilePosition(this.rowIndex, gapStartIndex);
  235. }
  236. /**
  237. * Finds the next available space large enough to fit the tile.
  238. * @private
  239. * @param {?} tileCols
  240. * @return {?}
  241. */
  242. _findMatchingGap(tileCols) {
  243. if (tileCols > this.tracker.length) {
  244. throw Error(`mat-grid-list: tile with colspan ${tileCols} is wider than ` +
  245. `grid with cols="${this.tracker.length}".`);
  246. }
  247. // Start index is inclusive, end index is exclusive.
  248. /** @type {?} */
  249. let gapStartIndex = -1;
  250. /** @type {?} */
  251. let gapEndIndex = -1;
  252. // Look for a gap large enough to fit the given tile. Empty spaces are marked with a zero.
  253. do {
  254. // If we've reached the end of the row, go to the next row.
  255. if (this.columnIndex + tileCols > this.tracker.length) {
  256. this._nextRow();
  257. gapStartIndex = this.tracker.indexOf(0, this.columnIndex);
  258. gapEndIndex = this._findGapEndIndex(gapStartIndex);
  259. continue;
  260. }
  261. gapStartIndex = this.tracker.indexOf(0, this.columnIndex);
  262. // If there are no more empty spaces in this row at all, move on to the next row.
  263. if (gapStartIndex == -1) {
  264. this._nextRow();
  265. gapStartIndex = this.tracker.indexOf(0, this.columnIndex);
  266. gapEndIndex = this._findGapEndIndex(gapStartIndex);
  267. continue;
  268. }
  269. gapEndIndex = this._findGapEndIndex(gapStartIndex);
  270. // If a gap large enough isn't found, we want to start looking immediately after the current
  271. // gap on the next iteration.
  272. this.columnIndex = gapStartIndex + 1;
  273. // Continue iterating until we find a gap wide enough for this tile. Since gapEndIndex is
  274. // exclusive, gapEndIndex is 0 means we didn't find a gap and should continue.
  275. } while ((gapEndIndex - gapStartIndex < tileCols) || (gapEndIndex == 0));
  276. // If we still didn't manage to find a gap, ensure that the index is
  277. // at least zero so the tile doesn't get pulled out of the grid.
  278. return Math.max(gapStartIndex, 0);
  279. }
  280. /**
  281. * Move "down" to the next row.
  282. * @private
  283. * @return {?}
  284. */
  285. _nextRow() {
  286. this.columnIndex = 0;
  287. this.rowIndex++;
  288. // Decrement all spaces by one to reflect moving down one row.
  289. for (let i = 0; i < this.tracker.length; i++) {
  290. this.tracker[i] = Math.max(0, this.tracker[i] - 1);
  291. }
  292. }
  293. /**
  294. * Finds the end index (exclusive) of a gap given the index from which to start looking.
  295. * The gap ends when a non-zero value is found.
  296. * @private
  297. * @param {?} gapStartIndex
  298. * @return {?}
  299. */
  300. _findGapEndIndex(gapStartIndex) {
  301. for (let i = gapStartIndex + 1; i < this.tracker.length; i++) {
  302. if (this.tracker[i] != 0) {
  303. return i;
  304. }
  305. }
  306. // The gap ends with the end of the row.
  307. return this.tracker.length;
  308. }
  309. /**
  310. * Update the tile tracker to account for the given tile in the given space.
  311. * @private
  312. * @param {?} start
  313. * @param {?} tile
  314. * @return {?}
  315. */
  316. _markTilePosition(start, tile) {
  317. for (let i = 0; i < tile.colspan; i++) {
  318. this.tracker[start + i] = tile.rowspan;
  319. }
  320. }
  321. }
  322. /**
  323. * Simple data structure for tile position (row, col).
  324. * \@docs-private
  325. */
  326. class TilePosition {
  327. /**
  328. * @param {?} row
  329. * @param {?} col
  330. */
  331. constructor(row, col) {
  332. this.row = row;
  333. this.col = col;
  334. }
  335. }
  336. /**
  337. * @fileoverview added by tsickle
  338. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  339. */
  340. /**
  341. * RegExp that can be used to check whether a value will
  342. * be allowed inside a CSS `calc()` expression.
  343. * @type {?}
  344. */
  345. const cssCalcAllowedValue = /^-?\d+((\.\d+)?[A-Za-z%$]?)+$/;
  346. /**
  347. * Sets the style properties for an individual tile, given the position calculated by the
  348. * Tile Coordinator.
  349. * \@docs-private
  350. * @abstract
  351. */
  352. class TileStyler {
  353. constructor() {
  354. this._rows = 0;
  355. this._rowspan = 0;
  356. }
  357. /**
  358. * Adds grid-list layout info once it is available. Cannot be processed in the constructor
  359. * because these properties haven't been calculated by that point.
  360. *
  361. * @param {?} gutterSize Size of the grid's gutter.
  362. * @param {?} tracker Instance of the TileCoordinator.
  363. * @param {?} cols Amount of columns in the grid.
  364. * @param {?} direction Layout direction of the grid.
  365. * @return {?}
  366. */
  367. init(gutterSize, tracker, cols, direction) {
  368. this._gutterSize = normalizeUnits(gutterSize);
  369. this._rows = tracker.rowCount;
  370. this._rowspan = tracker.rowspan;
  371. this._cols = cols;
  372. this._direction = direction;
  373. }
  374. /**
  375. * Computes the amount of space a single 1x1 tile would take up (width or height).
  376. * Used as a basis for other calculations.
  377. * @param {?} sizePercent Percent of the total grid-list space that one 1x1 tile would take up.
  378. * @param {?} gutterFraction Fraction of the gutter size taken up by one 1x1 tile.
  379. * @return {?} The size of a 1x1 tile as an expression that can be evaluated via CSS calc().
  380. */
  381. getBaseTileSize(sizePercent, gutterFraction) {
  382. // Take the base size percent (as would be if evenly dividing the size between cells),
  383. // and then subtracting the size of one gutter. However, since there are no gutters on the
  384. // edges, each tile only uses a fraction (gutterShare = numGutters / numCells) of the gutter
  385. // size. (Imagine having one gutter per tile, and then breaking up the extra gutter on the
  386. // edge evenly among the cells).
  387. return `(${sizePercent}% - (${this._gutterSize} * ${gutterFraction}))`;
  388. }
  389. /**
  390. * Gets The horizontal or vertical position of a tile, e.g., the 'top' or 'left' property value.
  391. * @param {?} baseSize Base size of a 1x1 tile (as computed in getBaseTileSize).
  392. * @param {?} offset Number of tiles that have already been rendered in the row/column.
  393. * @return {?} Position of the tile as a CSS calc() expression.
  394. */
  395. getTilePosition(baseSize, offset) {
  396. // The position comes the size of a 1x1 tile plus gutter for each previous tile in the
  397. // row/column (offset).
  398. return offset === 0 ? '0' : calc(`(${baseSize} + ${this._gutterSize}) * ${offset}`);
  399. }
  400. /**
  401. * Gets the actual size of a tile, e.g., width or height, taking rowspan or colspan into account.
  402. * @param {?} baseSize Base size of a 1x1 tile (as computed in getBaseTileSize).
  403. * @param {?} span The tile's rowspan or colspan.
  404. * @return {?} Size of the tile as a CSS calc() expression.
  405. */
  406. getTileSize(baseSize, span) {
  407. return `(${baseSize} * ${span}) + (${span - 1} * ${this._gutterSize})`;
  408. }
  409. /**
  410. * Sets the style properties to be applied to a tile for the given row and column index.
  411. * @param {?} tile Tile to which to apply the styling.
  412. * @param {?} rowIndex Index of the tile's row.
  413. * @param {?} colIndex Index of the tile's column.
  414. * @return {?}
  415. */
  416. setStyle(tile, rowIndex, colIndex) {
  417. // Percent of the available horizontal space that one column takes up.
  418. /** @type {?} */
  419. let percentWidthPerTile = 100 / this._cols;
  420. // Fraction of the vertical gutter size that each column takes up.
  421. // For example, if there are 5 columns, each column uses 4/5 = 0.8 times the gutter width.
  422. /** @type {?} */
  423. let gutterWidthFractionPerTile = (this._cols - 1) / this._cols;
  424. this.setColStyles(tile, colIndex, percentWidthPerTile, gutterWidthFractionPerTile);
  425. this.setRowStyles(tile, rowIndex, percentWidthPerTile, gutterWidthFractionPerTile);
  426. }
  427. /**
  428. * Sets the horizontal placement of the tile in the list.
  429. * @param {?} tile
  430. * @param {?} colIndex
  431. * @param {?} percentWidth
  432. * @param {?} gutterWidth
  433. * @return {?}
  434. */
  435. setColStyles(tile, colIndex, percentWidth, gutterWidth) {
  436. // Base horizontal size of a column.
  437. /** @type {?} */
  438. let baseTileWidth = this.getBaseTileSize(percentWidth, gutterWidth);
  439. // The width and horizontal position of each tile is always calculated the same way, but the
  440. // height and vertical position depends on the rowMode.
  441. /** @type {?} */
  442. let side = this._direction === 'rtl' ? 'right' : 'left';
  443. tile._setStyle(side, this.getTilePosition(baseTileWidth, colIndex));
  444. tile._setStyle('width', calc(this.getTileSize(baseTileWidth, tile.colspan)));
  445. }
  446. /**
  447. * Calculates the total size taken up by gutters across one axis of a list.
  448. * @return {?}
  449. */
  450. getGutterSpan() {
  451. return `${this._gutterSize} * (${this._rowspan} - 1)`;
  452. }
  453. /**
  454. * Calculates the total size taken up by tiles across one axis of a list.
  455. * @param {?} tileHeight Height of the tile.
  456. * @return {?}
  457. */
  458. getTileSpan(tileHeight) {
  459. return `${this._rowspan} * ${this.getTileSize(tileHeight, 1)}`;
  460. }
  461. /**
  462. * Calculates the computed height and returns the correct style property to set.
  463. * This method can be implemented by each type of TileStyler.
  464. * \@docs-private
  465. * @return {?}
  466. */
  467. getComputedHeight() { return null; }
  468. }
  469. /**
  470. * This type of styler is instantiated when the user passes in a fixed row height.
  471. * Example `<mat-grid-list cols="3" rowHeight="100px">`
  472. * \@docs-private
  473. */
  474. class FixedTileStyler extends TileStyler {
  475. /**
  476. * @param {?} fixedRowHeight
  477. */
  478. constructor(fixedRowHeight) {
  479. super();
  480. this.fixedRowHeight = fixedRowHeight;
  481. }
  482. /**
  483. * @param {?} gutterSize
  484. * @param {?} tracker
  485. * @param {?} cols
  486. * @param {?} direction
  487. * @return {?}
  488. */
  489. init(gutterSize, tracker, cols, direction) {
  490. super.init(gutterSize, tracker, cols, direction);
  491. this.fixedRowHeight = normalizeUnits(this.fixedRowHeight);
  492. if (!cssCalcAllowedValue.test(this.fixedRowHeight)) {
  493. throw Error(`Invalid value "${this.fixedRowHeight}" set as rowHeight.`);
  494. }
  495. }
  496. /**
  497. * @param {?} tile
  498. * @param {?} rowIndex
  499. * @return {?}
  500. */
  501. setRowStyles(tile, rowIndex) {
  502. tile._setStyle('top', this.getTilePosition(this.fixedRowHeight, rowIndex));
  503. tile._setStyle('height', calc(this.getTileSize(this.fixedRowHeight, tile.rowspan)));
  504. }
  505. /**
  506. * @return {?}
  507. */
  508. getComputedHeight() {
  509. return [
  510. 'height', calc(`${this.getTileSpan(this.fixedRowHeight)} + ${this.getGutterSpan()}`)
  511. ];
  512. }
  513. /**
  514. * @param {?} list
  515. * @return {?}
  516. */
  517. reset(list) {
  518. list._setListStyle(['height', null]);
  519. if (list._tiles) {
  520. list._tiles.forEach((/**
  521. * @param {?} tile
  522. * @return {?}
  523. */
  524. tile => {
  525. tile._setStyle('top', null);
  526. tile._setStyle('height', null);
  527. }));
  528. }
  529. }
  530. }
  531. /**
  532. * This type of styler is instantiated when the user passes in a width:height ratio
  533. * for the row height. Example `<mat-grid-list cols="3" rowHeight="3:1">`
  534. * \@docs-private
  535. */
  536. class RatioTileStyler extends TileStyler {
  537. /**
  538. * @param {?} value
  539. */
  540. constructor(value) {
  541. super();
  542. this._parseRatio(value);
  543. }
  544. /**
  545. * @param {?} tile
  546. * @param {?} rowIndex
  547. * @param {?} percentWidth
  548. * @param {?} gutterWidth
  549. * @return {?}
  550. */
  551. setRowStyles(tile, rowIndex, percentWidth, gutterWidth) {
  552. /** @type {?} */
  553. let percentHeightPerTile = percentWidth / this.rowHeightRatio;
  554. this.baseTileHeight = this.getBaseTileSize(percentHeightPerTile, gutterWidth);
  555. // Use padding-top and margin-top to maintain the given aspect ratio, as
  556. // a percentage-based value for these properties is applied versus the *width* of the
  557. // containing block. See http://www.w3.org/TR/CSS2/box.html#margin-properties
  558. tile._setStyle('marginTop', this.getTilePosition(this.baseTileHeight, rowIndex));
  559. tile._setStyle('paddingTop', calc(this.getTileSize(this.baseTileHeight, tile.rowspan)));
  560. }
  561. /**
  562. * @return {?}
  563. */
  564. getComputedHeight() {
  565. return [
  566. 'paddingBottom', calc(`${this.getTileSpan(this.baseTileHeight)} + ${this.getGutterSpan()}`)
  567. ];
  568. }
  569. /**
  570. * @param {?} list
  571. * @return {?}
  572. */
  573. reset(list) {
  574. list._setListStyle(['paddingBottom', null]);
  575. list._tiles.forEach((/**
  576. * @param {?} tile
  577. * @return {?}
  578. */
  579. tile => {
  580. tile._setStyle('marginTop', null);
  581. tile._setStyle('paddingTop', null);
  582. }));
  583. }
  584. /**
  585. * @private
  586. * @param {?} value
  587. * @return {?}
  588. */
  589. _parseRatio(value) {
  590. /** @type {?} */
  591. const ratioParts = value.split(':');
  592. if (ratioParts.length !== 2) {
  593. throw Error(`mat-grid-list: invalid ratio given for row-height: "${value}"`);
  594. }
  595. this.rowHeightRatio = parseFloat(ratioParts[0]) / parseFloat(ratioParts[1]);
  596. }
  597. }
  598. /**
  599. * This type of styler is instantiated when the user selects a "fit" row height mode.
  600. * In other words, the row height will reflect the total height of the container divided
  601. * by the number of rows. Example `<mat-grid-list cols="3" rowHeight="fit">`
  602. *
  603. * \@docs-private
  604. */
  605. class FitTileStyler extends TileStyler {
  606. /**
  607. * @param {?} tile
  608. * @param {?} rowIndex
  609. * @return {?}
  610. */
  611. setRowStyles(tile, rowIndex) {
  612. // Percent of the available vertical space that one row takes up.
  613. /** @type {?} */
  614. let percentHeightPerTile = 100 / this._rowspan;
  615. // Fraction of the horizontal gutter size that each column takes up.
  616. /** @type {?} */
  617. let gutterHeightPerTile = (this._rows - 1) / this._rows;
  618. // Base vertical size of a column.
  619. /** @type {?} */
  620. let baseTileHeight = this.getBaseTileSize(percentHeightPerTile, gutterHeightPerTile);
  621. tile._setStyle('top', this.getTilePosition(baseTileHeight, rowIndex));
  622. tile._setStyle('height', calc(this.getTileSize(baseTileHeight, tile.rowspan)));
  623. }
  624. /**
  625. * @param {?} list
  626. * @return {?}
  627. */
  628. reset(list) {
  629. if (list._tiles) {
  630. list._tiles.forEach((/**
  631. * @param {?} tile
  632. * @return {?}
  633. */
  634. tile => {
  635. tile._setStyle('top', null);
  636. tile._setStyle('height', null);
  637. }));
  638. }
  639. }
  640. }
  641. /**
  642. * Wraps a CSS string in a calc function
  643. * @param {?} exp
  644. * @return {?}
  645. */
  646. function calc(exp) {
  647. return `calc(${exp})`;
  648. }
  649. /**
  650. * Appends pixels to a CSS string if no units are given.
  651. * @param {?} value
  652. * @return {?}
  653. */
  654. function normalizeUnits(value) {
  655. return value.match(/([A-Za-z%]+)$/) ? value : `${value}px`;
  656. }
  657. /**
  658. * @fileoverview added by tsickle
  659. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  660. */
  661. // TODO(kara): Conditional (responsive) column count / row size.
  662. // TODO(kara): Re-layout on window resize / media change (debounced).
  663. // TODO(kara): gridTileHeader and gridTileFooter.
  664. /** @type {?} */
  665. const MAT_FIT_MODE = 'fit';
  666. class MatGridList {
  667. /**
  668. * @param {?} _element
  669. * @param {?} _dir
  670. */
  671. constructor(_element, _dir) {
  672. this._element = _element;
  673. this._dir = _dir;
  674. /**
  675. * The amount of space between tiles. This will be something like '5px' or '2em'.
  676. */
  677. this._gutter = '1px';
  678. }
  679. /**
  680. * Amount of columns in the grid list.
  681. * @return {?}
  682. */
  683. get cols() { return this._cols; }
  684. /**
  685. * @param {?} value
  686. * @return {?}
  687. */
  688. set cols(value) {
  689. this._cols = Math.max(1, Math.round(coerceNumberProperty(value)));
  690. }
  691. /**
  692. * Size of the grid list's gutter in pixels.
  693. * @return {?}
  694. */
  695. get gutterSize() { return this._gutter; }
  696. /**
  697. * @param {?} value
  698. * @return {?}
  699. */
  700. set gutterSize(value) { this._gutter = `${value == null ? '' : value}`; }
  701. /**
  702. * Set internal representation of row height from the user-provided value.
  703. * @return {?}
  704. */
  705. get rowHeight() { return this._rowHeight; }
  706. /**
  707. * @param {?} value
  708. * @return {?}
  709. */
  710. set rowHeight(value) {
  711. /** @type {?} */
  712. const newValue = `${value == null ? '' : value}`;
  713. if (newValue !== this._rowHeight) {
  714. this._rowHeight = newValue;
  715. this._setTileStyler(this._rowHeight);
  716. }
  717. }
  718. /**
  719. * @return {?}
  720. */
  721. ngOnInit() {
  722. this._checkCols();
  723. this._checkRowHeight();
  724. }
  725. /**
  726. * The layout calculation is fairly cheap if nothing changes, so there's little cost
  727. * to run it frequently.
  728. * @return {?}
  729. */
  730. ngAfterContentChecked() {
  731. this._layoutTiles();
  732. }
  733. /**
  734. * Throw a friendly error if cols property is missing
  735. * @private
  736. * @return {?}
  737. */
  738. _checkCols() {
  739. if (!this.cols) {
  740. throw Error(`mat-grid-list: must pass in number of columns. ` +
  741. `Example: <mat-grid-list cols="3">`);
  742. }
  743. }
  744. /**
  745. * Default to equal width:height if rowHeight property is missing
  746. * @private
  747. * @return {?}
  748. */
  749. _checkRowHeight() {
  750. if (!this._rowHeight) {
  751. this._setTileStyler('1:1');
  752. }
  753. }
  754. /**
  755. * Creates correct Tile Styler subtype based on rowHeight passed in by user
  756. * @private
  757. * @param {?} rowHeight
  758. * @return {?}
  759. */
  760. _setTileStyler(rowHeight) {
  761. if (this._tileStyler) {
  762. this._tileStyler.reset(this);
  763. }
  764. if (rowHeight === MAT_FIT_MODE) {
  765. this._tileStyler = new FitTileStyler();
  766. }
  767. else if (rowHeight && rowHeight.indexOf(':') > -1) {
  768. this._tileStyler = new RatioTileStyler(rowHeight);
  769. }
  770. else {
  771. this._tileStyler = new FixedTileStyler(rowHeight);
  772. }
  773. }
  774. /**
  775. * Computes and applies the size and position for all children grid tiles.
  776. * @private
  777. * @return {?}
  778. */
  779. _layoutTiles() {
  780. if (!this._tileCoordinator) {
  781. this._tileCoordinator = new TileCoordinator();
  782. }
  783. /** @type {?} */
  784. const tracker = this._tileCoordinator;
  785. /** @type {?} */
  786. const tiles = this._tiles.filter((/**
  787. * @param {?} tile
  788. * @return {?}
  789. */
  790. tile => !tile._gridList || tile._gridList === this));
  791. /** @type {?} */
  792. const direction = this._dir ? this._dir.value : 'ltr';
  793. this._tileCoordinator.update(this.cols, tiles);
  794. this._tileStyler.init(this.gutterSize, tracker, this.cols, direction);
  795. tiles.forEach((/**
  796. * @param {?} tile
  797. * @param {?} index
  798. * @return {?}
  799. */
  800. (tile, index) => {
  801. /** @type {?} */
  802. const pos = tracker.positions[index];
  803. this._tileStyler.setStyle(tile, pos.row, pos.col);
  804. }));
  805. this._setListStyle(this._tileStyler.getComputedHeight());
  806. }
  807. /**
  808. * Sets style on the main grid-list element, given the style name and value.
  809. * @param {?} style
  810. * @return {?}
  811. */
  812. _setListStyle(style) {
  813. if (style) {
  814. ((/** @type {?} */ (this._element.nativeElement.style)))[style[0]] = style[1];
  815. }
  816. }
  817. }
  818. MatGridList.decorators = [
  819. { type: Component, args: [{selector: 'mat-grid-list',
  820. exportAs: 'matGridList',
  821. template: "<div><ng-content></ng-content></div>",
  822. styles: [".mat-grid-list{display:block;position:relative}.mat-grid-tile{display:block;position:absolute;overflow:hidden}.mat-grid-tile .mat-figure{top:0;left:0;right:0;bottom:0;position:absolute;display:flex;align-items:center;justify-content:center;height:100%;padding:0;margin:0}.mat-grid-tile .mat-grid-tile-footer,.mat-grid-tile .mat-grid-tile-header{display:flex;align-items:center;height:48px;color:#fff;background:rgba(0,0,0,.38);overflow:hidden;padding:0 16px;position:absolute;left:0;right:0}.mat-grid-tile .mat-grid-tile-footer>*,.mat-grid-tile .mat-grid-tile-header>*{margin:0;padding:0;font-weight:400;font-size:inherit}.mat-grid-tile .mat-grid-tile-footer.mat-2-line,.mat-grid-tile .mat-grid-tile-header.mat-2-line{height:68px}.mat-grid-tile .mat-grid-list-text{display:flex;flex-direction:column;width:100%;box-sizing:border-box;overflow:hidden}.mat-grid-tile .mat-grid-list-text>*{margin:0;padding:0;font-weight:400;font-size:inherit}.mat-grid-tile .mat-grid-list-text:empty{display:none}.mat-grid-tile .mat-grid-tile-header{top:0}.mat-grid-tile .mat-grid-tile-footer{bottom:0}.mat-grid-tile .mat-grid-avatar{padding-right:16px}[dir=rtl] .mat-grid-tile .mat-grid-avatar{padding-right:0;padding-left:16px}.mat-grid-tile .mat-grid-avatar:empty{display:none}"],
  823. host: {
  824. 'class': 'mat-grid-list',
  825. },
  826. providers: [{
  827. provide: MAT_GRID_LIST,
  828. useExisting: MatGridList
  829. }],
  830. changeDetection: ChangeDetectionStrategy.OnPush,
  831. encapsulation: ViewEncapsulation.None,
  832. },] },
  833. ];
  834. /** @nocollapse */
  835. MatGridList.ctorParameters = () => [
  836. { type: ElementRef },
  837. { type: Directionality, decorators: [{ type: Optional }] }
  838. ];
  839. MatGridList.propDecorators = {
  840. _tiles: [{ type: ContentChildren, args: [MatGridTile, { descendants: true },] }],
  841. cols: [{ type: Input }],
  842. gutterSize: [{ type: Input }],
  843. rowHeight: [{ type: Input }]
  844. };
  845. /**
  846. * @fileoverview added by tsickle
  847. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  848. */
  849. class MatGridListModule {
  850. }
  851. MatGridListModule.decorators = [
  852. { type: NgModule, args: [{
  853. imports: [MatLineModule, MatCommonModule],
  854. exports: [
  855. MatGridList,
  856. MatGridTile,
  857. MatGridTileText,
  858. MatLineModule,
  859. MatCommonModule,
  860. MatGridTileHeaderCssMatStyler,
  861. MatGridTileFooterCssMatStyler,
  862. MatGridAvatarCssMatStyler
  863. ],
  864. declarations: [
  865. MatGridList,
  866. MatGridTile,
  867. MatGridTileText,
  868. MatGridTileHeaderCssMatStyler,
  869. MatGridTileFooterCssMatStyler,
  870. MatGridAvatarCssMatStyler
  871. ],
  872. },] },
  873. ];
  874. /**
  875. * @fileoverview added by tsickle
  876. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  877. */
  878. /**
  879. * @fileoverview added by tsickle
  880. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  881. */
  882. export { MatGridListModule, MatGridList, MatGridTile, MatGridTileText, MatGridAvatarCssMatStyler, MatGridTileHeaderCssMatStyler, MatGridTileFooterCssMatStyler, MAT_GRID_LIST as ɵa6 };
  883. //# sourceMappingURL=grid-list.js.map