thingsboard/ui-ngx/src/app/modules/home/components/entity/entity-details-panel.component.ts

214 lines
6.8 KiB
TypeScript
Raw Normal View History

2019-08-12 19:34:23 +03:00
///
2020-02-20 10:26:43 +02:00
/// Copyright © 2016-2020 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,
ChangeDetectionStrategy,
2019-08-12 19:34:23 +03:00
Component,
ComponentFactoryResolver,
EventEmitter,
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';
2019-08-12 19:34:23 +03:00
import { NgForm } 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';
2020-04-06 11:46:56 +03:00
import { WidgetsBundle } from '@shared/models/widgets-bundle.model';
2019-08-12 19:34:23 +03:00
@Component({
selector: 'tb-entity-details-panel',
templateUrl: './entity-details-panel.component.html',
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
@Input() entitiesTableConfig: EntityTableConfig<BaseData<HasId>>;
@Output()
closeEntityDetails = new EventEmitter<void>();
@Output()
entityUpdated = new EventEmitter<BaseData<HasId>>();
@Output()
entityAction = new EventEmitter<EntityAction<BaseData<HasId>>>();
entityComponent: EntityComponent<BaseData<HasId>>;
2019-08-21 18:18:46 +03:00
entityTabsComponent: EntityTabsComponent<BaseData<HasId>>;
2019-08-12 19:34:23 +03:00
detailsForm: NgForm;
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;
resources: EntityTypeResource;
2020-04-06 11:46:56 +03:00
entity: BaseData<HasId> | WidgetsBundle;
2019-08-12 19:34:23 +03:00
private currentEntityId: HasId;
private entityActionSubscription: Subscription;
constructor(protected store: Store<AppState>,
private componentFactoryResolver: ComponentFactoryResolver) {
super(store);
}
@Input()
set entityId(entityId: HasId) {
if (entityId && entityId !== this.currentEntityId) {
this.currentEntityId = entityId;
this.reload();
}
}
set isEdit(val: boolean) {
this.isEditValue = val;
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 {
this.translations = this.entitiesTableConfig.entityTranslations;
this.resources = this.entitiesTableConfig.entityResources;
this.buildEntityComponent();
}
ngOnDestroy(): void {
super.ngOnDestroy();
if (this.entityActionSubscription) {
this.entityActionSubscription.unsubscribe();
}
}
buildEntityComponent() {
const componentFactory = this.componentFactoryResolver.resolveComponentFactory(this.entitiesTableConfig.entityComponent);
const viewContainerRef = this.entityDetailsFormAnchor.viewContainerRef;
viewContainerRef.clear();
const componentRef = viewContainerRef.createComponent(componentFactory);
this.entityComponent = componentRef.instance;
this.entityComponent.isEdit = this.isEdit;
this.entityComponent.entitiesTableConfig = this.entitiesTableConfig;
this.detailsForm = this.entityComponent.entityNgForm;
this.entityActionSubscription = this.entityComponent.entityAction.subscribe((action) => {
this.entityAction.emit(action);
});
2019-08-21 18:18:46 +03:00
this.buildEntityTabsComponent();
}
buildEntityTabsComponent() {
if (this.entitiesTableConfig.entityTabsComponent) {
const componentTabsFactory = this.componentFactoryResolver.resolveComponentFactory(this.entitiesTableConfig.entityTabsComponent);
const viewContainerRef = this.entityTabsAnchor.viewContainerRef;
viewContainerRef.clear();
const componentTabsRef = viewContainerRef.createComponent(componentTabsFactory);
this.entityTabsComponent = componentTabsRef.instance;
this.entityTabsComponent.isEdit = this.isEdit;
this.entityTabsComponent.entitiesTableConfig = this.entitiesTableConfig;
}
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) {
this.isEdit = isEdit;
if (!this.isEdit) {
this.entityComponent.entity = this.entity;
2019-08-21 18:18:46 +03:00
if (this.entityTabsComponent) {
this.entityTabsComponent.entity = this.entity;
}
2019-08-12 19:34:23 +03:00
} else {
this.selectedTab = 0;
}
}
saveEntity() {
if (this.detailsForm.valid) {
const editingEntity = {...this.entity, ...this.entityComponent.entityFormValue()};
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 {
if (this.entityTabsComponent) {
this.entityTabsComponent.entityTabsChanged.subscribe(
(entityTabs) => {
if (entityTabs) {
this.matTabGroup._tabs.reset([...this.inclusiveTabs.toArray(), ...entityTabs]);
this.matTabGroup._tabs.notifyOnChanges();
}
}
);
}
}
2019-08-12 19:34:23 +03:00
}