tenant-profiles-table-config.resolver.ts
5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
///
/// 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.
///
import { Injectable } from '@angular/core';
import { Resolve } from '@angular/router';
import { TenantProfile } from '@shared/models/tenant.model';
import {
checkBoxCell,
DateEntityTableColumn,
EntityTableColumn,
EntityTableConfig
} 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 { TenantProfileService } from '@core/http/tenant-profile.service';
import { TenantProfileComponent } from '../../components/profile/tenant-profile.component';
import { TenantProfileTabsComponent } from './tenant-profile-tabs.component';
import { DialogService } from '@core/services/dialog.service';
@Injectable()
export class TenantProfilesTableConfigResolver implements Resolve<EntityTableConfig<TenantProfile>> {
private readonly config: EntityTableConfig<TenantProfile> = new EntityTableConfig<TenantProfile>();
constructor(private tenantProfileService: TenantProfileService,
private translate: TranslateService,
private datePipe: DatePipe,
private dialogService: DialogService) {
this.config.entityType = EntityType.TENANT_PROFILE;
this.config.entityComponent = TenantProfileComponent;
this.config.entityTabsComponent = TenantProfileTabsComponent;
this.config.entityTranslations = entityTypeTranslations.get(EntityType.TENANT_PROFILE);
this.config.entityResources = entityTypeResources.get(EntityType.TENANT_PROFILE);
this.config.columns.push(
new DateEntityTableColumn<TenantProfile>('createdTime', 'common.created-time', this.datePipe, '150px'),
new EntityTableColumn<TenantProfile>('name', 'tenant-profile.name', '40%'),
new EntityTableColumn<TenantProfile>('description', 'tenant-profile.description', '60%'),
new EntityTableColumn<TenantProfile>('isDefault', 'tenant-profile.default', '60px',
entity => {
return checkBoxCell(entity.default);
})
);
this.config.cellActionDescriptors.push(
{
name: this.translate.instant('tenant-profile.set-default'),
icon: 'flag',
isEnabled: (tenantProfile) => !tenantProfile.default,
onAction: ($event, entity) => this.setDefaultTenantProfile($event, entity)
}
);
this.config.deleteEntityTitle = tenantProfile => this.translate.instant('tenant-profile.delete-tenant-profile-title',
{ tenantProfileName: tenantProfile.name });
this.config.deleteEntityContent = () => this.translate.instant('tenant-profile.delete-tenant-profile-text');
this.config.deleteEntitiesTitle = count => this.translate.instant('tenant-profile.delete-tenant-profiles-title', {count});
this.config.deleteEntitiesContent = () => this.translate.instant('tenant-profile.delete-tenant-profiles-text');
this.config.entitiesFetchFunction = pageLink => this.tenantProfileService.getTenantProfiles(pageLink);
this.config.loadEntity = id => this.tenantProfileService.getTenantProfile(id.id);
this.config.saveEntity = tenantProfile => this.tenantProfileService.saveTenantProfile(tenantProfile);
this.config.deleteEntity = id => this.tenantProfileService.deleteTenantProfile(id.id);
this.config.onEntityAction = action => this.onTenantProfileAction(action);
this.config.deleteEnabled = (tenantProfile) => tenantProfile && !tenantProfile.default;
this.config.entitySelectionEnabled = (tenantProfile) => tenantProfile && !tenantProfile.default;
}
resolve(): EntityTableConfig<TenantProfile> {
this.config.tableTitle = this.translate.instant('tenant-profile.tenant-profiles');
return this.config;
}
setDefaultTenantProfile($event: Event, tenantProfile: TenantProfile) {
if ($event) {
$event.stopPropagation();
}
this.dialogService.confirm(
this.translate.instant('tenant-profile.set-default-tenant-profile-title', {tenantProfileName: tenantProfile.name}),
this.translate.instant('tenant-profile.set-default-tenant-profile-text'),
this.translate.instant('action.no'),
this.translate.instant('action.yes'),
true
).subscribe((res) => {
if (res) {
this.tenantProfileService.setDefaultTenantProfile(tenantProfile.id.id).subscribe(
() => {
this.config.table.updateData();
}
);
}
}
);
}
onTenantProfileAction(action: EntityAction<TenantProfile>): boolean {
switch (action.action) {
case 'setDefault':
this.setDefaultTenantProfile(action.event, action.entity);
return true;
}
return false;
}
}