d842a1119c040879052ca4b1733dbf0efcee1469
[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 { NgbModal } from "@ng-bootstrap/ng-bootstrap";
28 // Loading spinner
29 import { NgxSpinnerService } from "ngx-spinner";
30
31 import { Template, newTemplate } from "src/app/core/models/template.model";
32
33 @Component({
34   selector: 'app-new-template-modal',
35   templateUrl: './new-template-modal.component.html',
36   styleUrls: ['./new-template-modal.component.css']
37 })
38 export class NewTemplateModalComponent implements OnInit {
39   @Input() template: newTemplate;
40   @Input() templatelist_length;
41   templateInput: newTemplate
42   templatetypedata: Array<any> = [];
43   topicname: Array<any> = [];
44   @Output() passEntry: EventEmitter<any> = new EventEmitter();
45   @ViewChild("templatetype") templatetype: ElementRef;
46   @ViewChild("topic") topic: ElementRef;
47
48   constructor(
49     public activeModal: NgbActiveModal,
50     public dashboardApiService: DashboardApiService,
51     private spinner: NgxSpinnerService,
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       topicName: this.template.topicName,
70       designType: this.template.designType,
71       designTypeName: this.template.designTypeName,
72     };
73     this.templateInput = feed;
74   }
75   getTopicName() {
76     this.dashboardApiService.getTopicName().subscribe(data => {
77       this.topicname = data;
78     });
79   }
80
81   getTemplateTypeName() {
82     this.dashboardApiService.getTemplateTypeName().subscribe(data => {
83       this.templatetypedata = data;
84     });
85   }
86
87   jsReadFiles() {
88     var thiss = this;
89     var file = (<HTMLInputElement>document.querySelector("#f-file")).files[0];
90     this.fileName = file.name;
91     var reader = new FileReader();
92     reader.onload = function () {
93       // console.log(this.result, "this.result");
94       thiss.templateInput.body = String(this.result);
95     }
96     reader.readAsText(file);
97   }
98   passBack() {
99     this.spinner.show();
100     if (this.templateInput.name == '' || this.templateInput.name == undefined) {
101       return false;
102     }
103     this.template = this.templateInput;
104
105     // this.templatetypedata.map(item => {
106     //   if (item.name === this.templatetype.nativeElement.value) {
107     //     return this.template.designType = item.id;
108     //   }
109     // })
110
111     this.template.designType = this.templatetypedata.filter(item => {
112       return item.name === this.templatetype.nativeElement.value;
113     })[0].id || "";
114
115     this.template.designTypeName = this.templatetype.nativeElement.value;
116     this.template.topicName = this.topic.nativeElement.value;
117     this.template.submitted = false;
118     this.template.note = "";
119     this.passEntry.emit(this.template);
120     setTimeout(() => {
121       this.spinner.hide();
122     }, 500);
123   }
124 }