This repository has been archived by the owner on Nov 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
connection-table-dialog.component.ts
164 lines (150 loc) · 5.98 KB
/
connection-table-dialog.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
/**
* @license
* Copyright 2017 JBoss Inc
*
* 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, OnInit, ViewChild } from "@angular/core";
import { Output } from "@angular/core";
import { EventEmitter } from "@angular/core";
import { BsModalRef } from "ngx-bootstrap";
import { ConnectionTreeSelectorComponent } from "@dataservices/virtualization/view-editor/connection-table-dialog/connection-tree-selector/connection-tree-selector.component";
import { SchemaNode } from "@connections/shared/schema-node.model";
import { ConnectionsConstants } from "@connections/shared/connections-constants";
import { LoadingState } from "@shared/loading-state.enum";
import { ConnectionService } from "@connections/shared/connection.service";
import { LoggerService } from "@core/logger.service";
import { ViewEditorI18n } from "@dataservices/virtualization/view-editor/view-editor-i18n";
@Component({
selector: "app-connection-table-dialog",
templateUrl: "./connection-table-dialog.component.html",
styleUrls: ["./connection-table-dialog.component.css"]
})
/**
* ConnectionTable Dialog. Invoke this from another component as follows:
*
* this.modalRef = this.modalService.show(ConnectionTableDialogComponent, {initialState});
* this.modalRef.content.okAction.take(1).subscribe((selectedNodes) => {
* // do something with array of selected nodes - selectedNodes - SchemaNode[]
* });
*
* The expected initial state is as follows:
* const initialState = {
* title: "The dialog title",
* cancelButtonText: "Text for cancel button",
* confirmButtonText: "Text for confirm button"
* };
*/
export class ConnectionTableDialogComponent implements OnInit {
@ViewChild(ConnectionTreeSelectorComponent) public connectionTree: ConnectionTreeSelectorComponent;
@Output() public okAction = new EventEmitter();
public readonly title = ViewEditorI18n.connectionTableSelectionDialogTitle;
public readonly message = ViewEditorI18n.connectionTableSelectionDialogMessage;
public readonly cancelButtonText = ViewEditorI18n.cancelButtonText;
public readonly okButtonText = ViewEditorI18n.okButtonText;
public okButtonEnabled = false;
public bsModalRef: BsModalRef;
public selectionText = ViewEditorI18n.noSelection;
public readonly currentSelectionMsg = ViewEditorI18n.currentSelection;
private connectionService: ConnectionService;
private selectedTreeNodes: SchemaNode[] = [];
private loggerService: LoggerService;
private connectionLoadingState: LoadingState = LoadingState.LOADING;
constructor(bsModalRef: BsModalRef, connectionService: ConnectionService, logger: LoggerService) {
this.bsModalRef = bsModalRef;
this.connectionService = connectionService;
this.loggerService = logger;
}
public ngOnInit(): void {
// Load the connections
this.connectionLoadingState = LoadingState.LOADING;
const self = this;
this.connectionService
.getConnections(true, true)
.subscribe(
(connectionSummaries) => {
const conns = [];
const treeNodes = [];
for ( const connectionSummary of connectionSummaries ) {
const connStatus = connectionSummary.getStatus();
const conn = connectionSummary.getConnection();
conn.setStatus(connStatus);
conns.push(conn);
// Add active connection to tree root nodes
if (conn.isActive) {
const node = new SchemaNode();
node.setName(conn.getId());
node.setType(ConnectionsConstants.schemaNodeType_connection);
node.setHasChildren(true);
treeNodes.push(node);
}
}
self.connectionTree.setTreeRoots(treeNodes);
self.connectionLoadingState = LoadingState.LOADED_VALID;
},
(error) => {
self.loggerService.error("[ConnectionTableDialogComponent] Error getting connections: %o", error);
self.connectionLoadingState = LoadingState.LOADED_INVALID;
}
);
}
/**
* Handles tree node selection
* @param {SchemaNode} $event
*/
public onTreeNodeSelected( $event: SchemaNode ): void {
const selectedNode = $event;
if (selectedNode && selectedNode !== null && selectedNode.isQueryable()) {
this.selectedTreeNodes = [];
this.selectedTreeNodes.push(selectedNode);
} else {
this.selectedTreeNodes = [];
}
this.setSelectionTextAndOkButtonEnablement();
}
/**
* Handles tree node de-selection
* @param {SchemaNode} $event
*/
public onTreeNodeDeselected( $event: SchemaNode ): void {
const selectedNode = $event;
this.selectedTreeNodes = [];
this.setSelectionTextAndOkButtonEnablement();
}
/**
* OK selected. The array of selected SchemaNodes is emiited, then modal is closed
*/
public onOkSelected(): void {
this.okAction.emit(this.selectedTreeNodes);
this.bsModalRef.hide();
}
/**
* Cancel selected. The modal is closed.
*/
public onCancelSelected(): void {
this.bsModalRef.hide();
}
/**
* Sets the node selection text and OK button enablement, based upon the selections
*/
private setSelectionTextAndOkButtonEnablement(): void {
if (this.selectedTreeNodes.length > 0) {
this.selectionText = "[" + this.selectedTreeNodes[0].getConnectionName() + "] " +
this.selectedTreeNodes[0].getName();
this.okButtonEnabled = true;
} else {
this.selectionText = "Nothing selected";
this.okButtonEnabled = false;
}
}
}