06b2ff84077b82e9ac35b5bd6370443569789fd6
[dcaegen2/services.git] /
1 /*
2     Copyright (C) 2019 CMCC, Inc. and others. All rights reserved.
3
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7
8             http://www.apache.org/licenses/LICENSE-2.0
9
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 import {
17   Component,
18   OnInit,
19   Input,
20   Output,
21   EventEmitter,
22   ViewChild,
23   ElementRef
24 } from "@angular/core";
25 import { NgbActiveModal } from "@ng-bootstrap/ng-bootstrap";
26 import { RestApiService } from "src/app/core/services/rest-api.service";
27 // Loading spinner
28 import { NgxSpinnerService } from "ngx-spinner";
29
30 import { Template } from "src/app/core/models/template.model";
31
32 @Component({
33   selector: 'app-new-template-modal',
34   templateUrl: './new-template-modal.component.html',
35   styleUrls: ['./new-template-modal.component.css']
36 })
37 export class NewTemplateModalComponent implements OnInit {
38   @Input() template: Template;
39   @Input() templatelist_length;
40   templateInput: Template
41   templatetypedata: Array<any> = [];
42   dbId: string = "";
43   topicname: Array<any> = [];
44   dbList: Array<any> = [];
45   tempSeletedDbs: any = [];
46   @Output() passEntry: EventEmitter<any> = new EventEmitter();
47   @ViewChild("templatetype") templatetype: ElementRef;
48   @ViewChild("topic") topic: ElementRef;
49
50   constructor(
51     public activeModal: NgbActiveModal,
52     public dashboardApiService: RestApiService,
53     private spinner: NgxSpinnerService,
54   ) { }
55   inputtemplateName = null;
56   templatebody = null;
57   fileName = null;
58
59   ngOnInit() {
60     this.getTopicName();
61     this.getTemplateTypeName();
62
63     // cache for display
64     this.templateInput = new Template();
65     const feed = {
66       id: null,
67       name: this.template.name,
68       submitted: this.template.submitted,
69       body: this.template.body,
70       note: this.template.note,
71       topicName: this.template.topicName,
72       designType: this.template.designType,
73       designTypeName: this.template.designTypeName,
74       dbs: [],
75     };
76     this.templateInput = feed;
77   }
78   getTopicName() {
79     this.dashboardApiService.getTopicsFromFeeder().subscribe(data => {
80       this.topicname = data;
81     });
82   }
83
84   getDbList() {
85     if (this.dbId === "") {
86       this.dbId = this.templatetypedata[0].id
87     }
88     this.dashboardApiService.getTempDbList(this.dbId).subscribe(data => {
89       Object.keys(data).map(item => {
90         this.dbList.push({ key: item, name: data[item] })
91       })
92     });
93   }
94
95   getTemplateTypeName() {
96     this.dashboardApiService.getTemplateTypeName().subscribe(data => {
97       this.templatetypedata = data;
98       this.getDbList();
99     });
100   }
101
102   updateSelectedDB(event: any, name: any) {
103     if (event.target.checked) {
104       if (!this.tempSeletedDbs.find(db => db === name)) {
105         this.tempSeletedDbs.push(name.key);
106       }
107     } else {
108       const index = this.tempSeletedDbs.indexOf(name.key, 0);
109       if (index > -1) {
110         this.tempSeletedDbs.splice(index, 1);
111       }
112     }
113   }
114
115   jsReadFiles() {
116     var thiss = this;
117     var file = (<HTMLInputElement>document.querySelector("#f-file")).files[0];
118     this.fileName = file.name;
119     var reader = new FileReader();
120     reader.onload = function () {
121       // console.log(this.result, "this.result");
122       thiss.templateInput.body = String(this.result);
123     }
124     reader.readAsText(file);
125   }
126
127   selectType(e) {
128     this.dbList = [];
129     this.templatetypedata.map(item => {
130       if (item.name === e.target.value) {
131         this.dbId = item.id;
132         this.getDbList();
133       }
134     })
135   }
136
137   passBack() {
138     this.spinner.show();
139     if (this.templateInput.name == '' || this.templateInput.name == undefined) {
140       return false;
141     }
142     this.template = this.templateInput;
143
144     // this.templatetypedata.map(item => {
145     //   if (item.name === this.templatetype.nativeElement.value) {
146     //     return this.template.designType = item.id;
147     //   }
148     // })
149
150     this.template.designType = this.templatetypedata.filter(item => {
151       return item.name === this.templatetype.nativeElement.value;
152     })[0].id || "";
153
154     this.template.designTypeName = this.templatetype.nativeElement.value;
155     this.template.topicName = this.topic.nativeElement.value;
156     this.template.dbs = this.tempSeletedDbs;
157     this.template.submitted = false;
158     this.template.note = "";
159     this.passEntry.emit(this.template);
160     setTimeout(() => {
161       this.spinner.hide();
162     }, 500);
163   }
164 }