added svcapi ui and camunda code
[it/otf.git] / otf-frontend / client / src / app / shared / modules / create-test-head-form / create-test-head-form.component.ts
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\r
3 #   Licensed under the Apache License, Version 2.0 (the "License");            #\r
4 #   you may not use this file except in compliance with the License.           #\r
5 #   You may obtain a copy of the License at                                    #\r
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\r
9 #   Unless required by applicable law or agreed to in writing, software        #\r
10 #   distributed under the License is distributed on an "AS IS" BASIS,          #\r
11 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
12 #   See the License for the specific language governing permissions and        #\r
13 #   limitations under the License.                                             #\r
14 ##############################################################################*/\r
15 \r
16 \r
17 import { Component, OnInit, Input, Output, EventEmitter, ViewChild } from '@angular/core';\r
18 import { HttpClient } from '@angular/common/http';\r
19 import { ListService } from '../../services/list.service';\r
20 import { TestHeadService } from '../../services/test-head.service';\r
21 import { GroupService } from '../../services/group.service';\r
22 import 'codemirror/mode/yaml/yaml.js';\r
23 import { MatSnackBar, MatDialog, MatDialogRef } from '@angular/material';\r
24 import { AlertSnackbarComponent } from '../alert-snackbar/alert-snackbar.component';\r
25 import { AlertModalComponent } from '../alert-modal/alert-modal.component';\r
26 \r
27 \r
28 @Component({\r
29   selector: 'app-create-test-head-form',\r
30   templateUrl: './create-test-head-form.component.pug',\r
31   styleUrls: ['./create-test-head-form.component.scss']\r
32 })\r
33 export class CreateTestHeadFormComponent implements OnInit {\r
34   yaml;\r
35 \r
36   private hasPrevCredential;\r
37 \r
38   public codeConfig = {\r
39     mode: "yaml",\r
40     theme: "eclipse",\r
41     lineNumbers: true\r
42   };\r
43 \r
44   @Input() public formData;\r
45   @Input() public options;\r
46 \r
47   @Output() public childEvent = new EventEmitter();\r
48 \r
49   //Virtual Test Head Type Options\r
50   types = [\r
51     'Proxy',\r
52     'Regular',\r
53     'Script',\r
54     'Adapter'\r
55   ]\r
56 \r
57   //Implementation Language Options\r
58   langs = [\r
59     'Java',\r
60     'Python',\r
61     'JavaScript/NodeJS'\r
62   ]\r
63 \r
64   public vth;\r
65   public groups;\r
66 \r
67   @ViewChild('testHeadForm') form: any;\r
68 \r
69   constructor(public dialogRef: MatDialogRef<CreateTestHeadFormComponent>, private http: HttpClient, private list: ListService, private dialog: MatDialog, private snack: MatSnackBar, private testHead: TestHeadService, private group: GroupService) { }\r
70 \r
71   ngOnInit() {\r
72     this.setNew();\r
73     if(this.formData){\r
74       this.vth = Object.assign({}, this.formData);\r
75       if(!this.vth.authorizationCredential){\r
76           this.vth.authorizationCredential = "";\r
77           this.hasPrevCredential = false;\r
78       }\r
79       else{\r
80           this.hasPrevCredential = true\r
81       }\r
82     }\r
83   }\r
84 \r
85   markAsDirty(){\r
86     this.form.control.markAsDirty();\r
87   }\r
88 \r
89   create(){\r
90 \r
91     this.testHead.create(this.vth)\r
92     .subscribe((vth) => {\r
93       //this.list.addElement('vth', vth);\r
94       this.clear(this.form);\r
95       this.snack.openFromComponent(AlertSnackbarComponent, {\r
96         duration: 1500,\r
97         data: {\r
98           message:'Test Head Created'\r
99         }\r
100       });\r
101       this.dialogRef.close();\r
102       //this.dialog.closeAll();\r
103     }, err => {\r
104       this.dialog.open(AlertModalComponent, {\r
105         data: {\r
106           type: 'alert',\r
107           message: JSON.stringify(err)\r
108         },\r
109         width: '450px'\r
110       })\r
111     });\r
112 \r
113   }\r
114   //grab file\r
115   saveFileContents(){\r
116     this.getFileContents(val => {\r
117       this.vth.vthInputTemplate = val;\r
118     });\r
119   }\r
120 \r
121   getFileContents(callback) {\r
122     var val = "x";\r
123     var fileToLoad = (document.getElementById('file'))['files'][0];\r
124     var fileReader = new FileReader();\r
125     if (!fileToLoad) {\r
126       return null;\r
127     }\r
128     fileReader.onload = function (event) {\r
129       //\r
130       val = event.target['result'];\r
131 \r
132       //\r
133       callback(val);\r
134     }\r
135     fileReader.readAsText(fileToLoad);\r
136   }\r
137 \r
138   update(){\r
139     if(!this.hasPrevCredential && this.vth.authorizationCredential == ""){\r
140           delete this.vth.authorizationCredential;\r
141     }\r
142     this.testHead.patch(this.vth)\r
143     .subscribe((vth) => {\r
144       // this.list.updateElement('vth', '_id', vth['_id'], vth);\r
145       this.childEvent.emit();\r
146         this.snack.openFromComponent(AlertSnackbarComponent, {\r
147             duration: 1500,\r
148             data: {\r
149                 message:'Test Head Updated'\r
150             }\r
151         });\r
152         this.dialogRef.close();\r
153     });\r
154   }\r
155 \r
156   clear(form){\r
157     this.setNew();\r
158     if(form){\r
159       form.reset();\r
160     }\r
161     this.childEvent.emit();\r
162   }\r
163 \r
164   setNew(){\r
165     this.vth = {};\r
166     this.vth.vthInputTemplate = '';\r
167 \r
168     //this.vth.vthOutputTemplate = '';\r
169   }\r
170 }\r