This repository has been archived by the owner on Nov 22, 2019. It is now read-only.
/
create-virtualization-dialog.component.ts
204 lines (184 loc) · 7.33 KB
/
create-virtualization-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
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
/**
* @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 } from "@angular/core";
import { Output } from "@angular/core";
import { EventEmitter } from "@angular/core";
import { BsModalRef } from "ngx-bootstrap";
import { LoggerService } from "@core/logger.service";
import { ViewEditorI18n } from "@dataservices/virtualization/view-editor/view-editor-i18n";
import { AbstractControl, FormControl, FormGroup } from "@angular/forms";
import { VdbService } from "@dataservices/shared/vdb.service";
import { DataserviceService } from "@dataservices/shared/dataservice.service";
import { CreateVirtualizationResult } from "@dataservices/create-virtualization-dialog/create-virtualization-result.model";
@Component({
selector: "app-create-virtualization-dialog",
templateUrl: "./create-virtualization-dialog.component.html",
styleUrls: ["./create-virtualization-dialog.component.css"]
})
/**
* CreateVirtualization Dialog. Invoke this from another component as follows:
*
* this.modalRef = this.modalService.show(CreateVirtualizationDialogComponent, {initialState});
* this.modalRef.content.okAction.take(1).subscribe((createResult) => {
* // do something with dialog result - CreateVirtualizationResult
* });
*
* 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 CreateVirtualizationDialogComponent implements OnInit {
@Output() public okAction: EventEmitter<CreateVirtualizationResult> = new EventEmitter<CreateVirtualizationResult>();
public readonly title = ViewEditorI18n.createVirtualizationDialogTitle;
public readonly message = ViewEditorI18n.createVirtualizationDialogMessage;
public readonly cancelButtonText = ViewEditorI18n.cancelButtonText;
public readonly okButtonText = ViewEditorI18n.okButtonText;
public okButtonEnabled = false;
public bsModalRef: BsModalRef;
public virtNameValidationError = "";
public viewNameValidationError = "";
public virtualizationPropertyForm: FormGroup;
private loggerService: LoggerService;
private dataserviceService: DataserviceService;
private vdbService: VdbService;
private serviceVdbName = "";
constructor(bsModalRef: BsModalRef, logger: LoggerService,
dataserviceService: DataserviceService, vdbService: VdbService) {
this.bsModalRef = bsModalRef;
this.loggerService = logger;
this.dataserviceService = dataserviceService;
const dService = this.dataserviceService.getSelectedDataservice();
if ( dService && dService !== null ) {
this.serviceVdbName = dService.getServiceVdbName();
}
this.vdbService = vdbService;
this.createPropertyForm();
}
public ngOnInit(): void {
this.virtualizationPropertyForm.controls["virtName"].setValue("");
this.virtualizationPropertyForm.controls["virtDescription"].setValue("");
this.virtualizationPropertyForm.controls["viewName"].setValue("");
this.virtualizationPropertyForm.controls["viewDescription"].setValue("");
}
/*
* Creates the view property form
*/
private createPropertyForm(): void {
this.virtualizationPropertyForm = new FormGroup({
virtName: new FormControl( "", this.handleVirtNameChanged.bind( this ) ),
virtDescription: new FormControl(""),
viewName: new FormControl( "", this.handleViewNameChanged.bind( this ) ),
viewDescription: new FormControl("")
});
}
/**
* Handler for virtualization name changes.
* @param {AbstractControl} input
*/
public handleVirtNameChanged( input: AbstractControl ): void {
const self = this;
this.dataserviceService.isValidName( input.value ).subscribe(
( errorMsg ) => {
if ( errorMsg ) {
// only update if error has changed
if ( errorMsg !== self.virtNameValidationError ) {
self.virtNameValidationError = errorMsg;
}
} else { // name is valid
self.virtNameValidationError = "";
}
self.setOkButtonEnablement();
},
( error ) => {
self.loggerService.error( "[handleNameChanged] Error: %o", error );
self.virtNameValidationError = "Error validating view name";
self.setOkButtonEnablement();
} );
}
/**
* Handler for view name changes. Since this will be the first view in the virtualization,
* no need to make a service call - just do some general name checking
* @param {AbstractControl} input
*/
public handleViewNameChanged( input: AbstractControl ): void {
this.viewNameValidationError = this.validateViewName(input.value);
this.setOkButtonEnablement();
}
/**
* OK selected. Emit ViewDefinition with the view, then modal is closed
*/
public onOkSelected(): void {
const virtName = this.virtualizationPropertyForm.controls["virtName"].value;
const virtDescr = this.virtualizationPropertyForm.controls["virtDescription"].value;
const viewName = this.virtualizationPropertyForm.controls["viewName"].value;
const viewDescr = this.virtualizationPropertyForm.controls["viewDescription"].value;
const result = new CreateVirtualizationResult();
result.setVirtualizationName(virtName);
result.setVirtualizationDescription(virtDescr);
result.setViewName(viewName);
result.setViewDescription(viewDescr);
this.okAction.emit(result);
this.bsModalRef.hide();
}
/**
* Cancel selected. The modal is closed.
*/
public onCancelSelected(): void {
this.bsModalRef.hide();
}
/*
* Return the virtualization name valid state
*/
public get virtNameValid(): boolean {
return this.virtNameValidationError == null || this.virtNameValidationError.length === 0;
}
/*
* Return the view name valid state
*/
public get viewNameValid(): boolean {
return this.viewNameValidationError == null || this.viewNameValidationError.length === 0;
}
/**
* Validate the provided view name. If the name is valid, an empty string will be returned. If the view is invalid,
* the error message is returned.
* @param {string} viewName the view name being validated
* @return {string} the validation message
*/
private validateViewName( viewName: string ): string {
if ( !viewName || viewName === null || viewName.length === 0 ) {
return "View name cannot be empty";
}
const isValid = /^\w+$/.test(viewName);
if ( !isValid ) {
return "View name can only contain letters, digits and underscores";
}
return "";
}
/**
* Sets the OK button enablement. Both the virtualization name and view name must be valid.
*/
private setOkButtonEnablement(): void {
if (this.virtNameValid && this.viewNameValid) {
this.okButtonEnabled = true;
} else {
this.okButtonEnabled = false;
}
}
}