5e46bf92ef4a8559a2016e5bdf1e0273f2ae3196
[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   @Output() passEntry: EventEmitter<any> = new EventEmitter();
44   @ViewChild("templatetype") templatetype: ElementRef;
45   @ViewChild("topic") topic: ElementRef;
46
47   constructor(
48     public activeModal: NgbActiveModal,
49     public dashboardApiService: RestApiService,
50     private spinner: NgxSpinnerService,
51   ) { }
52   inputtemplateName = null;
53   templatebody = null;
54   fileName = null;
55
56   ngOnInit() {
57     this.getTopicName();
58     this.getTemplateTypeName();
59     // cache for display
60     this.templateInput = new Template();
61     const feed = {
62       id: null,
63       name: this.template.name,
64       submitted: this.template.submitted,
65       body: this.template.body,
66       note: this.template.note,
67       topicName: this.template.topicName,
68       designType: this.template.designType,
69       designTypeName: this.template.designTypeName,
70     };
71     this.templateInput = feed;
72   }
73   getTopicName() {
74     this.dashboardApiService.getTopicsFromFeeder().subscribe(data => {
75       this.topicname = data;
76     });
77   }
78
79   getTemplateTypeName() {
80     this.dashboardApiService.getTemplateTypeName().subscribe(data => {
81       this.templatetypedata = data;
82     });
83   }
84
85   jsReadFiles() {
86     var thiss = this;
87     var file = (<HTMLInputElement>document.querySelector("#f-file")).files[0];
88     this.fileName = file.name;
89     var reader = new FileReader();
90     reader.onload = function () {
91       // console.log(this.result, "this.result");
92       thiss.templateInput.body = String(this.result);
93     }
94     reader.readAsText(file);
95   }
96   passBack() {
97     this.spinner.show();
98     if (this.templateInput.name == '' || this.templateInput.name == undefined) {
99       return false;
100     }
101     this.template = this.templateInput;
102
103     // this.templatetypedata.map(item => {
104     //   if (item.name === this.templatetype.nativeElement.value) {
105     //     return this.template.designType = item.id;
106     //   }
107     // })
108
109     this.template.designType = this.templatetypedata.filter(item => {
110       return item.name === this.templatetype.nativeElement.value;
111     })[0].id || "";
112
113     this.template.designTypeName = this.templatetype.nativeElement.value;
114     this.template.topicName = this.topic.nativeElement.value;
115     this.template.submitted = false;
116     this.template.note = "";
117     this.passEntry.emit(this.template);
118     setTimeout(() => {
119       this.spinner.hide();
120     }, 500);
121   }
122 }