3af75bbfe0e1c67ae937981ed3b8f19aed0731a9
[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 { DashboardApiService } from "src/app/core/services/dashboard-api.service";
27 import { AdminService } from "src/app/core/services/admin.service";
28 import {NgbModal} from "@ng-bootstrap/ng-bootstrap";
29 import { Template,newTemplate} from "src/app/core/models/template.model";
30 import {AlertComponent} from "../../../../core/alert/alert.component";
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: newTemplate;
39   @Input() templatelist_length;
40   templateInput: newTemplate
41   templatetypedata: Array<any> = [];
42   topicname: Array<any> = [];
43   @Output() passEntry: EventEmitter<any> = new EventEmitter();
44   // @ViewChild("inputtemplateName") inputtemplateName: ElementRef;
45   // @ViewChild("templatebody") templatebody: ElementRef;
46   @ViewChild("templatetype") templatetype: ElementRef;
47   @ViewChild("topic") topic: ElementRef;
48
49   constructor(
50     public activeModal: NgbActiveModal,
51     public dashboardApiService: DashboardApiService,
52     private modalService: NgbModal,
53
54   ) { }
55   inputtemplateName = null;
56   templatebody = null;
57   fileName = null;
58
59   ngOnInit() {
60     this.getTopicName();
61     this.getTemplateTypeName();
62     // cache for display
63     this.templateInput = new Template();
64     const feed = {
65       name: this.template.name,
66       submitted: this.template.submitted,
67       body: this.template.body,
68       note: this.template.note,
69       topic: this.template.topic,
70       designType:  this.template.designType,
71       display: this.template.display
72     };
73     this.templateInput = feed;
74   }
75   getTopicName(){
76     this.dashboardApiService.getTopicName().subscribe(data => {
77       this.topicname = data;
78       console.log(this.topicname,"this.topicname")
79     });
80   }
81
82   getTemplateTypeName(){
83     this.dashboardApiService.getTemplateTypeName().subscribe(data => {
84       this.templatetypedata = data;
85       console.log(this.templatetypedata,"this.templatetypedata")
86     });
87   }
88
89   jsReadFiles(){
90     var thiss =this;
91     var file =(<HTMLInputElement>document.querySelector("#f-file")).files[0];
92     this.fileName = file.name;
93     var reader = new FileReader();
94     reader.onload = function() {
95       console.log(this.result,"this.result");
96       thiss.templateInput.body = String(this.result);
97     }
98     reader.readAsText(file);
99   }
100   passBack() {
101     if(this.templateInput.name == '' || this.templateInput.name == undefined){
102       return false;
103     }
104     this.template = this.templateInput;
105     console.log(this.templateInput);
106     this.template.display = this.templatetype.nativeElement.value;
107     this.template.designType = this.templatetypedata.find((item) => {
108         return item.display == this.template.display
109     }).designType;
110     this.template.topic = this.topic.nativeElement.value;
111     this.template.submitted = false;
112     this.template.note = "";
113     console.log(this.template);
114     this.passEntry.emit(this.template);
115   }
116 }