f42e737931563d1a1417d54c0bc4df36a3fc2e4c
[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 import { Template } from "src/app/core/models/template.model";
28
29 // Loading spinner
30 import { NgxSpinnerService } from "ngx-spinner";
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   defaultDesigntype: String;
41   defaultTopicname: String;
42   templatetypedata: Array<any> = [];
43   topicname: Array<any> = [];
44   @Output() passEntry: EventEmitter<any> = new EventEmitter();
45
46   @ViewChild("templatetype") templatetype: ElementRef;
47   @ViewChild("topic") topic: ElementRef;
48
49   constructor(
50     public activeModal: NgbActiveModal,
51     public dashboardApiService: RestApiService,
52     private spinner: NgxSpinnerService,
53   ) { }
54
55   inputtemplateName = null;
56   templateInputTitle = "";
57   fileName = null;
58
59   ngOnInit() {
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       topicName: this.edittemplate.topicName,
69       designType: this.edittemplate.designType,
70       designTypeName: this.edittemplate.designTypeName,
71     };
72     this.templateInput = feed;
73     this.templateInputTitle = "" + this.edittemplate.name;
74     this.getTopicName();
75     this.getTemplateTypeName();
76   }
77   getTopicName() {
78     this.dashboardApiService.getTopicsFromFeeder().subscribe(data => {
79       this.topicname = data;
80     });
81   }
82
83   getTemplateTypeName() {
84     this.dashboardApiService.getTemplateTypeName().subscribe(data => {
85       this.templatetypedata = data;
86       this.getDefaultOptions();
87     });
88   }
89
90   getDefaultOptions() {
91     this.templatetypedata.map(item => {
92       if (item.id === this.templateInput.designType) {
93         return this.defaultDesigntype = item.name;
94       }
95     });
96     this.defaultTopicname = this.templateInput.topicName;
97   }
98
99   jsReadFiles() {
100     var thiss = this;
101     var file = (<HTMLInputElement>document.querySelector("#f-file")).files[0];
102     this.fileName = file.name;
103     var reader = new FileReader();
104     reader.onload = function () {
105       thiss.templateInput.body = String(this.result);
106     }
107     reader.readAsText(file);
108   }
109
110   passBack() {
111     this.spinner.show();
112     if (this.templateInput.name == '' || this.templateInput.name == undefined) {
113       return false;
114     }
115     this.edittemplate = this.templateInput;
116     // this.templatetypedata.map(item => {
117     //   if (item.name === this.templatetype.nativeElement.value) {
118     //     return this.edittemplate.designType = item.id;
119     //   }
120     // });
121     this.edittemplate.designType = this.templatetypedata.filter(item => {
122       return item.name === this.templatetype.nativeElement.value;
123     })[0].id || "";
124
125     this.edittemplate.designTypeName = this.templatetype.nativeElement.value;
126     this.edittemplate.topicName = this.topic.nativeElement.value;
127     this.passEntry.emit(this.edittemplate);
128     setTimeout(() => {
129       this.spinner.hide();
130     }, 500);
131   }
132 }