dashboard-form.component.ts
4.0 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
///
/// 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 { Component, Inject } from '@angular/core';
import { Store } from '@ngrx/store';
import { AppState } from '@core/core.state';
import { EntityComponent } from '../../components/entity/entity.component';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { ActionNotificationShow } from '@core/notification/notification.actions';
import { TranslateService } from '@ngx-translate/core';
import {
Dashboard,
getDashboardAssignedCustomersText,
isCurrentPublicDashboardCustomer,
isPublicDashboard
} from '@shared/models/dashboard.models';
import { DashboardService } from '@core/http/dashboard.service';
import { EntityTableConfig } from '@home/models/entity/entities-table-config.models';
import { isEqual } from '@core/utils';
@Component({
selector: 'tb-dashboard-form',
templateUrl: './dashboard-form.component.html',
styleUrls: ['./dashboard-form.component.scss']
})
export class DashboardFormComponent extends EntityComponent<Dashboard> {
dashboardScope: 'tenant' | 'customer' | 'customer_user';
customerId: string;
publicLink: string;
assignedCustomersText: string;
constructor(protected store: Store<AppState>,
protected translate: TranslateService,
private dashboardService: DashboardService,
@Inject('entity') protected entityValue: Dashboard,
@Inject('entitiesTableConfig') protected entitiesTableConfigValue: EntityTableConfig<Dashboard>,
public fb: FormBuilder) {
super(store, fb, entityValue, entitiesTableConfigValue);
}
ngOnInit() {
this.dashboardScope = this.entitiesTableConfig.componentsData.dashboardScope;
this.customerId = this.entitiesTableConfig.componentsData.customerId;
super.ngOnInit();
}
isPublic(entity: Dashboard): boolean {
return isPublicDashboard(entity);
}
isCurrentPublicCustomer(entity: Dashboard): boolean {
return isCurrentPublicDashboardCustomer(entity, this.customerId);
}
hideDelete() {
if (this.entitiesTableConfig) {
return !this.entitiesTableConfig.deleteEnabled(this.entity);
} else {
return false;
}
}
buildForm(entity: Dashboard): FormGroup {
this.updateFields(entity);
return this.fb.group(
{
title: [entity ? entity.title : '', [Validators.required]],
configuration: this.fb.group(
{
description: [entity && entity.configuration ? entity.configuration.description : ''],
}
)
}
);
}
updateForm(entity: Dashboard) {
this.updateFields(entity);
this.entityForm.patchValue({title: entity.title});
this.entityForm.patchValue({configuration: {description: entity.configuration ? entity.configuration.description : ''}});
}
prepareFormValue(formValue: any): any {
formValue.configuration = {...(this.entity.configuration || {}), ...(formValue.configuration || {})};
return formValue;
}
onPublicLinkCopied($event) {
this.store.dispatch(new ActionNotificationShow(
{
message: this.translate.instant('dashboard.public-link-copied-message'),
type: 'success',
duration: 750,
verticalPosition: 'bottom',
horizontalPosition: 'right'
}));
}
private updateFields(entity: Dashboard): void {
if (entity && !isEqual(entity, {})) {
this.assignedCustomersText = getDashboardAssignedCustomersText(entity);
this.publicLink = this.dashboardService.getPublicDashboardLink(entity);
}
}
}