entities-table.component.html 10.7 KB
<!--

    Copyright © 2016-2019 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<mat-drawer-container hasBackdrop="false" class="tb-absolute-fill">
  <mat-drawer *ngIf="entitiesTableConfig.detailsPanelEnabled"
              class="tb-details-drawer mat-elevation-z4"
              #drawer
              mode="over"
              position="end"
              [opened]="isDetailsOpen">
    <tb-entity-details-panel
      [entitiesTableConfig]="entitiesTableConfig"
      [entityId]="dataSource.currentEntity?.id"
      (closeEntityDetails)="isDetailsOpen = false"
      (entityUpdated)="onEntityUpdated($event)"
      (entityAction)="onEntityAction($event)"
    >
    </tb-entity-details-panel>
  </mat-drawer>
  <mat-drawer-content>
    <div class="mat-padding tb-entity-table tb-absolute-fill">
      <div fxFlex fxLayout="column" class="mat-elevation-z1 tb-entity-table-content">
        <mat-toolbar class="mat-table-toolbar" [fxShow]="!textSearchMode && dataSource.selection.isEmpty()">
          <div class="mat-toolbar-tools">
            <span *ngIf="entitiesTableConfig.tableTitle" class="tb-entity-table-title">{{ entitiesTableConfig.tableTitle }}</span>
            <tb-timewindow *ngIf="entitiesTableConfig.useTimePageLink" [(ngModel)]="timewindow"
                            (ngModelChange)="onTimewindowChange()"
                            asButton historyOnly></tb-timewindow>
            <tb-anchor #entityTableHeader></tb-anchor>
            <span fxFlex *ngIf="!this.entitiesTableConfig.headerComponent"></span>
            <button mat-button mat-icon-button [disabled]="isLoading$ | async" [fxShow]="addEnabled()" (click)="addEntity($event)"
                    matTooltip="{{ translations.add | translate }}"
                    matTooltipPosition="above">
              <mat-icon>add</mat-icon>
            </button>
            <button mat-button mat-icon-button [disabled]="isLoading$ | async"
                    [fxShow]="actionDescriptor.isEnabled()" *ngFor="let actionDescriptor of headerActionDescriptors"
                    matTooltip="{{ actionDescriptor.name }}"
                    matTooltipPosition="above"
                    (click)="actionDescriptor.onAction($event)">
              <mat-icon>{{actionDescriptor.icon}}</mat-icon>
            </button>
            <button mat-button mat-icon-button [disabled]="isLoading$ | async" (click)="updateData()"
                    matTooltip="{{ 'action.refresh' | translate }}"
                    matTooltipPosition="above">
              <mat-icon>refresh</mat-icon>
            </button>
            <button *ngIf="entitiesTableConfig.searchEnabled"
                    mat-button mat-icon-button [disabled]="isLoading$ | async" (click)="enterFilterMode()"
                    matTooltip="{{ translations.search | translate }}"
                    matTooltipPosition="above">
              <mat-icon>search</mat-icon>
            </button>
          </div>
        </mat-toolbar>
        <mat-toolbar class="mat-table-toolbar" [fxShow]="textSearchMode && dataSource.selection.isEmpty()">
          <div class="mat-toolbar-tools">
            <button mat-button mat-icon-button
                    matTooltip="{{ translations.search | translate }}"
                    matTooltipPosition="above">
              <mat-icon>search</mat-icon>
            </button>
            <mat-form-field fxFlex>
              <mat-label>&nbsp;</mat-label>
              <input #searchInput matInput
                     [(ngModel)]="pageLink.textSearch"
                     placeholder="{{ translations.search | translate }}"/>
            </mat-form-field>
            <button mat-button mat-icon-button (click)="exitFilterMode()"
                    matTooltip="{{ 'action.close' | translate }}"
                    matTooltipPosition="above">
              <mat-icon>close</mat-icon>
            </button>
          </div>
        </mat-toolbar>
        <mat-toolbar *ngIf="entitiesTableConfig.selectionEnabled" class="mat-table-toolbar" color="primary" [fxShow]="!dataSource.selection.isEmpty()">
          <div class="mat-toolbar-tools">
                    <span>
                      {{ translate.get(translations.selectedEntities, {count: dataSource.selection.selected.length}) | async }}
                    </span>
            <span fxFlex></span>
            <button mat-button mat-icon-button [disabled]="isLoading$ | async"
                    [fxShow]="actionDescriptor.isEnabled" *ngFor="let actionDescriptor of groupActionDescriptors"
                    matTooltip="{{ actionDescriptor.name }}"
                    matTooltipPosition="above"
                    (click)="actionDescriptor.onAction($event, dataSource.selection.selected)">
              <mat-icon>{{actionDescriptor.icon}}</mat-icon>
            </button>
          </div>
        </mat-toolbar>
        <div fxFlex class="table-container">
          <mat-table [dataSource]="dataSource"
                     matSort [matSortActive]="pageLink.sortOrder.property" [matSortDirection]="(pageLink.sortOrder.direction + '').toLowerCase()" matSortDisableClear>
            <ng-container matColumnDef="select" sticky>
              <mat-header-cell *matHeaderCellDef>
                <mat-checkbox (change)="$event ? dataSource.masterToggle() : null"
                              [checked]="dataSource.selection.hasValue() && (dataSource.isAllSelected() | async)"
                              [indeterminate]="dataSource.selection.hasValue() && !(dataSource.isAllSelected() | async)">
                </mat-checkbox>
              </mat-header-cell>
              <mat-cell *matCellDef="let entity">
                <mat-checkbox (click)="$event.stopPropagation()"
                              (change)="$event ? dataSource.selection.toggle(entity) : null"
                              [checked]="dataSource.selection.isSelected(entity)">
                </mat-checkbox>
              </mat-cell>
            </ng-container>
            <ng-container [matColumnDef]="column.key" *ngFor="let column of columns">
              <mat-header-cell *matHeaderCellDef [ngStyle]="{maxWidth: column.maxWidth}" mat-sort-header> {{ column.title | translate }} </mat-header-cell>
              <mat-cell *matCellDef="let entity" [ngStyle]="cellStyle(entity, column)" [innerHTML]="cellContent(entity, column)"></mat-cell>
            </ng-container>
            <ng-container matColumnDef="actions" stickyEnd>
              <mat-header-cell *matHeaderCellDef [ngStyle.gt-md]="{ minWidth: (cellActionDescriptors.length * 40) + 'px' }">
                {{ entitiesTableConfig.actionsColumnTitle ? (entitiesTableConfig.actionsColumnTitle | translate) : '' }}
              </mat-header-cell>
              <mat-cell *matCellDef="let entity" [ngStyle.gt-md]="{ minWidth: (cellActionDescriptors.length * 40) + 'px' }">
                <div fxHide fxShow.gt-md fxFlex fxLayout="row" fxLayoutAlign="end">
                  <button mat-button mat-icon-button [disabled]="isLoading$ | async"
                          [fxShow]="actionDescriptor.isEnabled(entity)" *ngFor="let actionDescriptor of cellActionDescriptors"
                          matTooltip="{{ actionDescriptor.nameFunction ? actionDescriptor.nameFunction(entity) : actionDescriptor.name }}"
                          matTooltipPosition="above"
                          (click)="actionDescriptor.onAction($event, entity)">
                    <mat-icon *ngIf="!actionDescriptor.isMdiIcon" [ngStyle]="actionDescriptor.color ? {color: actionDescriptor.color} : {}">
                      {{actionDescriptor.icon}}</mat-icon>
                    <mat-icon *ngIf="actionDescriptor.isMdiIcon" [ngStyle]="actionDescriptor.color ? {color: actionDescriptor.color} : {}"
                              [svgIcon]="actionDescriptor.icon"></mat-icon>
                  </button>
                </div>
                <div fxHide fxShow.lt-lg>
                  <button mat-button mat-icon-button
                          (click)="$event.stopPropagation()"
                          [matMenuTriggerFor]="cellActionsMenu">
                    <mat-icon class="material-icons">more_vert</mat-icon>
                  </button>
                  <mat-menu #cellActionsMenu="matMenu" xPosition="before">
                    <button mat-menu-item *ngFor="let actionDescriptor of cellActionDescriptors"
                            [disabled]="isLoading$ | async"
                            [fxShow]="actionDescriptor.isEnabled(entity)"
                            (click)="actionDescriptor.onAction($event, entity)">
                      <mat-icon *ngIf="!actionDescriptor.isMdiIcon" [ngStyle]="actionDescriptor.color ? {color: actionDescriptor.color} : {}">
                        {{actionDescriptor.icon}}</mat-icon>
                      <mat-icon *ngIf="actionDescriptor.isMdiIcon" [ngStyle]="actionDescriptor.color ? {color: actionDescriptor.color} : {}"
                                [svgIcon]="actionDescriptor.icon"></mat-icon>
                      <span>{{ actionDescriptor.nameFunction ? actionDescriptor.nameFunction(entity) : actionDescriptor.name }}</span>
                    </button>
                  </mat-menu>
                </div>
              </mat-cell>
            </ng-container>
            <mat-header-row [ngClass]="{'mat-row-select': selectionEnabled}" *matHeaderRowDef="displayedColumns; sticky: true"></mat-header-row>
            <mat-row [ngClass]="{'mat-row-select': selectionEnabled,
                                 'mat-selected': dataSource.selection.isSelected(entity),
                                 'tb-current-entity': dataSource.isCurrentEntity(entity)}"
                     *matRowDef="let entity; columns: displayedColumns;" (click)="onRowClick($event, entity)"></mat-row>
          </mat-table>
          <span [fxShow]="dataSource.isEmpty() | async"
                fxLayoutAlign="center center"
                class="no-data-found" translate>{{ translations.noEntities }}</span>
        </div>
        <mat-divider></mat-divider>
        <mat-paginator [length]="dataSource.total() | async"
                       [pageIndex]="pageLink.page"
                       [pageSize]="pageLink.pageSize"
                       [pageSizeOptions]="[10, 20, 30]"></mat-paginator>
      </div>
    </div>
  </mat-drawer-content>
</mat-drawer-container>