/// /// 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. /// import { BaseData, HasId } from '@shared/models/base-data'; import { EntityId } from '@shared/models/id/entity-id'; import { EntitiesFetchFunction } from '@shared/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 '@shared/components/entity/entity.component'; import { Type } from '@angular/core'; import { EntityAction } from '@shared/components/entity/entity-component.models'; import { HasUUID } from '@shared/models/id/has-uuid'; import { PageLink } from '@shared/models/page/page-link'; import { EntitiesTableComponent } from '@shared/components/entity/entities-table.component'; import { EntityTableHeaderComponent } from '@shared/components/entity/entity-table-header.component'; import { ActivatedRoute } from '@angular/router'; export type EntityBooleanFunction> = (entity: T) => boolean; export type EntityStringFunction> = (entity: T) => string; export type EntityCountStringFunction = (count: number) => string; export type EntityTwoWayOperation> = (entity: T) => Observable; export type EntityByIdOperation> = (id: HasUUID) => Observable; export type EntityIdOneWayOperation = (id: HasUUID) => Observable; export type EntityActionFunction> = (action: EntityAction) => boolean; export type CreateEntityOperation> = () => Observable; export type CellContentFunction> = (entity: T, key: string) => string; export type CellStyleFunction> = (entity: T, key: string) => object; export interface CellActionDescriptor> { name: string; nameFunction?: (entity: T) => string; icon?: string; isMdiIcon?: boolean; color?: string; isEnabled: (entity: T) => boolean; onAction: ($event: MouseEvent, entity: T) => void; } export interface GroupActionDescriptor> { name: string; icon: string; isEnabled: boolean; onAction: ($event: MouseEvent, entities: T[]) => void; } export interface HeaderActionDescriptor { name: string; icon: string; isEnabled: () => boolean; onAction: ($event: MouseEvent) => void; } export class EntityTableColumn> { constructor(public key: string, public title: string, public maxWidth: string = '100%', public cellContentFunction: CellContentFunction = (entity, property) => entity[property], public cellStyleFunction: CellStyleFunction = () => ({}), public sortable: boolean = true) { } } export class DateEntityTableColumn> extends EntityTableColumn { constructor(key: string, title: string, datePipe: DatePipe, maxWidth: string = '100%', dateFormat: string = 'yyyy-MM-dd HH:mm:ss', cellStyleFunction: CellStyleFunction = () => ({})) { super(key, title, maxWidth, (entity, property) => datePipe.transform(entity[property], dateFormat), cellStyleFunction); } } export class EntityTableConfig, P extends PageLink = PageLink> { constructor() {} componentsData: any = null; loadDataOnInit = true; onLoadAction: (route: ActivatedRoute) => void = null; table: EntitiesTableComponent = null; useTimePageLink = false; entityType: EntityType = null; tableTitle = ''; selectionEnabled = true; searchEnabled = true; addEnabled = true; entitiesDeleteEnabled = true; detailsPanelEnabled = true; actionsColumnTitle = null; entityTranslations: EntityTypeTranslation; entityResources: EntityTypeResource; entityComponent: Type>; defaultSortOrder: SortOrder = {property: 'createdTime', direction: Direction.ASC}; columns: Array> = []; cellActionDescriptors: Array> = []; groupActionDescriptors: Array> = []; headerActionDescriptors: Array = []; addActionDescriptors: Array = []; headerComponent: Type>; addEntity: CreateEntityOperation = null; detailsReadonly: EntityBooleanFunction = () => false; deleteEnabled: EntityBooleanFunction = () => true; deleteEntityTitle: EntityStringFunction = () => ''; deleteEntityContent: EntityStringFunction = () => ''; deleteEntitiesTitle: EntityCountStringFunction = () => ''; deleteEntitiesContent: EntityCountStringFunction = () => ''; loadEntity: EntityByIdOperation = () => of(); saveEntity: EntityTwoWayOperation = (entity) => of(entity); deleteEntity: EntityIdOneWayOperation = () => of(); entitiesFetchFunction: EntitiesFetchFunction = () => of(emptyPageData()); onEntityAction: EntityActionFunction = () => false; } export function checkBoxCell(value: boolean): string { return `${value ? 'check_box' : 'check_box_outline_blank'}`; }