license import { ChangeDetectionStrategy , Component , Inject , Input , OnDestroy , OnInit , Optional , ViewChild , ViewEncapsulation , } from '@angular/core' ; import { CdkCellDef , CdkColumnDef , CdkHeaderCellDef } from './cell' ; import { CdkTable } from './table' ; import { getTableTextColumnMissingParentTableError , getTableTextColumnMissingNameError , } from './table-errors' ; import { TEXT_COLUMN_OPTIONS , TextColumnOptions } from './tokens' ; /**
 * Column that simply shows text content for the header and row cells. Assumes that the table
 * is using the native table implementation (`<table>`).
 *
 * By default, the name of this column will be the header text and data property accessor.
 * The header text can be overridden with the `headerText` input. Cell values can be overridden with
 * the `dataAccessor` input. Change the text justification to the start or end using the `justify`
 * input.
 */ @ Component ( { selector : 'cdk-text-column' , template : `
    <ng-container cdkColumnDef>
      <th cdk-header-cell *cdkHeaderCellDef [style.text-align]="justify">
        {{headerText}}
      </th>
      <td cdk-cell *cdkCellDef="let data" [style.text-align]="justify">
        {{dataAccessor(data, name)}}
      </td>
    </ng-container>
  ` , encapsulation : ViewEncapsulation . None , changeDetection : ChangeDetectionStrategy . Default , } ) export class CdkTextColumn < T > implements OnDestroy , OnInit { /** Column name that should be used to reference this column. */ @ Input ( ) get name ( ) : string { return this . _name ; } set name ( name ) { this . _name = name ; this . _syncColumnDefName ( ) ; } _name : string ; /**
     * Text label that should be used for the column header. If this property is not
     * set, the header text will default to the column name with its first letter capitalized.
     */ @ Input ( ) headerText : string ; /**
     * Accessor function to retrieve the data rendered for each cell. If this
     * property is not set, the data cells will render the value found in the data's property matching
     * the column's name. For example, if the column is named `id`, then the rendered value will be
     * value defined by the data's `id` property.
     */ @ Input ( ) dataAccessor : ( data : T , name : string ) => string ; /** Alignment of the cell values. */ @ Input ( ) justify : 'start' | 'end' = 'start' ; docs-private @ ViewChild ( CdkColumnDef , { static : true } ) columnDef : CdkColumnDef ; docs-private @ ViewChild ( CdkCellDef , { static : true } ) cell : CdkCellDef ; docs-private @ ViewChild ( CdkHeaderCellDef , { static : true } ) headerCell : CdkHeaderCellDef ; constructor ( @ Optional ( ) private _table , @ Optional ( ) @ Inject ( TEXT_COLUMN_OPTIONS ) private _options ) { this . _options = _options || { } ; } ngOnInit ( ) { this . _syncColumnDefName ( ) ; if ( this . headerText === undefined ) { this . headerText = this . _createDefaultHeaderText ( ) ; } if ( ! this . dataAccessor ) { this . dataAccessor = this . _options . defaultDataAccessor || ( ( data , name ) => ( data as any ) [ name ] ) ; } if ( this . _table ) { this . columnDef . cell = this . cell ; this . columnDef . headerCell = this . headerCell ; this . _table . addColumnDef ( this . columnDef ) ; } else if ( typeof ngDevMode === 'undefined' || ngDevMode ) { throw getTableTextColumnMissingParentTableError ( ) ; } } ngOnDestroy ( ) { if ( this . _table ) { this . _table . removeColumnDef ( this . columnDef ) ; } } /**
     * Creates a default header text. Use the options' header text transformation function if one
     * has been provided. Otherwise simply capitalize the column name.
     */ _createDefaultHeaderText ( ) { const name = this . name ; if ( ! name && ( typeof ngDevMode === 'undefined' || ngDevMode ) ) { throw getTableTextColumnMissingNameError ( ) ; } if ( this . _options && this . _options . defaultHeaderTextTransform ) { return this . _options . defaultHeaderTextTransform ( name ) ; } return name [ 0 ] . toUpperCase ( ) + name . slice ( 1 ) ; } /** Synchronizes the column definition name with the text column name. */ private _syncColumnDefName ( ) { if ( this . columnDef ) { this . columnDef . name = this . name ; } } }