496d08a1a14aaf409deab67cfa94ff79830ac5d4
[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   topicname: Array<any> = [];
43   dbList: Array<any> = [];
44   tempSeletedDbs: any = [];
45   @Output() passEntry: EventEmitter<any> = new EventEmitter();
46   @ViewChild("templatetype") templatetype: ElementRef;
47   @ViewChild("topic") topic: ElementRef;
48
49   constructor(
50     public activeModal: NgbActiveModal,
51     public dashboardApiService: RestApiService,
52     private spinner: NgxSpinnerService,
53   ) { }
54   inputtemplateName = null;
55   templatebody = null;
56   fileName = null;
57
58   ngOnInit() {
59     this.getTopicName();
60     this.getTemplateTypeName();
61     this.getDbList();
62     // cache for display
63     this.templateInput = new Template();
64     const feed = {
65       id: null,
66       name: this.template.name,
67       submitted: this.template.submitted,
68       body: this.template.body,
69       note: this.template.note,
70       topicName: this.template.topicName,
71       designType: this.template.designType,
72       designTypeName: this.template.designTypeName,
73       dbs: [],
74     };
75     this.templateInput = feed;
76   }
77   getTopicName() {
78     this.dashboardApiService.getTopicsFromFeeder().subscribe(data => {
79       this.topicname = data;
80     });
81   }
82
83   getDbList() {
84     this.dashboardApiService.getTempDbList().subscribe(data => {
85       Object.keys(data).map(item => {
86         this.dbList.push({ key: item, name: data[item] })
87       })
88     });
89   }
90
91   getTemplateTypeName() {
92     this.dashboardApiService.getTemplateTypeName().subscribe(data => {
93       this.templatetypedata = data;
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(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   jsReadFiles() {
111     var thiss = this;
112     var file = (<HTMLInputElement>document.querySelector("#f-file")).files[0];
113     this.fileName = file.name;
114     var reader = new FileReader();
115     reader.onload = function () {
116       // console.log(this.result, "this.result");
117       thiss.templateInput.body = String(this.result);
118     }
119     reader.readAsText(file);
120   }
121   passBack() {
122     this.spinner.show();
123     if (this.templateInput.name == '' || this.templateInput.name == undefined) {
124       return false;
125     }
126     this.template = this.templateInput;
127
128     // this.templatetypedata.map(item => {
129     //   if (item.name === this.templatetype.nativeElement.value) {
130     //     return this.template.designType = item.id;
131     //   }
132     // })
133
134     this.template.designType = this.templatetypedata.filter(item => {
135       return item.name === this.templatetype.nativeElement.value;
136     })[0].id || "";
137
138     this.template.designTypeName = this.templatetype.nativeElement.value;
139     this.template.topicName = this.topic.nativeElement.value;
140     this.template.dbs = this.tempSeletedDbs;
141     this.template.submitted = false;
142     this.template.note = "";
143     this.passEntry.emit(this.template);
144     setTimeout(() => {
145       this.spinner.hide();
146     }, 500);
147   }
148 }