thingsboard/ui-ngx/src/app/modules/home/pages/asset/assets-table-config.resolver.ts

566 lines
21 KiB
TypeScript
Raw Normal View History

2019-08-16 21:22:54 +03:00
///
2024-01-09 10:46:16 +02:00
/// Copyright © 2016-2024 The Thingsboard Authors
2019-08-16 21:22:54 +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 { Injectable } from '@angular/core';
2019-08-16 21:22:54 +03:00
import { ActivatedRouteSnapshot, Resolve, Router } from '@angular/router';
2019-08-16 21:22:54 +03:00
import {
CellActionDescriptor,
checkBoxCell,
DateEntityTableColumn,
EntityTableColumn,
EntityTableConfig,
GroupActionDescriptor,
HeaderActionDescriptor
2019-08-21 18:18:46 +03:00
} from '@home/models/entity/entities-table-config.models';
import { TranslateService } from '@ngx-translate/core';
import { DatePipe } from '@angular/common';
import { EntityType, entityTypeResources, entityTypeTranslations } from '@shared/models/entity-type.models';
import { EntityAction } from '@home/models/entity/entity-component.models';
import { forkJoin, Observable, of } from 'rxjs';
import { select, Store } from '@ngrx/store';
import { selectAuthUser } from '@core/auth/auth.selectors';
import { map, mergeMap, take, tap } from 'rxjs/operators';
import { AppState } from '@core/core.state';
import { Authority } from '@app/shared/models/authority.enum';
import { CustomerService } from '@core/http/customer.service';
import { Customer } from '@app/shared/models/customer.model';
import { NULL_UUID } from '@shared/models/id/has-uuid';
import { BroadcastService } from '@core/services/broadcast.service';
2020-02-10 13:15:29 +02:00
import { MatDialog } from '@angular/material/dialog';
import { DialogService } from '@core/services/dialog.service';
2019-08-16 21:22:54 +03:00
import {
AssignToCustomerDialogComponent,
AssignToCustomerDialogData
} from '@modules/home/dialogs/assign-to-customer-dialog.component';
import {
AddEntitiesToCustomerDialogComponent,
AddEntitiesToCustomerDialogData
} from '../../dialogs/add-entities-to-customer-dialog.component';
import { Asset, AssetInfo } from '@app/shared/models/asset.models';
import { AssetService } from '@app/core/http/asset.service';
import { AssetComponent } from '@modules/home/pages/asset/asset.component';
import { AssetTableHeaderComponent } from '@modules/home/pages/asset/asset-table-header.component';
import { AssetId } from '@app/shared/models/id/asset-id';
2019-08-22 13:34:15 +03:00
import { AssetTabsComponent } from '@home/pages/asset/asset-tabs.component';
import { HomeDialogsService } from '@home/dialogs/home-dialogs.service';
import { DeviceInfo } from '@shared/models/device.models';
import { EdgeService } from '@core/http/edge.service';
import {
AddEntitiesToEdgeDialogComponent,
AddEntitiesToEdgeDialogData
} from '@home/dialogs/add-entities-to-edge-dialog.component';
2019-08-16 21:22:54 +03:00
@Injectable()
export class AssetsTableConfigResolver implements Resolve<EntityTableConfig<AssetInfo>> {
private readonly config: EntityTableConfig<AssetInfo> = new EntityTableConfig<AssetInfo>();
private customerId: string;
constructor(private store: Store<AppState>,
private broadcast: BroadcastService,
private assetService: AssetService,
private customerService: CustomerService,
private edgeService: EdgeService,
2019-08-16 21:22:54 +03:00
private dialogService: DialogService,
private homeDialogs: HomeDialogsService,
2019-08-16 21:22:54 +03:00
private translate: TranslateService,
private datePipe: DatePipe,
private router: Router,
private dialog: MatDialog) {
this.config.entityType = EntityType.ASSET;
this.config.entityComponent = AssetComponent;
2019-08-22 13:34:15 +03:00
this.config.entityTabsComponent = AssetTabsComponent;
2019-08-16 21:22:54 +03:00
this.config.entityTranslations = entityTypeTranslations.get(EntityType.ASSET);
this.config.entityResources = entityTypeResources.get(EntityType.ASSET);
this.config.deleteEntityTitle = asset => this.translate.instant('asset.delete-asset-title', {assetName: asset.name});
2019-08-16 21:22:54 +03:00
this.config.deleteEntityContent = () => this.translate.instant('asset.delete-asset-text');
this.config.deleteEntitiesTitle = count => this.translate.instant('asset.delete-assets-title', {count});
this.config.deleteEntitiesContent = () => this.translate.instant('asset.delete-assets-text');
this.config.loadEntity = id => this.assetService.getAssetInfo(id.id);
this.config.saveEntity = asset => {
return this.assetService.saveAsset(asset).pipe(
tap(() => {
this.broadcast.broadcast('assetSaved');
}),
mergeMap((savedAsset) => this.assetService.getAssetInfo(savedAsset.id.id)
));
};
this.config.onEntityAction = action => this.onAssetAction(action, this.config);
2022-01-19 17:02:18 +02:00
this.config.detailsReadonly = () => (this.config.componentsData.assetScope === 'customer_user' ||
this.config.componentsData.assetScope === 'edge_customer_user');
2019-08-16 21:22:54 +03:00
this.config.headerComponent = AssetTableHeaderComponent;
}
resolve(route: ActivatedRouteSnapshot): Observable<EntityTableConfig<AssetInfo>> {
const routeParams = route.params;
this.config.componentsData = {
assetScope: route.data.assetsType,
2022-09-26 18:10:12 +03:00
assetProfileId: null,
assetType: '',
edgeId: routeParams.edgeId
2019-08-16 21:22:54 +03:00
};
this.customerId = routeParams.customerId;
return this.store.pipe(select(selectAuthUser), take(1)).pipe(
tap((authUser) => {
if (authUser.authority === Authority.CUSTOMER_USER) {
if (route.data.assetsType === 'edge') {
this.config.componentsData.assetScope = 'edge_customer_user';
} else {
this.config.componentsData.assetScope = 'customer_user';
}
2019-08-16 21:22:54 +03:00
this.customerId = authUser.customerId;
}
}),
mergeMap(() =>
this.customerId ? this.customerService.getCustomer(this.customerId) : of(null as Customer)
),
map((parentCustomer) => {
if (parentCustomer) {
if (parentCustomer.additionalInfo && parentCustomer.additionalInfo.isPublic) {
this.config.tableTitle = this.translate.instant('customer.public-assets');
} else {
this.config.tableTitle = parentCustomer.title + ': ' + this.translate.instant('asset.assets');
}
2020-06-22 12:08:31 +03:00
} else if (this.config.componentsData.assetScope === 'edge') {
this.edgeService.getEdge(this.config.componentsData.edgeId).subscribe(
edge => this.config.tableTitle = edge.name + ': ' + this.translate.instant('asset.assets')
);
2020-06-22 12:08:31 +03:00
} else {
2019-08-16 21:22:54 +03:00
this.config.tableTitle = this.translate.instant('asset.assets');
}
this.config.columns = this.configureColumns(this.config.componentsData.assetScope);
this.configureEntityFunctions(this.config.componentsData.assetScope);
this.config.cellActionDescriptors = this.configureCellActions(this.config.componentsData.assetScope);
this.config.groupActionDescriptors = this.configureGroupActions(this.config.componentsData.assetScope);
this.config.addActionDescriptors = this.configureAddActions(this.config.componentsData.assetScope);
this.config.addEnabled = !(this.config.componentsData.assetScope === 'customer_user' || this.config.componentsData.assetScope === 'edge_customer_user');
2019-08-16 21:22:54 +03:00
this.config.entitiesDeleteEnabled = this.config.componentsData.assetScope === 'tenant';
this.config.deleteEnabled = () => this.config.componentsData.assetScope === 'tenant';
return this.config;
})
);
}
configureColumns(assetScope: string): Array<EntityTableColumn<AssetInfo>> {
const columns: Array<EntityTableColumn<AssetInfo>> = [
2020-04-03 21:14:39 +03:00
new DateEntityTableColumn<AssetInfo>('createdTime', 'common.created-time', this.datePipe, '150px'),
new EntityTableColumn<AssetInfo>('name', 'asset.name', '25%'),
2022-09-26 18:10:12 +03:00
new EntityTableColumn<AssetInfo>('assetProfileName', 'asset-profile.asset-profile', '25%'),
new EntityTableColumn<AssetInfo>('label', 'asset.label', '25%'),
2019-08-16 21:22:54 +03:00
];
if (assetScope === 'tenant') {
columns.push(
new EntityTableColumn<AssetInfo>('customerTitle', 'customer.customer', '25%'),
2019-08-16 21:22:54 +03:00
new EntityTableColumn<AssetInfo>('customerIsPublic', 'asset.public', '60px',
entity => {
return checkBoxCell(entity.customerIsPublic);
}, () => ({}), false),
);
}
return columns;
}
configureEntityFunctions(assetScope: string): void {
if (assetScope === 'tenant') {
this.config.entitiesFetchFunction = pageLink =>
2022-09-26 18:10:12 +03:00
this.assetService.getTenantAssetInfosByAssetProfileId(pageLink, this.config.componentsData.assetProfileId !== null ?
this.config.componentsData.assetProfileId.id : '');
2019-08-16 21:22:54 +03:00
this.config.deleteEntity = id => this.assetService.deleteAsset(id.id);
} else if (assetScope === 'edge' || assetScope === 'edge_customer_user') {
this.config.entitiesFetchFunction = pageLink =>
this.assetService.getEdgeAssets(this.config.componentsData.edgeId, pageLink, this.config.componentsData.assetType);
2019-08-16 21:22:54 +03:00
} else {
this.config.entitiesFetchFunction = pageLink =>
2022-09-26 18:10:12 +03:00
this.assetService.getCustomerAssetInfosByAssetProfileId(this.customerId, pageLink,
this.config.componentsData.assetProfileId !== null ? this.config.componentsData.assetProfileId.id : '');
2019-08-16 21:22:54 +03:00
this.config.deleteEntity = id => this.assetService.unassignAssetFromCustomer(id.id);
}
}
configureCellActions(assetScope: string): Array<CellActionDescriptor<AssetInfo>> {
const actions: Array<CellActionDescriptor<AssetInfo>> = [];
if (assetScope === 'tenant') {
actions.push(
{
name: this.translate.instant('asset.make-public'),
icon: 'share',
isEnabled: (entity) => (!entity.customerId || entity.customerId.id === NULL_UUID),
onAction: ($event, entity) => this.makePublic($event, entity)
},
{
name: this.translate.instant('asset.assign-to-customer'),
icon: 'assignment_ind',
isEnabled: (entity) => (!entity.customerId || entity.customerId.id === NULL_UUID),
onAction: ($event, entity) => this.assignToCustomer($event, [entity.id])
},
{
name: this.translate.instant('asset.unassign-from-customer'),
icon: 'assignment_return',
isEnabled: (entity) => (entity.customerId && entity.customerId.id !== NULL_UUID && !entity.customerIsPublic),
onAction: ($event, entity) => this.unassignFromCustomer($event, entity)
},
{
name: this.translate.instant('asset.make-private'),
icon: 'reply',
isEnabled: (entity) => (entity.customerId && entity.customerId.id !== NULL_UUID && entity.customerIsPublic),
onAction: ($event, entity) => this.unassignFromCustomer($event, entity)
}
);
}
if (assetScope === 'customer') {
actions.push(
{
name: this.translate.instant('asset.unassign-from-customer'),
icon: 'assignment_return',
isEnabled: (entity) => (entity.customerId && entity.customerId.id !== NULL_UUID && !entity.customerIsPublic),
onAction: ($event, entity) => this.unassignFromCustomer($event, entity)
},
{
name: this.translate.instant('asset.make-private'),
icon: 'reply',
isEnabled: (entity) => (entity.customerId && entity.customerId.id !== NULL_UUID && entity.customerIsPublic),
onAction: ($event, entity) => this.unassignFromCustomer($event, entity)
}
);
}
if (assetScope === 'edge') {
actions.push(
{
2020-06-22 12:08:31 +03:00
name: this.translate.instant('edge.unassign-from-edge'),
icon: 'assignment_return',
2020-06-22 12:08:31 +03:00
isEnabled: (entity) => true,
onAction: ($event, entity) => this.unassignFromEdge($event, entity)
}
);
}
2019-08-16 21:22:54 +03:00
return actions;
}
configureGroupActions(assetScope: string): Array<GroupActionDescriptor<AssetInfo>> {
const actions: Array<GroupActionDescriptor<AssetInfo>> = [];
if (assetScope === 'tenant') {
actions.push(
{
name: this.translate.instant('asset.assign-assets'),
icon: 'assignment_ind',
isEnabled: true,
onAction: ($event, entities) => this.assignToCustomer($event, entities.map((entity) => entity.id))
}
);
}
2020-06-22 12:08:31 +03:00
if (assetScope === 'customer') {
actions.push(
{
2020-06-22 12:08:31 +03:00
name: this.translate.instant('asset.unassign-assets'),
icon: 'assignment_return',
isEnabled: true,
2020-06-22 12:08:31 +03:00
onAction: ($event, entities) => this.unassignAssetsFromCustomer($event, entities)
}
);
}
2020-06-22 12:08:31 +03:00
if (assetScope === 'edge') {
2019-08-16 21:22:54 +03:00
actions.push(
{
2020-06-22 12:08:31 +03:00
name: this.translate.instant('asset.unassign-assets-from-edge'),
icon: 'assignment_return',
2019-08-16 21:22:54 +03:00
isEnabled: true,
2020-06-22 12:08:31 +03:00
onAction: ($event, entities) => this.unassignAssetsFromEdge($event, entities)
2019-08-16 21:22:54 +03:00
}
);
}
return actions;
}
configureAddActions(assetScope: string): Array<HeaderActionDescriptor> {
const actions: Array<HeaderActionDescriptor> = [];
if (assetScope === 'tenant') {
actions.push(
{
name: this.translate.instant('asset.add-asset-text'),
icon: 'insert_drive_file',
isEnabled: () => true,
2022-01-19 17:02:18 +02:00
onAction: ($event) => this.config.getTable().addEntity($event)
2019-08-16 21:22:54 +03:00
},
{
name: this.translate.instant('asset.import'),
icon: 'file_upload',
isEnabled: () => true,
onAction: ($event) => this.importAssets($event)
}
);
}
if (assetScope === 'customer') {
actions.push(
{
name: this.translate.instant('asset.assign-new-asset'),
icon: 'add',
isEnabled: () => true,
onAction: ($event) => this.addAssetsToCustomer($event)
}
);
}
if (assetScope === 'edge') {
actions.push(
{
name: this.translate.instant('asset.assign-new-asset'),
icon: 'add',
isEnabled: () => true,
onAction: ($event) => this.addAssetsToEdge($event)
}
);
}
2019-08-16 21:22:54 +03:00
return actions;
}
importAssets($event: Event) {
this.homeDialogs.importEntities(EntityType.ASSET).subscribe((res) => {
if (res) {
this.broadcast.broadcast('assetSaved');
2022-01-19 17:02:18 +02:00
this.config.updateData();
}
});
2019-08-16 21:22:54 +03:00
}
private openAsset($event: Event, asset: Asset, config: EntityTableConfig<AssetInfo>) {
2021-12-28 12:01:26 +02:00
if ($event) {
$event.stopPropagation();
}
2022-01-19 17:02:18 +02:00
const url = this.router.createUrlTree([asset.id.id], {relativeTo: config.getActivatedRoute()});
this.router.navigateByUrl(url);
2021-12-28 12:01:26 +02:00
}
2019-08-16 21:22:54 +03:00
addAssetsToCustomer($event: Event) {
if ($event) {
$event.stopPropagation();
}
this.dialog.open<AddEntitiesToCustomerDialogComponent, AddEntitiesToCustomerDialogData,
boolean>(AddEntitiesToCustomerDialogComponent, {
disableClose: true,
panelClass: ['tb-dialog', 'tb-fullscreen-dialog'],
data: {
customerId: this.customerId,
entityType: EntityType.ASSET
}
}).afterClosed()
.subscribe((res) => {
if (res) {
2022-01-19 17:02:18 +02:00
this.config.updateData();
2019-08-16 21:22:54 +03:00
}
});
}
makePublic($event: Event, asset: Asset) {
if ($event) {
$event.stopPropagation();
}
this.dialogService.confirm(
this.translate.instant('asset.make-public-asset-title', {assetName: asset.name}),
this.translate.instant('asset.make-public-asset-text'),
this.translate.instant('action.no'),
this.translate.instant('action.yes'),
true
).subscribe((res) => {
if (res) {
this.assetService.makeAssetPublic(asset.id.id).subscribe(
() => {
2022-01-19 17:02:18 +02:00
this.config.updateData();
2019-08-16 21:22:54 +03:00
}
);
}
}
);
}
assignToCustomer($event: Event, assetIds: Array<AssetId>) {
if ($event) {
$event.stopPropagation();
}
this.dialog.open<AssignToCustomerDialogComponent, AssignToCustomerDialogData,
boolean>(AssignToCustomerDialogComponent, {
disableClose: true,
panelClass: ['tb-dialog', 'tb-fullscreen-dialog'],
data: {
entityIds: assetIds,
entityType: EntityType.ASSET
}
}).afterClosed()
.subscribe((res) => {
if (res) {
2022-01-19 17:02:18 +02:00
this.config.updateData();
2019-08-16 21:22:54 +03:00
}
});
}
unassignFromCustomer($event: Event, asset: AssetInfo) {
if ($event) {
$event.stopPropagation();
}
const isPublic = asset.customerIsPublic;
let title;
let content;
if (isPublic) {
title = this.translate.instant('asset.make-private-asset-title', {assetName: asset.name});
content = this.translate.instant('asset.make-private-asset-text');
} else {
title = this.translate.instant('asset.unassign-asset-title', {assetName: asset.name});
content = this.translate.instant('asset.unassign-asset-text');
}
this.dialogService.confirm(
title,
content,
this.translate.instant('action.no'),
this.translate.instant('action.yes'),
true
).subscribe((res) => {
if (res) {
this.assetService.unassignAssetFromCustomer(asset.id.id).subscribe(
() => {
2022-01-19 17:02:18 +02:00
this.config.updateData(this.config.componentsData.assetScope !== 'tenant');
2019-08-16 21:22:54 +03:00
}
);
}
}
);
}
unassignAssetsFromCustomer($event: Event, assets: Array<AssetInfo>) {
if ($event) {
$event.stopPropagation();
}
this.dialogService.confirm(
this.translate.instant('asset.unassign-assets-title', {count: assets.length}),
this.translate.instant('asset.unassign-assets-text'),
this.translate.instant('action.no'),
this.translate.instant('action.yes'),
true
).subscribe((res) => {
if (res) {
const tasks: Observable<any>[] = [];
assets.forEach(
(asset) => {
tasks.push(this.assetService.unassignAssetFromCustomer(asset.id.id));
}
);
forkJoin(tasks).subscribe(
() => {
2022-01-19 17:02:18 +02:00
this.config.updateData();
2019-08-16 21:22:54 +03:00
}
);
}
}
);
}
onAssetAction(action: EntityAction<AssetInfo>, config: EntityTableConfig<AssetInfo>): boolean {
2019-08-16 21:22:54 +03:00
switch (action.action) {
2021-12-28 12:01:26 +02:00
case 'open':
this.openAsset(action.event, action.entity, config);
2021-12-28 12:01:26 +02:00
return true;
2019-08-16 21:22:54 +03:00
case 'makePublic':
this.makePublic(action.event, action.entity);
return true;
case 'assignToCustomer':
this.assignToCustomer(action.event, [action.entity.id]);
return true;
case 'unassignFromCustomer':
this.unassignFromCustomer(action.event, action.entity);
return true;
case 'unassignFromEdge':
this.unassignFromEdge(action.event, action.entity);
return true;
2019-08-16 21:22:54 +03:00
}
return false;
}
2020-06-22 12:08:31 +03:00
addAssetsToEdge($event: Event) {
if ($event) {
$event.stopPropagation();
}
this.dialog.open<AddEntitiesToEdgeDialogComponent, AddEntitiesToEdgeDialogData,
boolean>(AddEntitiesToEdgeDialogComponent, {
disableClose: true,
panelClass: ['tb-dialog', 'tb-fullscreen-dialog'],
data: {
edgeId: this.config.componentsData.edgeId,
2020-06-22 12:08:31 +03:00
entityType: EntityType.ASSET
}
}).afterClosed()
.subscribe((res) => {
if (res) {
2022-01-19 17:02:18 +02:00
this.config.updateData();
2020-06-22 12:08:31 +03:00
}
});
}
unassignFromEdge($event: Event, asset: AssetInfo) {
if ($event) {
$event.stopPropagation();
}
this.dialogService.confirm(
this.translate.instant('asset.unassign-asset-from-edge-title', {assetName: asset.name}),
this.translate.instant('asset.unassign-asset-from-edge-text'),
this.translate.instant('action.no'),
this.translate.instant('action.yes'),
true
).subscribe((res) => {
if (res) {
this.assetService.unassignAssetFromEdge(this.config.componentsData.edgeId, asset.id.id).subscribe(
() => {
2022-01-19 17:02:18 +02:00
this.config.updateData(this.config.componentsData.assetScope !== 'tenant');
}
);
}
}
);
}
unassignAssetsFromEdge($event: Event, assets: Array<AssetInfo>) {
if ($event) {
$event.stopPropagation();
}
this.dialogService.confirm(
this.translate.instant('asset.unassign-assets-from-edge-title', {count: assets.length}),
this.translate.instant('asset.unassign-assets-from-edge-text'),
this.translate.instant('action.no'),
this.translate.instant('action.yes'),
true
).subscribe((res) => {
if (res) {
const tasks: Observable<any>[] = [];
assets.forEach(
(asset) => {
tasks.push(this.assetService.unassignAssetFromEdge(this.config.componentsData.edgeId, asset.id.id));
}
);
forkJoin(tasks).subscribe(
() => {
2022-01-19 17:02:18 +02:00
this.config.updateData();
}
);
}
}
);
}
2019-08-16 21:22:54 +03:00
}