a6b7be9fe26e402606fddd0e9bbc5dedf1b3eb14
[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   tempSeletedDbs: any = [];
46   @Output() passEntry: EventEmitter<any> = new EventEmitter();
47
48   @ViewChild("templatetype") templatetype: ElementRef;
49   @ViewChild("topic") topic: ElementRef;
50
51   constructor(
52     public activeModal: NgbActiveModal,
53     public dashboardApiService: RestApiService,
54     private spinner: NgxSpinnerService,
55   ) { }
56
57   inputtemplateName = null;
58   templateInputTitle = "";
59   fileName = null;
60
61   ngOnInit() {
62     // cache for display
63     this.templateInput = new Template();
64     this.getDbList();
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.dashboardApiService.getTempDbList().subscribe(data => {
91       Object.keys(data).map(item => {
92         this.dbList.push({ key: item, name: data[item] })
93       })
94     });
95   }
96
97   updateSelectedDB(event: any, name: any) {
98     if (event.target.checked) {
99       if (!this.tempSeletedDbs.find(db => db === name)) {
100         this.tempSeletedDbs.push(Number(name.key));
101       }
102     } else {
103       const index = this.tempSeletedDbs.indexOf(+name.key, 0);
104       if (index > -1) {
105         this.tempSeletedDbs.splice(index, 1);
106       }
107     }
108   }
109
110   getTemplateTypeName() {
111     this.dashboardApiService.getTemplateTypeName().subscribe(data => {
112       this.templatetypedata = data;
113       this.getDefaultOptions();
114     });
115   }
116
117   getDefaultOptions() {
118     this.templatetypedata.map(item => {
119       if (item.id === this.templateInput.designType) {
120         return this.defaultDesigntype = item.name;
121       }
122     });
123     this.defaultTopicname = this.templateInput.topicName;
124   }
125
126   jsReadFiles() {
127     var thiss = this;
128     var file = (<HTMLInputElement>document.querySelector("#f-file")).files[0];
129     this.fileName = file.name;
130     var reader = new FileReader();
131     reader.onload = function () {
132       thiss.templateInput.body = String(this.result);
133     }
134     reader.readAsText(file);
135   }
136
137   passBack() {
138     this.spinner.show();
139     if (this.templateInput.name == '' || this.templateInput.name == undefined) {
140       return false;
141     }
142     this.edittemplate = this.templateInput;
143     // this.templatetypedata.map(item => {
144     //   if (item.name === this.templatetype.nativeElement.value) {
145     //     return this.edittemplate.designType = item.id;
146     //   }
147     // });
148     this.edittemplate.designType = this.templatetypedata.filter(item => {
149       return item.name === this.templatetype.nativeElement.value;
150     })[0].id || "";
151
152     this.edittemplate.designTypeName = this.templatetype.nativeElement.value;
153     this.edittemplate.topicName = this.topic.nativeElement.value;
154     this.edittemplate.dbs = this.tempSeletedDbs;
155     this.passEntry.emit(this.edittemplate);
156     setTimeout(() => {
157       this.spinner.hide();
158     }, 500);
159   }
160 }