-
Notifications
You must be signed in to change notification settings - Fork 581
/
datasources.component.ts
208 lines (179 loc) · 7.09 KB
/
datasources.component.ts
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import {Component, EventEmitter, Inject, Input, OnInit, Output} from "@angular/core";
import {MatSnackBar} from '@angular/material/snack-bar';
import {TdDataTableService} from "@covalent/core/data-table";
import {TdDialogService} from "@covalent/core/dialogs";
import {LoadingMode, LoadingType, TdLoadingService} from "@covalent/core/loading";
import {TranslateService} from "@ngx-translate/core";
import {StateService} from "@uirouter/angular";
import {concatMap} from "rxjs/operators/concatMap";
import {filter} from "rxjs/operators/filter";
import {finalize} from 'rxjs/operators/finalize';
import {tap} from "rxjs/operators/tap";
import {AccessControlService} from "../../../services/AccessControlService";
import {EntityAccessControlService} from "../../shared/entity-access-control/EntityAccessControlService";
import {DataSource} from "../api/models/datasource";
import {CatalogService} from "../api/services/catalog.service";
export class DataSourceSelectedEvent {
constructor(public dataSource: DataSource, public params: any, public stateRef: string) {
}
}
/**
* Displays available datasources
*/
@Component({
selector: "catalog-datasources",
styleUrls: ["./datasources.component.scss"],
templateUrl: "./datasources.component.html"
})
export class DataSourcesComponent implements OnInit {
static readonly LOADER = "DataSourcesComponent.LOADER";
private static topOfPageLoader: string = "DataSourcesComponent.topOfPageLoader";
/**
* List of available data sources
*/
@Input("datasources")
public datasources: DataSource[];
@Input("loading")
public loading: boolean;
/**
* List of available data sources
*/
@Input()
public selection: string;
@Input()
public selectedDatasourceState: string;
@Input()
public stateParams: {};
@Input()
public readOnly?: boolean = false;
@Input()
public displayInCard?: boolean = true;
public displayHeight?: string = "42vh";
/**
* Indicates that edit actions are allowed
*/
allowEdit = false;
@Output()
datasourceSelected: EventEmitter<DataSourceSelectedEvent> = new EventEmitter<DataSourceSelectedEvent>();
/**
* Filtered list of datasources to display
*/
filteredDatasources: DataSource[];
/**
* Search term for filtering datasources
*/
searchTerm: string;
/**
* cache of the datasource.id to boolean flag (true has access, false no access)
* @type {{}}
*/
cachedDataSourceEditEntityAccess :{[key: string]: boolean} = {};
constructor(private catalog: CatalogService,
private dataTable: TdDataTableService,
private dialog: TdDialogService,
private loadingService: TdLoadingService,
private state: StateService,
@Inject("AccessControlService") private accessControlService: AccessControlService,
private snackBarService: MatSnackBar,
private translateService: TranslateService) {
this.loadingService.create({
name: DataSourcesComponent.topOfPageLoader,
mode: LoadingMode.Indeterminate,
type: LoadingType.Linear,
color: 'accent',
});
accessControlService.getUserAllowedActions()
.then((actionSet: any) => this.allowEdit = accessControlService.hasAction(AccessControlService.DATASOURCE_EDIT, actionSet.actions));
}
public ngOnInit() {
this.filter();
this.displayHeight = (this.displayInCard ? "70vh" : "42vh");
}
search(term: string) {
this.searchTerm = term;
this.filter();
}
/**
* Creates a new data set from the specified datasource.
*/
selectDatasource(datasource: DataSource) {
let stateRef = "catalog.datasource";
if (this.selectedDatasourceState != undefined) {
stateRef = this.selectedDatasourceState;
}
let params = {datasourceId: datasource.id};
if (this.stateParams) {
params = {...params, ...this.stateParams};
}
if (this.datasourceSelected.observers.length > 0) {
this.datasourceSelected.emit(new DataSourceSelectedEvent(datasource, params, stateRef));
}
else {
this.state.go(stateRef, params);
}
}
private hasEditEntityAccess(datasource:DataSource){
if(this.cachedDataSourceEditEntityAccess[datasource.id] != undefined){
return this.cachedDataSourceEditEntityAccess[datasource.id] == true;
}
else {
let access = !this.accessControlService.isEntityAccessControlled()
|| this.accessControlService.hasEntityAccess(EntityAccessControlService.ENTITY_ACCESS.DATASOURCE.EDIT_DETAILS, datasource, EntityAccessControlService.entityRoleTypes.DATASOURCE);
this.cachedDataSourceEditEntityAccess[datasource.id] = access;
return access;
}
}
isEditable(datasource: DataSource): boolean {
return (this.allowEdit && !this.readOnly
&& this.hasEditEntityAccess(datasource));
}
/**
* Edit properties of datasource
*/
editDatasource(event: any, datasource: DataSource) {
event.stopPropagation();
this.state.go("catalog.new-datasource", {connectorId: datasource.connector.id, datasourceId: datasource.id});
}
/**
* Delete datasource
*/
deleteDatasource(event: any, datasource: DataSource) {
event.stopPropagation();
this.translateService.get("CATALOG.DATA_SOURCES.CONFIRM_DELETE").pipe(
concatMap(messages => {
return this.dialog.openConfirm({
message: messages["MESSAGE"],
title: messages["TITLE"],
acceptButton: messages["ACCEPT"],
cancelButton: messages["CANCEL"]
}).afterClosed();
}),
filter(accept => accept),
tap(() => this.loadingService.register(DataSourcesComponent.topOfPageLoader)),
concatMap(() => this.catalog.deleteDataSource(datasource)),
finalize(() => this.loadingService.resolve(DataSourcesComponent.topOfPageLoader))
).subscribe(
() => this.state.go("catalog.datasources", {}, {reload: true}),
err => {
console.error(err);
if (err.status == 409) {
this.showSnackBar("Failed to delete. This data source is currently being used by a feed.");
} else {
this.showSnackBar('Failed to delete.', err.message);
}
}
);
}
/**
* Updates filteredDatasources by filtering availableDatasources.
*/
private filter() {
let filteredConnectors = this.dataTable.filterData(this.datasources, this.searchTerm, true);
filteredConnectors = this.dataTable.sortData(filteredConnectors, "title");
this.filteredDatasources = filteredConnectors;
}
showSnackBar(msg: string, err?: string): void {
this.snackBarService
.open(msg + ' ' + (err ? err : ""), 'OK', {duration: 5000});
}
}