2019-08-12 19:34:23 +03:00
|
|
|
///
|
2021-01-11 13:42:16 +02:00
|
|
|
/// Copyright © 2016-2021 The Thingsboard Authors
|
2019-08-12 19:34:23 +03:00
|
|
|
///
|
|
|
|
|
/// 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.
|
|
|
|
|
///
|
|
|
|
|
|
2020-04-07 17:06:04 +03:00
|
|
|
import { BaseData, HasId } from '@shared/models/base-data';
|
|
|
|
|
import { EntitiesDataSource, EntitiesFetchFunction } from '@home/models/datasource/entity-datasource';
|
|
|
|
|
import { Observable, of } from 'rxjs';
|
|
|
|
|
import { emptyPageData } from '@shared/models/page/page-data';
|
|
|
|
|
import { DatePipe } from '@angular/common';
|
|
|
|
|
import { Direction, SortOrder } from '@shared/models/page/sort-order';
|
|
|
|
|
import { EntityType, EntityTypeResource, EntityTypeTranslation } from '@shared/models/entity-type.models';
|
|
|
|
|
import { EntityComponent } from '@home/components/entity/entity.component';
|
|
|
|
|
import { Type } from '@angular/core';
|
|
|
|
|
import { EntityAction } from './entity-component.models';
|
|
|
|
|
import { HasUUID } from '@shared/models/id/has-uuid';
|
2021-11-08 19:21:26 +02:00
|
|
|
import { PageLink, TimePageLink } from '@shared/models/page/page-link';
|
|
|
|
|
import { EntitiesTableComponent } from '@home/components/entity/entities-table.component';
|
2020-04-07 17:06:04 +03:00
|
|
|
import { EntityTableHeaderComponent } from '@home/components/entity/entity-table-header.component';
|
|
|
|
|
import { ActivatedRoute } from '@angular/router';
|
2019-08-21 18:18:46 +03:00
|
|
|
import { EntityTabsComponent } from '../../components/entity/entity-tabs.component';
|
2021-02-16 11:56:49 +02:00
|
|
|
import { DAY, historyInterval } from '@shared/models/time/time.models';
|
2021-12-06 12:54:48 +02:00
|
|
|
import { IEntitiesTableComponent } from '@home/models/entity/entity-table-component.models';
|
2019-08-12 19:34:23 +03:00
|
|
|
|
|
|
|
|
export type EntityBooleanFunction<T extends BaseData<HasId>> = (entity: T) => boolean;
|
|
|
|
|
export type EntityStringFunction<T extends BaseData<HasId>> = (entity: T) => string;
|
2020-04-15 13:00:32 +03:00
|
|
|
export type EntityVoidFunction<T extends BaseData<HasId>> = (entity: T) => void;
|
|
|
|
|
export type EntityIdsVoidFunction<T extends BaseData<HasId>> = (ids: HasUUID[]) => void;
|
2019-08-12 19:34:23 +03:00
|
|
|
export type EntityCountStringFunction = (count: number) => string;
|
2021-06-02 19:03:31 +03:00
|
|
|
export type EntityTwoWayOperation<T extends BaseData<HasId>> = (entity: T, originalEntity?: T) => Observable<T>;
|
2019-08-12 19:34:23 +03:00
|
|
|
export type EntityByIdOperation<T extends BaseData<HasId>> = (id: HasUUID) => Observable<T>;
|
|
|
|
|
export type EntityIdOneWayOperation = (id: HasUUID) => Observable<any>;
|
|
|
|
|
export type EntityActionFunction<T extends BaseData<HasId>> = (action: EntityAction<T>) => boolean;
|
|
|
|
|
export type CreateEntityOperation<T extends BaseData<HasId>> = () => Observable<T>;
|
2020-04-08 19:31:08 +03:00
|
|
|
export type EntityRowClickFunction<T extends BaseData<HasId>> = (event: Event, entity: T) => boolean;
|
2019-08-12 19:34:23 +03:00
|
|
|
|
|
|
|
|
export type CellContentFunction<T extends BaseData<HasId>> = (entity: T, key: string) => string;
|
2020-01-22 20:05:30 +02:00
|
|
|
export type CellTooltipFunction<T extends BaseData<HasId>> = (entity: T, key: string) => string | undefined;
|
2019-08-22 18:44:48 +03:00
|
|
|
export type HeaderCellStyleFunction<T extends BaseData<HasId>> = (key: string) => object;
|
2019-08-12 19:34:23 +03:00
|
|
|
export type CellStyleFunction<T extends BaseData<HasId>> = (entity: T, key: string) => object;
|
2021-06-04 20:49:01 +03:00
|
|
|
export type CopyCellContent<T extends BaseData<HasId>> = (entity: T, key: string, length: number) => object;
|
|
|
|
|
|
|
|
|
|
export enum CellActionDescriptorType { 'DEFAULT', 'COPY_BUTTON'}
|
2019-08-12 19:34:23 +03:00
|
|
|
|
|
|
|
|
export interface CellActionDescriptor<T extends BaseData<HasId>> {
|
|
|
|
|
name: string;
|
|
|
|
|
nameFunction?: (entity: T) => string;
|
|
|
|
|
icon?: string;
|
2019-08-15 14:48:14 +03:00
|
|
|
mdiIcon?: string;
|
|
|
|
|
style?: any;
|
2019-08-12 19:34:23 +03:00
|
|
|
isEnabled: (entity: T) => boolean;
|
2021-06-04 20:49:01 +03:00
|
|
|
onAction: ($event: MouseEvent, entity: T) => any;
|
|
|
|
|
type?: CellActionDescriptorType;
|
2019-08-12 19:34:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface GroupActionDescriptor<T extends BaseData<HasId>> {
|
|
|
|
|
name: string;
|
|
|
|
|
icon: string;
|
|
|
|
|
isEnabled: boolean;
|
|
|
|
|
onAction: ($event: MouseEvent, entities: T[]) => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface HeaderActionDescriptor {
|
|
|
|
|
name: string;
|
|
|
|
|
icon: string;
|
2021-09-01 12:15:00 +03:00
|
|
|
isMdiIcon?: boolean;
|
2019-08-12 19:34:23 +03:00
|
|
|
isEnabled: () => boolean;
|
2021-11-08 19:21:26 +02:00
|
|
|
onAction: ($event: MouseEvent) => void;
|
2019-08-12 19:34:23 +03:00
|
|
|
}
|
|
|
|
|
|
2019-08-22 18:44:48 +03:00
|
|
|
export type EntityTableColumnType = 'content' | 'action';
|
|
|
|
|
|
|
|
|
|
export class BaseEntityTableColumn<T extends BaseData<HasId>> {
|
|
|
|
|
constructor(public type: EntityTableColumnType,
|
|
|
|
|
public key: string,
|
|
|
|
|
public title: string,
|
2020-01-24 19:05:41 +02:00
|
|
|
public width: string = '0px',
|
2020-04-07 17:06:04 +03:00
|
|
|
public sortable: boolean = true,
|
|
|
|
|
public ignoreTranslate: boolean = false,
|
|
|
|
|
public mobileHide: boolean = false) {
|
2019-08-22 18:44:48 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class EntityTableColumn<T extends BaseData<HasId>> extends BaseEntityTableColumn<T> {
|
2019-08-12 19:34:23 +03:00
|
|
|
constructor(public key: string,
|
|
|
|
|
public title: string,
|
2020-01-24 19:05:41 +02:00
|
|
|
public width: string = '0px',
|
2020-02-05 17:52:18 +02:00
|
|
|
public cellContentFunction: CellContentFunction<T> = (entity, property) => entity[property] ? entity[property] : '',
|
2019-08-13 19:58:35 +03:00
|
|
|
public cellStyleFunction: CellStyleFunction<T> = () => ({}),
|
2019-08-22 18:44:48 +03:00
|
|
|
public sortable: boolean = true,
|
2020-01-22 20:05:30 +02:00
|
|
|
public headerCellStyleFunction: HeaderCellStyleFunction<T> = () => ({}),
|
2020-01-24 19:05:41 +02:00
|
|
|
public cellTooltipFunction: CellTooltipFunction<T> = () => undefined,
|
2021-06-04 20:49:01 +03:00
|
|
|
public isNumberColumn: boolean = false,
|
2021-06-07 14:05:10 +03:00
|
|
|
public actionCell: CellActionDescriptor<T> = null) {
|
2020-01-24 19:05:41 +02:00
|
|
|
super('content', key, title, width, sortable);
|
2019-08-22 18:44:48 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class EntityActionTableColumn<T extends BaseData<HasId>> extends BaseEntityTableColumn<T> {
|
|
|
|
|
constructor(public key: string,
|
|
|
|
|
public title: string,
|
|
|
|
|
public actionDescriptor: CellActionDescriptor<T>,
|
2020-01-24 19:05:41 +02:00
|
|
|
public width: string = '0px') {
|
|
|
|
|
super('action', key, title, width, false);
|
2019-08-12 19:34:23 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class DateEntityTableColumn<T extends BaseData<HasId>> extends EntityTableColumn<T> {
|
|
|
|
|
constructor(key: string,
|
|
|
|
|
title: string,
|
|
|
|
|
datePipe: DatePipe,
|
2020-01-24 19:05:41 +02:00
|
|
|
width: string = '0px',
|
2019-08-12 19:34:23 +03:00
|
|
|
dateFormat: string = 'yyyy-MM-dd HH:mm:ss',
|
|
|
|
|
cellStyleFunction: CellStyleFunction<T> = () => ({})) {
|
|
|
|
|
super(key,
|
|
|
|
|
title,
|
2020-01-24 19:05:41 +02:00
|
|
|
width,
|
2019-08-12 19:34:23 +03:00
|
|
|
(entity, property) => datePipe.transform(entity[property], dateFormat),
|
|
|
|
|
cellStyleFunction);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-22 18:44:48 +03:00
|
|
|
export type EntityColumn<T extends BaseData<HasId>> = EntityTableColumn<T> | EntityActionTableColumn<T>;
|
|
|
|
|
|
2020-04-07 17:06:04 +03:00
|
|
|
export class EntityTableConfig<T extends BaseData<HasId>, P extends PageLink = PageLink, L extends BaseData<HasId> = T> {
|
2019-08-12 19:34:23 +03:00
|
|
|
|
|
|
|
|
constructor() {}
|
|
|
|
|
|
|
|
|
|
componentsData: any = null;
|
|
|
|
|
|
|
|
|
|
loadDataOnInit = true;
|
|
|
|
|
onLoadAction: (route: ActivatedRoute) => void = null;
|
2021-12-06 12:54:48 +02:00
|
|
|
table: IEntitiesTableComponent = null;
|
2019-08-12 19:34:23 +03:00
|
|
|
useTimePageLink = false;
|
2021-02-16 11:56:49 +02:00
|
|
|
defaultTimewindowInterval = historyInterval(DAY);
|
2019-08-12 19:34:23 +03:00
|
|
|
entityType: EntityType = null;
|
|
|
|
|
tableTitle = '';
|
|
|
|
|
selectionEnabled = true;
|
|
|
|
|
searchEnabled = true;
|
|
|
|
|
addEnabled = true;
|
|
|
|
|
entitiesDeleteEnabled = true;
|
|
|
|
|
detailsPanelEnabled = true;
|
2020-04-07 17:06:04 +03:00
|
|
|
hideDetailsTabsOnEdit = true;
|
2019-08-12 19:34:23 +03:00
|
|
|
actionsColumnTitle = null;
|
|
|
|
|
entityTranslations: EntityTypeTranslation;
|
2020-04-07 17:06:04 +03:00
|
|
|
entityResources: EntityTypeResource<T>;
|
|
|
|
|
entityComponent: Type<EntityComponent<T, P, L>>;
|
|
|
|
|
entityTabsComponent: Type<EntityTabsComponent<T, P, L>>;
|
2019-08-19 20:09:41 +03:00
|
|
|
addDialogStyle = {};
|
2020-05-22 15:08:00 +03:00
|
|
|
defaultSortOrder: SortOrder = {property: 'createdTime', direction: Direction.DESC};
|
2020-04-08 19:31:08 +03:00
|
|
|
displayPagination = true;
|
|
|
|
|
defaultPageSize = 10;
|
2020-04-07 17:06:04 +03:00
|
|
|
columns: Array<EntityColumn<L>> = [];
|
|
|
|
|
cellActionDescriptors: Array<CellActionDescriptor<L>> = [];
|
|
|
|
|
groupActionDescriptors: Array<GroupActionDescriptor<L>> = [];
|
2019-08-12 19:34:23 +03:00
|
|
|
headerActionDescriptors: Array<HeaderActionDescriptor> = [];
|
2019-08-14 19:55:24 +03:00
|
|
|
addActionDescriptors: Array<HeaderActionDescriptor> = [];
|
2020-04-08 19:31:08 +03:00
|
|
|
headerComponent: Type<EntityTableHeaderComponent<T, P, L>>;
|
2019-08-12 19:34:23 +03:00
|
|
|
addEntity: CreateEntityOperation<T> = null;
|
2020-04-09 17:48:16 +03:00
|
|
|
dataSource: (dataLoadedFunction: (col?: number, row?: number) => void)
|
|
|
|
|
=> EntitiesDataSource<L> = (dataLoadedFunction: (col?: number, row?: number) => void) => {
|
2020-04-07 17:06:04 +03:00
|
|
|
return new EntitiesDataSource(this.entitiesFetchFunction, this.entitySelectionEnabled, dataLoadedFunction);
|
2021-02-16 11:56:49 +02:00
|
|
|
}
|
2019-08-12 19:34:23 +03:00
|
|
|
detailsReadonly: EntityBooleanFunction<T> = () => false;
|
2020-04-07 17:06:04 +03:00
|
|
|
entitySelectionEnabled: EntityBooleanFunction<L> = () => true;
|
|
|
|
|
deleteEnabled: EntityBooleanFunction<T | L> = () => true;
|
|
|
|
|
deleteEntityTitle: EntityStringFunction<L> = () => '';
|
|
|
|
|
deleteEntityContent: EntityStringFunction<L> = () => '';
|
2019-08-12 19:34:23 +03:00
|
|
|
deleteEntitiesTitle: EntityCountStringFunction = () => '';
|
|
|
|
|
deleteEntitiesContent: EntityCountStringFunction = () => '';
|
2021-04-12 16:56:00 +03:00
|
|
|
loadEntity: EntityByIdOperation<T | L> = () => of();
|
2021-06-02 19:03:31 +03:00
|
|
|
saveEntity: EntityTwoWayOperation<T> = (entity, originalEntity) => of(entity);
|
2019-08-12 19:34:23 +03:00
|
|
|
deleteEntity: EntityIdOneWayOperation = () => of();
|
2020-04-07 17:06:04 +03:00
|
|
|
entitiesFetchFunction: EntitiesFetchFunction<L, P> = () => of(emptyPageData<L>());
|
2019-08-12 19:34:23 +03:00
|
|
|
onEntityAction: EntityActionFunction<T> = () => false;
|
2020-04-08 19:31:08 +03:00
|
|
|
handleRowClick: EntityRowClickFunction<L> = () => false;
|
2020-04-06 16:58:23 +03:00
|
|
|
entityTitle: EntityStringFunction<T> = (entity) => entity?.name;
|
2020-04-15 13:00:32 +03:00
|
|
|
entityAdded: EntityVoidFunction<T> = () => {};
|
|
|
|
|
entityUpdated: EntityVoidFunction<T> = () => {};
|
|
|
|
|
entitiesDeleted: EntityIdsVoidFunction<T> = () => {};
|
2019-08-12 19:34:23 +03:00
|
|
|
}
|
2019-08-13 19:58:35 +03:00
|
|
|
|
|
|
|
|
export function checkBoxCell(value: boolean): string {
|
|
|
|
|
return `<mat-icon class="material-icons mat-icon">${value ? 'check_box' : 'check_box_outline_blank'}</mat-icon>`;
|
|
|
|
|
}
|