c77bd56e1b7211cd8de3688a422ac07f28cd72e9
[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 { Template } from "src/app/core/models/template.model";
28 import { NgbModal } from "@ng-bootstrap/ng-bootstrap";
29
30 // Loading spinner
31 import { NgxSpinnerService } from "ngx-spinner";
32
33 @Component({
34   selector: 'app-edit-template-modal',
35   templateUrl: './edit-template-modal.component.html',
36   styleUrls: ['./edit-template-modal.component.css']
37 })
38 export class EditTemplateModalComponent implements OnInit {
39   @Input() edittemplate: Template;
40   templateInput: Template;
41   defaultDesigntype: String;
42   defaultTopicname: String;
43   templatetypedata: Array<any> = [];
44   topicname: Array<any> = [];
45   @Output() passEntry: EventEmitter<any> = new EventEmitter();
46
47   @ViewChild("templatetype") templatetype: ElementRef;
48   @ViewChild("topic") topic: ElementRef;
49
50   constructor(
51     public activeModal: NgbActiveModal,
52     public dashboardApiService: DashboardApiService,
53     private spinner: NgxSpinnerService,
54     private modalService: NgbModal,
55   ) { }
56
57   inputtemplateName = null;
58   templateInputTitle = "";
59   fileName = null;
60
61   ngOnInit() {
62     // cache for display
63     this.templateInput = new Template();
64     const feed = {
65       id: this.edittemplate.id,
66       name: this.edittemplate.name,
67       submitted: this.edittemplate.submitted,
68       body: this.edittemplate.body,
69       note: this.edittemplate.note,
70       topicName: this.edittemplate.topicName,
71       designType: this.edittemplate.designType,
72       designTypeName: this.edittemplate.designTypeName,
73     };
74     this.templateInput = feed;
75     this.templateInputTitle = "" + this.edittemplate.name;
76     this.getTopicName();
77     this.getTemplateTypeName();
78   }
79   getTopicName() {
80     this.dashboardApiService.getTopicName().subscribe(data => {
81       this.topicname = data;
82     });
83   }
84
85   getTemplateTypeName() {
86     this.dashboardApiService.getTemplateTypeName().subscribe(data => {
87       this.templatetypedata = data;
88       this.getDefaultOptions();
89     });
90   }
91
92   getDefaultOptions() {
93     this.templatetypedata.map(item => {
94       if (item.id === this.templateInput.designType) {
95         return this.defaultDesigntype = item.name;
96       }
97     });
98     this.defaultTopicname = this.templateInput.topicName;
99   }
100
101   jsReadFiles() {
102     var thiss = this;
103     var file = (<HTMLInputElement>document.querySelector("#f-file")).files[0];
104     this.fileName = file.name;
105     var reader = new FileReader();
106     reader.onload = function () {
107       thiss.templateInput.body = String(this.result);
108     }
109     reader.readAsText(file);
110   }
111
112   passBack() {
113     this.spinner.show();
114     if (this.templateInput.name == '' || this.templateInput.name == undefined) {
115       return false;
116     }
117     this.edittemplate = this.templateInput;
118     // this.templatetypedata.map(item => {
119     //   if (item.name === this.templatetype.nativeElement.value) {
120     //     return this.edittemplate.designType = item.id;
121     //   }
122     // });
123     this.edittemplate.designType = this.templatetypedata.filter(item => {
124       return item.name === this.templatetype.nativeElement.value;
125     })[0].id || "";
126     
127     this.edittemplate.designTypeName = this.templatetype.nativeElement.value;
128     this.edittemplate.topicName = this.topic.nativeElement.value;
129     this.passEntry.emit(this.edittemplate);
130     setTimeout(() => {
131       this.spinner.hide();
132     }, 500);
133   }
134 }