6d89a372171cc223680607b7b3a0201dd6817f4d
[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 import { Template } from "src/app/core/models/template.model";
28
29 // Loading spinner
30 import { NgxSpinnerService } from "ngx-spinner";
31
32 @Component({
33   selector: 'app-edit-template-modal',
34   templateUrl: './edit-template-modal.component.html',
35   styleUrls: ['./edit-template-modal.component.css']
36 })
37 export class EditTemplateModalComponent implements OnInit {
38   @Input() edittemplate: Template;
39   templateInput: Template;
40   defaultDesigntype: String;
41   defaultTopicname: String;
42   templatetypedata: Array<any> = [];
43   topicname: Array<any> = [];
44   dbList: Array<any> = [];
45   dbId: string = "";
46   tempSeletedDbs: any = [];
47   @Output() passEntry: EventEmitter<any> = new EventEmitter();
48
49   @ViewChild("templatetype") templatetype: ElementRef;
50   @ViewChild("topic") topic: ElementRef;
51
52   constructor(
53     public activeModal: NgbActiveModal,
54     public dashboardApiService: RestApiService,
55     private spinner: NgxSpinnerService,
56   ) { }
57
58   inputtemplateName = null;
59   templateInputTitle = "";
60   fileName = null;
61
62   ngOnInit() {
63     // cache for display
64     this.templateInput = new Template();
65     const feed = {
66       id: this.edittemplate.id,
67       name: this.edittemplate.name,
68       submitted: this.edittemplate.submitted,
69       body: this.edittemplate.body,
70       note: this.edittemplate.note,
71       topicName: this.edittemplate.topicName,
72       designType: this.edittemplate.designType,
73       designTypeName: this.edittemplate.designTypeName,
74       dbs: this.edittemplate.dbs,
75     };
76     this.templateInput = feed;
77     this.templateInputTitle = "" + this.edittemplate.name;
78     this.tempSeletedDbs = this.templateInput.dbs.map(item => { return Number(item) });
79
80     this.getTopicName();
81     this.getTemplateTypeName();
82   }
83   getTopicName() {
84     this.dashboardApiService.getTopicsFromFeeder().subscribe(data => {
85       this.topicname = data;
86     });
87   }
88
89   getDbList() {
90     this.templatetypedata.map(item => {
91       if (item.name === this.defaultDesigntype) {
92         this.dbId = item.id;
93       }
94     })
95     this.dashboardApiService.getTempDbList(this.dbId).subscribe(data => {
96       Object.keys(data).map(item => {
97         this.dbList.push({ key: item, name: data[item] })
98       })
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(Number(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   getTemplateTypeName() {
116     this.dashboardApiService.getTemplateTypeName().subscribe(data => {
117       this.templatetypedata = data;
118       this.getDefaultOptions();
119     });
120   }
121
122   getDefaultOptions() {
123     this.templatetypedata.map(item => {
124       if (item.id === this.templateInput.designType) {
125         return this.defaultDesigntype = item.name;
126       }
127     });
128     this.defaultTopicname = this.templateInput.topicName;
129     this.getDbList();
130   }
131
132   jsReadFiles() {
133     var thiss = this;
134     var file = (<HTMLInputElement>document.querySelector("#f-file")).files[0];
135     this.fileName = file.name;
136     var reader = new FileReader();
137     reader.onload = function () {
138       thiss.templateInput.body = String(this.result);
139     }
140     reader.readAsText(file);
141   }
142
143   passBack() {
144     this.spinner.show();
145     if (this.templateInput.name == '' || this.templateInput.name == undefined) {
146       return false;
147     }
148     this.edittemplate = this.templateInput;
149     // this.templatetypedata.map(item => {
150     //   if (item.name === this.templatetype.nativeElement.value) {
151     //     return this.edittemplate.designType = item.id;
152     //   }
153     // });
154     this.edittemplate.designType = this.templatetypedata.filter(item => {
155       return item.name === this.templatetype.nativeElement.value;
156     })[0].id || "";
157
158     this.edittemplate.designTypeName = this.templatetype.nativeElement.value;
159     this.edittemplate.topicName = this.topic.nativeElement.value;
160     this.edittemplate.dbs = this.tempSeletedDbs;
161     this.passEntry.emit(this.edittemplate);
162     setTimeout(() => {
163       this.spinner.hide();
164     }, 500);
165   }
166 }