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