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.
|
|
|
|
|
///
|
|
|
|
|
|
|
|
|
|
import {
|
2020-04-06 11:46:56 +03:00
|
|
|
AfterViewInit,
|
2020-04-27 09:27:14 +03:00
|
|
|
ChangeDetectionStrategy,
|
|
|
|
|
ChangeDetectorRef,
|
2019-08-12 19:34:23 +03:00
|
|
|
Component,
|
2020-04-27 09:27:14 +03:00
|
|
|
ComponentFactoryResolver,
|
|
|
|
|
ComponentRef,
|
|
|
|
|
EventEmitter,
|
|
|
|
|
Injector,
|
2019-08-12 19:34:23 +03:00
|
|
|
Input,
|
|
|
|
|
OnDestroy,
|
|
|
|
|
OnInit,
|
|
|
|
|
Output,
|
2019-08-21 18:18:46 +03:00
|
|
|
QueryList,
|
2020-04-06 11:46:56 +03:00
|
|
|
ViewChild,
|
|
|
|
|
ViewChildren
|
2019-08-12 19:34:23 +03:00
|
|
|
} from '@angular/core';
|
|
|
|
|
import { PageComponent } from '@shared/components/page.component';
|
|
|
|
|
import { Store } from '@ngrx/store';
|
|
|
|
|
import { AppState } from '@core/core.state';
|
2019-08-21 18:18:46 +03:00
|
|
|
import { EntityTableConfig } from '@home/models/entity/entities-table-config.models';
|
2019-08-12 19:34:23 +03:00
|
|
|
import { BaseData, HasId } from '@shared/models/base-data';
|
2020-04-06 11:46:56 +03:00
|
|
|
import { EntityType, EntityTypeResource, EntityTypeTranslation } from '@shared/models/entity-type.models';
|
2020-04-28 12:04:21 +03:00
|
|
|
import { FormGroup } from '@angular/forms';
|
2019-08-21 18:18:46 +03:00
|
|
|
import { EntityComponent } from './entity.component';
|
2019-08-12 19:34:23 +03:00
|
|
|
import { TbAnchorComponent } from '@shared/components/tb-anchor.component';
|
2019-08-21 18:18:46 +03:00
|
|
|
import { EntityAction } from '@home/models/entity/entity-component.models';
|
2019-08-12 19:34:23 +03:00
|
|
|
import { Subscription } from 'rxjs';
|
2020-04-06 11:46:56 +03:00
|
|
|
import { MatTab, MatTabGroup } from '@angular/material/tabs';
|
2019-08-21 18:18:46 +03:00
|
|
|
import { EntityTabsComponent } from '@home/components/entity/entity-tabs.component';
|
2021-02-04 13:06:22 +02:00
|
|
|
import { deepClone, mergeDeep } from '@core/utils';
|
2019-08-12 19:34:23 +03:00
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'tb-entity-details-panel',
|
|
|
|
|
templateUrl: './entity-details-panel.component.html',
|
2019-08-15 14:48:14 +03:00
|
|
|
styleUrls: ['./entity-details-panel.component.scss'],
|
|
|
|
|
changeDetection: ChangeDetectionStrategy.OnPush
|
2019-08-12 19:34:23 +03:00
|
|
|
})
|
2019-08-21 18:18:46 +03:00
|
|
|
export class EntityDetailsPanelComponent extends PageComponent implements OnInit, AfterViewInit, OnDestroy {
|
2019-08-12 19:34:23 +03:00
|
|
|
|
|
|
|
|
@Output()
|
|
|
|
|
closeEntityDetails = new EventEmitter<void>();
|
|
|
|
|
|
|
|
|
|
@Output()
|
|
|
|
|
entityUpdated = new EventEmitter<BaseData<HasId>>();
|
|
|
|
|
|
|
|
|
|
@Output()
|
|
|
|
|
entityAction = new EventEmitter<EntityAction<BaseData<HasId>>>();
|
|
|
|
|
|
2020-04-14 19:22:14 +03:00
|
|
|
entityComponentRef: ComponentRef<EntityComponent<BaseData<HasId>>>;
|
2019-08-12 19:34:23 +03:00
|
|
|
entityComponent: EntityComponent<BaseData<HasId>>;
|
2020-04-14 19:22:14 +03:00
|
|
|
|
|
|
|
|
entityTabsComponentRef: ComponentRef<EntityTabsComponent<BaseData<HasId>>>;
|
2019-08-21 18:18:46 +03:00
|
|
|
entityTabsComponent: EntityTabsComponent<BaseData<HasId>>;
|
2020-04-27 10:39:18 +03:00
|
|
|
detailsForm: FormGroup;
|
2019-08-12 19:34:23 +03:00
|
|
|
|
2020-04-08 19:31:08 +03:00
|
|
|
entitiesTableConfigValue: EntityTableConfig<BaseData<HasId>>;
|
2019-08-12 19:34:23 +03:00
|
|
|
isEditValue = false;
|
|
|
|
|
selectedTab = 0;
|
|
|
|
|
|
|
|
|
|
entityTypes = EntityType;
|
|
|
|
|
|
|
|
|
|
@ViewChild('entityDetailsForm', {static: true}) entityDetailsFormAnchor: TbAnchorComponent;
|
|
|
|
|
|
2019-08-21 18:18:46 +03:00
|
|
|
@ViewChild('entityTabs', {static: true}) entityTabsAnchor: TbAnchorComponent;
|
|
|
|
|
|
|
|
|
|
@ViewChild(MatTabGroup, {static: true}) matTabGroup: MatTabGroup;
|
|
|
|
|
|
|
|
|
|
@ViewChildren(MatTab) inclusiveTabs: QueryList<MatTab>;
|
|
|
|
|
|
2019-08-12 19:34:23 +03:00
|
|
|
translations: EntityTypeTranslation;
|
2020-04-07 17:06:04 +03:00
|
|
|
resources: EntityTypeResource<BaseData<HasId>>;
|
2020-04-06 17:07:37 +03:00
|
|
|
entity: BaseData<HasId>;
|
2020-04-07 17:06:04 +03:00
|
|
|
editingEntity: BaseData<HasId>;
|
2019-08-12 19:34:23 +03:00
|
|
|
|
|
|
|
|
private currentEntityId: HasId;
|
2020-04-14 19:22:14 +03:00
|
|
|
private subscriptions: Subscription[] = [];
|
|
|
|
|
private viewInited = false;
|
|
|
|
|
private pendingTabs: MatTab[];
|
2019-08-12 19:34:23 +03:00
|
|
|
|
|
|
|
|
constructor(protected store: Store<AppState>,
|
2020-04-07 17:06:04 +03:00
|
|
|
private injector: Injector,
|
|
|
|
|
private cd: ChangeDetectorRef,
|
2019-08-12 19:34:23 +03:00
|
|
|
private componentFactoryResolver: ComponentFactoryResolver) {
|
|
|
|
|
super(store);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
set entityId(entityId: HasId) {
|
|
|
|
|
if (entityId && entityId !== this.currentEntityId) {
|
|
|
|
|
this.currentEntityId = entityId;
|
|
|
|
|
this.reload();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-08 19:31:08 +03:00
|
|
|
@Input()
|
|
|
|
|
set entitiesTableConfig(entitiesTableConfig: EntityTableConfig<BaseData<HasId>>) {
|
2020-04-14 19:22:14 +03:00
|
|
|
if (this.entitiesTableConfigValue !== entitiesTableConfig) {
|
|
|
|
|
this.entitiesTableConfigValue = entitiesTableConfig;
|
|
|
|
|
if (this.entitiesTableConfigValue) {
|
|
|
|
|
this.currentEntityId = null;
|
|
|
|
|
this.isEdit = false;
|
|
|
|
|
this.entity = null;
|
|
|
|
|
this.init();
|
|
|
|
|
}
|
2020-04-08 19:31:08 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get entitiesTableConfig(): EntityTableConfig<BaseData<HasId>> {
|
|
|
|
|
return this.entitiesTableConfigValue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-12 19:34:23 +03:00
|
|
|
set isEdit(val: boolean) {
|
|
|
|
|
this.isEditValue = val;
|
2020-04-08 19:31:08 +03:00
|
|
|
if (this.entityComponent) {
|
|
|
|
|
this.entityComponent.isEdit = val;
|
|
|
|
|
}
|
2019-08-21 18:18:46 +03:00
|
|
|
if (this.entityTabsComponent) {
|
|
|
|
|
this.entityTabsComponent.isEdit = val;
|
|
|
|
|
}
|
2019-08-12 19:34:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get isEdit() {
|
|
|
|
|
return this.isEditValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
2020-04-14 19:22:14 +03:00
|
|
|
this.init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private init() {
|
2019-08-12 19:34:23 +03:00
|
|
|
this.translations = this.entitiesTableConfig.entityTranslations;
|
|
|
|
|
this.resources = this.entitiesTableConfig.entityResources;
|
|
|
|
|
this.buildEntityComponent();
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-14 19:22:14 +03:00
|
|
|
private clearSubscriptions() {
|
|
|
|
|
this.subscriptions.forEach((subscription) => {
|
|
|
|
|
subscription.unsubscribe();
|
|
|
|
|
});
|
|
|
|
|
this.subscriptions.length = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-12 19:34:23 +03:00
|
|
|
ngOnDestroy(): void {
|
|
|
|
|
super.ngOnDestroy();
|
2020-04-14 19:22:14 +03:00
|
|
|
this.clearSubscriptions();
|
2019-08-12 19:34:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buildEntityComponent() {
|
2020-04-14 19:22:14 +03:00
|
|
|
this.clearSubscriptions();
|
|
|
|
|
if (this.entityComponentRef) {
|
|
|
|
|
this.entityComponentRef.destroy();
|
|
|
|
|
this.entityComponentRef = null;
|
|
|
|
|
}
|
2019-08-12 19:34:23 +03:00
|
|
|
const componentFactory = this.componentFactoryResolver.resolveComponentFactory(this.entitiesTableConfig.entityComponent);
|
|
|
|
|
const viewContainerRef = this.entityDetailsFormAnchor.viewContainerRef;
|
|
|
|
|
viewContainerRef.clear();
|
2020-04-07 17:06:04 +03:00
|
|
|
const injector: Injector = Injector.create(
|
|
|
|
|
{
|
|
|
|
|
providers: [
|
|
|
|
|
{
|
|
|
|
|
provide: 'entity',
|
|
|
|
|
useValue: this.entity
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
provide: 'entitiesTableConfig',
|
|
|
|
|
useValue: this.entitiesTableConfig
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
parent: this.injector
|
|
|
|
|
}
|
|
|
|
|
);
|
2020-04-14 19:22:14 +03:00
|
|
|
this.entityComponentRef = viewContainerRef.createComponent(componentFactory, 0, injector);
|
|
|
|
|
this.entityComponent = this.entityComponentRef.instance;
|
2019-08-12 19:34:23 +03:00
|
|
|
this.entityComponent.isEdit = this.isEdit;
|
2020-04-27 10:39:18 +03:00
|
|
|
this.detailsForm = this.entityComponent.entityForm;
|
2020-04-14 19:22:14 +03:00
|
|
|
this.subscriptions.push(this.entityComponent.entityAction.subscribe((action) => {
|
2019-08-12 19:34:23 +03:00
|
|
|
this.entityAction.emit(action);
|
2020-04-14 19:22:14 +03:00
|
|
|
}));
|
2019-08-21 18:18:46 +03:00
|
|
|
this.buildEntityTabsComponent();
|
2020-04-14 19:22:14 +03:00
|
|
|
this.subscriptions.push(this.entityComponent.entityForm.valueChanges.subscribe(() => {
|
2020-04-07 17:06:04 +03:00
|
|
|
this.cd.detectChanges();
|
2020-04-14 19:22:14 +03:00
|
|
|
}));
|
2019-08-21 18:18:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buildEntityTabsComponent() {
|
2020-04-14 19:22:14 +03:00
|
|
|
if (this.entityTabsComponentRef) {
|
|
|
|
|
this.entityTabsComponentRef.destroy();
|
|
|
|
|
this.entityTabsComponentRef = null;
|
|
|
|
|
}
|
|
|
|
|
const viewContainerRef = this.entityTabsAnchor.viewContainerRef;
|
|
|
|
|
viewContainerRef.clear();
|
|
|
|
|
this.entityTabsComponent = null;
|
2019-08-21 18:18:46 +03:00
|
|
|
if (this.entitiesTableConfig.entityTabsComponent) {
|
|
|
|
|
const componentTabsFactory = this.componentFactoryResolver.resolveComponentFactory(this.entitiesTableConfig.entityTabsComponent);
|
2020-04-14 19:22:14 +03:00
|
|
|
this.entityTabsComponentRef = viewContainerRef.createComponent(componentTabsFactory);
|
|
|
|
|
this.entityTabsComponent = this.entityTabsComponentRef.instance;
|
2019-08-21 18:18:46 +03:00
|
|
|
this.entityTabsComponent.isEdit = this.isEdit;
|
|
|
|
|
this.entityTabsComponent.entitiesTableConfig = this.entitiesTableConfig;
|
2020-04-07 17:06:04 +03:00
|
|
|
this.entityTabsComponent.detailsForm = this.detailsForm;
|
2020-04-14 19:22:14 +03:00
|
|
|
this.subscriptions.push(this.entityTabsComponent.entityTabsChanged.subscribe(
|
|
|
|
|
(entityTabs) => {
|
|
|
|
|
if (entityTabs) {
|
|
|
|
|
if (this.viewInited) {
|
|
|
|
|
this.matTabGroup._tabs.reset([...this.inclusiveTabs.toArray(), ...entityTabs]);
|
|
|
|
|
this.matTabGroup._tabs.notifyOnChanges();
|
|
|
|
|
} else {
|
|
|
|
|
this.pendingTabs = entityTabs;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
));
|
2019-08-21 18:18:46 +03:00
|
|
|
}
|
2019-08-12 19:34:23 +03:00
|
|
|
}
|
|
|
|
|
|
2020-04-07 17:06:04 +03:00
|
|
|
hideDetailsTabs(): boolean {
|
|
|
|
|
return this.isEditValue && this.entitiesTableConfig.hideDetailsTabsOnEdit;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-12 19:34:23 +03:00
|
|
|
reload(): void {
|
|
|
|
|
this.isEdit = false;
|
|
|
|
|
this.entitiesTableConfig.loadEntity(this.currentEntityId).subscribe(
|
|
|
|
|
(entity) => {
|
|
|
|
|
this.entity = entity;
|
|
|
|
|
this.entityComponent.entity = entity;
|
2019-08-21 18:18:46 +03:00
|
|
|
if (this.entityTabsComponent) {
|
|
|
|
|
this.entityTabsComponent.entity = entity;
|
|
|
|
|
}
|
2019-08-12 19:34:23 +03:00
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onCloseEntityDetails() {
|
|
|
|
|
this.closeEntityDetails.emit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onToggleEditMode(isEdit: boolean) {
|
2020-09-10 19:37:14 +03:00
|
|
|
if (!isEdit) {
|
2019-08-12 19:34:23 +03:00
|
|
|
this.entityComponent.entity = this.entity;
|
2019-08-21 18:18:46 +03:00
|
|
|
if (this.entityTabsComponent) {
|
|
|
|
|
this.entityTabsComponent.entity = this.entity;
|
|
|
|
|
}
|
2020-09-10 19:37:14 +03:00
|
|
|
this.isEdit = isEdit;
|
2019-08-12 19:34:23 +03:00
|
|
|
} else {
|
2020-09-10 19:37:14 +03:00
|
|
|
this.isEdit = isEdit;
|
2020-04-07 17:06:04 +03:00
|
|
|
this.editingEntity = deepClone(this.entity);
|
|
|
|
|
this.entityComponent.entity = this.editingEntity;
|
|
|
|
|
if (this.entityTabsComponent) {
|
|
|
|
|
this.entityTabsComponent.entity = this.editingEntity;
|
|
|
|
|
}
|
|
|
|
|
if (this.entitiesTableConfig.hideDetailsTabsOnEdit) {
|
|
|
|
|
this.selectedTab = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
helpLinkId(): string {
|
|
|
|
|
if (this.resources.helpLinkIdForEntity && this.entityComponent.entityForm) {
|
|
|
|
|
return this.resources.helpLinkIdForEntity(this.entityComponent.entityForm.getRawValue());
|
|
|
|
|
} else {
|
|
|
|
|
return this.resources.helpLinkId;
|
2019-08-12 19:34:23 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
saveEntity() {
|
|
|
|
|
if (this.detailsForm.valid) {
|
2021-02-04 13:06:22 +02:00
|
|
|
const editingEntity = mergeDeep(this.editingEntity, this.entityComponent.entityFormValue());
|
2019-08-12 19:34:23 +03:00
|
|
|
this.entitiesTableConfig.saveEntity(editingEntity).subscribe(
|
|
|
|
|
(entity) => {
|
|
|
|
|
this.entity = entity;
|
|
|
|
|
this.entityComponent.entity = entity;
|
2019-08-21 18:18:46 +03:00
|
|
|
if (this.entityTabsComponent) {
|
|
|
|
|
this.entityTabsComponent.entity = entity;
|
|
|
|
|
}
|
2019-08-12 19:34:23 +03:00
|
|
|
this.isEdit = false;
|
|
|
|
|
this.entityUpdated.emit(this.entity);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-21 18:18:46 +03:00
|
|
|
ngAfterViewInit(): void {
|
2020-04-14 19:22:14 +03:00
|
|
|
this.viewInited = true;
|
|
|
|
|
if (this.pendingTabs) {
|
|
|
|
|
this.matTabGroup._tabs.reset([...this.inclusiveTabs.toArray(), ...this.pendingTabs]);
|
|
|
|
|
this.matTabGroup._tabs.notifyOnChanges();
|
|
|
|
|
this.pendingTabs = null;
|
2019-08-21 18:18:46 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-12 19:34:23 +03:00
|
|
|
}
|