thingsboard/ui-ngx/src/app/modules/home/components/widget/lib/edges-overview-widget.component.ts

213 lines
7.2 KiB
TypeScript
Raw Normal View History

2020-12-29 14:44:53 +02:00
///
/// Copyright © 2016-2020 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.
///
2020-12-31 12:01:11 +02:00
import {
AfterViewInit,
ChangeDetectorRef,
Component,
ElementRef,
Input,
OnInit,
ViewChild,
ViewContainerRef
} from '@angular/core';
2020-12-29 14:44:53 +02:00
import { PageComponent } from '@shared/components/page.component';
import { Store } from '@ngrx/store';
import { AppState } from '@core/core.state';
import { WidgetAction, WidgetContext } from '@home/models/widget-component.models';
2020-12-31 12:01:11 +02:00
import { WidgetConfig } from '@shared/models/widget.models';
import { IWidgetSubscription } from '@core/api/widget-api.models';
2020-12-29 14:44:53 +02:00
import { UtilsService } from '@core/services/utils.service';
import cssjs from '@core/css/css';
2020-12-31 12:01:11 +02:00
import { fromEvent } from 'rxjs';
import { debounceTime, distinctUntilChanged, tap } from 'rxjs/operators';
2020-12-29 14:44:53 +02:00
import { constructTableCssString } from '@home/components/widget/lib/table-widget.models';
import { Overlay } from '@angular/cdk/overlay';
import {
LoadNodesCallback,
2020-12-31 12:01:11 +02:00
NavTreeEditCallbacks
2020-12-29 14:44:53 +02:00
} from '@shared/components/nav-tree.component';
import { EntityType } from '@shared/models/entity-type.models';
2020-12-31 12:01:11 +02:00
import { hashCode } from '@core/utils';
2020-12-29 14:44:53 +02:00
import {
2020-12-31 12:01:11 +02:00
EdgeGroupNodeData,
2020-12-29 14:44:53 +02:00
edgeGroupsNodeText,
2020-12-31 12:01:11 +02:00
edgeGroupsTypes, EntityNodeData,
2020-12-29 14:44:53 +02:00
edgeNodeText,
2020-12-31 12:01:11 +02:00
EdgeOverviewNode, EntityNodeDatasource
2020-12-29 14:44:53 +02:00
} from '@home/components/widget/lib/edges-overview-widget.models';
import { EdgeService } from "@core/http/edge.service";
import { EntityService } from "@core/http/entity.service";
import { TranslateService } from "@ngx-translate/core";
import { PageLink } from "@shared/models/page/page-link";
2020-12-31 12:01:11 +02:00
import { Edge } from "@shared/models/edge.models";
2020-12-30 15:34:43 +02:00
import { BaseData } from "@shared/models/base-data";
import { EntityId } from "@shared/models/id/entity-id";
2020-12-29 14:44:53 +02:00
@Component({
selector: 'tb-edges-overview-widget',
templateUrl: './edges-overview-widget.component.html',
styleUrls: ['./edges-overview-widget.component.scss']
})
2020-12-31 12:01:11 +02:00
export class EdgesOverviewWidgetComponent extends PageComponent implements OnInit {
2020-12-29 14:44:53 +02:00
@Input()
ctx: WidgetContext;
@ViewChild('searchInput') searchInputField: ElementRef;
public toastTargetId = 'edges-overview-' + this.utils.guid();
2020-12-30 15:34:43 +02:00
public customerTitle: string = null;
2020-12-29 14:44:53 +02:00
private widgetConfig: WidgetConfig;
private subscription: IWidgetSubscription;
2020-12-31 12:01:11 +02:00
private datasources: Array<EntityNodeDatasource>;
2020-12-29 14:44:53 +02:00
private nodeIdCounter = 0;
private edgeNodesMap: {[parentNodeId: string]: {[edgeId: string]: string}} = {};
private edgeGroupsNodesMap: {[edgeNodeId: string]: {[groupType: string]: string}} = {};
constructor(protected store: Store<AppState>,
private elementRef: ElementRef,
private edgeService: EdgeService,
private entityService: EntityService,
private translateService: TranslateService,
private overlay: Overlay,
private viewContainerRef: ViewContainerRef,
2020-12-30 15:34:43 +02:00
private utils: UtilsService,
private cd: ChangeDetectorRef) {
2020-12-29 14:44:53 +02:00
super(store);
}
ngOnInit(): void {
this.widgetConfig = this.ctx.widgetConfig;
this.subscription = this.ctx.defaultSubscription;
2020-12-31 12:01:11 +02:00
this.datasources = this.subscription.datasources as Array<EntityNodeDatasource>;
if (this.datasources.length > 0 && this.datasources[0].entity.id.entityType === EntityType.EDGE) {
let selectedEdge = this.datasources[0].entity;
this.getCustomerTitle(selectedEdge.id.id);
this.ctx.widgetTitle = selectedEdge.name;
}
2020-12-29 14:44:53 +02:00
this.initializeConfig();
this.ctx.updateWidgetParams();
}
private initializeConfig() {
const cssString = constructTableCssString(this.widgetConfig);
const cssParser = new cssjs();
cssParser.testMode = false;
const namespace = 'edges-overview-' + hashCode(cssString);
cssParser.cssPreviewNamespace = namespace;
cssParser.createStyleElement(namespace, cssString);
$(this.elementRef.nativeElement).addClass(namespace);
}
public loadNodes: LoadNodesCallback = (node, cb) => {
2020-12-31 12:01:11 +02:00
var selectedEdge: BaseData<EntityId> = null;
if (this.datasources.length > 0 && this.datasources[0].entity && this.datasources[0].entity.id.entityType === EntityType.EDGE) {
selectedEdge = this.datasources[0].entity;
}
if (node.id === '#' && selectedEdge) {
cb(this.loadNodesForEdge(selectedEdge.id.id, selectedEdge));
} else if (node.data && node.data.type === 'edgeGroup') {
2020-12-29 14:44:53 +02:00
const pageLink = new PageLink(100);
this.entityService.getAssignedToEdgeEntitiesByType(node, pageLink).subscribe(
(entities) => {
if (entities.data.length > 0) {
cb(this.edgesToNodes(node.id, entities.data));
} else {
cb([]);
}
}
)
2020-12-31 12:01:11 +02:00
} else {
cb([]);
2020-12-29 14:44:53 +02:00
}
}
2020-12-30 15:34:43 +02:00
private loadNodesForEdge(parentNodeId: string, edge: any): EdgeOverviewNode[] {
2020-12-29 14:44:53 +02:00
const nodes: EdgeOverviewNode[] = [];
const nodesMap = {};
this.edgeGroupsNodesMap[parentNodeId] = nodesMap;
edgeGroupsTypes.forEach((entityType) => {
const node: EdgeOverviewNode = {
id: (++this.nodeIdCounter)+'',
icon: false,
text: edgeGroupsNodeText(this.translateService, entityType),
children: true,
data: {
2020-12-31 12:01:11 +02:00
type: 'edgeGroup',
2020-12-29 14:44:53 +02:00
entityType,
edge,
internalId: edge.id.id + '_' + entityType
2020-12-31 12:01:11 +02:00
} as EdgeGroupNodeData
2020-12-29 14:44:53 +02:00
};
nodes.push(node);
nodesMap[entityType] = node.id;
});
return nodes;
}
private createEdgeNode(parentNodeId: string, edge: Edge): EdgeOverviewNode {
let nodesMap = this.edgeNodesMap[parentNodeId];
if (!nodesMap) {
nodesMap = {};
this.edgeNodesMap[parentNodeId] = nodesMap;
}
const node: EdgeOverviewNode = {
id: (++this.nodeIdCounter)+'',
icon: false,
text: edgeNodeText(edge),
children: parentNodeId === '#',
state: {
disabled: false
},
data: {
2020-12-31 12:01:11 +02:00
type: 'entity',
2020-12-29 14:44:53 +02:00
entity: edge,
internalId: edge.id.id
2020-12-31 12:01:11 +02:00
} as EntityNodeData
2020-12-29 14:44:53 +02:00
};
nodesMap[edge.id.id] = node.id;
return node;
}
private edgesToNodes(parentNodeId: string, edges: Array<Edge>): EdgeOverviewNode[] {
const nodes: EdgeOverviewNode[] = [];
this.edgeNodesMap[parentNodeId] = {};
if (edges) {
edges.forEach((edge) => {
const node = this.createEdgeNode(parentNodeId, edge);
nodes.push(node);
});
}
return nodes;
}
2020-12-30 15:34:43 +02:00
private getCustomerTitle(edgeId) {
this.edgeService.getEdgeInfo(edgeId).subscribe(
(edge) => {
if (edge.customerTitle) {
this.customerTitle = this.translateService.instant('edge.assigned-to-customer') + ': ' + edge.customerTitle;
} else {
this.customerTitle = null;
2020-12-29 14:44:53 +02:00
}
2020-12-30 15:34:43 +02:00
this.cd.detectChanges();
});
2020-12-29 14:44:53 +02:00
}
}